From 8d1d10d1e79353bbaf55e2c7abf05ab1329da6b5 Mon Sep 17 00:00:00 2001 From: Simon Leary Date: Mon, 10 Nov 2025 16:09:18 -0500 Subject: [PATCH 1/2] throw and catch exception --- resources/autoload.php | 1 + resources/lib/UnityGroup.php | 14 +++++++------- resources/lib/UnityLDAP.php | 6 +++++- .../lib/exceptions/EntryNotFoundException.php | 5 +++++ test/phpunit-bootstrap.php | 1 + webroot/panel/groups.php | 6 +++++- webroot/panel/new_account.php | 6 +++++- 7 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 resources/lib/exceptions/EntryNotFoundException.php diff --git a/resources/autoload.php b/resources/autoload.php index 6ec01691..5d6dbcfa 100644 --- a/resources/autoload.php +++ b/resources/autoload.php @@ -28,6 +28,7 @@ require_once __DIR__ . "/lib/exceptions/NoDieException.php"; require_once __DIR__ . "/lib/exceptions/SSOException.php"; require_once __DIR__ . "/lib/exceptions/ArrayKeyException.php"; +require_once __DIR__ . "/lib/exceptions/EntryNotFoundException.php"; require_once __DIR__ . "/lib/exceptions/EnsureException.php"; require_once __DIR__ . "/lib/exceptions/EncodingUnknownException.php"; require_once __DIR__ . "/lib/exceptions/EncodingConversionException.php"; diff --git a/resources/lib/UnityGroup.php b/resources/lib/UnityGroup.php index cbce33db..a7fac375 100644 --- a/resources/lib/UnityGroup.php +++ b/resources/lib/UnityGroup.php @@ -478,14 +478,14 @@ public static function GID2OwnerUID(string $gid): string return substr($gid, strlen(self::PI_PREFIX)); } - public static function ownerMail2GID($email) + /** + * @throws \UnityWebPortal\lib\exceptions\EntryNotFoundException + */ + public static function ownerMail2GID(string $email): string { global $LDAP; - $entry = $LDAP->getUidFromEmail($email); - if ($entry !== null) { - $ownerUid = $entry->getAttribute("cn")[0]; - return self::PI_PREFIX . $ownerUid; - } - return $email; // Leave untouched + $entry = $LDAP->getUidFromEmail($email); // throws EntryNotFoundException + $ownerUid = $entry->getAttribute("cn")[0]; + return self::PI_PREFIX . $ownerUid; } } diff --git a/resources/lib/UnityLDAP.php b/resources/lib/UnityLDAP.php index 9a6f9bc2..e4f89144 100644 --- a/resources/lib/UnityLDAP.php +++ b/resources/lib/UnityLDAP.php @@ -429,12 +429,16 @@ public function getOrgGroupEntry(string $gid): LDAPEntry return $this->getEntry(UnityLDAP::RDN . "=$gid," . CONFIG["ldap"]["orggroup_ou"]); } - public function getUidFromEmail($email) + /** + * @throws \UnityWebPortal\lib\exceptions\EntryNotFoundException + */ + public function getUidFromEmail(string $email): LDAPEntry { $email = ldap_escape($email, "", LDAP_ESCAPE_FILTER); $cn = $this->search("mail=$email", CONFIG["ldap"]["user_ou"], ["cn"]); if ($cn && count($cn) == 1) { return $cn[0]; } + throw new exceptions\EntryNotFoundException($email); } } diff --git a/resources/lib/exceptions/EntryNotFoundException.php b/resources/lib/exceptions/EntryNotFoundException.php new file mode 100644 index 00000000..c6007905 --- /dev/null +++ b/resources/lib/exceptions/EntryNotFoundException.php @@ -0,0 +1,5 @@ +exists()) { diff --git a/webroot/panel/new_account.php b/webroot/panel/new_account.php index 88f637d2..604f95a4 100644 --- a/webroot/panel/new_account.php +++ b/webroot/panel/new_account.php @@ -2,6 +2,7 @@ require_once __DIR__ . "/../../resources/autoload.php"; +use UnityWebPortal\lib\exceptions\EntryNotFoundException; use UnityWebPortal\lib\UnityHTTPD; use UnityWebPortal\lib\UnityGroup; use UnityWebPortal\lib\UnitySQL; @@ -26,7 +27,10 @@ if ($_POST["new_user_sel"] == "not_pi") { $pi_groupname = $_POST["pi"]; if (substr($pi_groupname, 0, 3) !== "pi_" && str_contains($pi_groupname, "@")) { - $pi_groupname = UnityGroup::ownerMail2GID($pi_groupname); + try { + $pi_groupname = UnityGroup::ownerMail2GID($pi_groupname); + } catch (EntryNotFoundException) { + } } $form_group = new UnityGroup($pi_groupname, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); if (!$form_group->exists()) { From 7b884d645a01d3b0fc3e6b4dedd444a4d7fb487c Mon Sep 17 00:00:00 2001 From: Simon Leary Date: Mon, 10 Nov 2025 16:53:31 -0500 Subject: [PATCH 2/2] assert modal error --- test/functional/PIMemberRequestTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/functional/PIMemberRequestTest.php b/test/functional/PIMemberRequestTest.php index 51a6f705..71072fef 100644 --- a/test/functional/PIMemberRequestTest.php +++ b/test/functional/PIMemberRequestTest.php @@ -42,6 +42,8 @@ public function testRequestMembership() $this->assertTrue($SQL->requestExists($uid, $gid)); $this->cancelRequest($gid); $this->assertFalse($SQL->requestExists($uid, $gid)); + $this->requestMembership("asdlkjasldkj"); + $this->assertContains("This PI doesn't exist", $_SESSION["MODAL_ERRORS"]); $this->requestMembership($pi_group->getOwner()->getMail()); $this->assertTrue($SQL->requestExists($uid, $gid)); } finally {