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 @@ +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 { diff --git a/test/phpunit-bootstrap.php b/test/phpunit-bootstrap.php index 4463c74c..48a5464c 100644 --- a/test/phpunit-bootstrap.php +++ b/test/phpunit-bootstrap.php @@ -19,6 +19,7 @@ require_once __DIR__ . "/../resources/lib/exceptions/NoDieException.php"; require_once __DIR__ . "/../resources/lib/exceptions/SSOException.php"; require_once __DIR__ . "/../resources/lib/exceptions/ArrayKeyException.php"; +require_once __DIR__ . "/../resources/lib/exceptions/EntryNotFoundException.php"; require_once __DIR__ . "/../resources/lib/exceptions/EnsureException.php"; require_once __DIR__ . "/../resources/lib/exceptions/EncodingUnknownException.php"; require_once __DIR__ . "/../resources/lib/exceptions/EncodingConversionException.php"; diff --git a/webroot/panel/groups.php b/webroot/panel/groups.php index 96c683d9..37f6d526 100644 --- a/webroot/panel/groups.php +++ b/webroot/panel/groups.php @@ -2,6 +2,7 @@ require_once __DIR__ . "/../../resources/autoload.php"; +use UnityWebPortal\lib\exceptions\EntryNotFoundException; use UnityWebPortal\lib\UnityGroup; use UnityWebPortal\lib\UnityHTTPD; @@ -12,7 +13,10 @@ if (isset($_POST["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) { + } } $pi_account = new UnityGroup($pi_groupname, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); if (!$pi_account->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()) {