Skip to content

Commit 5b966cb

Browse files
committed
store attributes in request
1 parent 39f18c9 commit 5b966cb

File tree

10 files changed

+174
-78
lines changed

10 files changed

+174
-78
lines changed

resources/lib/UnityGroup.php

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function exists()
7676
// Portal-facing methods, these are the methods called by scripts in webroot
7777
//
7878

79-
public function requestGroup($send_mail_to_admins, $send_mail = true)
79+
public function requestGroup($firstname, $lastname, $email, $org, $send_mail_to_admins, $send_mail = true)
8080
{
8181
// check for edge cases...
8282
if ($this->exists()) {
@@ -88,7 +88,7 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
8888
return;
8989
}
9090

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

9393
if ($send_mail) {
9494
// send email to requestor
@@ -101,9 +101,9 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
101101
"group_request_admin",
102102
array(
103103
"user" => $this->getOwner()->getUID(),
104-
"org" => $this->getOwner()->getOrg(),
105-
"name" => $this->getOwner()->getFullname(),
106-
"email" => $this->getOwner()->getMail()
104+
"org" => $org,
105+
"name" => "$firstname $lastname",
106+
"email" => $email
107107
)
108108
);
109109

@@ -113,9 +113,9 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
113113
"group_request_admin",
114114
array(
115115
"user" => $this->getOwner()->getUID(),
116-
"org" => $this->getOwner()->getOrg(),
117-
"name" => $this->getOwner()->getFullname(),
118-
"email" => $this->getOwner()->getMail()
116+
"org" => $org,
117+
"name" => "$firstname $lastname",
118+
"email" => $email
119119
)
120120
);
121121
}
@@ -125,9 +125,9 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
125125
"group_request_admin",
126126
array(
127127
"user" => $this->getOwner()->getUID(),
128-
"org" => $this->getOwner()->getOrg(),
129-
"name" => $this->getOwner()->getFullname(),
130-
"email" => $this->getOwner()->getMail()
128+
"org" => $org,
129+
"name" => "$firstname $lastname",
130+
"email" => $email
131131
)
132132
);
133133
}
@@ -138,10 +138,11 @@ public function requestGroup($send_mail_to_admins, $send_mail = true)
138138
*/
139139
public function approveGroup($operator = null, $send_mail = true)
140140
{
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-
);
141+
$uid = $this->getOwner()->getUID();
142+
$gid = $this->getPIUID();
143+
$request = $this->SQL->getRequest($uid, $gid);
144+
if (is_null($request)) {
145+
throw new Exception("uid '$uid' does not have a group request!");
145146
}
146147

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

152153
// check if owner exists
153154
if (!$this->getOwner()->exists()) {
154-
$this->getOwner()->init();
155+
$this->getOwner()->init(
156+
$request["firstname"],
157+
$request["lastname"],
158+
$request["email"],
159+
$request["org"],
160+
$send_mail
161+
);
155162
}
156163

157164
// initialize ldap objects, if this fails the script will crash, but nothing will persistently break
@@ -288,15 +295,22 @@ public function cancelGroupJoinRequest($user, $send_mail = true)
288295
*/
289296
public function approveUser($new_user, $send_mail = true)
290297
{
291-
if (!$this->requestExists($new_user)) {
292-
throw new Exception(
293-
"attempt to approve nonexistent request for group='{$this->getPIUID()}' uid='$new_user'"
294-
);
298+
299+
$uid = $new_user->getUID();
300+
$gid = $this->getPIUID();
301+
$request = $this->SQL->getRequest($uid, $gid);
302+
if (is_null($request)) {
303+
throw new Exception("uid '$uid' does not have a request for group '$gid'!");
295304
}
296305

297306
// check if user exists
298307
if (!$new_user->exists()) {
299-
$new_user->init();
308+
$new_user->init(
309+
$request["firstname"],
310+
$request["lastname"],
311+
$request["email"],
312+
$request["org"],
313+
);
300314
}
301315

302316
// add user to the LDAP object
@@ -320,18 +334,21 @@ public function approveUser($new_user, $send_mail = true)
320334
array(
321335
"group" => $this->pi_uid,
322336
"user" => $new_user->getUID(),
323-
"name" => $new_user->getFullName(),
324-
"email" => $new_user->getMail(),
325-
"org" => $new_user->getOrg()
337+
"name" => $request["firstname"] . " " . $request["lastname"],
338+
"email" => $request["email"],
339+
"org" => $request["org"],
326340
)
327341
);
328342
}
329343
}
330344

331345
public function denyUser($new_user, $send_mail = true)
332346
{
333-
if (!$this->requestExists($new_user)) {
334-
return;
347+
$uid = $new_user->getUID();
348+
$gid = $this->getPIUID();
349+
$request = $this->SQL->getRequest($uid, $gid);
350+
if (is_null($request)) {
351+
throw new Exception("uid '$uid' does not have a request for group '$gid'!");
335352
}
336353

337354
// remove request, this will fail silently if the request doesn't exist
@@ -396,7 +413,7 @@ public function removeUser($new_user, $send_mail = true)
396413
}
397414
}
398415

399-
public function newUserRequest($new_user, $send_mail = true)
416+
public function newUserRequest($new_user, $firstname, $lastname, $email, $org, $send_mail = true)
400417
{
401418
if ($this->userExists($new_user)) {
402419
UnitySite::errorLog("warning", "user '$new_user' already in group");
@@ -413,7 +430,7 @@ public function newUserRequest($new_user, $send_mail = true)
413430
return;
414431
}
415432

416-
$this->addRequest($new_user->getUID());
433+
$this->addRequest($new_user->getUID(), $firstname, $lastname, $email, $org);
417434

418435
if ($send_mail) {
419436
// send email to user
@@ -430,9 +447,9 @@ public function newUserRequest($new_user, $send_mail = true)
430447
array(
431448
"group" => $this->pi_uid,
432449
"user" => $new_user->getUID(),
433-
"name" => $new_user->getFullName(),
434-
"email" => $new_user->getMail(),
435-
"org" => $new_user->getOrg()
450+
"name" => "$firstname $lastname",
451+
"email" => $email,
452+
"org" => $org,
436453
)
437454
);
438455
}
@@ -452,7 +469,17 @@ public function getRequests()
452469
$this->REDIS,
453470
$this->WEBHOOK
454471
);
455-
array_push($out, [$user, $request["timestamp"]]);
472+
array_push(
473+
$out,
474+
[
475+
$user,
476+
$request["timestamp"],
477+
$request["firstname"],
478+
$request["lastname"],
479+
$request["email"],
480+
$request["org"],
481+
]
482+
);
456483
}
457484

458485
return $out;
@@ -563,9 +590,9 @@ public function userExists($user)
563590
return in_array($user->getUID(), $this->getGroupMemberUIDs());
564591
}
565592

566-
private function addRequest($uid)
593+
private function addRequest($uid, $firstname, $lastname, $email, $org)
567594
{
568-
$this->SQL->addRequest($uid, $this->pi_uid);
595+
$this->SQL->addRequest($uid, $firstname, $lastname, $email, $org, $this->pi_uid);
569596
}
570597

571598
//

resources/lib/UnitySQL.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,23 @@ public function getConn()
3838
//
3939
// requests table methods
4040
//
41-
public function addRequest($requestor, $dest = self::REQUEST_BECOME_PI)
41+
public function addRequest($requestor, $firstname, $lastname, $email, $org, $dest = self::REQUEST_BECOME_PI)
4242
{
4343
if ($this->requestExists($requestor, $dest)) {
4444
return;
4545
}
4646

4747
$stmt = $this->conn->prepare(
48-
"INSERT INTO " . self::TABLE_REQS . " (uid, request_for) VALUES (:uid, :request_for)"
48+
"INSERT INTO " . self::TABLE_REQS . " " .
49+
"(uid, firstname, lastname, email, org, request_for) VALUES " .
50+
"(:uid, :firstname, :lastname, :email, :org, :request_for)"
4951
);
5052
$stmt->bindParam(":uid", $requestor);
5153
$stmt->bindParam(":request_for", $dest);
54+
$stmt->bindParam(":firstname", $firstname);
55+
$stmt->bindParam(":lastname", $lastname);
56+
$stmt->bindParam(":email", $email);
57+
$stmt->bindParam(":org", $org);
5258

5359
$stmt->execute();
5460
}
@@ -78,17 +84,27 @@ public function removeRequests($dest = self::REQUEST_BECOME_PI)
7884
$stmt->execute();
7985
}
8086

81-
public function requestExists($requestor, $dest = self::REQUEST_BECOME_PI)
87+
public function getRequest($user, $dest)
8288
{
8389
$stmt = $this->conn->prepare(
8490
"SELECT * FROM " . self::TABLE_REQS . " WHERE uid=:uid and request_for=:request_for"
8591
);
86-
$stmt->bindParam(":uid", $requestor);
92+
$stmt->bindParam(":uid", $user);
8793
$stmt->bindParam(":request_for", $dest);
88-
8994
$stmt->execute();
95+
$result = $stmt->fetchAll();
96+
if (count($result) == 0) {
97+
throw new Exception("no such request: uid='$user' request_for='$dest'");
98+
}
99+
if (count($result) > 1) {
100+
throw new Exception("too many requests for uid='$user' request_for='$dest'");
101+
}
102+
return $result[0];
103+
}
90104

91-
return count($stmt->fetchAll()) > 0;
105+
public function requestExists($requestor, $dest = self::REQUEST_BECOME_PI)
106+
{
107+
return (!is_null(self::getRequest($requestor, $dest)));
92108
}
93109

94110
public function getRequests($dest = self::REQUEST_BECOME_PI)

resources/lib/UnityUser.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ public function __toString()
4949
* @param string $firstname First name of new account
5050
* @param string $lastname Last name of new account
5151
* @param string $email email of new account
52+
* @param string $org organization name of new account
5253
* @param bool $isPI boolean value for if the user checked the "I am a PI box"
5354
* @return void
5455
*/
55-
public function init($send_mail = true)
56+
public function init($firstname, $lastname, $email, $org, $send_mail = true)
5657
{
5758
//
5859
// Create LDAP group
@@ -74,14 +75,14 @@ public function init($send_mail = true)
7475
if (!$ldapUserEntry->exists()) {
7576
$ldapUserEntry->setAttribute("objectclass", UnityLDAP::POSIX_ACCOUNT_CLASS);
7677
$ldapUserEntry->setAttribute("uid", $this->uid);
77-
$ldapUserEntry->setAttribute("givenname", $this->getFirstname());
78-
$ldapUserEntry->setAttribute("sn", $this->getLastname());
78+
$ldapUserEntry->setAttribute("givenname", $firstname);
79+
$ldapUserEntry->setAttribute("sn", $lastname);
7980
$ldapUserEntry->setAttribute(
8081
"gecos",
8182
\transliterator_transliterate("Latin-ASCII", "{$this->getFirstname()} {$this->getLastname()}")
8283
);
83-
$ldapUserEntry->setAttribute("mail", $this->getMail());
84-
$ldapUserEntry->setAttribute("o", $this->getOrg());
84+
$ldapUserEntry->setAttribute("mail", $email);
85+
$ldapUserEntry->setAttribute("o", $org);
8586
$ldapUserEntry->setAttribute("homedirectory", self::HOME_DIR . $this->uid);
8687
$ldapUserEntry->setAttribute("loginshell", $this->LDAP->getDefUserShell());
8788
$ldapUserEntry->setAttribute("uidnumber", strval($id));
@@ -90,10 +91,10 @@ public function init($send_mail = true)
9091
}
9192

9293
// update cache
93-
//$this->REDIS->setCache($this->uid, "firstname", $this->getFirstname());
94-
//$this->REDIS->setCache($this->uid, "lastname", $this->getLastname());
95-
//$this->REDIS->setCache($this->uid, "mail", $this->getMail());
96-
//$this->REDIS->setCache($this->uid, "org", $this->getOrg());
94+
$this->REDIS->setCache($this->uid, "firstname", $firstname);
95+
$this->REDIS->setCache($this->uid, "lastname", $lastname);
96+
$this->REDIS->setCache($this->uid, "mail", $email);
97+
$this->REDIS->setCache($this->uid, "org", $org);
9798
$this->REDIS->setCache($this->uid, "homedir", self::HOME_DIR . $this->uid);
9899
$this->REDIS->setCache($this->uid, "loginshell", $this->LDAP->getDefUserShell());
99100
$this->REDIS->setCache($this->uid, "sshkeys", array());

tools/docker-dev/sql/bootstrap.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ CREATE TABLE `requests` (
173173
`id` int(11) NOT NULL,
174174
`request_for` varchar(131) NOT NULL,
175175
`uid` varchar(128) NOT NULL,
176+
`firstname` varchar(768) NOT NULL,
177+
`lastname` varchar(768) NOT NULL,
178+
`email` varchar(768) NOT NULL,
179+
`org` varchar(768) NOT NULL,
176180
`timestamp` timestamp NOT NULL DEFAULT current_timestamp()
177181
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
178182

webroot/admin/ajax/get_group_members.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,22 @@
4848
$key++;
4949
}
5050

51-
foreach ($requests as $key => $request) {
51+
foreach ($requests as $key => [$user, $timestamp, $firstname, $lastname, $email, $org]) {
5252
if ($key >= $count - 1) {
5353
echo "<tr class='expanded $key last'>";
5454
} else {
5555
echo "<tr class='expanded $key'>";
5656
}
57-
58-
[$request, $timestamp] = $request;
59-
echo "<td>" . $request->getFirstname() . " " . $request->getLastname() . "</td>";
60-
echo "<td>" . $request->getUID() . "</td>";
61-
echo "<td><a href='mailto:" . $request->getMail() . "'>" . $request->getMail() . "</a></td>";
57+
$uid = $user->getUID();
58+
echo "<td>" . $firstname . " " . $lastname . "</td>";
59+
echo "<td>" . $uid . "</td>";
60+
echo "<td><a href='mailto:" . $email . "'>" . $email . "</a></td>";
6261
echo "<td>";
6362
echo
6463
"<form action='' method='POST'
65-
onsubmit='return confirm(\"Are you sure you want to approve " . $request->getUID() . "?\");'>
64+
onsubmit='return confirm(\"Are you sure you want to approve " . $uid . "?\");'>
6665
<input type='hidden' name='form_type' value='reqChild'>
67-
<input type='hidden' name='uid' value='" . $request->getUID() . "'>
66+
<input type='hidden' name='uid' value='" . $uid . "'>
6867
<input type='hidden' name='pi' value='" . $group->getPIUID() . "'>
6968
<input type='submit' name='action' value='Approve'>
7069
<input type='submit' name='action' value='Deny'></form>";

webroot/admin/pi-mgmt.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,19 @@
7777
$request_user = new UnityUser($request["uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
7878

7979
echo "<tr>";
80-
echo "<td>" . $request_user->getFirstname() . " " . $request_user->getLastname() . "</td>";
81-
echo "<td>" . $request_user->getUID() . "</td>";
82-
echo "<td><a href='mailto:" . $request_user->getMail() . "'>" . $request_user->getMail() . "</a></td>";
80+
echo "<td>" . $request["firstname"] . " " . $request["lastname"] . "</td>";
81+
echo "<td>" . $request["uid"] . "</td>";
82+
echo "<td><a href='mailto:" . $request["mail"] . "'>" . $request["mail"] . "</a></td>";
8383
echo "<td>" . date("jS F, Y", strtotime($request['timestamp'])) . "</td>";
8484
echo "<td>";
8585
echo
8686
"<form action='' method='POST'>
8787
<input type='hidden' name='form_type' value='req'>
88-
<input type='hidden' name='uid' value='" . $request_user->getUID() . "'>
88+
<input type='hidden' name='uid' value='" . $request["uid"] . "'>
8989
<input type='submit' name='action' value='Approve'
90-
onclick='return confirm(\"Are you sure you want to approve " . $request_user->getUID() . "?\");'>
90+
onclick='return confirm(\"Are you sure you want to approve " . $request["uid"] . "?\");'>
9191
<input type='submit' name='action' value='Deny'
92-
onclick='return confirm(\"Are you sure you want to deny " . $request_user->getUID() . "?\");'>
92+
onclick='return confirm(\"Are you sure you want to deny " . $request["uid"] . "?\");'>
9393
</form>";
9494
echo "</td>";
9595
echo "</tr>";

0 commit comments

Comments
 (0)