| Conditions | 9 |
| Paths | 10 |
| Total Lines | 73 |
| Code Lines | 41 |
| 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 |
||
| 50 | public function acs() |
||
| 51 | { |
||
| 52 | $auth = Injector::inst()->get('SilverStripe\\ActiveDirectory\\Helpers\\SAMLHelper')->getSAMLAuth(); |
||
| 53 | $auth->processResponse(); |
||
| 54 | |||
| 55 | $error = $auth->getLastErrorReason(); |
||
| 56 | if (!empty($error)) { |
||
| 57 | $this->getLogger()->error($error); |
||
| 58 | Form::messageForForm('SAMLLoginForm_LoginForm', "Authentication error: '{$error}'", 'bad'); |
||
| 59 | Session::save(); |
||
| 60 | return $this->getRedirect(); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (!$auth->isAuthenticated()) { |
||
| 64 | Form::messageForForm('SAMLLoginForm_LoginForm', _t('Member.ERRORWRONGCRED'), 'bad'); |
||
| 65 | Session::save(); |
||
| 66 | return $this->getRedirect(); |
||
| 67 | } |
||
| 68 | |||
| 69 | $decodedNameId = base64_decode($auth->getNameId()); |
||
| 70 | // check that the NameID is a binary string (which signals that it is a guid |
||
| 71 | if (ctype_print($decodedNameId)) { |
||
| 72 | Form::messageForForm('SAMLLoginForm_LoginForm', 'Name ID provided by IdP is not a binary GUID.', 'bad'); |
||
| 73 | Session::save(); |
||
| 74 | return $this->getRedirect(); |
||
| 75 | } |
||
| 76 | |||
| 77 | // transform the NameId to guid |
||
| 78 | $guid = LDAPUtil::bin_to_str_guid($decodedNameId); |
||
| 79 | if (!LDAPUtil::validGuid($guid)) { |
||
| 80 | $errorMessage = "Not a valid GUID '{$guid}' recieved from server."; |
||
| 81 | $this->getLogger()->error($errorMessage); |
||
| 82 | Form::messageForForm('SAMLLoginForm_LoginForm', $errorMessage, 'bad'); |
||
| 83 | Session::save(); |
||
| 84 | return $this->getRedirect(); |
||
| 85 | } |
||
| 86 | |||
| 87 | // Write a rudimentary member with basic fields on every login, so that we at least have something |
||
| 88 | // if LDAP synchronisation fails. |
||
| 89 | $member = Member::get()->filter('GUID', $guid)->limit(1)->first(); |
||
| 90 | if (!($member && $member->exists())) { |
||
| 91 | $member = new Member(); |
||
| 92 | $member->GUID = $guid; |
||
| 93 | } |
||
| 94 | |||
| 95 | $attributes = $auth->getAttributes(); |
||
| 96 | |||
| 97 | foreach ($member->config()->claims_field_mappings as $claim => $field) { |
||
| 98 | if (!isset($attributes[$claim][0])) { |
||
| 99 | $this->getLogger()->warn( |
||
| 100 | sprintf( |
||
| 101 | 'Claim rule \'%s\' configured in LDAPMember.claims_field_mappings, but wasn\'t passed through. Please check IdP claim rules.', |
||
| 102 | $claim |
||
| 103 | ) |
||
| 104 | ); |
||
| 105 | |||
| 106 | continue; |
||
| 107 | } |
||
| 108 | |||
| 109 | $member->$field = $attributes[$claim][0]; |
||
| 110 | } |
||
| 111 | |||
| 112 | $member->SAMLSessionIndex = $auth->getSessionIndex(); |
||
| 113 | |||
| 114 | // This will trigger LDAP update through LDAPMemberExtension::memberLoggedIn. |
||
| 115 | // The LDAP update will also write the Member record. We shouldn't write before |
||
| 116 | // calling this, as any onAfterWrite hooks that attempt to update LDAP won't |
||
| 117 | // have the Username field available yet for new Member records, and fail. |
||
| 118 | // Both SAML and LDAP identify Members by the GUID field. |
||
| 119 | $member->logIn(); |
||
| 120 | |||
| 121 | return $this->getRedirect(); |
||
| 122 | } |
||
| 123 | |||
| 185 |