Skip to content

Commit 0e64bdf

Browse files
authored
throw exceptions in OwnerMail2GID (#352)
1 parent ecf6bc0 commit 0e64bdf

File tree

8 files changed

+31
-10
lines changed

8 files changed

+31
-10
lines changed

resources/autoload.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
require_once __DIR__ . "/lib/exceptions/NoDieException.php";
2929
require_once __DIR__ . "/lib/exceptions/SSOException.php";
3030
require_once __DIR__ . "/lib/exceptions/ArrayKeyException.php";
31+
require_once __DIR__ . "/lib/exceptions/EntryNotFoundException.php";
3132
require_once __DIR__ . "/lib/exceptions/EnsureException.php";
3233
require_once __DIR__ . "/lib/exceptions/EncodingUnknownException.php";
3334
require_once __DIR__ . "/lib/exceptions/EncodingConversionException.php";

resources/lib/UnityGroup.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -478,14 +478,14 @@ public static function GID2OwnerUID(string $gid): string
478478
return substr($gid, strlen(self::PI_PREFIX));
479479
}
480480

481-
public static function ownerMail2GID($email)
481+
/**
482+
* @throws \UnityWebPortal\lib\exceptions\EntryNotFoundException
483+
*/
484+
public static function ownerMail2GID(string $email): string
482485
{
483486
global $LDAP;
484-
$entry = $LDAP->getUidFromEmail($email);
485-
if ($entry !== null) {
486-
$ownerUid = $entry->getAttribute("cn")[0];
487-
return self::PI_PREFIX . $ownerUid;
488-
}
489-
return $email; // Leave untouched
487+
$entry = $LDAP->getUidFromEmail($email); // throws EntryNotFoundException
488+
$ownerUid = $entry->getAttribute("cn")[0];
489+
return self::PI_PREFIX . $ownerUid;
490490
}
491491
}

resources/lib/UnityLDAP.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,12 +429,16 @@ public function getOrgGroupEntry(string $gid): LDAPEntry
429429
return $this->getEntry(UnityLDAP::RDN . "=$gid," . CONFIG["ldap"]["orggroup_ou"]);
430430
}
431431

432-
public function getUidFromEmail($email)
432+
/**
433+
* @throws \UnityWebPortal\lib\exceptions\EntryNotFoundException
434+
*/
435+
public function getUidFromEmail(string $email): LDAPEntry
433436
{
434437
$email = ldap_escape($email, "", LDAP_ESCAPE_FILTER);
435438
$cn = $this->search("mail=$email", CONFIG["ldap"]["user_ou"], ["cn"]);
436439
if ($cn && count($cn) == 1) {
437440
return $cn[0];
438441
}
442+
throw new exceptions\EntryNotFoundException($email);
439443
}
440444
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace UnityWebPortal\lib\exceptions;
4+
5+
class EntryNotFoundException extends \Exception {}

test/functional/PIMemberRequestTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function testRequestMembership()
4242
$this->assertTrue($SQL->requestExists($uid, $gid));
4343
$this->cancelRequest($gid);
4444
$this->assertFalse($SQL->requestExists($uid, $gid));
45+
$this->requestMembership("asdlkjasldkj");
46+
$this->assertContains("This PI doesn't exist", $_SESSION["MODAL_ERRORS"]);
4547
$this->requestMembership($pi_group->getOwner()->getMail());
4648
$this->assertTrue($SQL->requestExists($uid, $gid));
4749
} finally {

test/phpunit-bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
require_once __DIR__ . "/../resources/lib/exceptions/NoDieException.php";
2020
require_once __DIR__ . "/../resources/lib/exceptions/SSOException.php";
2121
require_once __DIR__ . "/../resources/lib/exceptions/ArrayKeyException.php";
22+
require_once __DIR__ . "/../resources/lib/exceptions/EntryNotFoundException.php";
2223
require_once __DIR__ . "/../resources/lib/exceptions/EnsureException.php";
2324
require_once __DIR__ . "/../resources/lib/exceptions/EncodingUnknownException.php";
2425
require_once __DIR__ . "/../resources/lib/exceptions/EncodingConversionException.php";

webroot/panel/groups.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_once __DIR__ . "/../../resources/autoload.php";
44

5+
use UnityWebPortal\lib\exceptions\EntryNotFoundException;
56
use UnityWebPortal\lib\UnityGroup;
67
use UnityWebPortal\lib\UnityHTTPD;
78

@@ -12,7 +13,10 @@
1213
if (isset($_POST["pi"])) {
1314
$pi_groupname = $_POST["pi"];
1415
if (substr($pi_groupname, 0, 3) !== "pi_" && str_contains($pi_groupname, "@")) {
15-
$pi_groupname = UnityGroup::ownerMail2GID($pi_groupname);
16+
try {
17+
$pi_groupname = UnityGroup::ownerMail2GID($pi_groupname);
18+
} catch (EntryNotFoundException) {
19+
}
1620
}
1721
$pi_account = new UnityGroup($pi_groupname, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
1822
if (!$pi_account->exists()) {

webroot/panel/new_account.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require_once __DIR__ . "/../../resources/autoload.php";
44

5+
use UnityWebPortal\lib\exceptions\EntryNotFoundException;
56
use UnityWebPortal\lib\UnityHTTPD;
67
use UnityWebPortal\lib\UnityGroup;
78
use UnityWebPortal\lib\UnitySQL;
@@ -26,7 +27,10 @@
2627
if ($_POST["new_user_sel"] == "not_pi") {
2728
$pi_groupname = $_POST["pi"];
2829
if (substr($pi_groupname, 0, 3) !== "pi_" && str_contains($pi_groupname, "@")) {
29-
$pi_groupname = UnityGroup::ownerMail2GID($pi_groupname);
30+
try {
31+
$pi_groupname = UnityGroup::ownerMail2GID($pi_groupname);
32+
} catch (EntryNotFoundException) {
33+
}
3034
}
3135
$form_group = new UnityGroup($pi_groupname, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
3236
if (!$form_group->exists()) {

0 commit comments

Comments
 (0)