misuzu/public/manage/users/user.php

190 lines
6.7 KiB
PHP
Raw Normal View History

2019-06-10 00:10:59 +02:00
<?php
namespace Misuzu;
2020-05-25 19:58:06 +00:00
use Misuzu\Users\User;
2020-06-04 18:48:01 +00:00
use Misuzu\Users\UserNotFoundException;
use Misuzu\Users\UserRole;
use Misuzu\Users\UserRoleNotFoundException;
2020-05-25 19:58:06 +00:00
2019-06-10 00:10:59 +02:00
require_once '../../../misuzu.php';
2020-05-25 19:58:06 +00:00
if(!User::hasCurrent() || !perms_check_user(MSZ_PERMS_USER, User::getCurrent()->getId(), MSZ_PERM_USER_MANAGE_USERS)) {
2019-06-10 00:10:59 +02:00
echo render_error(403);
return;
}
$notices = [];
2020-06-04 18:48:01 +00:00
$userId = (int)filter_input(INPUT_GET, 'u', FILTER_SANITIZE_NUMBER_INT);
$currentUser = User::getCurrent();
$currentUserId = $currentUser->getId();
2019-06-10 00:10:59 +02:00
2020-06-04 18:48:01 +00:00
try {
$userInfo = User::byId($userId);
} catch(UserNotFoundException $ex) {
2019-06-10 00:10:59 +02:00
echo render_error(404);
return;
}
2020-06-04 18:48:01 +00:00
$canEdit = $currentUser->hasAuthorityOver($userInfo);
2019-06-10 00:10:59 +02:00
$canEditPerms = $canEdit && perms_check_user(MSZ_PERMS_USER, $currentUserId, MSZ_PERM_USER_MANAGE_PERMS);
2019-06-10 15:43:55 +02:00
$permissions = manage_perms_list(perms_get_user_raw($userId));
2019-06-10 00:10:59 +02:00
2019-12-11 19:10:54 +01:00
if(CSRF::validateRequest() && $canEdit) {
2019-06-10 00:10:59 +02:00
if(!empty($_POST['roles']) && is_array($_POST['roles']) && array_test($_POST['roles'], 'ctype_digit')) {
// Fetch existing roles
2020-06-04 18:48:01 +00:00
$existingRoles = $userInfo->getRoles();
2019-06-10 00:10:59 +02:00
2020-06-04 18:48:01 +00:00
// Initialise set array with existing roles
$setRoles = $existingRoles;
2019-06-10 00:10:59 +02:00
// Read user input array and throw intval on em
$applyRoles = array_apply($_POST['roles'], 'intval');
// Storage array for roles to dump
$removeRoles = [];
// STEP 1: Check for roles to be removed in the existing set.
// Roles that the current users isn't allowed to touch (hierarchy) will stay.
foreach($setRoles as $role) {
// Also prevent the main role from being removed.
2020-06-04 18:48:01 +00:00
if($role->isDefault() || !$currentUser->hasAuthorityOver($role))
2019-06-10 00:10:59 +02:00
continue;
2020-06-04 18:48:01 +00:00
if(!in_array($role->getId(), $applyRoles))
2019-06-10 00:10:59 +02:00
$removeRoles[] = $role;
}
// STEP 2: Purge the ones marked for removal.
$setRoles = array_diff($setRoles, $removeRoles);
// STEP 3: Add roles to the set array from the user input, if the user has authority over the given roles.
2020-06-04 18:48:01 +00:00
foreach($applyRoles as $roleId) {
try {
$role = $existingRoles[$roleId] ?? UserRole::byId($roleId);
} catch(UserRoleNotFoundException $ex) {
2019-06-10 00:10:59 +02:00
continue;
}
2020-06-04 18:48:01 +00:00
if(!$currentUser->hasAuthorityOver($role))
continue;
if(!in_array($role, $setRoles))
2019-06-10 00:10:59 +02:00
$setRoles[] = $role;
}
2020-06-04 18:48:01 +00:00
foreach($removeRoles as $role)
$userInfo->removeRole($role);
foreach($setRoles as $role)
$userInfo->addRole($role);
2019-06-10 00:10:59 +02:00
}
$setUserInfo = [];
if(!empty($_POST['user']) && is_array($_POST['user'])) {
$setUserInfo['username'] = (string)($_POST['user']['username'] ?? '');
$setUserInfo['email'] = (string)($_POST['user']['email'] ?? '');
$setUserInfo['user_country'] = (string)($_POST['user']['country'] ?? '');
$setUserInfo['user_title'] = (string)($_POST['user']['title'] ?? '');
$displayRole = (int)($_POST['user']['display_role'] ?? 0);
2020-06-04 18:48:01 +00:00
try {
$userInfo->setDisplayRole(UserRole::byId($displayRole));
} catch(UserRoleNotFoundException $ex) {}
2019-06-10 00:10:59 +02:00
$usernameValidation = User::validateUsername($setUserInfo['username']);
$emailValidation = User::validateEMailAddress($setUserInfo['email']);
2019-06-10 00:10:59 +02:00
$countryValidation = strlen($setUserInfo['user_country']) === 2
&& ctype_alpha($setUserInfo['user_country'])
&& ctype_upper($setUserInfo['user_country']);
if(!empty($usernameValidation))
$notices[] = User::usernameValidationErrorString($usernameValidation);
2019-06-10 00:10:59 +02:00
if(!empty($emailValidation)) {
$notices[] = $emailValidation === 'in-use'
? 'This e-mail address has already been used!'
: 'This e-mail address is invalid!';
2020-06-04 18:48:01 +00:00
} else
2019-06-10 00:10:59 +02:00
$setUserInfo['email'] = mb_strtolower($setUserInfo['email']);
2020-06-04 18:48:01 +00:00
if(!$countryValidation)
2019-06-10 00:10:59 +02:00
$notices[] = 'Country code was invalid.';
2020-06-04 18:48:01 +00:00
if(strlen($setUserInfo['user_title']) < 1)
2019-06-10 00:10:59 +02:00
$setUserInfo['user_title'] = null;
2020-06-04 18:48:01 +00:00
elseif(strlen($setUserInfo['user_title']) > 64)
2019-06-10 00:10:59 +02:00
$notices[] = 'User title was invalid.';
}
if(!empty($_POST['colour']) && is_array($_POST['colour'])) {
2019-12-12 01:42:28 +01:00
$setUserInfo['user_colour'] = null;
2019-06-10 00:10:59 +02:00
if(!empty($_POST['colour']['enable'])) {
2019-12-12 01:42:28 +01:00
$userColour = new Colour;
2019-06-10 00:10:59 +02:00
2019-12-12 01:42:28 +01:00
try {
$userColour->setHex((string)($_POST['colour']['hex'] ?? ''));
} catch(\Exception $ex) {
$notices[] = $ex->getMessage();
2019-06-10 00:10:59 +02:00
}
2019-12-12 01:42:28 +01:00
$setUserInfo['user_colour'] = $userColour->getRaw();
}
2019-06-10 00:10:59 +02:00
}
if(!empty($_POST['password']) && is_array($_POST['password'])) {
$passwordNewValue = (string)($_POST['password']['new'] ?? '');
$passwordConfirmValue = (string)($_POST['password']['confirm'] ?? '');
if(!empty($passwordNewValue)) {
2020-06-04 18:48:01 +00:00
if($passwordNewValue !== $passwordConfirmValue)
2019-06-10 00:10:59 +02:00
$notices[] = 'Confirm password does not match.';
2020-06-04 18:48:01 +00:00
elseif(!empty(User::validatePassword($passwordNewValue)))
2019-06-10 00:10:59 +02:00
$notices[] = 'New password is too weak.';
2020-06-04 18:48:01 +00:00
else
$setUserInfo['password'] = User::hashPassword($passwordNewValue);
2019-06-10 00:10:59 +02:00
}
}
if(empty($notices) && !empty($setUserInfo)) {
2019-09-29 00:38:39 +02:00
$userUpdate = DB::prepare(sprintf(
2019-06-10 00:10:59 +02:00
'
UPDATE `msz_users`
SET %s
WHERE `user_id` = :set_user_id
',
pdo_prepare_array_update($setUserInfo, true)
));
2019-09-29 00:38:39 +02:00
$userUpdate->bind('set_user_id', $userId);
2019-06-10 00:10:59 +02:00
2020-06-04 18:48:01 +00:00
foreach($setUserInfo as $key => $value)
2019-09-29 00:38:39 +02:00
$userUpdate->bind($key, $value);
2019-06-10 00:10:59 +02:00
2020-06-04 18:48:01 +00:00
if(!$userUpdate->execute())
2019-06-10 00:10:59 +02:00
$notices[] = 'Something went wrong while updating the user.';
}
2019-06-10 15:43:55 +02:00
if($canEditPerms && !empty($_POST['perms']) && is_array($_POST['perms'])) {
2019-06-10 00:10:59 +02:00
$perms = manage_perms_apply($permissions, $_POST['perms']);
if($perms !== null) {
2020-06-04 18:48:01 +00:00
if(!perms_set_user_raw($userId, $perms))
2019-06-10 00:10:59 +02:00
$notices[] = 'Failed to update permissions.';
} else {
2020-06-04 18:48:01 +00:00
if(!perms_delete_user($userId))
2019-06-10 00:10:59 +02:00
$notices[] = 'Failed to remove permissions.';
}
// this smells, make it refresh/apply in a non-retarded way
$permissions = manage_perms_list(perms_get_user_raw($userId));
}
}
Template::render('manage.users.user', [
2020-06-04 18:48:01 +00:00
'user_info' => $userInfo,
2019-06-10 00:10:59 +02:00
'manage_notices' => $notices,
2020-06-04 18:48:01 +00:00
'manage_roles' => UserRole::all(true),
2019-06-10 00:10:59 +02:00
'can_edit_user' => $canEdit,
'can_edit_perms' => $canEdit && $canEditPerms,
'permissions' => $permissions ?? [],
]);