Skip to content

Commit e3a171b

Browse files
committed
Merge branch 'main' into patch-6
2 parents fa6fdbc + 8b2264c commit e3a171b

File tree

15 files changed

+413
-175
lines changed

15 files changed

+413
-175
lines changed

resources/lib/UnityGroup.php

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public function equals($other_group)
4747
return $this->getPIUID() == $other_group->getPIUID();
4848
}
4949

50+
public function __toString()
51+
{
52+
return $this->getPIUID();
53+
}
54+
5055
/**
5156
* Returns this group's PI UID
5257
*
@@ -133,6 +138,12 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
133138
*/
134139
public function approveGroup($operator = null, $send_mail = true)
135140
{
141+
if (!$this->SQL->requestExists($this->getOwner()->getUID())) {
142+
throw new Exception(
143+
"attempt to approve nonexistent request for group='{$this->getPIUID()}' uid='$new_user'"
144+
);
145+
}
146+
136147
// check for edge cases...
137148
if ($this->exists()) {
138149
return;
@@ -277,6 +288,12 @@ public function cancelGroupJoinRequest($user, $send_mail = true)
277288
*/
278289
public function approveUser($new_user, $send_mail = true)
279290
{
291+
if (!$this->requestExists($new_user)) {
292+
throw new Exception(
293+
"attempt to approve nonexistent request for group='{$this->getPIUID()}' uid='$new_user'"
294+
);
295+
}
296+
280297
// check if user exists
281298
if (!$new_user->exists()) {
282299
$new_user->init();
@@ -382,15 +399,17 @@ public function removeUser($new_user, $send_mail = true)
382399
public function newUserRequest($new_user, $send_mail = true)
383400
{
384401
if ($this->userExists($new_user)) {
402+
UnitySite::errorLog("warning", "user '$new_user' already in group");
385403
return;
386404
}
387405

388406
if ($this->requestExists($new_user)) {
407+
UnitySite::errorLog("warning", "user '$new_user' already requested group membership");
389408
return;
390409
}
391410

392-
// check if account deletion request already exists
393411
if ($this->SQL->accDeletionRequestExists($new_user->getUID())) {
412+
throw new Exception("user '$new_user' requested account deletion");
394413
return;
395414
}
396415

@@ -441,22 +460,8 @@ public function getRequests()
441460

442461
public function getGroupMembers($ignorecache = false)
443462
{
444-
if (!$ignorecache) {
445-
$cached_val = $this->REDIS->getCache($this->getPIUID(), "members");
446-
if (!is_null($cached_val)) {
447-
$members = $cached_val;
448-
}
449-
}
450-
451-
$updatecache = false;
452-
if (!isset($members)) {
453-
$pi_group = $this->getLDAPPiGroup();
454-
$members = $pi_group->getAttribute("memberuid");
455-
$updatecache = true;
456-
}
457-
463+
$members = $this->getGroupMemberUIDs($ignorecache);
458464
$out = array();
459-
$cache_arr = array();
460465
$owner_uid = $this->getOwner()->getUID();
461466
foreach ($members as $member) {
462467
$user_obj = new UnityUser(
@@ -468,22 +473,28 @@ public function getGroupMembers($ignorecache = false)
468473
$this->WEBHOOK
469474
);
470475
array_push($out, $user_obj);
471-
array_push($cache_arr, $user_obj->getUID());
472-
}
473-
474-
if (!$ignorecache && $updatecache) {
475-
sort($cache_arr);
476-
$this->REDIS->setCache($this->getPIUID(), "members", $cache_arr);
477476
}
478-
479477
return $out;
480478
}
481479

482-
public function getGroupMemberUIDs()
480+
public function getGroupMemberUIDs($ignorecache = false)
483481
{
484-
$pi_group = $this->getLDAPPiGroup();
485-
$members = $pi_group->getAttribute("memberuid");
486-
482+
if (!$ignorecache) {
483+
$cached_val = $this->REDIS->getCache($this->getPIUID(), "members");
484+
if (!is_null($cached_val)) {
485+
$members = $cached_val;
486+
}
487+
}
488+
$updatecache = false;
489+
if (!isset($members)) {
490+
$pi_group = $this->getLDAPPiGroup();
491+
$members = $pi_group->getAttribute("memberuid");
492+
$updatecache = true;
493+
}
494+
if (!$ignorecache && $updatecache) {
495+
sort($members);
496+
$this->REDIS->setCache($this->getPIUID(), "members", $members);
497+
}
487498
return $members;
488499
}
489500

resources/lib/UnitySite.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class UnitySite
99
{
10-
public static function die($x = null)
10+
public static function die($x = null, $show_user = false)
1111
{
1212
if (@$GLOBALS["PHPUNIT_NO_DIE_PLEASE"] == true) {
1313
if (is_null($x)) {
@@ -16,18 +16,18 @@ public static function die($x = null)
1616
throw new PhpUnitNoDieException($x);
1717
}
1818
} else {
19-
if (is_null($x)) {
20-
die();
21-
} else {
19+
if (!is_null($x) and $show_user) {
2220
die($x);
21+
} else {
22+
die();
2323
}
2424
}
2525
}
2626

2727
public static function redirect($destination)
2828
{
2929
header("Location: $destination");
30-
self::die("Redirect failed, click <a href='$destination'>here</a> to continue.");
30+
self::die("Redirect failed, click <a href='$destination'>here</a> to continue.", true);
3131
}
3232

3333
private static function headerResponseCode(int $code, string $reason)
@@ -55,25 +55,34 @@ public static function badRequest($message)
5555
{
5656
self::headerResponseCode(400, "bad request");
5757
self::errorLog("bad request", $message);
58-
self::die();
58+
self::die($message);
5959
}
6060

6161
public static function forbidden($message)
6262
{
6363
self::headerResponseCode(403, "forbidden");
6464
self::errorLog("forbidden", $message);
65-
self::die();
65+
self::die($message);
6666
}
6767

68-
public static function removeTrailingWhitespace($arr)
68+
public static function arrayGetOrBadRequest(array $array, ...$keys)
6969
{
70-
$out = array();
71-
foreach ($arr as $str) {
72-
$new_string = rtrim($str);
73-
array_push($out, $new_string);
70+
$cursor = $array;
71+
$keysTraversed = [];
72+
foreach ($keys as $key) {
73+
array_push($keysTraversed, $key);
74+
if (!isset($cursor[$key])) {
75+
self::badRequest("array key not found: " . json_encode($keysTraversed));
76+
}
77+
$cursor = $cursor[$key];
7478
}
79+
return $cursor;
80+
}
7581

76-
return $out;
82+
public static function alert(string $message)
83+
{
84+
// json_encode escapes quotes
85+
echo "<script type='text/javascript'>alert(" . json_encode($message) . ");</script>";
7786
}
7887

7988
public static function testValidSSHKey($key_str)

resources/lib/UnityUser.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function equals($other_user)
3838
return $this->getUID() == $other_user->getUID();
3939
}
4040

41+
public function __toString()
42+
{
43+
return $this->uid;
44+
}
45+
4146
/**
4247
* This is the method that is run when a new account is created
4348
*

test/functional/CancelRequestTest.php

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)