Skip to content

Made UnityGroup class more generic #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
removed remove role + ranked roles per priority + implemented isNameable
  • Loading branch information
sheldor1510 committed Aug 30, 2023
commit 623d648ac7b82e1af7dce5ffb98be58498dbce9b
11 changes: 11 additions & 0 deletions resources/lib/UnityGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,7 @@ public function getAvailableRoles()
$role_obj = array();
$role_obj["slug"] = $role;
$role_obj["display_name"] = $this->SQL->getRoleName($role);
$role_obj["priority"] = $this->SQL->getPriority($role);
array_push($out, $role_obj);
}

Expand Down Expand Up @@ -752,4 +753,14 @@ public function getGroupAdmins()

return $admins;
}

public function getRolePriority($user) {
$user_role = $this->SQL->getRole($user, $this->getGroupUID());
return $this->SQL->getPriority($user_role);
}

public function getMemberRole($user) {
$user_role = $this->SQL->getRole($user, $this->getGroupUID());
return $user_role;
}
}
1 change: 1 addition & 0 deletions resources/lib/UnitySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,7 @@ public function getGroupTypes()
"name" => $row['name'],
"time_limited" => $row['time_limited'],
"prefix" => $row['prefix'],
"isNameable" => $row['isNameable'],
);
}

Expand Down
4 changes: 2 additions & 2 deletions resources/lib/UnityUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ public function isAdmin()
*/
public function isPI()
{
$groups = $this->getGroups(true);
$groups = $this->getGroups();
foreach ($groups as $group) {
if ($group->getGroupType() == "pi") {
$admins = $group->getGroupAdmins();
Expand Down Expand Up @@ -649,7 +649,7 @@ public function getGroups($ignorecache = false)
}
}

if ($ignorecache) {
if (!$ignorecache) {
$this->REDIS->setCache($this->getUID(), "groups", $cache_arr);
}

Expand Down
9 changes: 6 additions & 3 deletions tools/docker-dev/sql/bootstrap.sql
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,18 @@ CREATE TABLE `groupTypes` (
`av_roles` varchar(1000) NOT NULL,
`can_request` tinyint(1) NOT NULL,
`prefix` varchar(1000) NOT NULL,
`defSuperRole` varchar(1000) NOT NULL
`defSuperRole` varchar(1000) NOT NULL,
`isNameable` tinyint(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

--
-- Dumping data for table `groupTypes`
--

INSERT INTO `groupTypes` (`id`, `name`, `slug`, `color`, `time_limited`, `def_role`, `av_roles`, `can_request`, `prefix`, `defSuperRole`) VALUES
(1, 'PI', 'pi', '#800000', 0, 'member', 'member,owner,member_approve,ta', 1, 'pi_', 'owner');
INSERT INTO `groupTypes` (`id`, `name`, `slug`, `color`, `time_limited`, `def_role`, `av_roles`, `can_request`, `prefix`, `defSuperRole`, 'isNameable') VALUES
(1, 'PI', 'pi', '#800000', 0, 'member', 'member,owner,member_approve,ta', 1, 'pi_', 'owner', 0),
(2, 'Class', 'class', '#800000', 1, 'member', 'member,owner,member_approve,ta', 1, 'class_', 'owner', 1),
(3, 'Center', 'center', '#800000', 0, 'member', 'member,owner,member_approve,ta', 1, 'center_', 'owner', 1);

-- --------------------------------------------------------

Expand Down
2 changes: 1 addition & 1 deletion webroot/panel/modal/member_search.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}

$group = new UnityGroup($_GET["group"], $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
$assocs = $group->getUsersWithoutRole();
$assocs = $group->getGroupMembers();

$MAX_COUNT = 10; // Max results of PI search

Expand Down
62 changes: 50 additions & 12 deletions webroot/panel/new_group.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ function getTypeNameFromSlug($slug) {
$group_end_date = null;
}

$group_uid = $group_type_prefix . $_POST['group_name'];
if ($_POST['group_name'] == "") {
$group_name = $USER->getUID();
} else {
$group_name = $_POST['group_name'];
}

$group_uid = $group_type_prefix . $group_name;

$new_group = new UnityGroup($group_uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
$new_group->requestGroup($USER->getUID(), $group_type_slug, $_POST['group_name'], $SEND_PIMESG_TO_ADMINS, $group_start_date, $group_end_date);
$new_group->requestGroup($USER->getUID(), $group_type_slug, $group_name, $SEND_PIMESG_TO_ADMINS, $group_start_date, $group_end_date);
header("Refresh:0");
}

Expand Down Expand Up @@ -70,16 +76,13 @@ function getTypeNameFromSlug($slug) {
<form id="newGroupForm" action="" method="POST">
<p>Fill in the following information to request a new group</p>
<div>
<strong>Name (cannot have spaces)&nbsp;&nbsp;</strong><br>
<input type="text" name="group_name" placeholder="name_of_the_group" required style="margin-bottom: 15px"><br>
<div style="color: red; font-size: 0.8rem; display: none; margin-top: -10px;" id="groupNameError">(Name not available. Try something different)/(Invalid name. Make sure to not have spaces)<br></div>
<strong>Type of Group</strong><br>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For specifically the PI group type, I am unable to request a new group. The page refreshes but nothing happens. The other 2 types work fine.

<?php
foreach ($types as $type) {
if ($type['slug'] == 'pi') {
echo "<label><input type='radio' name='group_type' value='" . $type["prefix"] . "-" . $type["slug"] . "-" . $type['time_limited'] . "' checked> " . $type["name"] . "</label><br>";
echo "<label><input type='radio' name='group_type' value='" . $type["prefix"] . "-" . $type["slug"] . "-" . $type['time_limited'] . "-" . $type['isNameable'] . "' checked> " . $type["name"] . "</label><br>";
} else {
echo "<label><input type='radio' name='group_type' value='" . $type["prefix"] . "-" . $type["slug"] . "-" . $type['time_limited'] . "'> " . $type["name"] . "</label><br>";
echo "<label><input type='radio' name='group_type' value='" . $type["prefix"] . "-" . $type["slug"] . "-" . $type['time_limited'] . "-" . $type['isNameable'] . "'> " . $type["name"] . "</label><br>";
}
}
?>
Expand All @@ -88,27 +91,59 @@ function getTypeNameFromSlug($slug) {
<label>Start Date: &nbsp;&nbsp;<input type='date' name='group_start_date'></label><br>
<label>End Date: &nbsp;&nbsp;<input type='date' name='group_end_date'></label><br>
</div>
<div id="nameInputBox" style="margin-top: 10px;">
<strong>Name (cannot have spaces)&nbsp;&nbsp;</strong><br>
<input type="text" name="group_name" placeholder="name_of_the_group" style="margin-bottom: 15px"><br>
<div style="color: red; font-size: 0.8rem; display: none; margin-top: -10px;" id="groupNameError">(Name not available. Try something different)/(Invalid name. Make sure to not have spaces)<br></div>
</div>
</div>
<input style='margin-top: 10px;' type='submit' value='Request Group'>
<input style='margin-top: 10px;' type='submit' value='Request Group' id="requestGroupButton">
</form>

<script>
$(window).on("load", function() {
let type_info = $('input[type=radio][name=group_type]:checked').val().split('-');
const isNameable = type_info[3];
const time_limited = type_info[2];
let date_selector = document.getElementById('dateSelector');
if (time_limited == 1) {
date_selector.style.display = 'block';
} else if (time_limited == 0) {
date_selector.style.display = 'none';
}
let nameInputBox = document.getElementById('nameInputBox');
if (isNameable == 1) {
nameInputBox.style.display = 'block';
} else if (isNameable == 0) {
nameInputBox.style.display = 'none';
}
})

$('input[type=radio][name=group_type]').change(function() {
let type_info = this.value.split('-');
const isNameable = type_info[3];
const time_limited = type_info[2];
let date_selector = document.getElementById('dateSelector');
if (this.value.endsWith('1')) {
if (time_limited == 1) {
date_selector.style.display = 'block';
} else if (this.value.endsWith('0')) {
} else if (time_limited == 0) {
date_selector.style.display = 'none';
}
let nameInputBox = document.getElementById('nameInputBox');
if (isNameable == 1) {
nameInputBox.style.display = 'block';
} else if (isNameable == 0) {
nameInputBox.style.display = 'none';
}
});


$("input[type=text][name=group_name]").keyup(function() {
$group_name = $(this).val();
$span = $("#groupNameError");
if ($group_name.includes(" ")) {
$span.text("Invalid name. Make sure to not have spaces.");
$span.show();
$("#requestGroupButton").prop("disabled", true);
} else {
$span.hide();
$.ajax({url: "<?php echo $CONFIG["site"]["prefix"] ?>/panel/ajax/check_group_name.php?group_name="
Expand All @@ -118,10 +153,13 @@ function getTypeNameFromSlug($slug) {
$span.show();
} else {
$span.hide();
$("#requestGroupButton").prop("disabled", false);
}
}});
}});
$("#requestGroupButton").prop("disabled", true);
}
});

</script>

<?php
Expand Down
52 changes: 23 additions & 29 deletions webroot/panel/view_group.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,36 @@
switch ($_POST["form_name"]) {
case "assignRoleForm":
if (!$unityPerms->checkGrantRole($USER->getUID(), $group->getGroupUID(), $_COOKIE['role']) && !$OPERATOR->isAdmin()) {
echo '<script>alert("You do not have permission to assign roles to this user")</script>';
array_push($modalErrors, "You do not have permission to assign roles to this user");
echo '<script>alert("You do not have permission to assign this role to the user")</script>';
array_push($modalErrors, "You do not have permission to assign this role to the user");
header("Refresh:0");
}

if (empty($modalErrors)) {
$group->assignRole($operated_on, $_COOKIE['role']);
UnitySite::redirect($CONFIG["site"]["prefix"] . "/panel/view_group.php?group=" . $group->getGroupUID());
$operated_on_role = $group->getMemberRole($operated_on->getUID());
if (!$unityPerms->checkRevokeRole($USER->getUID(), $group->getGroupUID(), $operated_on_role) && !$OPERATOR->isAdmin()) {
echo "<script>alert('You do not have permission to revoke this role')</script>";
array_push($modalErrors, "You do not have permission to revoke this role");
header("Refresh:0");
} else {
if (empty($modalErrors)) {
$group->revokeRole($operated_on->getUID(), $operated_on_role);
$group->assignRole($operated_on, $_COOKIE['role']);
UnitySite::redirect($CONFIG["site"]["prefix"] . "/panel/view_group.php?group=" . $group->getGroupUID());
}
}
break;
case "userReq":
if ($_POST["action"] == "Approve") {
if (!$unityPerms->checkApproveUser($USER->getUID(), $group->getGroupUID()) && !$OPERATOR->isAdmin()) {
echo "<script>alert('You do not have permission to approve this user')</script>";
header("Refresh:0");
}

$group->approveUser($form_user);
} elseif ($_POST["action"] == "Deny") {
if (!$unityPerms->checkDenyUser($USER->getUID(), $group->getGroupUID()) && !$OPERATOR->isAdmin()) {
echo "<script>alert('You do not have permission to deny this user')</script>";
header("Refresh:0");
}

$group->denyUser($form_user);
Expand All @@ -75,17 +86,6 @@
UnitySite::redirect($CONFIG["site"]["prefix"] . "/panel/account.php");

break;
case "revokeRole":
if ($revoke_uid == $USER->getUID() && !$OPERATOR->isAdmin()) {
echo "<script>alert('You cannot revoke your own roles')</script>";
} else {
if (!$unityPerms->checkRevokeRole($USER->getUID(), $group->getGroupUID(), $revoke_role) && !$OPERATOR->isAdmin()) {
echo "<script>alert('You do not have permission to revoke this role')</script>";
} else {
$group->revokeRole($revoke_uid, $revoke_role);
UnitySite::redirect($CONFIG["site"]["prefix"] . "/panel/view_group.php?group=" . $group->getGroupUID());
}
}
}
}

Expand Down Expand Up @@ -177,6 +177,10 @@

if ($USER->hasPermission($_GET["group"], "unity.grant_role") || $USER->hasPermission($_GET["group"], "unity.revoke_role") || $OPERATOR->isAdmin()) {
$roles = $group->getAvailableRoles();
$user_priority = $group->getRolePriority($USER->getUID());
usort($roles, function ($a, $b) {
return $b["priority"] - $a["priority"];
});

echo "<br>";
echo "<h2>Manage Roles</h2>";
Expand All @@ -190,18 +194,6 @@
foreach ($users_with_role as $user) {
echo "<table>";
echo "<tr>";
if ($USER->hasPermission($_GET["group"], "unity.admin") || $USER->hasPermission($_GET["group"], "unity.revoke_role" || $OPERATOR->isAdmin())) {
echo "<td>";
echo
"<form action='' method='POST'>
<input type='hidden' name='form_name' value='revokeRole'>
<input type='hidden' name='revoke_uid' value='" . $user->getUID() . "'>
<input type='hidden' name='revoke_role' value='" . $role["slug"] . "'>
<input type='submit' value='Revoke'
onclick='return confirm(\"Are you sure you want to revoke the role from " . $user->getUID() . "?\")'>
</form>";
echo "</td>";
}
echo "<td>" . $user->getFirstname() . " " . $user->getLastname() . "</td>";
echo "<td>" . $user->getUID() . "</td>";
echo "<td><a href='mailto:" . $user->getMail() . "'>" . $user->getMail() . "</a></td>";
Expand All @@ -210,7 +202,9 @@
}
echo "<div>";
echo "<input type='hidden' name='role' value='" . $role["slug"] . "'>";
echo "<button type='button' class='plusBtn btnAssignRole' style='font-size: 13px; padding-top: 7px; padding-bottom: 7px; margin-bottom: 20px;'>Assign " . $role["display_name"] . " Role</button>";
if ($user_priority >= $role["priority"] && ($USER->hasPermission($_GET["group"], "unity.grant_role") || $OPERATOR->isAdmin())) {
echo "<button type='button' class='plusBtn btnAssignRole' style='font-size: 13px; padding-top: 7px; padding-bottom: 7px; margin-bottom: 20px;'>Assign " . $role["display_name"] . " Role</button>";
};
echo "</div>";
}
}
Expand Down