Skip to content

Store user data in request #244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f0afa80
prevent access to user attributes when they might not be defined
simonLeary42 Jun 7, 2025
39f18c9
getuid without existing is OK
simonLeary42 Jun 7, 2025
5b966cb
store attributes in request
simonLeary42 Jun 7, 2025
f6d5442
fix UnitySQL::requestExists
simonLeary42 Jun 7, 2025
a1ca915
Merge branch 'main' into request-name-volatile
simonLeary42 Jun 7, 2025
80ef816
email -> mail
simonLeary42 Jun 7, 2025
10a60e8
fix addRequest
simonLeary42 Jun 7, 2025
c6a4ad7
use new vars
simonLeary42 Jun 7, 2025
567d9d2
fix request fetch
simonLeary42 Jun 7, 2025
aa50390
make sql exception more generic
simonLeary42 Jun 7, 2025
0b75c4d
fix tests
simonLeary42 Jun 7, 2025
8b4e307
Merge branch 'main' into request-name-volatile
simonLeary42 Jun 7, 2025
d61b3d9
Merge branch 'main' into request-name-volatile
simonLeary42 Jun 7, 2025
0fc9c69
use email from request
simonLeary42 Jun 7, 2025
b568d89
fix tests
simonLeary42 Jun 7, 2025
2f3edab
remove extra space
simonLeary42 Jun 7, 2025
48905b0
rewrite tests
simonLeary42 Jun 7, 2025
6857f71
fix tests
simonLeary42 Jun 7, 2025
a0a4648
fix tests
simonLeary42 Jun 7, 2025
2dcdbd2
fix tests
simonLeary42 Jun 7, 2025
c6739ad
fix test
simonLeary42 Jun 7, 2025
193b980
remove prune
simonLeary42 Jun 7, 2025
1e78723
remove old comment
simonLeary42 Jun 7, 2025
2966ded
more similar to previous revision
simonLeary42 Jun 7, 2025
c1760ff
wording
simonLeary42 Jun 7, 2025
b57f269
Merge branch 'main' into request-name-volatile
simonLeary42 Jun 7, 2025
351cffd
Merge branch 'main' into request-name-volatile
simonLeary42 Jun 7, 2025
45e0600
consistent
simonLeary42 Jun 7, 2025
c523e2e
Revert "more similar to previous revision"
simonLeary42 Jun 7, 2025
6f689d2
Merge branch 'main' into request-name-volatile
simonLeary42 Jun 7, 2025
5166377
Merge branch 'main' into request-name-volatile
simonLeary42 Jun 7, 2025
ee7300c
Merge branch 'main' into request-name-volatile
simonLeary42 Jun 7, 2025
48677b9
delete org before user
simonLeary42 Jun 7, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions resources/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
require_once __DIR__ . "/lib/UnityWebhook.php";
require_once __DIR__ . "/lib/UnityRedis.php";
require_once __DIR__ . "/lib/UnityGithub.php";
require_once __DIR__ . "/lib/exceptions/PhpUnitNoDieException.php";
require_once __DIR__ . "/lib/exceptions/UnitySQLRecordNotFound.php";

// run init script
require __DIR__ . "/init.php";
101 changes: 63 additions & 38 deletions resources/lib/UnityGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function exists()
// Portal-facing methods, these are the methods called by scripts in webroot
//

public function requestGroup($send_mail_to_admins, $send_mail = true)
public function requestGroup($firstname, $lastname, $email, $org, $send_mail_to_admins, $send_mail = true)
{
// check for edge cases...
if ($this->exists()) {
Expand All @@ -88,22 +88,22 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
return;
}

$this->SQL->addRequest($this->getOwner()->getUID());
$this->SQL->addRequest($this->getOwner()->getUID(), $firstname, $lastname, $email, $org);

if ($send_mail) {
// send email to requestor
$this->MAILER->sendMail(
$this->getOwner()->getMail(),
$email,
"group_request"
);

$this->WEBHOOK->sendWebhook(
"group_request_admin",
array(
"user" => $this->getOwner()->getUID(),
"org" => $this->getOwner()->getOrg(),
"name" => $this->getOwner()->getFullname(),
"email" => $this->getOwner()->getMail()
"org" => $org,
"name" => "$firstname $lastname",
"email" => $email
)
);

Expand All @@ -113,9 +113,9 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
"group_request_admin",
array(
"user" => $this->getOwner()->getUID(),
"org" => $this->getOwner()->getOrg(),
"name" => $this->getOwner()->getFullname(),
"email" => $this->getOwner()->getMail()
"org" => $org,
"name" => "$firstname $lastname",
"email" => $email
)
);
}
Expand All @@ -125,9 +125,9 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
"group_request_admin",
array(
"user" => $this->getOwner()->getUID(),
"org" => $this->getOwner()->getOrg(),
"name" => $this->getOwner()->getFullname(),
"email" => $this->getOwner()->getMail()
"org" => $org,
"name" => "$firstname $lastname",
"email" => $email
)
);
}
Expand All @@ -138,10 +138,10 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
*/
public function approveGroup($operator = null, $send_mail = true)
{
if (!$this->SQL->requestExists($this->getOwner()->getUID())) {
throw new Exception(
"attempt to approve nonexistent request for group='{$this->getPIUID()}'"
);
$uid = $this->getOwner()->getUID();
$request = $this->SQL->getRequest($uid, UnitySQL::REQUEST_BECOME_PI);
if (is_null($request)) {
throw new Exception("uid '$uid' does not have a group request!");
}

// check for edge cases...
Expand All @@ -151,7 +151,13 @@ public function approveGroup($operator = null, $send_mail = true)

// check if owner exists
if (!$this->getOwner()->exists()) {
$this->getOwner()->init();
$this->getOwner()->init(
$request["firstname"],
$request["lastname"],
$request["email"],
$request["org"],
$send_mail
);
}

// initialize ldap objects, if this fails the script will crash, but nothing will persistently break
Expand All @@ -173,7 +179,7 @@ public function approveGroup($operator = null, $send_mail = true)
// send email to the newly approved PI
if ($send_mail) {
$this->MAILER->sendMail(
$this->getOwner()->getMail(),
$request["email"],
"group_created"
);
}
Expand Down Expand Up @@ -288,21 +294,27 @@ public function cancelGroupJoinRequest($user, $send_mail = true)
*/
public function approveUser($new_user, $send_mail = true)
{
if (!$this->requestExists($new_user)) {
throw new Exception(
"attempt to approve nonexistent request for group='{$this->getPIUID()}' uid='$new_user'"
);

$uid = $new_user->getUID();
$gid = $this->getPIUID();
$request = $this->SQL->getRequest($uid, $gid);
if (is_null($request)) {
throw new Exception("uid '$uid' does not have a request for group '$gid'!");
}

// check if user exists
if (!$new_user->exists()) {
$new_user->init();
$new_user->init(
$request["firstname"],
$request["lastname"],
$request["email"],
$request["org"],
);
}

// add user to the LDAP object
$this->addUserToGroup($new_user);

// remove request, this will fail silently if the request doesn't exist
$this->SQL->removeRequest($new_user->getUID(), $this->pi_uid);

// send email to the requestor
Expand All @@ -320,18 +332,21 @@ public function approveUser($new_user, $send_mail = true)
array(
"group" => $this->pi_uid,
"user" => $new_user->getUID(),
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg()
"name" => $request["firstname"] . " " . $request["lastname"],
"email" => $request["email"],
"org" => $request["org"],
)
);
}
}

public function denyUser($new_user, $send_mail = true)
{
if (!$this->requestExists($new_user)) {
return;
$uid = $new_user->getUID();
$gid = $this->getPIUID();
$request = $this->SQL->getRequest($uid, $gid);
if (is_null($request)) {
throw new Exception("uid '$uid' does not have a request for group '$gid'!");
}

// remove request, this will fail silently if the request doesn't exist
Expand Down Expand Up @@ -396,7 +411,7 @@ public function removeUser($new_user, $send_mail = true)
}
}

public function newUserRequest($new_user, $send_mail = true)
public function newUserRequest($new_user, $firstname, $lastname, $email, $org, $send_mail = true)
{
if ($this->userExists($new_user)) {
UnitySite::errorLog("warning", "user '$new_user' already in group");
Expand All @@ -413,12 +428,12 @@ public function newUserRequest($new_user, $send_mail = true)
return;
}

$this->addRequest($new_user->getUID());
$this->addRequest($new_user->getUID(), $firstname, $lastname, $email, $org);

if ($send_mail) {
// send email to user
$this->MAILER->sendMail(
$new_user->getMail(),
$email,
"group_user_request",
array("group" => $this->pi_uid)
);
Expand All @@ -430,9 +445,9 @@ public function newUserRequest($new_user, $send_mail = true)
array(
"group" => $this->pi_uid,
"user" => $new_user->getUID(),
"name" => $new_user->getFullName(),
"email" => $new_user->getMail(),
"org" => $new_user->getOrg()
"name" => "$firstname $lastname",
"email" => $email,
"org" => $org,
)
);
}
Expand All @@ -452,7 +467,17 @@ public function getRequests()
$this->REDIS,
$this->WEBHOOK
);
array_push($out, [$user, $request["timestamp"]]);
array_push(
$out,
[
$user,
$request["timestamp"],
$request["firstname"],
$request["lastname"],
$request["email"],
$request["org"],
]
);
}

return $out;
Expand Down Expand Up @@ -563,9 +588,9 @@ public function userExists($user)
return in_array($user->getUID(), $this->getGroupMemberUIDs());
}

private function addRequest($uid)
private function addRequest($uid, $firstname, $lastname, $email, $org)
{
$this->SQL->addRequest($uid, $this->pi_uid);
$this->SQL->addRequest($uid, $firstname, $lastname, $email, $org, $this->pi_uid);
}

//
Expand Down
43 changes: 38 additions & 5 deletions resources/lib/UnitySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PDO;
use PDOException;
use UnityWebPortal\lib\exceptions\UnitySQLRecordNotFound;

class UnitySQL
{
Expand Down Expand Up @@ -109,12 +110,28 @@ private function update($table, $filters, $data)
$stmt->execute();
}

public function addRequest($requestor, $dest = self::REQUEST_BECOME_PI)
{
public function addRequest(
$requestor,
$firstname,
$lastname,
$email,
$org,
$dest = self::REQUEST_BECOME_PI
) {
if ($this->requestExists($requestor, $dest)) {
return;
}
$this->insert(self::TABLE_REQS, ["uid" => $requestor, "request_for" => $dest]);
$this->insert(
self::TABLE_REQS,
[
"uid" => $requestor,
"firstname" => $firstname,
"lastname" => $lastname,
"email" => $email,
"org" => $org,
"request_for" => $dest
]
);
}

public function removeRequest($requestor, $dest = self::REQUEST_BECOME_PI)
Expand All @@ -130,10 +147,26 @@ public function removeRequests($dest = self::REQUEST_BECOME_PI)
$this->delete(self::TABLE_REQS, ["request_for" => $dest]);
}

public function requestExists($requestor, $dest = self::REQUEST_BECOME_PI)
public function getRequest($user, $dest)
{
$results = $this->search(self::TABLE_REQS, ["request_for" => $dest]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't this search also filter by $user?

return count($results) > 0;
if (count($results) == 0) {
throw new UnitySQLRecordNotFound("no such request: uid='$user' request_for='$dest'");
}
if (count($results) > 1) {
throw new Exception("multiple requests for uid='$user' request_for='$dest'");
}
return $results[0];
}

public function requestExists($requestor, $dest = self::REQUEST_BECOME_PI)
{
try {
self::getRequest($requestor, $dest);
return true;
} catch (UnitySQLRecordNotFound) {
return false;
}
}

public function getRequests($dest = self::REQUEST_BECOME_PI)
Expand Down
Loading