Total Complexity | 64 |
Total Lines | 383 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like IdP 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 IdP, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class IdP extends EntityWithDBProperties |
||
50 | { |
||
51 | |||
52 | const EXTERNAL_DB_SYNCSTATE_NOT_SYNCED = 0; |
||
53 | const EXTERNAL_DB_SYNCSTATE_SYNCED = 1; |
||
54 | const EXTERNAL_DB_SYNCSTATE_NOTSUBJECTTOSYNCING = 2; |
||
55 | |||
56 | /** |
||
57 | * |
||
58 | * @var integer synchronisation state with external database, if any |
||
59 | */ |
||
60 | private $externalDbSyncstate; |
||
61 | |||
62 | /** |
||
63 | * The shortname of this IdP's federation |
||
64 | * @var string |
||
65 | */ |
||
66 | public $federation; |
||
67 | |||
68 | /** |
||
69 | * Constructs an IdP object based on its details in the database. |
||
70 | * Cannot be used to define a new IdP in the database! This happens via Federation::newIdP() |
||
71 | * |
||
72 | * @param int $instId the database row identifier |
||
73 | */ |
||
|
|||
74 | public function __construct(int $instId) |
||
75 | { |
||
76 | $this->databaseType = "INST"; |
||
77 | parent::__construct(); // now databaseHandle and logging is available |
||
78 | $this->entityOptionTable = "institution_option"; |
||
79 | $this->entityIdColumn = "institution_id"; |
||
80 | |||
81 | $this->identifier = $instId; |
||
82 | |||
83 | $idp = $this->databaseHandle->exec("SELECT inst_id, country,external_db_syncstate FROM institution WHERE inst_id = $this->identifier"); |
||
84 | // SELECT -> returns resource, not boolean |
||
85 | if (!$instQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $idp)) { |
||
86 | throw new Exception("IdP $this->identifier not found in database!"); |
||
87 | } |
||
88 | |||
89 | $this->federation = $instQuery->country; |
||
90 | $this->externalDbSyncstate = $instQuery->external_db_syncstate; |
||
91 | |||
92 | // fetch attributes from DB; populates $this->attributes array |
||
93 | $this->attributes = $this->retrieveOptionsFromDatabase("SELECT DISTINCT option_name, option_lang, option_value, row |
||
94 | FROM $this->entityOptionTable |
||
95 | WHERE $this->entityIdColumn = ? |
||
96 | ORDER BY option_name", "IdP"); |
||
97 | |||
98 | $this->attributes[] = ["name" => "internal:country", |
||
99 | "lang" => NULL, |
||
100 | "value" => $this->federation, |
||
101 | "level" => "IdP", |
||
102 | "row" => 0, |
||
103 | "flag" => NULL]; |
||
104 | |||
105 | $this->name = $this->languageInstance->getLocalisedValue($this->getAttributes('general:instname')); |
||
106 | $this->loggerInstance->debug(3, "--- END Constructing new IdP object ... ---\n"); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * This function retrieves all registered profiles for this IdP from the database |
||
111 | * |
||
112 | * @param bool $activeOnly if and set to non-zero will cause listing of only those institutions which have some valid profiles defined. |
||
113 | * @return array<AbstractProfile> list of Profiles of this IdP |
||
114 | */ |
||
115 | public function listProfiles(bool $activeOnly = FALSE) |
||
116 | { |
||
117 | $query = "SELECT profile_id FROM profile WHERE inst_id = $this->identifier" . ($activeOnly ? " AND showtime = 1" : ""); |
||
118 | $allProfiles = $this->databaseHandle->exec($query); |
||
119 | $returnarray = []; |
||
120 | // SELECT -> resource, not boolean |
||
121 | while ($profileQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allProfiles)) { |
||
122 | $oneProfile = ProfileFactory::instantiate($profileQuery->profile_id, $this); |
||
123 | $oneProfile->institution = $this->identifier; |
||
124 | $returnarray[] = $oneProfile; |
||
125 | } |
||
126 | |||
127 | $this->loggerInstance->debug(4, "listProfiles: " . print_r($returnarray, true)); |
||
128 | return $returnarray; |
||
129 | } |
||
130 | |||
131 | const PROFILES_INCOMPLETE = 0; |
||
132 | const PROFILES_CONFIGURED = 1; |
||
133 | const PROFILES_SHOWTIME = 2; |
||
134 | |||
135 | /** |
||
136 | * looks through all the profiles of the inst and determines the highest prod-ready level among the profiles |
||
137 | * @return int highest level of completeness of all the profiles of the inst |
||
138 | */ |
||
139 | public function maxProfileStatus() |
||
140 | { |
||
141 | $allProfiles = $this->databaseHandle->exec("SELECT sufficient_config + showtime AS maxlevel FROM profile WHERE inst_id = $this->identifier ORDER BY maxlevel DESC LIMIT 1"); |
||
142 | // SELECT yields a resource, not a boolean |
||
143 | while ($res = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allProfiles)) { |
||
144 | return $res->maxlevel; |
||
145 | } |
||
146 | return self::PROFILES_INCOMPLETE; |
||
147 | } |
||
148 | |||
149 | /** This function retrieves an array of authorised users which can |
||
150 | * manipulate this institution. |
||
151 | * |
||
152 | * @return array owners of the institution; numbered array with members ID, MAIL and LEVEL |
||
153 | */ |
||
154 | public function listOwners() |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Primary owners are allowed to invite other (secondary) admins to the institution |
||
167 | * |
||
168 | * @param string $user ID of a logged-in user |
||
169 | * @return boolean TRUE if this user is an admin with FED-level blessing |
||
170 | */ |
||
171 | public function isPrimaryOwner($user) |
||
172 | { |
||
173 | foreach ($this->listOwners() as $oneOwner) { |
||
174 | if ($oneOwner['ID'] == $user && $oneOwner['LEVEL'] == "FED") { |
||
175 | return TRUE; |
||
176 | } |
||
177 | } |
||
178 | return FALSE; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * This function gets the profile count for a given IdP. |
||
183 | * |
||
184 | * The count could be retreived from the listProfiles method |
||
185 | * but this is less expensive. |
||
186 | * |
||
187 | * @return int profile count |
||
188 | */ |
||
189 | public function profileCount() |
||
190 | { |
||
191 | $result = $this->databaseHandle->exec("SELECT profile_id FROM profile |
||
192 | WHERE inst_id = $this->identifier"); |
||
193 | // SELECT -> resource, not boolean |
||
194 | return(mysqli_num_rows(/** @scrutinizer ignore-type */ $result)); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * This function sets the timestamp of last modification of the child profiles to the current timestamp. |
||
199 | * |
||
200 | * This is needed for installer caching: all installers which are on disk |
||
201 | * must be re-created if an attribute changes. This timestamp here |
||
202 | * is used to determine if the installer on disk is still new enough. |
||
203 | * |
||
204 | * @return void |
||
205 | */ |
||
206 | public function updateFreshness() |
||
207 | { |
||
208 | // freshness is always defined for *Profiles* |
||
209 | // IdP needs to update timestamp of all its profiles if an IdP-wide attribute changed |
||
210 | $this->databaseHandle->exec("UPDATE profile SET last_change = CURRENT_TIMESTAMP WHERE inst_id = '$this->identifier'"); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Adds a new profile to this IdP. |
||
215 | * |
||
216 | * Only creates the DB entry for the Profile. If you want to add attributes later, see Profile::addAttribute(). |
||
217 | * |
||
218 | * @param string $type exactly "RADIUS" or "SILVERBULLET", all other values throw an Exception |
||
219 | * @return AbstractProfile|NULL new Profile object if successful, or NULL if an error occured |
||
220 | */ |
||
221 | public function newProfile(string $type) |
||
222 | { |
||
223 | $this->databaseHandle->exec("INSERT INTO profile (inst_id) VALUES($this->identifier)"); |
||
224 | $identifier = $this->databaseHandle->lastID(); |
||
225 | if ($identifier > 0) { |
||
226 | switch ($type) { |
||
227 | case AbstractProfile::PROFILETYPE_RADIUS: |
||
228 | return new ProfileRADIUS($identifier, $this); |
||
229 | case AbstractProfile::PROFILETYPE_SILVERBULLET: |
||
230 | $theProfile = new ProfileSilverbullet($identifier, $this); |
||
231 | $theProfile->addSupportedEapMethod(new \core\common\EAP(\core\common\EAP::EAPTYPE_SILVERBULLET), 1); |
||
232 | $theProfile->setRealm($this->identifier . "-" . $theProfile->identifier . "." . strtolower($this->federation) . strtolower(CONFIG_CONFASSISTANT['SILVERBULLET']['realm_suffix'])); |
||
233 | return $theProfile; |
||
234 | default: |
||
235 | throw new Exception("This type of profile is unknown and can not be added."); |
||
236 | } |
||
237 | } |
||
238 | return NULL; |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * deletes the IdP and all its profiles |
||
243 | * |
||
244 | * @return void |
||
245 | */ |
||
246 | public function destroy() |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * Performs a lookup in an external database to determine matching entities to this IdP. |
||
285 | * |
||
286 | * The business logic of this function is roaming consortium specific; if no match algorithm is known for the consortium, FALSE is returned. |
||
287 | * |
||
288 | * @return mixed list of entities in external database that correspond to this IdP or FALSE if no consortium-specific matching function is defined |
||
289 | */ |
||
290 | public function getExternalDBSyncCandidates() |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * returns the state of sync with the external DB. |
||
334 | * |
||
335 | * @return int |
||
336 | */ |
||
337 | public function getExternalDBSyncState() |
||
338 | { |
||
339 | if (CONFIG_CONFASSISTANT['CONSORTIUM']['name'] == "eduroam" && isset(CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo']) && CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
340 | return $this->externalDbSyncstate; |
||
341 | } |
||
342 | return self::EXTERNAL_DB_SYNCSTATE_NOTSUBJECTTOSYNCING; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Retrieves the external DB identifier of this institution. Returns FALSE if no ID is known. |
||
347 | * |
||
348 | * @return string|boolean the external identifier; or FALSE if no external ID is known |
||
349 | */ |
||
350 | public function getExternalDBId() |
||
351 | { |
||
352 | if (CONFIG_CONFASSISTANT['CONSORTIUM']['name'] == "eduroam" && isset(CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo']) && CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
353 | $idQuery = $this->databaseHandle->exec("SELECT external_db_id FROM institution WHERE inst_id = $this->identifier AND external_db_syncstate = " . self::EXTERNAL_DB_SYNCSTATE_SYNCED); |
||
354 | // SELECT -> it's a resource, not a boolean |
||
355 | if (mysqli_num_rows(/** @scrutinizer ignore-type */ $idQuery) == 0) { |
||
356 | return FALSE; |
||
357 | } |
||
358 | $externalIdQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $idQuery); |
||
359 | return $externalIdQuery->external_db_id; |
||
360 | } |
||
361 | return FALSE; |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Associates the external DB id with a CAT id |
||
366 | * |
||
367 | * @param string $identifier the external DB id, which can be alpha-numeric |
||
368 | * @return void |
||
369 | */ |
||
370 | public function setExternalDBId(string $identifier) |
||
378 | } |
||
379 | } |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * removes the link between a CAT institution and the external DB |
||
384 | * |
||
385 | * @return void |
||
386 | */ |
||
387 | public function removeExternalDBId() |
||
393 | } |
||
394 | } |
||
395 | } |
||
396 | |||
397 | public const INSTNAME_CHANGED = 1; |
||
398 | |||
399 | /** |
||
400 | * |
||
401 | * @param IdP $old the IdP instance with the old state |
||
402 | * @param IdP $new the IdP instance with the new state |
||
403 | * @return array list of changed things, and details about the change |
||
404 | */ |
||
405 | public static function significantChanges($old, $new) |
||
435 |