diff --git a/resources/lib/UnityGroup.php b/resources/lib/UnityGroup.php index e6e9c609..cbce33db 100644 --- a/resources/lib/UnityGroup.php +++ b/resources/lib/UnityGroup.php @@ -477,4 +477,15 @@ public static function GID2OwnerUID(string $gid): string } return substr($gid, strlen(self::PI_PREFIX)); } + + public static function ownerMail2GID($email) + { + global $LDAP; + $entry = $LDAP->getUidFromEmail($email); + if ($entry !== null) { + $ownerUid = $entry->getAttribute("cn")[0]; + return self::PI_PREFIX . $ownerUid; + } + return $email; // Leave untouched + } } diff --git a/resources/lib/UnityLDAP.php b/resources/lib/UnityLDAP.php index db8ba3f2..df5c4cc2 100644 --- a/resources/lib/UnityLDAP.php +++ b/resources/lib/UnityLDAP.php @@ -6,9 +6,9 @@ use PHPOpenLDAPer\LDAPEntry; /** - * An LDAP connection class which extends ldapConn tailored for the Unity Cluster + * An LDAP connection class which extends LDAPConn tailored for the Unity Cluster */ -class UnityLDAP extends ldapConn +class UnityLDAP extends LDAPConn { private const string RDN = "cn"; // The defauls RDN for LDAP entries is set to "common name" @@ -428,4 +428,13 @@ public function getOrgGroupEntry(string $gid): LDAPEntry $gid = ldap_escape($gid, "", LDAP_ESCAPE_DN); return $this->getEntry(UnityLDAP::RDN . "=$gid," . CONFIG["ldap"]["orggroup_ou"]); } + + public function getUidFromEmail($email) + { + $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]; + } + } } diff --git a/webroot/panel/groups.php b/webroot/panel/groups.php index 18b210c8..96c683d9 100644 --- a/webroot/panel/groups.php +++ b/webroot/panel/groups.php @@ -10,20 +10,25 @@ if (isset($_POST["form_type"])) { if (isset($_POST["pi"])) { - $pi_account = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); + $pi_groupname = $_POST["pi"]; + if (substr($pi_groupname, 0, 3) !== "pi_" && str_contains($pi_groupname, "@")) { + $pi_groupname = UnityGroup::ownerMail2GID($pi_groupname); + } + $pi_account = new UnityGroup($pi_groupname, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); if (!$pi_account->exists()) { - // "\'" instead of "'", otherwise it will close a single quote from HTML - array_push($modalErrors, "This PI doesn\'t exist"); + array_push($modalErrors, "This PI doesn't exist"); } } switch ($_POST["form_type"]) { case "addPIform": - if ($pi_account->requestExists($USER)) { - array_push($modalErrors, "You\'ve already requested this"); - } - if ($pi_account->memberExists($USER)) { - array_push($modalErrors, "You\'re already in this PI group"); + if ($pi_account->exists()) { + if ($pi_account->requestExists($USER)) { + array_push($modalErrors, "You've already requested this"); + } + if ($pi_account->memberExists($USER)) { + array_push($modalErrors, "You're already in this PI group"); + } } if ($USER->uid != $SSO["user"]) { $sso_user = $SSO["user"]; @@ -50,8 +55,15 @@ break; } } + $_SESSION['MODAL_ERRORS'] = $modalErrors; +} else { + if (isset($_SESSION['MODAL_ERRORS'])) { + $modalErrors = $_SESSION['MODAL_ERRORS']; + $_SESSION['MODAL_ERRORS'] = array(); // Forget after shown + } } + require $LOC_HEADER; ?> @@ -178,7 +190,7 @@ if (isset($modalErrors) && is_array($modalErrors) && count($modalErrors) > 0) { $errorHTML = ""; foreach ($modalErrors as $error) { - $errorHTML .= "$error"; + $errorHTML .= "" . htmlentities($error) . ""; } echo "openModal('Add New PI', '" . diff --git a/webroot/panel/new_account.php b/webroot/panel/new_account.php index 34863657..88f637d2 100644 --- a/webroot/panel/new_account.php +++ b/webroot/panel/new_account.php @@ -24,9 +24,13 @@ ); } if ($_POST["new_user_sel"] == "not_pi") { - $form_group = new UnityGroup($_POST["pi"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); + $pi_groupname = $_POST["pi"]; + if (substr($pi_groupname, 0, 3) !== "pi_" && str_contains($pi_groupname, "@")) { + $pi_groupname = UnityGroup::ownerMail2GID($pi_groupname); + } + $form_group = new UnityGroup($pi_groupname, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK); if (!$form_group->exists()) { - UnityHTTPD::badRequest("The selected PI '" . $_POST["pi"] . "'does not exist"); + UnityHTTPD::badRequest("The selected PI '" . $pi_groupname . "'does not exist"); } $form_group->newUserRequest( $USER,