| Total Complexity | 44 |
| Total Lines | 291 |
| 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 | public $edugain = false; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Class constructor. The required argument is a user's persistent identifier as was returned by the authentication source. |
||
| 56 | * |
||
| 57 | * @param string $userId User Identifier as per authentication source |
||
| 58 | */ |
||
| 59 | public function __construct($userId) |
||
| 60 | { |
||
| 61 | $this->databaseType = "USER"; |
||
| 62 | parent::__construct(); // database handle is now available |
||
| 63 | $this->attributes = []; |
||
| 64 | $this->entityOptionTable = "user_options"; |
||
| 65 | $this->entityIdColumn = "user_id"; |
||
| 66 | $this->identifier = 0; // not used |
||
| 67 | $this->userName = $userId; |
||
| 68 | $this->edugain = $this->isFromEduGAIN(); |
||
| 69 | $optioninstance = Options::instance(); |
||
| 70 | |||
| 71 | if (\config\ConfAssistant::CONSORTIUM['name'] == "eduroam" && isset(\config\ConfAssistant::CONSORTIUM['deployment-voodoo']) && \config\ConfAssistant::CONSORTIUM['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
| 72 | // e d u r o a m DB doesn't follow the usual approach |
||
| 73 | // we could get multiple rows below (if administering multiple |
||
| 74 | // federations), so consolidate all into the usual options |
||
| 75 | $info = $this->databaseHandle->exec("SELECT email, common_name, role, realm FROM view_admin WHERE eptid = ?", "s", $this->userName); |
||
| 76 | $visited = FALSE; |
||
| 77 | // SELECT -> resource, not boolean |
||
| 78 | while ($userDetailQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $info)) { |
||
| 79 | if (!$visited) { |
||
| 80 | $mailOptinfo = $optioninstance->optionType("user:email"); |
||
| 81 | $this->attributes[] = ["name" => "user:email", "lang" => NULL, "value" => $userDetailQuery->email, "level" => Options::LEVEL_USER, "row_id" => 0, "flag" => $mailOptinfo['flag']]; |
||
| 82 | $realnameOptinfo = $optioninstance->optionType("user:realname"); |
||
| 83 | $this->attributes[] = ["name" => "user:realname", "lang" => NULL, "value" => $userDetailQuery->common_name, "level" => Options::LEVEL_USER, "row_id" => 0, "flag" => $realnameOptinfo['flag']]; |
||
| 84 | $visited = TRUE; |
||
| 85 | } |
||
| 86 | if ($userDetailQuery->role == "fedadmin") { |
||
| 87 | $optinfo = $optioninstance->optionType("user:fedadmin"); |
||
| 88 | $this->attributes[] = ["name" => "user:fedadmin", "lang" => NULL, "value" => strtoupper($userDetailQuery->realm), "level" => Options::LEVEL_USER, "row_id" => 0, "flag" => $optinfo['flag']]; |
||
| 89 | } |
||
| 90 | } |
||
| 91 | } else { |
||
| 92 | $this->attributes = $this->retrieveOptionsFromDatabase("SELECT DISTINCT option_name, option_lang, option_value, row_id |
||
| 93 | FROM $this->entityOptionTable |
||
| 94 | WHERE $this->entityIdColumn = ?", "User"); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * This function checks whether a user is a federation administrator. When called without argument, it only checks if the |
||
| 100 | * user is a federation administrator of *any* federation. When given a parameter (ISO shortname of federation), it checks |
||
| 101 | * if the user administers this particular federation. |
||
| 102 | * |
||
| 103 | * @param string $federation optional: federation to be checked |
||
| 104 | * @return boolean TRUE if the user is federation admin, FALSE if not |
||
| 105 | */ |
||
| 106 | public function isFederationAdmin($federation = 0) |
||
| 107 | { |
||
| 108 | $feds = $this->getAttributes("user:fedadmin"); |
||
| 109 | if (count($feds) == 0) { // not a fedadmin at all |
||
| 110 | return FALSE; |
||
| 111 | } |
||
| 112 | if ($federation === 0) { // fedadmin for one; that's all we want to know |
||
| 113 | return TRUE; |
||
| 114 | } |
||
| 115 | foreach ($feds as $fed) { // check if authz is for requested federation |
||
| 116 | if (strtoupper($fed['value']) == strtoupper($federation)) { |
||
| 117 | return TRUE; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | return FALSE; // no luck so far? Not the admin we are looking for. |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * This function tests if the current user has been configured as the system superadmin, i.e. if the user is allowed |
||
| 125 | * to execute the 112365365321.php script and obtain read-only access to admin areas. |
||
| 126 | * |
||
| 127 | * @return boolean TRUE if the user is a superadmin, FALSE if not |
||
| 128 | */ |
||
| 129 | public function isSuperadmin() |
||
| 130 | { |
||
| 131 | return in_array($this->userName, \config\Master::SUPERADMINS); |
||
| 132 | } |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * This function tests if the current user has been configured as the system superadmin, i.e. if the user is allowed |
||
| 137 | * obtain read-only access to admin areas. |
||
| 138 | * |
||
| 139 | * @return boolean TRUE if the user is a support member, FALSE if not |
||
| 140 | */ |
||
| 141 | public function isSupport() |
||
| 142 | { |
||
| 143 | return in_array($this->userName, \config\Master::SUPPORT); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * This function tests if the current user is an ovner of a given IdP |
||
| 148 | * |
||
| 149 | * @param int $idp integer identifier of the IdP |
||
| 150 | * @return boolean TRUE if the user is an owner, FALSE if not |
||
| 151 | */ |
||
| 152 | public function isIdPOwner($idp) |
||
| 153 | { |
||
| 154 | $temp = new IdP($idp); |
||
| 155 | foreach ($temp->listOwners() as $oneowner) { |
||
| 156 | if ($oneowner['ID'] == $this->userName) { |
||
| 157 | return TRUE; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | return FALSE; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** This function tests if user's IdP is listed in eduGAIN - it uses an external |
||
| 164 | * call to technical eduGAIN API |
||
| 165 | * |
||
| 166 | * @return boolean true if the IdP is listed, false otherwise |
||
| 167 | * |
||
| 168 | */ |
||
| 169 | public function isFromEduGAIN() |
||
| 170 | { |
||
| 171 | $loggerInstance = new common\Logging(); |
||
| 172 | $entityId = preg_replace('/^.*=!/','', $_SESSION['user']); |
||
| 173 | $url = \config\Diagnostics::EDUGAINRESOLVER['url'] . "?action=get_entity_name&type=idp&e_id=$entityId"; |
||
| 174 | $ch = curl_init($url); |
||
| 175 | if ($ch === FALSE) { |
||
| 176 | $loggerInstance->debug(2, "Unable ask eduGAIN about IdP - CURL init failed!"); |
||
| 177 | return false; |
||
| 178 | } |
||
| 179 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 180 | curl_setopt($ch, CURLOPT_TIMEOUT, \config\Diagnostics::EDUGAINRESOLVER['timeout']); |
||
| 181 | $response = curl_exec($ch); |
||
| 182 | if ($response === "null") { |
||
| 183 | return false; |
||
| 184 | } else { |
||
| 185 | return true; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * This function lists all institution ids for which the user appears as admin |
||
| 191 | * |
||
| 192 | * @return array if institution ids. |
||
| 193 | */ |
||
| 194 | public function listOwnerships() { |
||
| 195 | $dbHandle = \core\DBConnection::handle("INST"); |
||
| 196 | $query = $dbHandle->exec("SELECT institution_id FROM ownership WHERE user_id='".$this->userName."'"); |
||
| 197 | return array_column($query->fetch_all(), 0); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * shorthand function for email sending to the user |
||
| 202 | * |
||
| 203 | * @param string $subject addressee of the mail |
||
| 204 | * @param string $content content of the mail |
||
| 205 | * @return boolean did it work? |
||
| 206 | */ |
||
| 207 | public function sendMailToUser($subject, $content) |
||
| 208 | { |
||
| 209 | |||
| 210 | $mailaddr = $this->getAttributes("user:email"); |
||
| 211 | if (count($mailaddr) == 0) { // we don't know user's mail address |
||
| 212 | return FALSE; |
||
| 213 | } |
||
| 214 | common\Entity::intoThePotatoes(); |
||
| 215 | $mail = \core\common\OutsideComm::mailHandle(); |
||
| 216 | // who to whom? |
||
| 217 | $mail->FromName = \config\Master::APPEARANCE['productname'] . " Notification System"; |
||
| 218 | $mail->addReplyTo(\config\Master::APPEARANCE['support-contact']['developer-mail'], \config\Master::APPEARANCE['productname'] . " " . _("Feedback")); |
||
| 219 | $mail->addAddress($mailaddr[0]["value"]); |
||
| 220 | // what do we want to say? |
||
| 221 | $mail->Subject = $subject; |
||
| 222 | $mail->Body = $content; |
||
| 223 | |||
| 224 | $sent = $mail->send(); |
||
| 225 | common\Entity::outOfThePotatoes(); |
||
| 226 | return $sent; |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * NOOP in this class, only need to override abstract base class |
||
| 231 | * |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | public function updateFreshness() |
||
| 236 | // User is always fresh |
||
| 237 | } |
||
| 238 | |||
| 239 | const PROVIDER_STRINGS = [ |
||
| 240 | "eduPersonTargetedID" => "eduGAIN", |
||
| 241 | "facebook_targetedID" => "Facebook", |
||
| 242 | "google_eppn" => "Google", |
||
| 243 | "linkedin_targetedID" => "LinkedIn", |
||
| 244 | "twitter_targetedID" => "Twitter", |
||
| 245 | "openid" => "Google (defunct)", |
||
| 246 | ]; |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Some users apparently forget which eduGAIN/social ID they originally used |
||
| 250 | * to log into CAT. We can try to help them: if they tell us the email |
||
| 251 | * address by which they received the invitation token, then we can see if |
||
| 252 | * any CAT IdPs are associated to an account which originally came in via |
||
| 253 | * that email address. We then see which pretty-print auth provider name |
||
| 254 | * was used |
||
| 255 | * |
||
| 256 | * @param string $mail mail address to search with |
||
| 257 | * @param string $lang language for the eduGAIN request |
||
| 258 | * @return boolean|array the list of auth source IdPs we found for the mail, or FALSE if none found or invalid input |
||
| 259 | */ |
||
| 260 | public static function findLoginIdPByEmail($mail, $lang) |
||
| 334 | } |
||
| 335 | } |