Skip to content

Commit 3b0c51e

Browse files
committed
catch edge cases
1 parent 4c8160f commit 3b0c51e

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

resources/lib/UnityGroup.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,16 @@ public function exists()
7878

7979
public function requestGroup($send_mail_to_admins, $send_mail = true)
8080
{
81-
// check for edge cases...
81+
if ($this->SQL->requestExists($this->getOwner()->getUID())) {
82+
throw new Exception("group '$this' creation request already exists!");
83+
}
84+
8285
if ($this->exists()) {
83-
return;
86+
throw new Exception("requested to create group '$this' that already exists!");
8487
}
8588

86-
// check if account deletion request already exists
8789
if ($this->SQL->accDeletionRequestExists($this->getOwner()->getUID())) {
88-
return;
90+
throw new Exception("group owner '$this' requested account deletion");
8991
}
9092

9193
$this->SQL->addRequest($this->getOwner()->getUID());
@@ -140,13 +142,16 @@ public function approveGroup($operator = null, $send_mail = true)
140142
{
141143
if (!$this->SQL->requestExists($this->getOwner()->getUID())) {
142144
throw new Exception(
143-
"attempt to approve nonexistent request for group='{$this->getPIUID()}' uid='$new_user'"
145+
"attempt to approve nonexistent request for group='$this' uid='$new_user'"
144146
);
145147
}
146148

147-
// check for edge cases...
148149
if ($this->exists()) {
149-
return;
150+
throw new Exception("group '$this' exists");
151+
}
152+
153+
if ($this->SQL->accDeletionRequestExists($this->getOwner()->getUID())) {
154+
throw new Exception("group owner '{$this->getOwner()->getUID()}' requested account deletion");
150155
}
151156

152157
// check if owner exists
@@ -188,7 +193,7 @@ public function denyGroup($operator = null, $send_mail = true)
188193
$this->SQL->removeRequest($this->getOwner()->getUID());
189194

190195
if ($this->exists()) {
191-
return;
196+
throw new Exception("group '$this' creation request cannot be denied, it already exists!");
192197
}
193198

194199
$operator = is_null($operator) ? $this->getOwner()->getUID() : $operator->getUID();
@@ -212,6 +217,7 @@ public function denyGroup($operator = null, $send_mail = true)
212217
public function cancelGroupRequest($send_mail = true)
213218
{
214219
if (!$this->SQL->requestExists($this->getOwner()->getUID())) {
220+
UnitySite::errorLog("warning", "attempt to cancel nonexistent group creation request ($this)");
215221
return;
216222
}
217223

@@ -229,6 +235,7 @@ public function cancelGroupRequest($send_mail = true)
229235
public function cancelGroupJoinRequest($user, $send_mail = true)
230236
{
231237
if (!$this->requestExists($user)) {
238+
UnitySite::errorLog("warning", "attempt to cancel nonexistent group join request ($this)");
232239
return;
233240
}
234241

@@ -290,7 +297,7 @@ public function approveUser($new_user, $send_mail = true)
290297
{
291298
if (!$this->requestExists($new_user)) {
292299
throw new Exception(
293-
"attempt to approve nonexistent request for group='{$this->getPIUID()}' uid='$new_user'"
300+
"attempt to approve nonexistent request for group='$this' uid='$new_user'"
294301
);
295302
}
296303

@@ -331,7 +338,7 @@ public function approveUser($new_user, $send_mail = true)
331338
public function denyUser($new_user, $send_mail = true)
332339
{
333340
if (!$this->requestExists($new_user)) {
334-
return;
341+
throw new Exception("request from user '$new_user' does not exist");
335342
}
336343

337344
// remove request, this will fail silently if the request doesn't exist
@@ -363,11 +370,12 @@ public function denyUser($new_user, $send_mail = true)
363370
public function removeUser($new_user, $send_mail = true)
364371
{
365372
if (!$this->userExists($new_user)) {
373+
UnitySite::errorLog("warning", "requested to delete absent user '$new_user' from group '$this'");
366374
return;
367375
}
368376

369377
if ($new_user->getUID() == $this->getOwner()->getUID()) {
370-
throw new Exception("Cannot delete group owner from group. Disband group instead");
378+
throw new Exception("Cannot delete group owner '{$this->getOwner()}' from group. Disband group instead");
371379
}
372380

373381
// remove request, this will fail silently if the request doesn't exist
@@ -399,18 +407,15 @@ public function removeUser($new_user, $send_mail = true)
399407
public function newUserRequest($new_user, $send_mail = true)
400408
{
401409
if ($this->userExists($new_user)) {
402-
UnitySite::errorLog("warning", "user '$new_user' already in group");
403-
return;
410+
throw new Exception("user '$new_user' already in group");
404411
}
405412

406413
if ($this->requestExists($new_user)) {
407-
UnitySite::errorLog("warning", "user '$new_user' already requested group membership");
408-
return;
414+
throw new Exception("user '$new_user' already requested group membership");
409415
}
410416

411417
if ($this->SQL->accDeletionRequestExists($new_user->getUID())) {
412418
throw new Exception("user '$new_user' requested account deletion");
413-
return;
414419
}
415420

416421
$this->addRequest($new_user->getUID());
@@ -607,7 +612,7 @@ public static function getUIDfromPIUID($pi_uid)
607612
if (substr($pi_uid, 0, strlen(self::PI_PREFIX)) == self::PI_PREFIX) {
608613
return substr($pi_uid, strlen(self::PI_PREFIX));
609614
} else {
610-
throw new Exception("PI netid doesn't have the correct prefix.");
615+
throw new Exception("PI netid '$pi_uid' doesn't have the correct prefix.");
611616
}
612617
}
613618
}

test/functional/PiBecomeRequestTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,10 @@ public function testRequestBecomePiUserRequestedAccountDeletion()
6060
$this->assertFalse($USER->isPI());
6161
$this->assertNumberPiBecomeRequests(0);
6262
$this->assertTrue($SQL->accDeletionRequestExists($USER->getUID()));
63-
try {
64-
http_post(
65-
__DIR__ . "/../../webroot/panel/account.php",
66-
["form_type" => "pi_request"]
67-
);
68-
$this->assertNumberPiBecomeRequests(0);
69-
} finally {
70-
$SQL->removeRequest($USER->getUID());
71-
}
63+
$this->expectException(Exception::class);
64+
http_post(
65+
__DIR__ . "/../../webroot/panel/account.php",
66+
["form_type" => "pi_request"]
67+
);
7268
}
7369
}

0 commit comments

Comments
 (0)