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