Total Complexity | 41 |
Total Lines | 262 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like User often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use User, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class User extends EntityWithDBProperties |
||
44 | { |
||
45 | |||
46 | /** |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | public $userName; |
||
51 | |||
52 | /** |
||
53 | * Class constructor. The required argument is a user's persistent identifier as was returned by the authentication source. |
||
54 | * |
||
55 | * @param string $userId User Identifier as per authentication source |
||
56 | */ |
||
57 | public function __construct($userId) |
||
92 | } |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * This function checks whether a user is a federation administrator. When called without argument, it only checks if the |
||
97 | * user is a federation administrator of *any* federation. When given a parameter (ISO shortname of federation), it checks |
||
98 | * if the user administers this particular federation. |
||
99 | * |
||
100 | * @param string $federation optional: federation to be checked |
||
101 | * @return boolean TRUE if the user is federation admin, FALSE if not |
||
102 | */ |
||
103 | public function isFederationAdmin($federation = 0) |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * This function tests if the current user has been configured as the system superadmin, i.e. if the user is allowed |
||
122 | * to execute the 112365365321.php script and obtain read-only access to admin areas. |
||
123 | * |
||
124 | * @return boolean TRUE if the user is a superadmin, FALSE if not |
||
125 | */ |
||
126 | public function isSuperadmin() |
||
129 | } |
||
130 | |||
131 | |||
132 | /** |
||
133 | * This function tests if the current user has been configured as the system superadmin, i.e. if the user is allowed |
||
134 | * obtain read-only access to admin areas. |
||
135 | * |
||
136 | * @return boolean TRUE if the user is a support member, FALSE if not |
||
137 | */ |
||
138 | public function isSupport() |
||
139 | { |
||
140 | return in_array($this->userName, \config\Master::SUPPORT); |
||
141 | } |
||
142 | |||
143 | /** |
||
144 | * This function tests if the current user is an ovner of a given IdP |
||
145 | * |
||
146 | * @param int $idp integer identifier of the IdP |
||
147 | * @return boolean TRUE if the user is an owner, FALSE if not |
||
148 | */ |
||
149 | public function isIdPOwner($idp) |
||
150 | { |
||
151 | $temp = new IdP($idp); |
||
152 | foreach ($temp->listOwners() as $oneowner) { |
||
153 | if ($oneowner['ID'] == $this->userName) { |
||
154 | return TRUE; |
||
155 | } |
||
156 | } |
||
157 | return FALSE; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * This function lists all institution ids for which the user appears as admin |
||
162 | * |
||
163 | * @return array if institution ids. |
||
164 | */ |
||
165 | public function listOwnerships() { |
||
166 | $dbHandle = \core\DBConnection::handle("INST"); |
||
167 | $query = $dbHandle->exec("SELECT institution_id FROM ownership WHERE user_id='".$this->userName."'"); |
||
168 | return array_column($query->fetch_all(), 0); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * shorthand function for email sending to the user |
||
173 | * |
||
174 | * @param string $subject addressee of the mail |
||
175 | * @param string $content content of the mail |
||
176 | * @return boolean did it work? |
||
177 | */ |
||
178 | public function sendMailToUser($subject, $content) |
||
179 | { |
||
180 | |||
181 | $mailaddr = $this->getAttributes("user:email"); |
||
182 | if (count($mailaddr) == 0) { // we don't know user's mail address |
||
183 | return FALSE; |
||
184 | } |
||
185 | common\Entity::intoThePotatoes(); |
||
186 | $mail = \core\common\OutsideComm::mailHandle(); |
||
187 | // who to whom? |
||
188 | $mail->FromName = \config\Master::APPEARANCE['productname'] . " Notification System"; |
||
189 | $mail->addReplyTo(\config\Master::APPEARANCE['support-contact']['developer-mail'], \config\Master::APPEARANCE['productname'] . " " . _("Feedback")); |
||
190 | $mail->addAddress($mailaddr[0]["value"]); |
||
191 | // what do we want to say? |
||
192 | $mail->Subject = $subject; |
||
193 | $mail->Body = $content; |
||
194 | |||
195 | $sent = $mail->send(); |
||
196 | common\Entity::outOfThePotatoes(); |
||
197 | return $sent; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * NOOP in this class, only need to override abstract base class |
||
202 | * |
||
203 | * @return void |
||
204 | */ |
||
205 | public function updateFreshness() |
||
207 | // User is always fresh |
||
208 | } |
||
209 | |||
210 | const PROVIDER_STRINGS = [ |
||
211 | "eduPersonTargetedID" => "eduGAIN", |
||
212 | "facebook_targetedID" => "Facebook", |
||
213 | "google_eppn" => "Google", |
||
214 | "linkedin_targetedID" => "LinkedIn", |
||
215 | "twitter_targetedID" => "Twitter", |
||
216 | "openid" => "Google (defunct)", |
||
217 | ]; |
||
218 | |||
219 | /** |
||
220 | * Some users apparently forget which eduGAIN/social ID they originally used |
||
221 | * to log into CAT. We can try to help them: if they tell us the email |
||
222 | * address by which they received the invitation token, then we can see if |
||
223 | * any CAT IdPs are associated to an account which originally came in via |
||
224 | * that email address. We then see which pretty-print auth provider name |
||
225 | * was used |
||
226 | * |
||
227 | * @param string $mail mail address to search with |
||
228 | * @param string $lang language for the eduGAIN request |
||
229 | * @return boolean|array the list of auth source IdPs we found for the mail, or FALSE if none found or invalid input |
||
230 | */ |
||
231 | public static function findLoginIdPByEmail($mail, $lang) |
||
305 | } |
||
306 | } |