| Conditions | 26 |
| Paths | 1624 |
| Total Lines | 212 |
| Code Lines | 117 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 128 | public function main(Request $request): Template |
||
| 129 | { |
||
| 130 | $authority = $this->moduleConfig->getValue('authority'); |
||
| 131 | |||
| 132 | $as = new $this->authSimple($authority); |
||
| 133 | |||
| 134 | // If request is a logout request |
||
| 135 | $logout = $request->get('logout'); |
||
| 136 | if ($logout !== null) { |
||
| 137 | $returnURL = $this->moduleConfig->getValue('returnURL'); |
||
| 138 | $as->logout($returnURL); |
||
| 139 | } |
||
| 140 | |||
| 141 | $hashAttributes = $this->moduleConfig->getValue('attributes.hash', false); |
||
| 142 | |||
| 143 | $excludeAttributes = $this->moduleConfig->getValue('attributes.exclude', []); |
||
| 144 | |||
| 145 | // Check if valid local session exists |
||
| 146 | $as->requireAuth(); |
||
| 147 | |||
| 148 | // Get released attributes |
||
| 149 | $attributes = $as->getAttributes(); |
||
| 150 | |||
| 151 | // Get metadata storage handler |
||
| 152 | $metadata = $this->metadataStorageHandler; |
||
| 153 | |||
| 154 | /* |
||
| 155 | * Get IdP id and metadata |
||
| 156 | */ |
||
| 157 | $idp_entityid = $metadata->getMetaDataCurrentEntityID('saml20-idp-hosted'); |
||
| 158 | $idp_metadata = $metadata->getMetaData($idp_entityid, 'saml20-idp-hosted'); |
||
| 159 | |||
| 160 | // Calc correct source |
||
| 161 | if ($as->getAuthData('saml:sp:IdP') !== null) { |
||
| 162 | // from a remote idp (as bridge) |
||
| 163 | $source = 'saml20-idp-remote|' . $as->getAuthData('saml:sp:IdP'); |
||
| 164 | } else { |
||
| 165 | // from the local idp |
||
| 166 | $source = $idp_metadata['metadata-set'] . '|' . $idp_entityid; |
||
| 167 | } |
||
| 168 | |||
| 169 | // Get user ID |
||
| 170 | if (isset($idp_metadata['userid.attribute']) && is_string($idp_metadata['userid.attribute'])) { |
||
| 171 | $userid_attributename = $idp_metadata['userid.attribute']; |
||
| 172 | } else { |
||
| 173 | $userid_attributename = 'eduPersonPrincipalName'; |
||
| 174 | } |
||
| 175 | |||
| 176 | $userids = $attributes[$userid_attributename]; |
||
| 177 | |||
| 178 | if (empty($userids)) { |
||
| 179 | throw new Exception(sprintf( |
||
| 180 | 'Could not generate useridentifier for storing consent. Attribute [%s] was not available.', |
||
| 181 | $userid_attributename |
||
| 182 | )); |
||
| 183 | } |
||
| 184 | |||
| 185 | $userid = $userids[0]; |
||
| 186 | |||
| 187 | // Get all SP metadata |
||
| 188 | $all_sp_metadata = $metadata->getList('saml20-sp-remote'); |
||
| 189 | |||
| 190 | $sp_entityid = $request->get('cv');; |
||
| 191 | $action = $request->get('action'); |
||
| 192 | |||
| 193 | Logger::critical('consentAdmin: sp: ' . $sp_entityid . ' action: ' . $action); |
||
| 194 | |||
| 195 | // Remove services, whitch have consent disabled |
||
| 196 | if (isset($idp_metadata['consent.disable'])) { |
||
| 197 | foreach ($idp_metadata['consent.disable'] as $disable) { |
||
| 198 | if (array_key_exists($disable, $all_sp_metadata)) { |
||
| 199 | unset($all_sp_metadata[$disable]); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | Logger::info('consentAdmin: ' . $idp_entityid); |
||
| 205 | |||
| 206 | // Parse consent config |
||
| 207 | $consent_storage = $this->store::parseStoreConfig($this->moduleConfig->getValue('consentadmin')); |
||
| 208 | |||
| 209 | // Calc correct user ID hash |
||
| 210 | $hashed_user_id = $this->consent::getHashedUserID($userid, $source); |
||
| 211 | |||
| 212 | // If a checkbox have been clicked |
||
| 213 | if ($action !== null && $sp_entityid !== null) { |
||
| 214 | // init template to enable translation of status messages |
||
| 215 | $template = new Template( |
||
| 216 | $this->config, |
||
| 217 | 'consentAdmin:consentadminajax.twig', |
||
| 218 | 'consentAdmin:consentadmin' |
||
| 219 | ); |
||
| 220 | |||
| 221 | // Get SP metadata |
||
| 222 | $sp_metadata = $metadata->getMetaData($sp_entityid, 'saml20-sp-remote'); |
||
| 223 | |||
| 224 | // Run AuthProc filters |
||
| 225 | list($targeted_id, $attribute_hash, $attributes) = $this->driveProcessingChain( |
||
| 226 | $idp_metadata, |
||
| 227 | $source, |
||
| 228 | $sp_metadata, |
||
| 229 | $sp_entityid, |
||
| 230 | $attributes, |
||
| 231 | $userid, |
||
| 232 | $hashAttributes, |
||
| 233 | $excludeAttributes |
||
| 234 | ); |
||
| 235 | |||
| 236 | // Add a consent (or update if attributes have changed and old consent for SP and IdP exists) |
||
| 237 | if ($action == 'true') { |
||
| 238 | $isStored = $consent_storage->saveConsent($hashed_user_id, $targeted_id, $attribute_hash); |
||
| 239 | } else { |
||
| 240 | if ($action == 'false') { |
||
| 241 | // Got consent, so this is a request to remove it |
||
| 242 | $consent_storage->deleteConsent($hashed_user_id, $targeted_id); |
||
| 243 | $isStored = false; |
||
| 244 | } else { |
||
| 245 | Logger::info('consentAdmin: unknown action'); |
||
| 246 | $isStored = null; |
||
| 247 | } |
||
| 248 | } |
||
| 249 | $template->data['isStored'] = $isStored; |
||
| 250 | return $template; |
||
| 251 | } |
||
| 252 | |||
| 253 | // Get all consents for user |
||
| 254 | $user_consent_list = $consent_storage->getConsents($hashed_user_id); |
||
| 255 | |||
| 256 | // Parse list of consents |
||
| 257 | $user_consent = []; |
||
| 258 | foreach ($user_consent_list as $c) { |
||
| 259 | $user_consent[$c[0]] = $c[1]; |
||
| 260 | } |
||
| 261 | |||
| 262 | $template_sp_content = []; |
||
|
|
|||
| 263 | |||
| 264 | // Init template |
||
| 265 | $template = new Template($this->config, 'consentAdmin:consentadmin.twig', 'consentAdmin:consentadmin'); |
||
| 266 | $template->getLocalization()->addAttributeDomains(); |
||
| 267 | $translator = $template->getTranslator(); |
||
| 268 | |||
| 269 | $sp_list = []; |
||
| 270 | |||
| 271 | // Process consents for all SP |
||
| 272 | foreach ($all_sp_metadata as $sp_entityid => $sp_values) { |
||
| 273 | // Get metadata for SP |
||
| 274 | $sp_metadata = $metadata->getMetaData($sp_entityid, 'saml20-sp-remote'); |
||
| 275 | |||
| 276 | // Run attribute filters |
||
| 277 | list($targeted_id, $attribute_hash, $attributes) = $this->driveProcessingChain( |
||
| 278 | $idp_metadata, |
||
| 279 | $source, |
||
| 280 | $sp_metadata, |
||
| 281 | $sp_entityid, |
||
| 282 | $attributes, |
||
| 283 | $userid, |
||
| 284 | $hashAttributes, |
||
| 285 | $excludeAttributes |
||
| 286 | ); |
||
| 287 | |||
| 288 | // Check if consent exists |
||
| 289 | if (array_key_exists($targeted_id, $user_consent)) { |
||
| 290 | $sp_status = "changed"; |
||
| 291 | Logger::info('consentAdmin: changed'); |
||
| 292 | // Check if consent is valid. (Possible that attributes has changed) |
||
| 293 | if ($user_consent[$targeted_id] == $attribute_hash) { |
||
| 294 | Logger::info('consentAdmin: ok'); |
||
| 295 | $sp_status = "ok"; |
||
| 296 | } |
||
| 297 | // Consent does not exist |
||
| 298 | } else { |
||
| 299 | Logger::info('consentAdmin: none'); |
||
| 300 | $sp_status = "none"; |
||
| 301 | } |
||
| 302 | |||
| 303 | // Set name of SP |
||
| 304 | if (isset($sp_values['name']) && is_array($sp_values['name'])) { |
||
| 305 | $sp_name = $sp_metadata['name']; |
||
| 306 | } elseif (isset($sp_values['name']) && is_string($sp_values['name'])) { |
||
| 307 | $sp_name = $sp_metadata['name']; |
||
| 308 | } elseif (isset($sp_values['OrganizationDisplayName']) && is_array($sp_values['OrganizationDisplayName'])) { |
||
| 309 | $sp_name = $sp_metadata['OrganizationDisplayName']; |
||
| 310 | } else { |
||
| 311 | $sp_name = $sp_entityid; |
||
| 312 | } |
||
| 313 | |||
| 314 | // Set description of SP |
||
| 315 | $sp_description = null; |
||
| 316 | if (!empty($sp_metadata['description']) && is_array($sp_metadata['description'])) { |
||
| 317 | $sp_description = $sp_metadata['description']; |
||
| 318 | } |
||
| 319 | |||
| 320 | // Add a URL to the service if present in metadata |
||
| 321 | $sp_service_url = isset($sp_metadata['ServiceURL']) ? $sp_metadata['ServiceURL'] : null; |
||
| 322 | |||
| 323 | // Fill out array for the template |
||
| 324 | $sp_list[$sp_entityid] = [ |
||
| 325 | 'spentityid' => $sp_entityid, |
||
| 326 | 'name' => $sp_name, |
||
| 327 | 'description' => $sp_description, |
||
| 328 | 'consentStatus' => $sp_status, |
||
| 329 | 'consentValue' => $sp_entityid, |
||
| 330 | 'attributes_by_sp' => $attributes, |
||
| 331 | 'serviceurl' => $sp_service_url, |
||
| 332 | ]; |
||
| 333 | } |
||
| 334 | |||
| 335 | $template->data['header'] = 'Consent Administration'; |
||
| 336 | $template->data['spList'] = $sp_list; |
||
| 337 | $template->data['showDescription'] = $this->moduleConfig->getValue('showDescription'); |
||
| 338 | |||
| 339 | return $template; |
||
| 340 | } |
||
| 422 |