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