Skip to content

Commit 7832371

Browse files
committed
PI become another PI 2
1 parent e82d30d commit 7832371

File tree

8 files changed

+133
-35
lines changed

8 files changed

+133
-35
lines changed

resources/lib/UnityLDAP.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,22 @@ public function getNonDisabledPIGroupGIDsWithMemberUID(string $uid): array
252252
);
253253
}
254254

255+
/** @return string[] */
256+
public function getPIGroupGIDsWithOwnerMail(string $mail): array
257+
{
258+
$users_attributes = $this->userOU->getChildrenArrayStrict(
259+
attributes: ["uid"],
260+
recursive: false,
261+
filter: sprintf("(mail=%s)", ldap_escape($mail, flags: LDAP_ESCAPE_FILTER)),
262+
);
263+
$uids = array_map(fn($x) => $x["uid"][0], $users_attributes);
264+
$gids = array_map(UnityGroup::ownerUID2GID(...), $uids);
265+
$entries = array_map($this->getPIGroupEntry(...), $gids);
266+
$entries_that_exist = array_filter($entries, fn($x) => $x->exists());
267+
$gids_that_exist = array_map(fn($x) => $x->getAttribute("cn")[0], $entries_that_exist);
268+
return $gids_that_exist;
269+
}
270+
255271
/** @return string[] */
256272
public function getAllNonDisabledPIGroupOwnerUIDs(): array
257273
{

resources/lib/utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function _json_encode(mixed $value, int $flags = 0, int $depth = 512): string
8787
* @param int<1,max> $depth
8888
* @throws Exception
8989
*/
90-
function _json_decode(string $x, ?bool $associative, int $depth = 512, int $flags = 0): mixed
90+
function _json_decode(string $x, ?bool $associative = null, int $depth = 512, int $flags = 0): mixed
9191
{
9292
$output = json_decode($x, $associative, $depth, $flags);
9393
if ($output === null) {

test/functional/PageLoadTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,38 @@ public function testLoadPageNonexistentUser($path)
8383
$output = http_get($path, ignore_die: true);
8484
$this->assertMatchesRegularExpression("/panel\/new_account\.php/", $output);
8585
}
86+
87+
public function testLoadPIPageForAnotherGroup()
88+
{
89+
$this->switchUser("CourseTeacher");
90+
$output = http_get(__DIR__ . "/../../webroot/panel/pi.php", [
91+
"gid" => "pi_cs123_org1_test",
92+
]);
93+
$this->assertMatchesRegularExpression("/My Users/", $output);
94+
}
95+
96+
public function testLoadPIPageForAnotherGroupForbidden()
97+
{
98+
global $USER;
99+
$this->switchUser("EmptyPIGroupOwner");
100+
$gid = $USER->getPIGroup()->gid;
101+
$this->switchUser("Blank");
102+
$output = http_get(
103+
__DIR__ . "/../../webroot/panel/pi.php",
104+
["gid" => $gid],
105+
ignore_die: true,
106+
);
107+
$this->assertMatchesRegularExpression("/not allowed/", $output);
108+
}
109+
110+
public function testLoadPIPageForNonexistentGroup()
111+
{
112+
$this->switchUser("CourseTeacher");
113+
$output = http_get(
114+
__DIR__ . "/../../webroot/panel/pi.php",
115+
["gid" => "foobar"],
116+
ignore_die: true,
117+
);
118+
$this->assertMatchesRegularExpression("/This group does not exist/", $output);
119+
}
86120
}

test/functional/WorkerUnityCourseTest.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ public function testCreateCourse()
66
{
77
global $LDAP;
88
$this->switchUser("Admin");
9-
$pi_group_entry = $LDAP->getPIGroupEntry("pi_cs123_org1_test");
10-
$owner_user_entry = $LDAP->getUserEntry("cs123_org1_test");
9+
$pi_group_entry = $LDAP->getPIGroupEntry("pi_cs124_org1_test");
10+
$owner_user_entry = $LDAP->getUserEntry("cs124_org1_test");
1111
$this->assertFalse($pi_group_entry->exists());
1212
$this->assertFalse($owner_user_entry->exists());
1313
$stdin_file = writeLinesToTmpFile([
14-
"cs123",
14+
"cs124",
1515
"Fall 2025",
16-
"cs123_org1_test",
16+
"cs124_org1_test",
1717
"user1_org1_test",
1818
]);
1919
$stdin_file_path = getPathFromFileHandle($stdin_file);
@@ -26,21 +26,18 @@ public function testCreateCourse()
2626
// our LDAP conn doesn't know about changes from subprocess
2727
unset($GLOBALS["ldapconn"]);
2828
$this->switchUser("Admin");
29-
$pi_group_entry = $LDAP->getPIGroupEntry("pi_cs123_org1_test");
30-
$owner_user_entry = $LDAP->getUserEntry("cs123_org1_test");
29+
$pi_group_entry = $LDAP->getPIGroupEntry("pi_cs124_org1_test");
30+
$owner_user_entry = $LDAP->getUserEntry("cs124_org1_test");
3131
$this->assertTrue($pi_group_entry->exists());
3232
$this->assertTrue($owner_user_entry->exists());
33-
$this->assertEquals(
34-
"user1+cs123_org1_test@org1.test",
35-
$owner_user_entry->getAttribute("mail")[0],
36-
);
33+
$this->assertEquals("user1@org1.test", $owner_user_entry->getAttribute("mail")[0]);
3734
$this->assertEqualsCanonicalizing(
38-
["cs123_org1_test", "user1_org1_test"],
35+
["cs124_org1_test", "user1_org1_test"],
3936
$pi_group_entry->getAttribute("memberuid"),
4037
);
4138
} finally {
42-
ensurePIGroupDoesNotExist("pi_cs123_org1_test");
43-
ensureUserDoesNotExist("cs123_org1_test");
39+
ensurePIGroupDoesNotExist("pi_cs124_org1_test");
40+
ensureUserDoesNotExist("cs124_org1_test");
4441
unlink($stdin_file_path);
4542
}
4643
}

tools/docker-dev/identity/bootstrap.ldif

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,7 @@ memberuid: user1299_org1_test
12001200
memberuid: user1300_org1_test
12011201
memberuid: user1301_org1_test
12021202
memberuid: user1304_org1_test
1203+
memberuid: cs123_org1_test
12031204

12041205
dn: cn=user15_org3_test,ou=groups,dc=unityhpc,dc=test
12051206
cn: user15_org3_test
@@ -37345,3 +37346,34 @@ sn: Price
3734537346
gecos: Melissa Price
3734637347
uid: user2005_org1_test
3734737348
uidnumber: 33130
37349+
37350+
dn: cn=cs123_org1_test,ou=groups,dc=unityhpc,dc=test
37351+
objectClass: posixGroup
37352+
objectClass: top
37353+
gidNumber: 20005
37354+
cn: cs123_org1_test
37355+
37356+
dn: cn=cs123_org1_test,ou=users,dc=unityhpc,dc=test
37357+
objectClass: inetOrgPerson
37358+
objectClass: posixAccount
37359+
objectClass: top
37360+
objectClass: ldapPublicKey
37361+
uid: cs123_org1_test
37362+
givenName: cs123
37363+
sn: Fall 2025
37364+
gecos: cs123 Fall 2025
37365+
mail: user1@org1.test
37366+
o: org1_test
37367+
homeDirectory: /home/cs123_org1_test
37368+
loginShell: /bin/bash
37369+
uidNumber: 20005
37370+
gidNumber: 20005
37371+
cn: cs123_org1_test
37372+
37373+
dn: cn=pi_cs123_org1_test,ou=pi_groups,dc=unityhpc,dc=test
37374+
objectClass: posixGroup
37375+
objectClass: top
37376+
gidNumber: 20007
37377+
cn: pi_cs123_org1_test
37378+
memberUid: cs123_org1_test
37379+
memberUid: user1_org1_test

webroot/css/navbar.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ nav.mainNav a {
2626
text-decoration: none;
2727
cursor: pointer;
2828
transition: background 0.1s;
29+
overflow: hidden;
2930
}
3031

3132
nav.mainNav a:hover {

webroot/panel/pi.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44

55
use UnityWebPortal\lib\UnityUser;
66
use UnityWebPortal\lib\UnityHTTPD;
7+
use UnityWebPortal\lib\UnityGroup;
78

8-
$group = $USER->getPIGroup();
9-
10-
if (!$USER->isPI()) {
11-
UnityHTTPD::forbidden("not a PI", "You are not a PI.");
9+
if (($gid = $_GET["gid"] ?? null) !== null) {
10+
$group = new UnityGroup($gid, $LDAP, $SQL, $MAILER, $WEBHOOK);
11+
if (!$group->exists()) {
12+
UnityHTTPD::badRequest("no such group: '$gid'", "This group does not exist.");
13+
}
14+
if ($group->getOwner()->getMail() !== $USER->getMail()) {
15+
UnityHTTPD::forbidden(
16+
"user '$USER->uid' is not allowed to manage PI group '$gid'",
17+
"You are not allowed to manage this PI group."
18+
);
19+
}
20+
} else {
21+
$group = $USER->getPIGroup();
22+
if (!$USER->isPI()) {
23+
UnityHTTPD::forbidden("not a PI", "You are not a PI.");
24+
}
1225
}
1326

1427
$getUserFromPost = function () {
@@ -57,6 +70,18 @@
5770
<hr>
5871

5972
<?php
73+
foreach ($LDAP->getPIGroupGIDsWithOwnerMail($USER->getMail()) as $gid) {
74+
if ($gid === $group->gid) {
75+
continue;
76+
}
77+
echo "
78+
<form method='GET' action=''>
79+
<input type='hidden' value'$gid'>
80+
<input type='submit' value\"Manage Group '$gid'\">
81+
</form>
82+
";
83+
}
84+
6085
$requests = $group->getRequests();
6186
$assocs = $group->getGroupMembers();
6287

@@ -122,8 +147,9 @@
122147
<tbody>
123148
";
124149

150+
$owner_uid = $group->getOwner()->uid;
125151
foreach ($assocs as $assoc) {
126-
if ($assoc->uid == $USER->uid) {
152+
if ($assoc->uid == $owner_uid) {
127153
continue;
128154
}
129155

workers/unity-course.php

100644100755
Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ function cn2org($cn)
1313
return $matches[1];
1414
}
1515

16-
function insert_plus_address($email, $plus)
17-
{
18-
$parts = explode("@", $email, 2);
19-
return $parts[0] . "+" . $plus . "@" . $parts[1];
20-
}
21-
2216
// if array is length 1 then replace it with its one element
2317
function flatten_attributes(array $attributes): array
2418
{
@@ -30,16 +24,14 @@ function flatten_attributes(array $attributes): array
3024
$cn = strtolower(
3125
trim(readline("Please enter the cn to be used for the course (example: cs123_umass_edu): ")),
3226
);
33-
$operator_uid = trim(
34-
readline(
35-
"Enter the UID of the Unity team member responsible for the course (example: simonleary_umass_edu): ",
36-
),
27+
$teacher_uid = trim(
28+
readline("Enter the UID of the user teaching the course (example: simonleary_umass_edu): "),
3729
);
3830
$org_gid = cn2org($cn);
3931

40-
$operator = new UnityUser($operator_uid, $LDAP, $SQL, $MAILER, $WEBHOOK);
41-
if (!$operator->exists()) {
42-
_die("no such user: '$operator_uid'", 1);
32+
$teacher = new UnityUser($teacher_uid, $LDAP, $SQL, $MAILER, $WEBHOOK);
33+
if (!$teacher->exists()) {
34+
_die("no such user: '$teacher_uid'", 1);
4335
}
4436

4537
$course_user = new UnityUser($cn, $LDAP, $SQL, $MAILER, $WEBHOOK);
@@ -51,7 +43,7 @@ function flatten_attributes(array $attributes): array
5143
if (!$org->exists()) {
5244
print "WARNING: creating new org '$org_gid'...\n";
5345
}
54-
$mail = insert_plus_address($operator->getMail(), $cn);
46+
$mail = $teacher->getMail();
5547
$course_user->init($givenName, $sn, $mail, $org_gid);
5648

5749
$course_pi_group = $course_user->getPIGroup();
@@ -73,6 +65,6 @@ function flatten_attributes(array $attributes): array
7365
JSON_PRETTY_PRINT,
7466
);
7567

76-
$course_pi_group->newUserRequest($operator, false);
77-
$course_pi_group->approveUser($operator);
68+
$course_pi_group->newUserRequest($teacher, false);
69+
$course_pi_group->approveUser($teacher);
7870

0 commit comments

Comments
 (0)