Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
8 | class SAMLController extends Controller |
||
|
|||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | private static $allowed_actions = [ |
||
14 | 'index', |
||
15 | 'login', |
||
16 | 'logout', |
||
17 | 'acs', |
||
18 | 'sls', |
||
19 | 'metadata' |
||
20 | ]; |
||
21 | |||
22 | /** |
||
23 | * Assertion Consumer Service |
||
24 | * |
||
25 | * The user gets sent back here after authenticating with the IdP, off-site. |
||
26 | * The earlier redirection to the IdP can be found in the SAMLAuthenticator::authenticate. |
||
27 | * |
||
28 | * After this handler completes, we end up with a rudimentary Member record (which will be created on-the-fly |
||
29 | * if not existent), with the user already logged in. Login triggers memberLoggedIn hooks, which allows |
||
30 | * LDAP side of this module to finish off loading Member data. |
||
31 | * |
||
32 | * @throws OneLogin_Saml2_Error |
||
33 | */ |
||
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 | |||
112 | /** |
||
113 | * Generate this SP's metadata. This is needed for intialising the SP-IdP relationship. |
||
114 | * IdP is instructed to call us back here to establish the relationship. IdP may also be configured |
||
115 | * to hit this endpoint periodically during normal operation, to check the SP availability. |
||
116 | */ |
||
117 | public function metadata() |
||
138 | |||
139 | /** |
||
140 | * @return SS_HTTPResponse |
||
141 | */ |
||
142 | protected function getRedirect() |
||
162 | } |
||
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.