Skip to content

Commit 94e11a8

Browse files
authored
last second fixes (#239)
* form_name -> form_type * bump version * fix update-ldap-cache.php * help long option * executable * no die
1 parent 243c6b5 commit 94e11a8

File tree

12 files changed

+61
-62
lines changed

12 files changed

+61
-62
lines changed

defaults/config.ini.default

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
; DO NOT EDIT THIS FILE. Instead make a config.ini file in the config folder.
33
;
44
[upstream]
5-
version = "1.1.2" ; Current upstream version of the web portal
5+
version = "1.2.0" ; Current upstream version of the web portal
66
repo = "https://github.com/UnityHPC/unity-web-portal" ; Upstream URL for the web portal
77

88
[site]

resources/lib/UnityLDAP.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -332,35 +332,4 @@ public function getOrgGroupEntry($gid)
332332
$gid = ldap_escape($gid, LDAP_ESCAPE_DN);
333333
return $this->getEntry(unityLDAP::RDN . "=$gid," . $this->STR_ORGGROUPOU);
334334
}
335-
336-
public static function parseUserChildrenArray(array $userChildrenArray): array
337-
{
338-
// input comes from LdapEntry::getChildrenArray on a UnityUser
339-
$output = [];
340-
$required_string_attributes = [
341-
"gidnumber",
342-
"givenname",
343-
"homedirectory",
344-
"loginshell",
345-
"mail",
346-
"o",
347-
"sn",
348-
"uid",
349-
"uidnumber",
350-
"gecos",
351-
];
352-
foreach ($required_string_attributes as $key) {
353-
$output[$key] = $userChildrenArray[$key][0];
354-
}
355-
$output["firstname"] = $output["givenname"];
356-
$output["lastname"] = $output["sn"];
357-
$output["org"] = $output["o"];
358-
$output["objectclass"] = $userChildrenArray["objectclass"];
359-
if (array_key_exists("sspublickey", $userChildrenArray)) {
360-
$output["sshpublickey"] = $userChildrenArray["sshpublickey"];
361-
} else {
362-
$output["sshpublickey"] = [];
363-
}
364-
return $output;
365-
}
366335
}

resources/templates/header.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
if ($_SERVER["REQUEST_METHOD"] == "POST") {
66
if ((@$_SESSION["is_admin"] ?? false) == true
7-
&& (@$_POST["form_name"] ?? null) == "clearView"
7+
&& (@$_POST["form_type"] ?? null) == "clearView"
88
) {
99
unset($_SESSION["viewUser"]);
1010
UnitySite::redirect($CONFIG["site"]["prefix"] . "/admin/user-mgmt.php");
@@ -141,7 +141,7 @@
141141
<div id='viewAsBar'>
142142
<span>You are accessing the web portal as the user <strong>$viewUser</strong></span>
143143
<form method='POST' action=''>
144-
<input type='hidden' name='form_name' value='clearView'>
144+
<input type='hidden' name='form_type' value='clearView'>
145145
<input type='hidden' name='uid' value='$viewUser'>
146146
<input type='submit' value='Return to My User'>
147147
</form>

test/functional/PiRemoveUserTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ private function removeUser(string $uid)
99
{
1010
http_post(
1111
__DIR__ . "/../../webroot/panel/pi.php",
12-
["form_name" => "remUser", "uid" => $uid]
12+
["form_type" => "remUser", "uid" => $uid]
1313
);
1414
}
1515

test/functional/ViewAsUserTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function _testViewAsUser(array $beforeUser, array $afterUser)
1818
http_post(
1919
__DIR__ . "/../../webroot/admin/user-mgmt.php",
2020
[
21-
"form_name" => "viewAsUser",
21+
"form_type" => "viewAsUser",
2222
"uid" => $afterUid,
2323
],
2424
);
@@ -32,7 +32,7 @@ public function _testViewAsUser(array $beforeUser, array $afterUser)
3232
// $this->assertTrue($_SESSION["user_exists"]);
3333
http_post(
3434
__DIR__ . "/../../resources/templates/header.php",
35-
["form_name" => "clearView"],
35+
["form_type" => "clearView"],
3636
);
3737
$this->assertArrayNotHasKey("viewUser", $_SESSION);
3838
// redirect means that php process dies and user's browser will initiate a new one
@@ -68,7 +68,7 @@ public function testNonAdminViewAsAdmin()
6868
http_post(
6969
__DIR__ . "/../../webroot/admin/user-mgmt.php",
7070
[
71-
"form_name" => "viewAsUser",
71+
"form_type" => "viewAsUser",
7272
"uid" => $adminUid,
7373
],
7474
);

webroot/admin/ajax/get_group_members.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
echo
3838
"<form action='' method='POST' onsubmit='return confirm(\"Are you sure you want to remove " .
3939
$member->getUID() . " from this group?\");'>
40-
<input type='hidden' name='form_name' value='remUserChild'>
40+
<input type='hidden' name='form_type' value='remUserChild'>
4141
<input type='hidden' name='uid' value='" . $member->getUID() . "'>
4242
<input type='hidden' name='pi' value='" . $group->getPIUID() . "'>
4343
<input type='submit' value='Remove'>
@@ -63,7 +63,7 @@
6363
echo
6464
"<form action='' method='POST'
6565
onsubmit='return confirm(\"Are you sure you want to approve " . $request->getUID() . "?\");'>
66-
<input type='hidden' name='form_name' value='reqChild'>
66+
<input type='hidden' name='form_type' value='reqChild'>
6767
<input type='hidden' name='uid' value='" . $request->getUID() . "'>
6868
<input type='hidden' name='pi' value='" . $group->getPIUID() . "'>
6969
<input type='submit' name='action' value='Approve'>

webroot/admin/pi-mgmt.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
$form_user = new UnityUser($_POST["uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
1616
}
1717

18-
switch ($_POST["form_name"]) {
18+
switch ($_POST["form_type"]) {
1919
case "req":
2020
if ($_POST["action"] == "Approve") {
2121
// approve group
@@ -84,7 +84,7 @@
8484
echo "<td>";
8585
echo
8686
"<form action='' method='POST'>
87-
<input type='hidden' name='form_name' value='req'>
87+
<input type='hidden' name='form_type' value='req'>
8888
<input type='hidden' name='uid' value='" . $request_user->getUID() . "'>
8989
<input type='submit' name='action' value='Approve'
9090
onclick='return confirm(\"Are you sure you want to approve " . $request_user->getUID() . "?\");'>

webroot/admin/user-mgmt.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010

1111
if ($_SERVER["REQUEST_METHOD"] == "POST") {
12-
switch ($_POST["form_name"]) {
12+
switch ($_POST["form_type"]) {
1313
case "viewAsUser":
1414
$_SESSION["viewUser"] = $_POST["uid"];
1515
UnitySite::redirect($CONFIG["site"]["prefix"] . "/panel/account.php");
@@ -65,7 +65,7 @@
6565
echo "<td>";
6666
echo "<form class='viewAsUserForm' action='' method='POST'
6767
onsubmit='return confirm(\"Are you sure you want to switch to the user " . $user->getUID() . "?\");'>
68-
<input type='hidden' name='form_name' value='viewAsUser'>
68+
<input type='hidden' name='form_type' value='viewAsUser'>
6969
<input type='hidden' name='uid' value='" . $user->getUID() . "'>
7070
<input type='submit' name='action' value='Access'>
7171
</form>";

webroot/panel/groups.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
$modalErrors = array();
1010
$errors = array();
1111

12-
if (isset($_POST["form_name"])) {
12+
if (isset($_POST["form_type"])) {
1313
if (isset($_POST["pi"])) {
1414
$pi_account = new UnityGroup(trim($_POST["pi"]), $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
1515
if (!$pi_account->exists()) {
@@ -18,7 +18,7 @@
1818
}
1919
}
2020

21-
switch ($_POST["form_name"]) {
21+
switch ($_POST["form_type"]) {
2222
case "addPIform":
2323
// The new PI modal was submitted
2424
// existing PI request
@@ -116,7 +116,7 @@
116116
"<td>
117117
<form action='' method='POST'
118118
onsubmit='return confirm(\"Are you sure you want to leave the PI group " . $group->getPIUID() . "?\")'>
119-
<input type='hidden' name='form_name' value='removePIForm'>
119+
<input type='hidden' name='form_type' value='removePIForm'>
120120
<input type='hidden' name='pi' value='" . $group->getPIUID() . "'>
121121
<input type='submit' value='Leave Group'>
122122
</form>

webroot/panel/modal/new_pi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
?>
55

66
<form id="newPIform" method="POST" action="<?php echo $CONFIG["site"]["prefix"]; ?>/panel/groups.php">
7-
<input type="hidden" name="form_name" value="addPIform">
7+
<input type="hidden" name="form_type" value="addPIform">
88
<div style="position: relative;">
99
<input type="text" id="pi_search" name="pi" placeholder="Search PI by NetID" required>
1010
<div class="searchWrapper" style="display: none;"></div>

webroot/panel/pi.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
$form_user = new UnityUser($_POST["uid"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
1717
}
1818

19-
switch ($_POST["form_name"]) {
19+
switch ($_POST["form_type"]) {
2020
case "userReq":
2121
if ($_POST["action"] == "Approve") {
2222
$group->approveUser($form_user);
@@ -62,7 +62,7 @@
6262
echo "<td>";
6363
echo
6464
"<form action='' method='POST'>
65-
<input type='hidden' name='form_name' value='userReq'>
65+
<input type='hidden' name='form_type' value='userReq'>
6666
<input type='hidden' name='uid' value='" . $request[0]->getUID() . "'>
6767
<input type='submit' name='action' value='Approve'
6868
onclick='return confirm(\"Are you sure you want to approve " . $request[0]->getUID() . "?\")'>
@@ -92,7 +92,7 @@
9292
echo "<td>";
9393
echo
9494
"<form action='' method='POST'>
95-
<input type='hidden' name='form_name' value='remUser'>
95+
<input type='hidden' name='form_type' value='remUser'>
9696
<input type='hidden' name='uid' value='" . $assoc->getUID() . "'>
9797
<input type='submit' value='Remove'
9898
onclick='return confirm(\"Are you sure you want to remove " . $assoc->getUID() . " from your PI group?\")'>

workers/update-ldap-cache.php

100644100755
Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/bin/php
12
<?php
23

34
require_once __DIR__ . "/../resources/autoload.php";
@@ -13,44 +14,73 @@
1314
use UnityWebPortal\lib\UnityWebhook;
1415
use PHPOpenLDAPer\LDAPEntry;
1516

16-
$options = getopt("fu");
17+
// in PHP LDAP all attributes are arrays, we need these as strings instead
18+
// it's possible but probably difficult to find this out using LDAP schema information
19+
$user_string_attributes = [
20+
"gidnumber",
21+
"givenname",
22+
"homedirectory",
23+
"loginshell",
24+
"mail",
25+
"o",
26+
"sn",
27+
"uid",
28+
"uidnumber",
29+
"gecos",
30+
];
31+
32+
$pi_group_string_attributes = [
33+
"gidnumber",
34+
];
35+
36+
$options = getopt("fuh", ["help"]);
37+
if (array_key_exists("h", $options) or array_key_exists("help", $options)) {
38+
echo "arguments:
39+
f: flush cache and then update
40+
u: update cache even if already initialized
41+
h --help: display this message\n";
42+
UnitySite::die();
43+
}
1744
if (array_key_exists("f", $options)) {
1845
echo "flushing cache...\n";
1946
$REDIS->flushAll();
2047
}
2148

2249
if ((!is_null($REDIS->getCache("initialized", "")) and (!array_key_exists("u", $options)))) {
2350
echo "cache is already initialized, nothing doing.";
24-
echo " use -f argument to flush cache, or -u argument to update without flush.";
51+
echo " use -f argument to flush cache, or -u argument to update without flush.\n";
2552
} else {
2653
echo "updating cache...\n";
27-
$user_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["user_ou"]);
2854
echo "waiting for LDAP response (users)...\n";
29-
$users = $user_ou->getChildrenArray(true);
55+
$users = $LDAP->search("objectClass=posixAccount", $CONFIG["ldap"]["basedn"]);
3056
echo "response received.\n";
3157
// phpcs:disable
32-
$user_CNs = array_map(function ($x){return $x["cn"][0];}, $users);
58+
$user_CNs = array_map(function ($x){return $x->getAttribute("cn")[0];}, $users);
3359
// phpcs:enable
3460
sort($user_CNs);
3561
$REDIS->setCache("sorted_users", "", $user_CNs);
3662
foreach ($users as $user) {
37-
$attribute_array = UnityLDAP::parseUserChildrenArray($user);
38-
foreach ($attribute_array as $key => $val) {
39-
$REDIS->setCache($user["cn"][0], $key, $val);
63+
$cn = $user->getAttribute("cn")[0];
64+
foreach ($user->getAttributes() as $key => $val) {
65+
if (in_array($key, $user_string_attributes)) {
66+
$REDIS->setCache($cn, $key, $val[0]);
67+
} else {
68+
$REDIS->setCache($cn, $key, $val);
69+
}
4070
}
4171
}
4272

4373
$org_group_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["orggroup_ou"]);
4474
echo "waiting for LDAP response (org_groups)...\n";
45-
$org_groups = $org_group_ou->getChildrenArray(true);
75+
$org_groups = $LDAP->search("objectClass=posixGroup", $CONFIG["ldap"]["basedn"]);
4676
echo "response received.\n";
4777
// phpcs:disable
48-
$org_group_CNs = array_map(function($x){return $x["cn"][0];}, $org_groups);
78+
$org_group_CNs = array_map(function($x){return $x->getAttribute("cn")[0];}, $org_groups);
4979
// phpcs:enable
5080
sort($org_group_CNs);
5181
$REDIS->setCache("sorted_orgs", "", $org_group_CNs);
5282
foreach ($org_groups as $org_group) {
53-
$REDIS->setCache($org_group["cn"][0], "members", $org_group["memberuid"]);
83+
$REDIS->setCache($org_group->getAttribute("cn")[0], "members", $org_group->getAttribute("memberuid"));
5484
}
5585

5686
$pi_group_ou = new LDAPEntry($LDAP->getConn(), $CONFIG["ldap"]["pigroup_ou"]);

0 commit comments

Comments
 (0)