Skip to content

Commit 1a37669

Browse files
committed
move to enum
1 parent e99063a commit 1a37669

20 files changed

+68
-49
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
This will enable strict mode and throw an exception rather than returning `false`.
2323
- `UnityHTTPD`'s user-facing error functionality (ex: `badRequest`) should only be called from `webroot/**/*.php`.
2424
`resources/**/*.php` should throw exceptions instead.
25-
- all pages under `webroot/admin/` must check for `$USER->getFlag("admin")` and call `UnityHTTPD::forbidden()` if not admin.
25+
- all pages under `webroot/admin/` must check for `$USER->getFlag(UserFlag::ADMIN)` and call `UnityHTTPD::forbidden()` if not admin.
2626

2727
This repository will automatically check PRs for linting compliance.
2828

resources/init.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use UnityWebPortal\lib\UnityWebhook;
1313
use UnityWebPortal\lib\UnityGithub;
1414
use UnityWebPortal\lib\UnityHTTPD;
15+
use UnityWebPortal\lib\UserFlag;
1516

1617
if (CONFIG["site"]["enable_exception_handler"]) {
1718
set_exception_handler(["UnityWebPortal\lib\UnityHTTPD", "exceptionHandler"]);
@@ -56,7 +57,7 @@
5657
$_SESSION["SSO"] = $SSO;
5758

5859
$OPERATOR = new UnityUser($SSO["user"], $LDAP, $SQL, $MAILER, $WEBHOOK);
59-
$_SESSION["is_admin"] = $OPERATOR->getFlag("admin");
60+
$_SESSION["is_admin"] = $OPERATOR->getFlag(UserFlag::ADMIN);
6061

6162
if (isset($_SESSION["viewUser"]) && $_SESSION["is_admin"]) {
6263
$USER = new UnityUser($_SESSION["viewUser"], $LDAP, $SQL, $MAILER, $WEBHOOK);

resources/lib/UnityGroup.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function approveGroup(?UnityUser $operator = null, bool $send_mail = true
8585
if ($send_mail) {
8686
$this->MAILER->sendMail($this->getOwner()->getMail(), "group_created");
8787
}
88-
$this->getOwner()->setFlag("qualified", true); // having your own group makes you qualified
88+
$this->getOwner()->setFlag(UserFlag::QUALIFIED, true); // having your own group makes you qualified
8989
}
9090

9191
/**
@@ -192,7 +192,7 @@ public function approveUser(UnityUser $new_user, bool $send_mail = true): void
192192
]);
193193
}
194194
// being in a group makes you qualified
195-
$new_user->setFlag("qualified", true, doSendMail: true, doSendMailAdmin: false);
195+
$new_user->setFlag(UserFlag::QUALIFIED, true, doSendMail: true, doSendMailAdmin: false);
196196
}
197197

198198
public function denyUser(UnityUser $new_user, bool $send_mail = true): void

resources/lib/UnityLDAP.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
use PHPOpenLDAPer\LDAPEntry;
88
use UnityWebPortal\lib\PosixGroup;
99

10+
enum UserFlag: string
11+
{
12+
case ADMIN = "admin";
13+
case GHOST = "ghost";
14+
case IDLELOCKED = "idlelocked";
15+
case LOCKED = "locked";
16+
case QUALIFIED = "qualified";
17+
}
18+
1019
/**
1120
* An LDAP connection class which extends LDAPConn tailored for the UnityHPC Platform
1221
*/
@@ -48,8 +57,9 @@ public function __construct()
4857
$this->pi_groupOU = $this->getEntry(CONFIG["ldap"]["pigroup_ou"]);
4958
$this->org_groupOU = $this->getEntry(CONFIG["ldap"]["orggroup_ou"]);
5059
$this->userFlagGroups = [];
51-
foreach (CONFIG["ldap"]["user_flag_groups"] as $gid => $dn) {
52-
$this->userFlagGroups[$gid] = new PosixGroup(new LDAPEntry($this->conn, $dn));
60+
foreach (UserFlag::cases() as $flag) {
61+
$dn = CONFIG["ldap"]["user_flag_groups"][$flag->value];
62+
$this->userFlagGroups[$flag->value] = new PosixGroup(new LDAPEntry($this->conn, $dn));
5363
}
5464
}
5565

resources/lib/UnityUser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ public function init(
9797
$this->SQL->addLog($this->uid, $_SERVER["REMOTE_ADDR"], "user_added", $this->uid);
9898
}
9999

100-
public function getFlag(string $flag): bool
100+
public function getFlag(UserFlag $flag): bool
101101
{
102-
return $this->LDAP->userFlagGroups[$flag]->memberUIDExists($this->uid);
102+
return $this->LDAP->userFlagGroups[$flag->value]->memberUIDExists($this->uid);
103103
}
104104

105105
public function setFlag(
106-
string $flag,
106+
UserFlag $flag,
107107
bool $newValue,
108108
bool $doSendMail = true,
109109
bool $doSendMailAdmin = true,
@@ -113,7 +113,7 @@ public function setFlag(
113113
return;
114114
}
115115
if ($newValue) {
116-
$this->LDAP->userFlagGroups[$flag]->addMemberUID($this->uid);
116+
$this->LDAP->userFlagGroups[$flag->value]->addMemberUID($this->uid);
117117
if ($doSendMail) {
118118
$this->MAILER->sendMail($this->getMail(), "user_flag_added", [
119119
"user" => $this->uid,
@@ -129,7 +129,7 @@ public function setFlag(
129129
]);
130130
}
131131
} else {
132-
$this->LDAP->userFlagGroups[$flag]->removeMemberUID($this->uid);
132+
$this->LDAP->userFlagGroups[$flag->value]->removeMemberUID($this->uid);
133133
if ($doSendMail) {
134134
$this->MAILER->sendMail($this->getMail(), "user_flag_removed", [
135135
"user" => $this->uid,

resources/mail/user_flag_added.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
<?php use UnityWebPortal\lib\UserFlag; ?>
12
<?php switch ($data["flag"]):
2-
case "qualified": ?>
3+
case UserFlag::QUALIFIED: ?>
34
<?php $this->Subject = "User Activated"; ?>
45
<p>Hello,</p>
56
<p>Your account on the UnityHPC Platform has been activated. Your account details are below:</p>
@@ -17,31 +18,31 @@
1718
<?php break; ?>
1819

1920
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
20-
<?php case "ghost": ?>
21+
<?php case UserFlag::GHOST: ?>
2122
<?php $this->Subject = "User Deleted"; ?>
2223
<p>Hello,</p>
2324
<p>Your account on the UnityHPC Platform has been deleted.</p>
2425
<p>If you believe this to be a mistake, please reply to this email as soon as possible.</p>
2526
<?php break; ?>
2627

2728
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
28-
<?php case "locked": ?>
29+
<?php case UserFlag::LOCKED: ?>
2930
<?php $this->Subject = "User Locked"; ?>
3031
<p>Hello,</p>
3132
<p>Your account on the UnityHPC Platform has been locked.</p>
3233
<p>If you believe this to be a mistake, please reply to this email as soon as possible.</p>
3334
<?php break; ?>
3435

3536
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
36-
<?php case "idlelocked": ?>
37+
<?php case UserFlag::IDLELOCKED: ?>
3738
<?php $this->Subject = "User Locked"; ?>
3839
<p>Hello,</p>
3940
<p>Your account on the UnityHPC Platform has been locked due to inactivity.</p>
4041
<p>If you believe this to be a mistake, please reply to this email as soon as possible.</p>
4142
<?php break; ?>
4243

4344
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
44-
<?php case "admin": ?>
45+
<?php case UserFlag::ADMIN: ?>
4546
<?php $this->Subject = "User Promoted"; ?>
4647
<p>Hello,</p>
4748
<p>Your account on the UnityHPC Platform has been promoted to admin.</p>

resources/mail/user_flag_added_admin.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1+
<?php use UnityWebPortal\lib\UserFlag; ?>
12
<?php switch ($data["flag"]):
2-
case "qualified": ?>
3+
case UserFlag::QUALIFIED: ?>
34
<?php $this->Subject = "User Qualified"; ?>
45
<p>Hello,</p>
56
<p>User "<?php echo $data["user"] ?>" has been qualified. </p>
67
<?php break; ?>
78

89
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
9-
<?php case "ghost": ?>
10+
<?php case UserFlag::GHOST: ?>
1011
<?php $this->Subject = "User Ghosted"; ?>
1112
<p>Hello,</p>
1213
<p>User "<?php echo $data["user"] ?>" has been marked as ghost. </p>
1314
<?php break; ?>
1415

1516
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
16-
<?php case "locked": ?>
17+
<?php case UserFlag::LOCKED: ?>
1718
<?php $this->Subject = "User Locked"; ?>
1819
<p>Hello,</p>
1920
<p>User "<?php echo $data["user"] ?>" has been locked. </p>
2021
<?php break; ?>
2122

2223
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
23-
<?php case "idlelocked": ?>
24+
<?php case UserFlag::IDLELOCKED: ?>
2425
<?php $this->Subject = "User Idle Locked"; ?>
2526
<p>Hello,</p>
2627
<p>User "<?php echo $data["user"] ?>" has been idle locked. </p>
2728
<?php break; ?>
2829

2930
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
30-
<?php case "admin": ?>
31+
<?php case UserFlag::ADMIN: ?>
3132
<?php $this->Subject = "User Promoted"; ?>
3233
<p>Hello,</p>
3334
<p>User "<?php echo $data["user"] ?>" has been promoted to admin. </p>

resources/mail/user_flag_removed.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1+
<?php use UnityWebPortal\lib\UserFlag; ?>
12
<?php switch ($data["flag"]):
2-
case "qualified": ?>
3+
case UserFlag::QUALIFIED: ?>
34
<?php $this->Subject = "User Deactivated"; ?>
45
<p>Hello,</p>
56
<p>Your account on the UnityHPC Platform has been deactivated.</p>
67
<p>If you believe this to be a mistake, please reply to this email as soon as possible.</p>
78
<?php break; ?>
89

910
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
10-
<?php case "ghost": ?>
11+
<?php case UserFlag::GHOST: ?>
1112
<?php $this->Subject = "User Resurrected"; ?>
1213
<p>Hello,</p>
1314
<p>Your account on the UnityHPC Platform has been resurrected.</p>
1415
<p>If you believe this to be a mistake, please reply to this email as soon as possible.</p>
1516
<?php break; ?>
1617

1718
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
18-
<?php case "locked": ?>
19+
<?php case UserFlag::LOCKED: ?>
1920
<?php $this->Subject = "User Unlocked"; ?>
2021
<p>Hello,</p>
2122
<p>Your account on the UnityHPC Platform has been unlocked.</p>
2223
<p>If you believe this to be a mistake, please reply to this email as soon as possible.</p>
2324
<?php break; ?>
2425

2526
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
26-
<?php case "idlelocked": ?>
27+
<?php case UserFlag::IDLELOCKED: ?>
2728
<?php $this->Subject = "User Unlocked"; ?>
2829
<p>Hello,</p>
2930
<p>Your account on the UnityHPC Platform has been unlocked.</p>
3031
<p>If you believe this to be a mistake, please reply to this email as soon as possible.</p>
3132
<?php break; ?>
3233

3334
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
34-
<?php case "admin": ?>
35+
<?php case UserFlag::ADMIN: ?>
3536
<?php $this->Subject = "User Demoted"; ?>
3637
<p>Hello,</p>
3738
<p>Your account on the UnityHPC Platform has been demoted from admin.</p>

resources/mail/user_flag_removed_admin.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1+
<?php use UnityWebPortal\lib\UserFlag; ?>
12
<?php switch ($data["flag"]):
2-
case "qualified": ?>
3+
case UserFlag::QUALIFIED: ?>
34
<?php $this->Subject = "User Dequalified"; ?>
45
<p>Hello,</p>
56
<p>User "<?php echo $data["user"] ?>" has been dequalified. </p>
67
<?php break; ?>
78

89
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
9-
<?php case "ghost": ?>
10+
<?php case UserFlag::GHOST: ?>
1011
<?php $this->Subject = "User Resurrected"; ?>
1112
<p>Hello,</p>
1213
<p>User "<?php echo $data["user"] ?>" has been resurrected (no longer marked as ghost). </p>
1314
<?php break; ?>
1415

1516
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
16-
<?php case "locked": ?>
17+
<?php case UserFlag::LOCKED: ?>
1718
<?php $this->Subject = "User Unlocked"; ?>
1819
<p>Hello,</p>
1920
<p>User "<?php echo $data["user"] ?>" has been unlocked. </p>
2021
<?php break; ?>
2122

2223
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
23-
<?php case "idlelocked": ?>
24+
<?php case UserFlag::IDLELOCKED: ?>
2425
<?php $this->Subject = "User Idle Unlocked"; ?>
2526
<p>Hello,</p>
2627
<p>User "<?php echo $data["user"] ?>" has been idle unlocked. </p>
2728
<?php break; ?>
2829

2930
<?php /////////////////////////////////////////////////////////////////////////////////////////// ?>
30-
<?php case "admin": ?>
31+
<?php case UserFlag::ADMIN: ?>
3132
<?php $this->Subject = "User Demoted"; ?>
3233
<p>Hello,</p>
3334
<p>User "<?php echo $data["user"] ?>" has been demoted from admin. </p>

test/functional/PIBecomeApproveTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22

3-
use UnityWebPortal\lib\UnityOrg;
4-
use UnityWebPortal\lib\UnitySQL;
3+
use UnityWebPortal\lib\UserFlag;
54

65
class PIBecomeApproveTest extends UnityWebPortalTestCase
76
{
@@ -64,7 +63,7 @@ public function testApprovePI()
6463

6564
$this->assertRequestedPIGroup(false);
6665
$this->assertTrue($pi_group->exists());
67-
$this->assertTrue($USER->getFlag("qualified"));
66+
$this->assertTrue($USER->getFlag(UserFlag::QUALIFIED));
6867

6968
// $third_request_failed = false;
7069
// try {

0 commit comments

Comments
 (0)