| Conditions | 10 |
| Paths | 12 |
| Total Lines | 77 |
| 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 |
||
| 34 | public function acs() |
||
| 35 | { |
||
| 36 | $auth = Injector::inst()->get('SAMLHelper')->getSAMLAuth(); |
||
| 37 | $auth->processResponse(); |
||
| 38 | |||
| 39 | $error = $auth->getLastErrorReason(); |
||
| 40 | if (!empty($error)) { |
||
| 41 | SS_Log::log($error, SS_Log::ERR); |
||
| 42 | Form::messageForForm("SAMLLoginForm_LoginForm", "Authentication error: '{$error}'", 'bad'); |
||
| 43 | Session::save(); |
||
| 44 | return $this->getRedirect(); |
||
| 45 | } |
||
| 46 | |||
| 47 | if (!$auth->isAuthenticated()) { |
||
| 48 | Form::messageForForm("SAMLLoginForm_LoginForm", _t('Member.ERRORWRONGCRED'), 'bad'); |
||
| 49 | Session::save(); |
||
| 50 | return $this->getRedirect(); |
||
| 51 | } |
||
| 52 | |||
| 53 | $decodedNameId = base64_decode($auth->getNameId()); |
||
| 54 | // check that the NameID is a binary string (which signals that it is a guid |
||
| 55 | if (ctype_print($decodedNameId)) { |
||
| 56 | Form::messageForForm("SAMLLoginForm_LoginForm", "Name ID provided by IdP is not a binary GUID.", 'bad'); |
||
| 57 | Session::save(); |
||
| 58 | return $this->getRedirect(); |
||
| 59 | } |
||
| 60 | |||
| 61 | // transform the NameId to guid |
||
| 62 | $guid = LDAPUtil::bin_to_str_guid($decodedNameId); |
||
| 63 | if (!LDAPUtil::validGuid($guid)) { |
||
| 64 | $errorMessage = "Not a valid GUID '{$guid}' recieved from server."; |
||
| 65 | SS_Log::log($errorMessage, SS_Log::ERR); |
||
| 66 | Form::messageForForm("SAMLLoginForm_LoginForm", $errorMessage, 'bad'); |
||
| 67 | Session::save(); |
||
| 68 | return $this->getRedirect(); |
||
| 69 | } |
||
| 70 | |||
| 71 | // Write a rudimentary member with basic fields on every login, so that we at least have something |
||
| 72 | // if LDAP synchronisation fails. |
||
| 73 | $member = Member::get()->filter('GUID', $guid)->limit(1)->first(); |
||
| 74 | if (!($member && $member->exists())) { |
||
| 75 | $member = new Member(); |
||
| 76 | $member->GUID = $guid; |
||
| 77 | } |
||
| 78 | |||
| 79 | $attributes = $auth->getAttributes(); |
||
| 80 | |||
| 81 | foreach ($member->config()->claims_field_mappings as $claim => $field) { |
||
| 82 | if (!isset($attributes[$claim][0])) { |
||
| 83 | SS_Log::log( |
||
| 84 | sprintf( |
||
| 85 | 'Claim rule \'%s\' configured in LDAPMember.claims_field_mappings, but wasn\'t passed through. Please check IdP claim rules.', |
||
| 86 | $claim |
||
| 87 | ), SS_Log::WARN |
||
| 88 | ); |
||
| 89 | |||
| 90 | continue; |
||
| 91 | } |
||
| 92 | |||
| 93 | if(count($attributes[$claim]) > 1) { |
||
| 94 | $member->$field = $attributes[$claim]; |
||
| 95 | } else { |
||
| 96 | $member->$field = $attributes[$claim][0]; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | $member->SAMLSessionIndex = $auth->getSessionIndex(); |
||
| 101 | |||
| 102 | // This will trigger LDAP update through LDAPMemberExtension::memberLoggedIn. |
||
| 103 | // The LDAP update will also write the Member record. We shouldn't write before |
||
| 104 | // calling this, as any onAfterWrite hooks that attempt to update LDAP won't |
||
| 105 | // have the Username field available yet for new Member records, and fail. |
||
| 106 | // Both SAML and LDAP identify Members by the GUID field. |
||
| 107 | $member->logIn(); |
||
| 108 | |||
| 109 | return $this->getRedirect(); |
||
| 110 | } |
||
| 111 | |||
| 163 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.