| Conditions | 10 |
| Paths | 13 |
| Total Lines | 52 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 56 | public function authenticate() { |
||
| 57 | \core\common\Entity::intoThePotatoes(); |
||
| 58 | $loggerInstance = new \core\common\Logging(); |
||
| 59 | $authSimple = new \SimpleSAML\Auth\Simple(\config\Master::AUTHENTICATION['ssp-authsource']); |
||
| 60 | $authSimple->requireAuth(); |
||
| 61 | $admininfo = $authSimple->getAttributes(); |
||
| 62 | $session = \SimpleSAML\Session::getSessionFromRequest(); |
||
| 63 | $session->cleanup(); |
||
| 64 | |||
| 65 | if (!isset($admininfo[\config\Master::AUTHENTICATION['ssp-attrib-identifier']][0])) { |
||
| 66 | $failtext = "FATAL ERROR: we did not receive a unique user identifier from the authentication source!"; |
||
| 67 | echo $failtext; |
||
| 68 | throw new Exception($failtext); |
||
| 69 | } |
||
| 70 | |||
| 71 | $user = $admininfo[\config\Master::AUTHENTICATION['ssp-attrib-identifier']][0]; |
||
| 72 | |||
| 73 | $_SESSION['user'] = $user; |
||
| 74 | $_SESSION['name'] = $admininfo[\config\Master::AUTHENTICATION['ssp-attrib-name']][0] ?? _("Unnamed User"); |
||
| 75 | /* |
||
| 76 | * This is a nice pathological test case for a user ID. |
||
| 77 | * |
||
| 78 | * */ |
||
| 79 | //$_SESSION['user'] = "<saml:NameID xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\" NameQualifier=\"https://idp.jisc.ac.uk/idp/shibboleth\" SPNameQualifier=\"https://cat-beta.govroam.uk/simplesaml/module.php/saml/sp/metadata.php/default-sp\" Format=\"urn:oasis:names:tc:SAML:2.0:nameid-format:persistent\">XXXXXXXXXXXXXXXX</saml:NameID>"; |
||
| 80 | |||
| 81 | |||
| 82 | $newNameReceived = FALSE; |
||
| 83 | |||
| 84 | $userObject = new \core\User($user); |
||
| 85 | |||
| 86 | $attribMapping = [ |
||
| 87 | "ssp-attrib-name" => "user:realname", |
||
| 88 | "ssp-attrib-email" => "user:email"]; |
||
| 89 | |||
| 90 | foreach ($attribMapping as $SSPside => $CATside) { |
||
| 91 | if (isset($admininfo[\config\Master::AUTHENTICATION[$SSPside]][0]) && (count($userObject->getAttributes($CATside)) == 0) && \config\Master::DB['USER']['readonly'] === FALSE) { |
||
| 92 | $name = $admininfo[\config\Master::AUTHENTICATION[$SSPside]][0]; |
||
| 93 | $userObject->addAttribute($CATside, NULL, $name); |
||
| 94 | $loggerInstance->writeAudit($_SESSION['user'], "NEW", "User - added $CATside from external auth source"); |
||
| 95 | if ($CATside == "user:realname") { |
||
| 96 | $newNameReceived = TRUE; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | if (count($userObject->getAttributes('user:realname')) > 0 || $newNameReceived) { // we have a real name in the DB. We trust this more than a session one, so set it |
||
| 102 | $nameArray = $userObject->getAttributes("user:realname"); |
||
| 103 | if (!empty($nameArray[0])) { |
||
| 104 | $_SESSION['name'] = $nameArray[0]['value']; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | \core\common\Entity::outOfThePotatoes(); |
||
| 108 | } |
||
| 138 |