diff --git a/resources/lib/UnityConfig.php b/resources/lib/UnityConfig.php index ce8f05b5..e844400d 100644 --- a/resources/lib/UnityConfig.php +++ b/resources/lib/UnityConfig.php @@ -4,8 +4,10 @@ class UnityConfig { - public static function getConfig($def_config_loc, $deploy_loc) - { + public static function getConfig( + string $def_config_loc, + string $deploy_loc, + ): array { $CONFIG = parse_ini_file( $def_config_loc . "/config.ini.default", true, @@ -22,7 +24,7 @@ public static function getConfig($def_config_loc, $deploy_loc) return $CONFIG; } - private static function pullConfig($CONFIG, $loc) + private static function pullConfig(array $CONFIG, string $loc): array { $file_loc = $loc . "/config/config.ini"; if (file_exists($file_loc)) { diff --git a/resources/lib/UnityGithub.php b/resources/lib/UnityGithub.php index 33417f77..cea01e4f 100644 --- a/resources/lib/UnityGithub.php +++ b/resources/lib/UnityGithub.php @@ -4,7 +4,7 @@ class UnityGithub { - public function getSshPublicKeys($username) + public function getSshPublicKeys(string $username): array { $url = "https://api.github.com/users/$username/keys"; $headers = ["User-Agent: Unity Cluster User Portal"]; diff --git a/resources/lib/UnityGroup.php b/resources/lib/UnityGroup.php index 54f98d72..52dc3d1d 100644 --- a/resources/lib/UnityGroup.php +++ b/resources/lib/UnityGroup.php @@ -1,6 +1,7 @@ - * @param LDAP $LDAP LDAP Connection - * @param SQL $SQL SQL Connection - */ - public function __construct($gid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK) - { + public const string PI_PREFIX = "pi_"; + + public string $gid; + private LDAPEntry $entry; + private UnityLDAP $LDAP; + private UnitySQL $SQL; + private UnityMailer $MAILER; + private UnityWebhook $WEBHOOK; + private UnityRedis $REDIS; + + public function __construct( + string $gid, + UnityLDAP $LDAP, + UnitySQL $SQL, + UnityMailer $MAILER, + UnityRedis $REDIS, + UnityWebhook $WEBHOOK, + ) { $gid = trim($gid); $this->gid = $gid; $this->entry = $LDAP->getPIGroupEntry($gid); @@ -40,7 +39,7 @@ public function __construct($gid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK) $this->WEBHOOK = $WEBHOOK; } - public function equals($other_group) + public function equals(UnityGroup $other_group): bool { if (!is_a($other_group, self::class)) { throw new Exception( @@ -53,29 +52,27 @@ public function equals($other_group) return $this->gid == $other_group->gid; } - public function __toString() + public function __toString(): string { return $this->gid; } /** * Checks if the current PI is an approved and existent group - * - * @return bool true if yes, false if no */ - public function exists() + public function exists(): bool { return $this->entry->exists(); } public function requestGroup( - $firstname, - $lastname, - $email, - $org, - $send_mail_to_admins, - $send_mail = true, - ) { + string $firstname, + string $lastname, + string $email, + string $org, + bool $send_mail_to_admins, + bool $send_mail = true, + ): void { if ($this->exists()) { return; } @@ -117,8 +114,10 @@ public function requestGroup( /** * This method will create the group (this is what is executed when an admin approved the group) */ - public function approveGroup($operator = null, $send_mail = true) - { + public function approveGroup( + ?UnityUser $operator = null, + bool $send_mail = true, + ): void { $uid = $this->getOwner()->uid; $request = $this->SQL->getRequest($uid, UnitySQL::REQUEST_BECOME_PI); if ($this->exists()) { @@ -152,8 +151,10 @@ public function approveGroup($operator = null, $send_mail = true) /** * This method is executed when an admin denys the PI group request */ - public function denyGroup($operator = null, $send_mail = true) - { + public function denyGroup( + ?UnityUser $operator = null, + bool $send_mail = true, + ): void { $request = $this->SQL->getRequest( $this->getOwner()->uid, UnitySQL::REQUEST_BECOME_PI, @@ -176,7 +177,7 @@ public function denyGroup($operator = null, $send_mail = true) } } - public function cancelGroupRequest($send_mail = true) + public function cancelGroupRequest(bool $send_mail = true): void { if (!$this->SQL->requestExists($this->getOwner()->uid)) { return; @@ -189,8 +190,10 @@ public function cancelGroupRequest($send_mail = true) } } - public function cancelGroupJoinRequest($user, $send_mail = true) - { + public function cancelGroupJoinRequest( + UnityUser $user, + bool $send_mail = true, + ): void { if (!$this->requestExists($user)) { return; } @@ -245,8 +248,10 @@ public function cancelGroupJoinRequest($user, $send_mail = true) * This method is executed when a user is approved to join the group * (either by admin or the group owner) */ - public function approveUser($new_user, $send_mail = true) - { + public function approveUser( + UnityUser $new_user, + bool $send_mail = true, + ): void { $request = $this->SQL->getRequest($new_user->uid, $this->gid); if (!$new_user->exists()) { $new_user->init( @@ -277,7 +282,7 @@ public function approveUser($new_user, $send_mail = true) } } - public function denyUser($new_user, $send_mail = true) + public function denyUser(UnityUser $new_user, bool $send_mail = true): void { $request = $this->SQL->getRequest($new_user->uid, $this->gid); // remove request, this will fail silently if the request doesn't exist @@ -300,8 +305,10 @@ public function denyUser($new_user, $send_mail = true) } } - public function removeUser($new_user, $send_mail = true) - { + public function removeUser( + UnityUser $new_user, + bool $send_mail = true, + ): void { if (!$this->memberExists($new_user)) { return; } @@ -333,13 +340,13 @@ public function removeUser($new_user, $send_mail = true) } public function newUserRequest( - $new_user, - $firstname, - $lastname, - $email, - $org, - $send_mail = true, - ) { + UnityUser $new_user, + string $firstname, + string $lastname, + string $email, + string $org, + bool $send_mail = true, + ): void { if ($this->memberExists($new_user)) { UnityHTTPD::errorLog( "warning", @@ -377,7 +384,7 @@ public function newUserRequest( } } - public function getRequests() + public function getRequests(): array { $requests = $this->SQL->getRequests($this->gid); $out = []; @@ -402,7 +409,7 @@ public function getRequests() return $out; } - public function getGroupMembers($ignorecache = false) + public function getGroupMembers(bool $ignorecache = false): array { $members = $this->getGroupMemberUIDs($ignorecache); $out = []; @@ -420,7 +427,7 @@ public function getGroupMembers($ignorecache = false) return $out; } - public function getGroupMemberUIDs($ignorecache = false) + public function getGroupMemberUIDs(bool $ignorecache = false): array { if (!$ignorecache) { $cached_val = $this->REDIS->getCache($this->gid, "members"); @@ -440,7 +447,7 @@ public function getGroupMemberUIDs($ignorecache = false) return $members; } - public function requestExists($user) + public function requestExists(UnityUser $user): bool { $requesters = $this->getRequests(); if (count($requesters) > 0) { @@ -453,7 +460,7 @@ public function requestExists($user) return false; } - private function init() + private function init(): void { $owner = $this->getOwner(); \ensure(!$this->entry->exists()); @@ -467,7 +474,7 @@ private function init() // we need to update the cache here with the memberuid } - private function addUserToGroup($new_user) + private function addUserToGroup(UnityUser $new_user): void { $this->entry->appendAttribute("memberuid", $new_user->uid); $this->entry->write(); @@ -475,7 +482,7 @@ private function addUserToGroup($new_user) $this->REDIS->appendCacheArray($new_user->uid, "groups", $this->gid); } - private function removeUserFromGroup($old_user) + private function removeUserFromGroup(UnityUser $old_user): void { $this->entry->removeAttributeEntryByValue("memberuid", $old_user->uid); $this->entry->write(); @@ -483,13 +490,18 @@ private function removeUserFromGroup($old_user) $this->REDIS->removeCacheArray($old_user->uid, "groups", $this->gid); } - public function memberExists($user) + public function memberExists(UnityUser $user): bool { return in_array($user->uid, $this->getGroupMemberUIDs()); } - private function addRequest($uid, $firstname, $lastname, $email, $org) - { + private function addRequest( + string $uid, + string $firstname, + string $lastname, + string $email, + string $org, + ): void { $this->SQL->addRequest( $uid, $firstname, @@ -500,7 +512,7 @@ private function addRequest($uid, $firstname, $lastname, $email, $org) ); } - public function getOwner() + public function getOwner(): UnityUser { return new UnityUser( self::GID2OwnerUID($this->gid), @@ -512,12 +524,12 @@ public function getOwner() ); } - public static function ownerUID2GID($uid) + public static function ownerUID2GID(string $uid): string { return self::PI_PREFIX . $uid; } - public static function GID2OwnerUID($gid) + public static function GID2OwnerUID(string $gid): string { if (substr($gid, 0, strlen(self::PI_PREFIX)) != self::PI_PREFIX) { throw new Exception( diff --git a/resources/lib/UnityHTTPD.php b/resources/lib/UnityHTTPD.php index bdadb22b..6504f0cb 100644 --- a/resources/lib/UnityHTTPD.php +++ b/resources/lib/UnityHTTPD.php @@ -7,7 +7,7 @@ class UnityHTTPD { - public static function die($x = null, $show_user = false) + public static function die(mixed $x = null, bool $show_user = false): never { if (CONFIG["site"]["allow_die"] == false) { if (is_null($x)) { @@ -24,7 +24,7 @@ public static function die($x = null, $show_user = false) } } - public static function redirect($dest) + public static function redirect($dest): never { header("Location: $dest"); self::errorToUser( @@ -41,7 +41,7 @@ public static function errorLog( ?string $errorid = null, ?\Throwable $error = null, mixed $data = null, - ) { + ): void { if (!CONFIG["site"]["enable_verbose_error_log"]) { error_log("$title: $message"); return; @@ -93,7 +93,7 @@ private static function errorToUser( string $msg, int $http_response_code, ?string $errorid = null, - ) { + ): void { if (!CONFIG["site"]["enable_error_to_user"]) { return; } @@ -110,8 +110,11 @@ private static function errorToUser( echo "

$msg

$notes

"; } - public static function badRequest($message, $error = null, $data = null) - { + public static function badRequest( + string $message, + ?\Throwable $error = null, + ?array $data = null, + ): never { $errorid = uniqid(); self::errorToUser( "Invalid requested action or submitted data.", @@ -122,8 +125,11 @@ public static function badRequest($message, $error = null, $data = null) self::die($message); } - public static function forbidden($message, $error = null, $data = null) - { + public static function forbidden( + string $message, + ?\Throwable $error = null, + ?array $data = null, + ): never { $errorid = uniqid(); self::errorToUser("Permission denied.", 403, $errorid); self::errorLog("forbidden", $message, $errorid, $error, $data); @@ -131,10 +137,10 @@ public static function forbidden($message, $error = null, $data = null) } public static function internalServerError( - $message, - $error = null, - $data = null, - ) { + string $message, + ?\Throwable $error = null, + ?array $data = null, + ): never { $errorid = uniqid(); self::errorToUser( "An internal server error has occurred.", @@ -152,7 +158,7 @@ public static function internalServerError( } // https://www.php.net/manual/en/function.set-exception-handler.php - public static function exceptionHandler($e) + public static function exceptionHandler(\Throwable $e): void { ini_set("log_errors", true); // in case something goes wrong and error is not logged self::internalServerError( @@ -162,7 +168,7 @@ public static function exceptionHandler($e) ini_set("log_errors", false); // error logged successfully } - public static function getPostData(...$keys) + public static function getPostData(...$keys): mixed { try { return \arrayGet($_POST, ...$keys); @@ -174,10 +180,10 @@ public static function getPostData(...$keys) } public static function getUploadedFileContents( - $filename, - $do_delete_tmpfile_after_read = true, - $encoding = "UTF-8", - ) { + string $filename, + bool $do_delete_tmpfile_after_read = true, + string $encoding = "UTF-8", + ): string { try { $tmpfile_path = \arrayGet($_FILES, $filename, "tmp_name"); } catch (ArrayKeyException $e) { @@ -198,7 +204,7 @@ public static function getUploadedFileContents( // in firefox, the user can disable alert/confirm/prompt after the 2nd or 3rd popup // after I disable alerts, if I quit and reopen my browser, the alerts come back - public static function alert(string $message) + public static function alert(string $message): void { // jsonEncode escapes quotes echo "