Skip to content

Commit 576dc14

Browse files
authored
Allow joining PI group by PI email address (#350)
* Allow user join by PI email instead of pi_group * groups.php: Store modalErrors to display on reload
1 parent 31d0221 commit 576dc14

File tree

4 files changed

+49
-13
lines changed

4 files changed

+49
-13
lines changed

resources/lib/UnityGroup.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,15 @@ public static function GID2OwnerUID(string $gid): string
477477
}
478478
return substr($gid, strlen(self::PI_PREFIX));
479479
}
480+
481+
public static function ownerMail2GID($email)
482+
{
483+
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
490+
}
480491
}

resources/lib/UnityLDAP.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
use PHPOpenLDAPer\LDAPEntry;
77

88
/**
9-
* An LDAP connection class which extends ldapConn tailored for the Unity Cluster
9+
* An LDAP connection class which extends LDAPConn tailored for the Unity Cluster
1010
*/
11-
class UnityLDAP extends ldapConn
11+
class UnityLDAP extends LDAPConn
1212
{
1313
private const string RDN = "cn"; // The defauls RDN for LDAP entries is set to "common name"
1414

@@ -428,4 +428,13 @@ public function getOrgGroupEntry(string $gid): LDAPEntry
428428
$gid = ldap_escape($gid, "", LDAP_ESCAPE_DN);
429429
return $this->getEntry(UnityLDAP::RDN . "=$gid," . CONFIG["ldap"]["orggroup_ou"]);
430430
}
431+
432+
public function getUidFromEmail($email)
433+
{
434+
$email = ldap_escape($email, "", LDAP_ESCAPE_FILTER);
435+
$cn = $this->search("mail=$email", CONFIG["ldap"]["user_ou"], ["cn"]);
436+
if ($cn && count($cn) == 1) {
437+
return $cn[0];
438+
}
439+
}
431440
}

webroot/panel/groups.php

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,25 @@
1010

1111
if (isset($_POST["form_type"])) {
1212
if (isset($_POST["pi"])) {
13-
$pi_account = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
13+
$pi_groupname = $_POST["pi"];
14+
if (substr($pi_groupname, 0, 3) !== "pi_" && str_contains($pi_groupname, "@")) {
15+
$pi_groupname = UnityGroup::ownerMail2GID($pi_groupname);
16+
}
17+
$pi_account = new UnityGroup($pi_groupname, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
1418
if (!$pi_account->exists()) {
15-
// "\'" instead of "'", otherwise it will close a single quote from HTML
16-
array_push($modalErrors, "This PI doesn\'t exist");
19+
array_push($modalErrors, "This PI doesn't exist");
1720
}
1821
}
1922

2023
switch ($_POST["form_type"]) {
2124
case "addPIform":
22-
if ($pi_account->requestExists($USER)) {
23-
array_push($modalErrors, "You\'ve already requested this");
24-
}
25-
if ($pi_account->memberExists($USER)) {
26-
array_push($modalErrors, "You\'re already in this PI group");
25+
if ($pi_account->exists()) {
26+
if ($pi_account->requestExists($USER)) {
27+
array_push($modalErrors, "You've already requested this");
28+
}
29+
if ($pi_account->memberExists($USER)) {
30+
array_push($modalErrors, "You're already in this PI group");
31+
}
2732
}
2833
if ($USER->uid != $SSO["user"]) {
2934
$sso_user = $SSO["user"];
@@ -50,8 +55,15 @@
5055
break;
5156
}
5257
}
58+
$_SESSION['MODAL_ERRORS'] = $modalErrors;
59+
} else {
60+
if (isset($_SESSION['MODAL_ERRORS'])) {
61+
$modalErrors = $_SESSION['MODAL_ERRORS'];
62+
$_SESSION['MODAL_ERRORS'] = array(); // Forget after shown
63+
}
5364
}
5465

66+
5567
require $LOC_HEADER;
5668
?>
5769

@@ -178,7 +190,7 @@
178190
if (isset($modalErrors) && is_array($modalErrors) && count($modalErrors) > 0) {
179191
$errorHTML = "";
180192
foreach ($modalErrors as $error) {
181-
$errorHTML .= "<span>$error</span>";
193+
$errorHTML .= "<span>" . htmlentities($error) . "</span>";
182194
}
183195

184196
echo "openModal('Add New PI', '" .

webroot/panel/new_account.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424
);
2525
}
2626
if ($_POST["new_user_sel"] == "not_pi") {
27-
$form_group = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
27+
$pi_groupname = $_POST["pi"];
28+
if (substr($pi_groupname, 0, 3) !== "pi_" && str_contains($pi_groupname, "@")) {
29+
$pi_groupname = UnityGroup::ownerMail2GID($pi_groupname);
30+
}
31+
$form_group = new UnityGroup($pi_groupname, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
2832
if (!$form_group->exists()) {
29-
UnityHTTPD::badRequest("The selected PI '" . $_POST["pi"] . "'does not exist");
33+
UnityHTTPD::badRequest("The selected PI '" . $pi_groupname . "'does not exist");
3034
}
3135
$form_group->newUserRequest(
3236
$USER,

0 commit comments

Comments
 (0)