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

242 lines
8.6 KiB
PHP
Raw Normal View History

2022-09-13 13:14:49 +00:00
<?php
namespace Misuzu;
use RuntimeException;
use Index\Colour\Colour;
2023-08-30 22:37:21 +00:00
use Misuzu\Perm;
use Misuzu\Auth\AuthTokenCookie;
2022-09-13 13:14:49 +00:00
use Misuzu\Users\User;
2023-08-30 22:37:21 +00:00
$viewerPerms = $msz->getAuthInfo()->getPerms('user');
if(!$msz->isLoggedIn())
Template::throwError(403);
2022-09-13 13:14:49 +00:00
$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
$currentUser = $msz->getActiveUser();
2022-09-13 13:14:49 +00:00
2023-08-30 22:37:21 +00:00
$canManageUsers = $viewerPerms->check(Perm::U_USERS_MANAGE);
$canManagePerms = $viewerPerms->check(Perm::U_PERMS_MANAGE);
$canManageNotes = $viewerPerms->check(Perm::U_NOTES_MANAGE);
$canManageWarnings = $viewerPerms->check(Perm::U_WARNINGS_MANAGE);
$canManageBans = $viewerPerms->check(Perm::U_BANS_MANAGE);
$canImpersonate = $viewerPerms->check(Perm::U_CAN_IMPERSONATE);
$canSendTestMail = $currentUser->isSuperUser();
$hasAccess = $canManageUsers || $canManageNotes || $canManageWarnings || $canManageBans;
if(!$hasAccess)
Template::throwError(403);
$notices = [];
$userId = (int)filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT);
2022-09-13 13:14:49 +00:00
try {
$userInfo = $users->getUser($userId, 'id');
} catch(RuntimeException $ex) {
Template::throwError(404);
2022-09-13 13:14:49 +00:00
}
$currentUserRank = $users->getUserRank($currentUser);
$userRank = $users->getUserRank($userInfo);
$canEdit = $canManageUsers && ($currentUser->isSuperUser() || (string)$currentUser->getId() === $userInfo->getId() || $currentUserRank > $userRank);
$canEditPerms = $canEdit && $canManagePerms;
2023-08-30 22:37:21 +00:00
$permsInfos = $perms->getPermissionInfo(userInfo: $userInfo, categoryNames: Perm::INFO_FOR_USER);
$permsLists = Perm::createList(Perm::LISTS_FOR_USER);
$permsNeedRecalc = false;
2022-09-13 13:14:49 +00:00
if(CSRF::validateRequest() && $canEdit) {
2023-05-21 18:15:04 +00:00
if(!empty($_POST['impersonate_user'])) {
if(!$canImpersonate) {
2023-05-21 18:15:04 +00:00
$notices[] = 'You must be a super user to do this.';
} elseif(!is_string($_POST['impersonate_user']) || $_POST['impersonate_user'] !== 'meow') {
$notices[] = 'You didn\'t say the magic word.';
} else {
$allowToImpersonate = $currentUser->isSuperUser();
if(!$allowToImpersonate) {
$allowImpersonateUsers = $msz->getConfig()->getArray(sprintf('impersonate.allow.u%s', $currentUser->getId()));
$allowToImpersonate = in_array($userInfo->getId(), $allowImpersonateUsers, true);
}
if($allowToImpersonate) {
$msz->createAuditLog('USER_IMPERSONATE', [$userInfo->getId(), $userInfo->getName()]);
$tokenBuilder = $msz->getAuthInfo()->getTokenInfo()->toBuilder();
$tokenBuilder->setImpersonatedUserId($userInfo->getId());
$tokenInfo = $tokenBuilder->toInfo();
AuthTokenCookie::apply($tokenPacker->pack($tokenInfo));
url_redirect('index');
return;
} else $notices[] = 'You aren\'t allowed to impersonate this user.';
2023-05-21 18:15:04 +00:00
}
}
2022-09-13 13:14:49 +00:00
if(!empty($_POST['send_test_email'])) {
if(!$canSendTestMail) {
2022-09-13 13:14:49 +00:00
$notices[] = 'You must be a super user to do this.';
} elseif(!is_string($_POST['send_test_email']) || $_POST['send_test_email'] !== 'yes_send_it') {
$notices[] = 'Invalid request thing shut the fuck up.';
} else {
$testMail = Mailer::sendMessage(
[$userInfo->getEMailAddress() => $userInfo->getName()],
2022-09-13 13:14:49 +00:00
'Flashii Test E-mail',
'You were sent this e-mail to validate if you can receive e-mails from Flashii. You may discard it.'
);
if(!$testMail)
$notices[] = 'Failed to send test e-mail.';
}
}
2023-07-05 01:33:12 +00:00
if(!empty($_POST['roles']) && is_array($_POST['roles'])) {
// Read user input array and throw intval on em
$applyRoles = [];
foreach($_POST['roles'] as $item) {
if(!ctype_digit($item))
die('Invalid item encountered in roles list.');
2023-07-27 23:26:05 +00:00
$applyRoles[] = (string)$item;
2023-07-05 01:33:12 +00:00
}
2023-07-27 23:26:05 +00:00
$existingRoles = [];
foreach($roles->getRoles(userInfo: $userInfo) as $roleInfo)
$existingRoles[$roleInfo->getId()] = $roleInfo;
2022-09-13 13:14:49 +00:00
$removeRoles = [];
2023-07-27 23:26:05 +00:00
foreach($existingRoles as $roleInfo) {
if($roleInfo->isDefault() || !($currentUser->isSuperUser() || $userRank > $roleInfo->getRank()))
2022-09-13 13:14:49 +00:00
continue;
2023-07-27 23:26:05 +00:00
if(!in_array($roleInfo->getId(), $applyRoles))
$removeRoles[] = $roleInfo;
2022-09-13 13:14:49 +00:00
}
2023-07-27 23:26:05 +00:00
if(!empty($removeRoles))
$users->removeRoles($userInfo, $removeRoles);
$addRoles = [];
2022-09-13 13:14:49 +00:00
foreach($applyRoles as $roleId) {
try {
2023-07-27 23:26:05 +00:00
$roleInfo = $existingRoles[$roleId] ?? $roles->getRole($roleId);
} catch(RuntimeException $ex) {
2022-09-13 13:14:49 +00:00
continue;
}
2023-07-27 23:26:05 +00:00
if(!$currentUser->isSuperUser() && $userRank <= $roleInfo->getRank())
2022-09-13 13:14:49 +00:00
continue;
2023-07-27 23:26:05 +00:00
if(!in_array($roleInfo, $existingRoles))
$addRoles[] = $roleInfo;
}
2022-09-13 13:14:49 +00:00
2023-07-27 23:26:05 +00:00
if(!empty($addRoles))
$users->addRoles($userInfo, $addRoles);
2023-08-30 22:37:21 +00:00
if(!empty($addRoles) || !empty($removeRoles))
$permsNeedRecalc = true;
2022-09-13 13:14:49 +00:00
}
if(!empty($_POST['user']) && is_array($_POST['user'])) {
2023-08-30 22:37:21 +00:00
$setCountry = (string)($_POST['user']['country'] ?? '');
$setTitle = (string)($_POST['user']['title'] ?? '');
2022-09-13 13:14:49 +00:00
2023-07-27 23:26:05 +00:00
$displayRole = (string)($_POST['user']['display_role'] ?? 0);
if(!$users->hasRole($userInfo, $displayRole))
$notices[] = 'User does not have the role you\'re trying to assign as primary.';
2022-09-13 13:14:49 +00:00
$countryValidation = strlen($setCountry) === 2
&& ctype_alpha($setCountry)
&& ctype_upper($setCountry);
if(!$countryValidation)
$notices[] = 'Country code was invalid.';
if(strlen($setTitle) > 64)
$notices[] = 'User title was invalid.';
2023-07-27 23:26:05 +00:00
if(empty($notices)) {
$users->updateUser(
userInfo: $userInfo,
displayRoleInfo: $displayRole,
countryCode: (string)($_POST['user']['country'] ?? 'XX'),
title: (string)($_POST['user']['title'] ?? '')
2023-07-27 23:26:05 +00:00
);
}
2022-09-13 13:14:49 +00:00
}
if(!empty($_POST['colour']) && is_array($_POST['colour'])) {
$setColour = null;
if(!empty($_POST['colour']['enable'])) {
$setColour = \Index\Colour\Colour::parse((string)($_POST['colour']['hex'] ?? ''));
if($setColour->shouldInherit())
$notices[] = 'Invalid colour specified.';
}
2022-09-13 13:14:49 +00:00
if(empty($notices))
$users->updateUser(userInfo: $userInfo, colour: $setColour);
2022-09-13 13:14:49 +00:00
}
if(!empty($_POST['password']) && is_array($_POST['password'])) {
$passwordNewValue = (string)($_POST['password']['new'] ?? '');
$passwordConfirmValue = (string)($_POST['password']['confirm'] ?? '');
if(!empty($passwordNewValue)) {
if($passwordNewValue !== $passwordConfirmValue)
$notices[] = 'Confirm password does not match.';
else {
$passwordValidation = $users->validatePassword($passwordNewValue);
if($passwordValidation !== '')
$notices[] = $users->validatePasswordText($passwordValidation);
}
if(empty($notices))
$users->updateUser(userInfo: $userInfo, password: $passwordNewValue);
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_USER
);
2022-09-13 13:14:49 +00:00
2023-08-30 22:37:21 +00:00
foreach($permsApply as $categoryName => $values)
$perms->setPermissions($categoryName, $values['allow'], $values['deny'], userInfo: $userInfo);
2022-09-13 13:14:49 +00:00
$permsNeedRecalc = true;
2022-09-13 13:14:49 +00:00
}
if($permsNeedRecalc)
$perms->precalculatePermissions($msz->getForum(), [$userInfo->getId()]);
url_redirect('manage-user', ['user' => $userInfo->getId()]);
return;
2022-09-13 13:14:49 +00:00
}
2023-07-27 23:26:05 +00:00
$rolesAll = $roles->getRoles();
$userRoleIds = $users->hasRoles($userInfo, $rolesAll);
2022-09-13 13:14:49 +00:00
Template::render('manage.users.user', [
'user_info' => $userInfo,
'manage_notices' => $notices,
2023-07-27 23:26:05 +00:00
'manage_roles' => $rolesAll,
'manage_user_has_roles' => $userRoleIds,
2022-09-13 13:14:49 +00:00
'can_edit_user' => $canEdit,
'can_edit_perms' => $canEdit && $canEditPerms,
2023-07-25 14:40:31 +00:00
'can_manage_notes' => $canManageNotes,
'can_manage_warnings' => $canManageWarnings,
'can_manage_bans' => $canManageBans,
'can_impersonate' => $canImpersonate,
'can_send_test_mail' => $canSendTestMail,
2023-08-30 22:37:21 +00:00
'perms_lists' => $permsLists,
'perms_infos' => $permsInfos,
2022-09-13 13:14:49 +00:00
]);