| Conditions | 15 |
| Paths | 196 |
| Total Lines | 67 |
| Code Lines | 45 |
| 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 |
||
| 60 | public function authenticate() { |
||
| 61 | \core\common\Entity::intoThePotatoes(); |
||
| 62 | $loggerInstance = new \core\common\Logging(); |
||
| 63 | $authSimple = new \SimpleSAML\Auth\Simple(\config\Master::AUTHENTICATION['ssp-authsource']); |
||
| 64 | if (!$authSimple->isAuthenticated()) { |
||
| 65 | $_SESSION['saveLog'] = true; |
||
| 66 | } |
||
| 67 | |||
| 68 | $authSimple->requireAuth(); |
||
| 69 | $admininfo = $authSimple->getAttributes(); |
||
| 70 | \core\common\Logging::debug_s(4, $admininfo, "SAML ATTR:\n", "\n"); |
||
| 71 | if (isset($_SESSION['saveLog']) && $_SESSION['saveLog'] == true) { |
||
| 72 | $saveLog = true; |
||
| 73 | } else { |
||
| 74 | $saveLog = false; |
||
| 75 | } |
||
| 76 | unset($_SESSION['saveLog']); |
||
| 77 | $session = \SimpleSAML\Session::getSessionFromRequest(); |
||
| 78 | $session->cleanup(); |
||
| 79 | if (!isset($admininfo[\config\Master::AUTHENTICATION['ssp-attrib-identifier']][0])) { |
||
| 80 | $failtext = "FATAL ERROR: we did not receive a unique user identifier from the authentication source!"; |
||
| 81 | echo $failtext; |
||
| 82 | throw new Exception($failtext); |
||
| 83 | } |
||
| 84 | $user = $admininfo[\config\Master::AUTHENTICATION['ssp-attrib-identifier']][0]; |
||
| 85 | if ($saveLog) { |
||
| 86 | $loggerInstance->debug(4, "Writing log\n"); |
||
| 87 | $this->logLoginTime($user); |
||
| 88 | } |
||
| 89 | $_SESSION['user'] = $user; |
||
| 90 | $_SESSION['name'] = $admininfo[\config\Master::AUTHENTICATION['ssp-attrib-name']][0] ?? _("Unnamed User"); |
||
| 91 | $_SESSION['auth_email'] = $admininfo[\config\Master::AUTHENTICATION['ssp-attrib-email']][0] ?? _(""); |
||
| 92 | if (isset($admininfo[\config\Master::AUTHENTICATION['ssp-entitlement']])) { |
||
| 93 | $_SESSION['entitlement'] = $admininfo[\config\Master::AUTHENTICATION['ssp-entitlement']]; |
||
| 94 | } |
||
| 95 | /* |
||
| 96 | * This is a nice pathological test case for a user ID. |
||
| 97 | * |
||
| 98 | * */ |
||
| 99 | //$_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>"; |
||
| 100 | |||
| 101 | |||
| 102 | $newNameReceived = FALSE; |
||
| 103 | |||
| 104 | $userObject = new \core\User($user); |
||
| 105 | |||
| 106 | $attribMapping = [ |
||
| 107 | "ssp-attrib-name" => "user:realname", |
||
| 108 | "ssp-attrib-email" => "user:email"]; |
||
| 109 | |||
| 110 | foreach ($attribMapping as $SSPside => $CATside) { |
||
| 111 | if (isset($admininfo[\config\Master::AUTHENTICATION[$SSPside]][0]) && (count($userObject->getAttributes($CATside)) == 0) && \config\Master::DB['USER']['readonly'] === FALSE) { |
||
| 112 | $name = $admininfo[\config\Master::AUTHENTICATION[$SSPside]][0]; |
||
| 113 | $userObject->addAttribute($CATside, NULL, $name); |
||
| 114 | $loggerInstance->writeAudit($_SESSION['user'], "NEW", "User - added $CATside from external auth source"); |
||
| 115 | if ($CATside == "user:realname") { |
||
| 116 | $newNameReceived = TRUE; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | 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 |
||
| 121 | $nameArray = $userObject->getAttributes("user:realname"); |
||
| 122 | if (!empty($nameArray[0])) { |
||
| 123 | $_SESSION['name'] = $nameArray[0]['value']; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | \core\common\Entity::outOfThePotatoes(); |
||
| 127 | } |
||
| 166 |