Total Complexity | 71 |
Total Lines | 497 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
Complex classes like Federation 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 Federation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
55 | class Federation extends EntityWithDBProperties { |
||
56 | |||
57 | /** |
||
58 | * the handle to the FRONTEND database (only needed for some stats access) |
||
59 | * |
||
60 | * @var DBConnection |
||
61 | */ |
||
62 | private $frontendHandle; |
||
63 | |||
64 | /** |
||
65 | * the top-level domain of the Federation |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | public $tld; |
||
70 | |||
71 | /** |
||
72 | * retrieve the statistics from the database in an internal array representation |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | private function downloadStatsCore() { |
||
77 | $grossAdmin = 0; |
||
78 | $grossUser = 0; |
||
79 | $grossSilverbullet = 0; |
||
80 | $dataArray = []; |
||
81 | // first, find out which profiles belong to this federation |
||
82 | $cohesionQuery = "SELECT downloads.device_id as dev_id, sum(downloads.downloads_user) as dl_user, sum(downloads.downloads_silverbullet) as dl_sb, sum(downloads.downloads_admin) as dl_admin FROM profile, institution, downloads WHERE profile.inst_id = institution.inst_id AND institution.country = ? AND profile.profile_id = downloads.profile_id group by device_id"; |
||
83 | $profilesList = $this->databaseHandle->exec($cohesionQuery, "s", $this->tld); |
||
84 | $deviceArray = \devices\Devices::listDevices(); |
||
85 | // SELECT -> resource, no boolean |
||
86 | while ($queryResult = mysqli_fetch_object(/** @scrutinizer ignore-type */ $profilesList)) { |
||
87 | if (isset($deviceArray[$queryResult->dev_id])) { |
||
88 | $displayName = $deviceArray[$queryResult->dev_id]['display']; |
||
89 | } else { // this device has stats, but doesn't exist in current config. We don't even know its display name, so display its raw representation |
||
90 | $displayName = sprintf(_("(discontinued) %s"), $queryResult->dev_id); |
||
91 | } |
||
92 | $dataArray[$displayName] = ["ADMIN" => $queryResult->dl_admin, "SILVERBULLET" => $queryResult->dl_sb, "USER" => $queryResult->dl_user]; |
||
93 | $grossAdmin = $grossAdmin + $queryResult->dl_admin; |
||
94 | $grossSilverbullet = $grossSilverbullet + $queryResult->dl_sb; |
||
95 | $grossUser = $grossUser + $queryResult->dl_user; |
||
96 | } |
||
97 | $dataArray["TOTAL"] = ["ADMIN" => $grossAdmin, "SILVERBULLET" => $grossSilverbullet, "USER" => $grossUser]; |
||
98 | return $dataArray; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * when a Federation attribute changes, invalidate caches of all IdPs |
||
103 | * in that federation (e.g. change of fed logo changes the actual |
||
104 | * installers) |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | public function updateFreshness() { |
||
109 | $idplist = $this->listIdentityProviders(); |
||
110 | foreach ($idplist as $idpDetail) { |
||
111 | $idpDetail['instance']->updateFreshness(); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * gets the download statistics for the federation |
||
117 | * @param string $format either as an html *table* or *XML* or *JSON* |
||
118 | * @return string|array |
||
119 | */ |
||
|
|||
120 | public function downloadStats($format) { |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * |
||
157 | * Constructs a Federation object. |
||
158 | * |
||
159 | * @param string $fedname textual representation of the Federation object |
||
160 | * Example: "lu" (for Luxembourg) |
||
161 | */ |
||
162 | public function __construct($fedname) { |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Creates a new IdP inside the federation. |
||
218 | * |
||
219 | * @param string $ownerId Persistent identifier of the user for whom this IdP is created (first administrator) |
||
220 | * @param string $level Privilege level of the first administrator (was he blessed by a federation admin or a peer?) |
||
221 | * @param string $mail e-mail address with which the user was invited to administer (useful for later user identification if the user chooses a "funny" real name) |
||
222 | * @param string $bestnameguess name of the IdP, if already known, in the best-match language |
||
223 | * @return int identifier of the new IdP |
||
224 | */ |
||
225 | public function newIdP($ownerId, $level, $mail = NULL, $bestnameguess = NULL) { |
||
272 | } |
||
273 | |||
274 | private $idpListAll; |
||
275 | private $idpListActive; |
||
276 | |||
277 | /** |
||
278 | * fetches all known certificate information for RADIUS/TLS certs from the DB |
||
279 | * |
||
280 | * @return array |
||
281 | */ |
||
282 | public function listTlsCertificates() { |
||
283 | $certQuery = "SELECT ca_name, request_serial, distinguished_name, status, expiry, certificate, revocation_pin FROM federation_servercerts WHERE federation_id = ?"; |
||
284 | $upperTld = strtoupper($this->tld); |
||
285 | $certList = $this->databaseHandle->exec($certQuery, "s", $upperTld); |
||
286 | $retArray = []; |
||
287 | // SELECT -> resource, not boolean |
||
288 | while ($certListResult = mysqli_fetch_object(/** @scrutinizer ignore-type */ $certList)) { |
||
289 | $retArray[] = [ |
||
290 | 'CA' => $certListResult->ca_name, |
||
291 | 'REQSERIAL' => $certListResult->request_serial, |
||
292 | 'DN' => $certListResult->distinguished_name, |
||
293 | 'STATUS' => $certListResult->status, |
||
294 | 'EXPIRY' => $certListResult->expiry, |
||
295 | 'CERT' => $certListResult->certificate, |
||
296 | 'REVPIN' => $certListResult->revocation_pin, |
||
297 | ]; |
||
298 | } |
||
299 | return$retArray; |
||
300 | } |
||
301 | |||
302 | public function requestCertificate($csr, $expiryDays) { |
||
310 | } |
||
311 | |||
312 | public function updateCertificateStatus($reqSerial) { |
||
313 | $ca = new CertificationAuthorityEduPkiServer(); |
||
314 | $entryInQuestion = $ca->pickupFinalCert($reqSerial, FALSE); |
||
315 | if ($entryInQuestion === FALSE) { |
||
316 | return; // no update to fetch |
||
317 | } |
||
318 | $certDetails = openssl_x509_parse($entryInQuestion['CERT']); |
||
319 | echo $certDetails['full_details']; |
||
320 | openssl_x509_export($entryInQuestion['CERT'], $pem); |
||
321 | $updateQuery = "UPDATE federation_servercerts SET status = 'ISSUED', certificate = ?, expiry = ? WHERE ca_name = 'eduPKI' AND request_serial = ?"; |
||
322 | $this->databaseHandle->exec($updateQuery, "ssi", $pem, $certDetails['full_details']['NotAfter'], $reqSerial); |
||
323 | } |
||
324 | |||
325 | public function triggerRevocation($reqSerial) { |
||
326 | // revocation at the CA side works with the serial of the certificate, not the request |
||
327 | // so find that out first |
||
328 | $certInfoResource = $this->databaseHandle->exec("SELECT certificate FROM federation_servercerts WHERE ca_name = 'eduPKI' AND request_serial = ?", "i", $reqSerial); |
||
329 | $certInfo = mysqli_fetch_row($certInfoResource); |
||
330 | $certData = openssl_x509_parse($certInfo); |
||
331 | $serial = $certData['full_details']['serialNumber']; |
||
332 | $eduPki = new CertificationAuthorityEduPkiServer(); |
||
333 | $eduPki->revokeCertificate($serial); |
||
334 | $this->databaseHandle->exec("UPDATE federation_servercerts SET status = 'REVOKED' WHERE ca_name = 'eduPKI' AND request_serial = ?", "i", $reqSerial); |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Lists all Identity Providers in this federation |
||
339 | * |
||
340 | * @param int $activeOnly if set to non-zero will list only those institutions which have some valid profiles defined. |
||
341 | * @return array (Array of IdP instances) |
||
342 | * |
||
343 | */ |
||
344 | public function listIdentityProviders($activeOnly = 0) { |
||
345 | // maybe we did this exercise before? |
||
346 | if ($activeOnly != 0 && count($this->idpListActive) > 0) { |
||
347 | return $this->idpListActive; |
||
348 | } |
||
349 | if ($activeOnly == 0 && count($this->idpListAll) > 0) { |
||
350 | return $this->idpListAll; |
||
351 | } |
||
352 | // default query is: |
||
353 | $allIDPs = $this->databaseHandle->exec("SELECT inst_id FROM institution |
||
354 | WHERE country = '$this->tld' ORDER BY inst_id"); |
||
355 | // the one for activeOnly is much more complex: |
||
356 | if ($activeOnly) { |
||
357 | $allIDPs = $this->databaseHandle->exec("SELECT distinct institution.inst_id AS inst_id |
||
358 | FROM institution |
||
359 | JOIN profile ON institution.inst_id = profile.inst_id |
||
360 | WHERE institution.country = '$this->tld' |
||
361 | AND profile.showtime = 1 |
||
362 | ORDER BY inst_id"); |
||
363 | } |
||
364 | |||
365 | $returnarray = []; |
||
366 | // SELECT -> resource, not boolean |
||
367 | while ($idpQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $allIDPs)) { |
||
368 | $idp = new IdP($idpQuery->inst_id); |
||
369 | $name = $idp->name; |
||
370 | $idpInfo = ['entityID' => $idp->identifier, |
||
371 | 'title' => $name, |
||
372 | 'country' => strtoupper($idp->federation), |
||
373 | 'instance' => $idp]; |
||
374 | $returnarray[$idp->identifier] = $idpInfo; |
||
375 | } |
||
376 | if ($activeOnly != 0) { // we're only doing this once. |
||
377 | $this->idpListActive = $returnarray; |
||
378 | } else { |
||
379 | $this->idpListAll = $returnarray; |
||
380 | } |
||
381 | return $returnarray; |
||
382 | } |
||
383 | |||
384 | /** |
||
385 | * returns an array with information about the authorised administrators of the federation |
||
386 | * |
||
387 | * @return array list of the admins of this federation |
||
388 | */ |
||
389 | public function listFederationAdmins() { |
||
390 | $returnarray = []; |
||
391 | $query = "SELECT user_id FROM user_options WHERE option_name = 'user:fedadmin' AND option_value = ?"; |
||
392 | if (CONFIG_CONFASSISTANT['CONSORTIUM']['name'] == "eduroam" && isset(CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo']) && CONFIG_CONFASSISTANT['CONSORTIUM']['deployment-voodoo'] == "Operations Team") { // SW: APPROVED |
||
393 | $query = "SELECT eptid as user_id FROM view_admin WHERE role = 'fedadmin' AND realm = ?"; |
||
394 | } |
||
395 | $userHandle = DBConnection::handle("USER"); // we need something from the USER database for a change |
||
396 | $upperFed = strtoupper($this->tld); |
||
397 | // SELECT -> resource, not boolean |
||
398 | $admins = $userHandle->exec($query, "s", $upperFed); |
||
399 | |||
400 | while ($fedAdminQuery = mysqli_fetch_object(/** @scrutinizer ignore-type */ $admins)) { |
||
401 | $returnarray[] = $fedAdminQuery->user_id; |
||
402 | } |
||
403 | return $returnarray; |
||
404 | } |
||
405 | |||
406 | public const EDUROAM_DB_TYPE_IDP = "1"; |
||
407 | public const EDUROAM_DB_TYPE_SP = "2"; |
||
408 | public const EDUROAM_DB_TYPE_IDPSP = "3"; |
||
409 | |||
410 | /** |
||
411 | * cross-checks in the EXTERNAL customer DB which institutions exist there for the federations |
||
412 | * |
||
413 | * @param bool $unmappedOnly if set to TRUE, only returns those which do not have a known mapping to our internally known institutions |
||
414 | * @param string $type type of institution to search for, see constants above |
||
415 | * @return array |
||
416 | */ |
||
417 | public function listExternalEntities($unmappedOnly, $type) { |
||
484 | } |
||
485 | |||
486 | const UNKNOWN_IDP = -1; |
||
487 | const AMBIGUOUS_IDP = -2; |
||
488 | |||
489 | /** |
||
490 | * for a MySQL list of institutions, find an institution or find out that |
||
491 | * there is no single best match |
||
492 | * |
||
493 | * @param \mysqli_result $dbResult the query object to work with |
||
494 | * @param string $country used to return the country of the inst, if can be found out |
||
495 | * @return int the identifier of the inst, or one of the special return values if unsuccessful |
||
496 | */ |
||
497 | private static function findCandidates(\mysqli_result $dbResult, &$country) { |
||
513 | } |
||
514 | |||
515 | /** |
||
516 | * If we are running diagnostics, our input from the user is the realm. We |
||
517 | * need to find out which IdP this realm belongs to. |
||
518 | * @param string $realm the realm to search for |
||
519 | * @return array an array with two entries, CAT ID and DB ID, with either the respective ID of the IdP in the system, or UNKNOWN_IDP or AMBIGUOUS_IDP |
||
520 | */ |
||
521 | public static function determineIdPIdByRealm($realm) { |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * helper function to sort institutions by their name |
||
546 | * @param array $a an array with institution a's information |
||
547 | * @param array $b an array with institution b's information |
||
548 | * @return int the comparison result |
||
549 | */ |
||
550 | private function usortInstitution($a, $b) { |
||
552 | } |
||
553 | |||
554 | } |
||
555 |