misuzu/public-legacy/manage/users/role.php

182 lines
5.9 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
use RuntimeException;
use Index\Colour\Colour;
use Index\Colour\ColourRGB;
2023-08-30 22:37:21 +00:00
use Misuzu\Perm;
2022-09-13 13:14:49 +00:00
$authInfo = $msz->getAuthInfo();
$viewerPerms = $authInfo->getPerms('user');
if(!$viewerPerms->check(Perm::U_ROLES_MANAGE))
Template::throwError(403);
2022-09-13 13:14:49 +00:00
$roleInfo = null;
$usersCtx = $msz->getUsersContext();
$users = $usersCtx->getUsers();
$roles = $usersCtx->getRoles();
2023-08-30 22:37:21 +00:00
$perms = $msz->getPerms();
2023-07-27 23:26:05 +00:00
if(filter_has_var(INPUT_GET, 'r')) {
$roleId = (string)filter_input(INPUT_GET, 'r', FILTER_SANITIZE_NUMBER_INT);
2022-09-13 13:14:49 +00:00
try {
2023-07-27 23:26:05 +00:00
$isNew = false;
$roleInfo = $roles->getRole($roleId);
} catch(RuntimeException $ex) {
Template::throwError(404);
2022-09-13 13:14:49 +00:00
}
2023-07-27 23:26:05 +00:00
} else $isNew = true;
2022-09-13 13:14:49 +00:00
$currentUser = $authInfo->getUserInfo();
2023-08-30 22:37:21 +00:00
$canEditPerms = $viewerPerms->check(Perm::U_PERMS_MANAGE);
2022-09-13 13:14:49 +00:00
$permsInfos = $roleInfo === null ? null : $perms->getPermissionInfo(roleInfo: $roleInfo, categoryNames: Perm::INFO_FOR_ROLE);
2023-08-30 22:37:21 +00:00
$permsLists = Perm::createList(Perm::LISTS_FOR_ROLE);
2022-09-13 13:14:49 +00:00
2023-07-27 23:26:05 +00:00
while($_SERVER['REQUEST_METHOD'] === 'POST' && CSRF::validateRequest()) {
$userRank = $users->getUserRank($currentUser);
if(!$isNew && !$currentUser->isSuperUser() && $roleInfo->getRank() >= $userRank) {
2023-07-27 23:26:05 +00:00
echo 'You aren\'t allowed to edit this role.';
break;
2022-09-13 13:14:49 +00:00
}
$roleString = (string)filter_input(INPUT_POST, 'ur_string');
2023-07-27 23:26:05 +00:00
$roleName = (string)filter_input(INPUT_POST, 'ur_name');
$roleHide = !empty($_POST['ur_hidden']);
$roleLeavable = !empty($_POST['ur_leavable']);
$roleRank = (int)filter_input(INPUT_POST, 'ur_rank', FILTER_SANITIZE_NUMBER_INT);
$roleTitle = (string)filter_input(INPUT_POST, 'ur_title');
$roleDesc = (string)filter_input(INPUT_POST, 'ur_desc');
$colourInherit = !empty($_POST['ur_col_inherit']);
$colourRed = (int)filter_input(INPUT_POST, 'ur_col_red', FILTER_SANITIZE_NUMBER_INT);
$colourGreen = (int)filter_input(INPUT_POST, 'ur_col_green', FILTER_SANITIZE_NUMBER_INT);
$colourBlue = (int)filter_input(INPUT_POST, 'ur_col_blue', FILTER_SANITIZE_NUMBER_INT);
Template::set([
'role_ur_string' => $roleString,
2023-07-27 23:26:05 +00:00
'role_ur_name' => $roleName,
'role_ur_hidden' => $roleHide,
'role_ur_leavable' => $roleLeavable,
'role_ur_rank' => $roleRank,
'role_ur_title' => $roleTitle,
'role_ur_desc' => $roleDesc,
'role_ur_col_inherit' => $colourInherit,
'role_ur_col_red' => $colourRed,
'role_ur_col_green' => $colourGreen,
'role_ur_col_blue' => $colourBlue,
]);
if(!$currentUser->isSuperUser() && $roleRank >= $userRank) {
2023-07-27 23:26:05 +00:00
echo 'You aren\'t allowed to make a role with equal rank to your own.';
break;
2022-09-13 13:14:49 +00:00
}
2023-07-27 23:26:05 +00:00
$roleNameLength = mb_strlen($roleName);
if($roleNameLength < 1 || $roleNameLength > 100) {
echo 'Provided role name is either too long or too short.';
break;
2022-09-13 13:14:49 +00:00
}
2023-07-27 23:26:05 +00:00
if($roleRank < 1 || $roleRank > 100) {
echo 'Role rank may not be less than 1 or more than 100.';
break;
2022-09-13 13:14:49 +00:00
}
2023-07-27 23:26:05 +00:00
$roleColour = $colourInherit
? Colour::none()
: new ColourRGB($colourRed, $colourGreen, $colourBlue);
2022-09-13 13:14:49 +00:00
2023-07-27 23:26:05 +00:00
if(mb_strlen($roleDesc) > 1000) {
echo 'Description may not be longer than 1000 characters.';
break;
2022-09-13 13:14:49 +00:00
}
2023-07-27 23:26:05 +00:00
if(mb_strlen($roleTitle) > 64) {
echo 'Role title may not be longer than 64 characters.';
break;
2022-09-13 13:14:49 +00:00
}
if(strlen($roleString) > 20) {
echo 'Role string may not be longer than 20 characters.';
break;
}
if(strlen($roleString) > 1 && !ctype_alpha($roleString[0])) {
echo 'Role string most start with an alphabetical character.';
break;
}
2023-07-27 23:26:05 +00:00
if($isNew) {
$roleInfo = $roles->createRole(
$roleName,
$roleRank,
$roleColour,
string: $roleString,
title: $roleTitle,
description: $roleDesc,
hidden: $roleHide,
leavable: $roleLeavable
);
2023-07-27 23:26:05 +00:00
} else {
if($roleName === $roleInfo->getName())
$roleName = null;
if($roleString === $roleInfo->getString())
$roleString = null;
2023-07-27 23:26:05 +00:00
if($roleHide === $roleInfo->isHidden())
$roleHide = null;
if($roleLeavable === $roleInfo->isLeavable())
$roleLeavable = null;
if($roleRank === $roleInfo->getRank())
$roleRank = null;
if($roleTitle === $roleInfo->getTitle())
$roleTitle = null;
if($roleDesc === $roleInfo->getDescription())
$roleDesc = null;
// local genius did not implement colour comparison
if((string)$roleColour === (string)$roleInfo->getColour())
$roleColour = null;
$roles->updateRole(
$roleInfo,
string: $roleString,
name: $roleName,
rank: $roleRank,
colour: $roleColour,
title: $roleTitle,
description: $roleDesc,
hidden: $roleHide,
leavable: $roleLeavable
);
2023-07-27 23:26:05 +00:00
}
2022-09-13 13:14:49 +00:00
2023-07-27 23:26:05 +00:00
$msz->createAuditLog(
$isNew ? 'ROLE_CREATE' : 'ROLE_UPDATE',
[$roleInfo->getId()]
);
2022-09-13 13:14:49 +00:00
2023-08-30 22:37:21 +00:00
if($canEditPerms && filter_has_var(INPUT_POST, 'perms')) {
$permsApply = Perm::convertSubmission(
filter_input(INPUT_POST, 'perms', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY),
Perm::INFO_FOR_ROLE
);
foreach($permsApply as $categoryName => $values)
$perms->setPermissions($categoryName, $values['allow'], $values['deny'], roleInfo: $roleInfo);
// could target all users with the role but ech
2023-08-30 22:37:21 +00:00
$msz->getConfig()->setBoolean('perms.needsRecalc', true);
2022-09-13 13:14:49 +00:00
}
2023-09-08 20:40:48 +00:00
Tools::redirect($msz->getURLs()->format('manage-role', ['role' => $roleInfo->getId()]));
2022-09-13 13:14:49 +00:00
return;
}
Template::render('manage.users.role', [
2023-07-27 23:26:05 +00:00
'role_new' => $isNew,
2022-09-13 13:14:49 +00:00
'role_info' => $roleInfo ?? null,
2023-08-30 22:37:21 +00:00
'can_edit_perms' => $canEditPerms,
'perms_lists' => $permsLists,
'perms_infos' => $permsInfos,
2022-09-13 13:14:49 +00:00
]);