2021-06-26 11:23:15 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers;
|
2015-08-08 15:05:30 -04:00
|
|
|
|
2020-11-20 13:53:01 -05:00
|
|
|
use BookStack\Actions\ActivityType;
|
2018-09-25 07:30:50 -04:00
|
|
|
use BookStack\Auth\Access\SocialAuthService;
|
2019-08-18 08:11:30 -04:00
|
|
|
use BookStack\Auth\Access\UserInviteService;
|
2018-09-25 07:30:50 -04:00
|
|
|
use BookStack\Auth\User;
|
2018-09-25 11:58:03 -04:00
|
|
|
use BookStack\Auth\UserRepo;
|
2021-03-09 18:06:12 -05:00
|
|
|
use BookStack\Exceptions\ImageUploadException;
|
2018-12-30 11:11:58 -05:00
|
|
|
use BookStack\Exceptions\UserUpdateException;
|
2019-05-04 10:48:15 -04:00
|
|
|
use BookStack\Uploads\ImageRepo;
|
2021-03-09 18:06:12 -05:00
|
|
|
use Exception;
|
2018-09-25 11:58:03 -04:00
|
|
|
use Illuminate\Http\Request;
|
2019-09-13 18:58:40 -04:00
|
|
|
use Illuminate\Support\Str;
|
2021-12-18 11:31:48 -05:00
|
|
|
use Illuminate\Validation\Rules\Password;
|
2021-03-09 18:06:12 -05:00
|
|
|
use Illuminate\Validation\ValidationException;
|
2015-08-08 15:05:30 -04:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
protected $user;
|
2015-09-06 07:14:32 -04:00
|
|
|
protected $userRepo;
|
2019-08-18 08:11:30 -04:00
|
|
|
protected $inviteService;
|
2019-05-04 10:48:15 -04:00
|
|
|
protected $imageRepo;
|
2015-08-08 15:05:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserController constructor.
|
|
|
|
*/
|
2019-08-18 08:11:30 -04:00
|
|
|
public function __construct(User $user, UserRepo $userRepo, UserInviteService $inviteService, ImageRepo $imageRepo)
|
2015-08-08 15:05:30 -04:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
2015-09-06 07:14:32 -04:00
|
|
|
$this->userRepo = $userRepo;
|
2019-08-18 08:11:30 -04:00
|
|
|
$this->inviteService = $inviteService;
|
2019-05-04 10:48:15 -04:00
|
|
|
$this->imageRepo = $imageRepo;
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the users.
|
|
|
|
*/
|
2016-05-22 05:44:31 -04:00
|
|
|
public function index(Request $request)
|
2015-08-08 15:05:30 -04:00
|
|
|
{
|
2016-03-05 07:09:09 -05:00
|
|
|
$this->checkPermission('users-manage');
|
2016-05-22 05:44:31 -04:00
|
|
|
$listDetails = [
|
2021-06-26 11:23:15 -04:00
|
|
|
'order' => $request->get('order', 'asc'),
|
2017-11-19 10:56:06 -05:00
|
|
|
'search' => $request->get('search', ''),
|
2021-06-26 11:23:15 -04:00
|
|
|
'sort' => $request->get('sort', 'name'),
|
2016-05-22 05:44:31 -04:00
|
|
|
];
|
|
|
|
$users = $this->userRepo->getAllUsersPaginatedAndSorted(20, $listDetails);
|
2021-01-10 17:43:22 -05:00
|
|
|
|
2016-12-04 11:51:39 -05:00
|
|
|
$this->setPageTitle(trans('settings.users'));
|
2016-05-22 05:44:31 -04:00
|
|
|
$users->appends($listDetails);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-04-06 11:21:20 -04:00
|
|
|
return view('users.index', ['users' => $users, 'listDetails' => $listDetails]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new user.
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2016-02-27 14:24:42 -05:00
|
|
|
$this->checkPermission('users-manage');
|
2016-01-13 17:22:30 -05:00
|
|
|
$authMethod = config('auth.method');
|
2016-09-29 07:43:46 -04:00
|
|
|
$roles = $this->userRepo->getAllRoles();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-04-06 11:21:20 -04:00
|
|
|
return view('users.create', ['authMethod' => $authMethod, 'roles' => $roles]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created user in storage.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2018-12-30 11:11:58 -05:00
|
|
|
* @throws UserUpdateException
|
2021-03-09 18:06:12 -05:00
|
|
|
* @throws ValidationException
|
2015-08-08 15:05:30 -04:00
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2016-02-27 14:24:42 -05:00
|
|
|
$this->checkPermission('users-manage');
|
2016-01-17 10:20:07 -05:00
|
|
|
$validationRules = [
|
2021-11-04 20:26:55 -04:00
|
|
|
'name' => ['required'],
|
|
|
|
'email' => ['required', 'email', 'unique:users,email'],
|
2016-01-17 10:20:07 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
$authMethod = config('auth.method');
|
2019-08-18 08:11:30 -04:00
|
|
|
$sendInvite = ($request->get('send_invite', 'false') === 'true');
|
|
|
|
|
|
|
|
if ($authMethod === 'standard' && !$sendInvite) {
|
2021-12-18 11:31:48 -05:00
|
|
|
$validationRules['password'] = ['required', Password::default()];
|
2021-11-04 20:26:55 -04:00
|
|
|
$validationRules['password-confirm'] = ['required', 'same:password'];
|
2020-07-01 17:27:50 -04:00
|
|
|
} elseif ($authMethod === 'ldap' || $authMethod === 'saml2' || $authMethod === 'openid') {
|
2021-11-04 20:26:55 -04:00
|
|
|
$validationRules['external_auth_id'] = ['required'];
|
2016-01-17 10:20:07 -05:00
|
|
|
}
|
|
|
|
$this->validate($request, $validationRules);
|
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$user = $this->user->fill($request->all());
|
2015-08-29 10:03:42 -04:00
|
|
|
|
2016-01-17 10:20:07 -05:00
|
|
|
if ($authMethod === 'standard') {
|
2019-09-13 18:58:40 -04:00
|
|
|
$user->password = bcrypt($request->get('password', Str::random(32)));
|
2020-07-01 17:27:50 -04:00
|
|
|
} elseif ($authMethod === 'ldap' || $authMethod === 'saml2' || $authMethod === 'openid') {
|
2016-01-17 10:20:07 -05:00
|
|
|
$user->external_auth_id = $request->get('external_auth_id');
|
|
|
|
}
|
|
|
|
|
2021-03-09 18:06:12 -05:00
|
|
|
$user->refreshSlug();
|
2016-01-17 10:20:07 -05:00
|
|
|
$user->save();
|
2016-02-27 14:24:42 -05:00
|
|
|
|
2019-08-18 08:11:30 -04:00
|
|
|
if ($sendInvite) {
|
|
|
|
$this->inviteService->sendInvitation($user);
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:56:06 -05:00
|
|
|
if ($request->filled('roles')) {
|
2016-02-27 14:24:42 -05:00
|
|
|
$roles = $request->get('roles');
|
2018-12-30 11:11:58 -05:00
|
|
|
$this->userRepo->setUserRoles($user, $roles);
|
2016-02-27 14:24:42 -05:00
|
|
|
}
|
2015-12-14 15:13:32 -05:00
|
|
|
|
2018-12-22 14:29:19 -05:00
|
|
|
$this->userRepo->downloadAndAssignUserAvatar($user);
|
2015-12-14 15:13:32 -05:00
|
|
|
|
2020-11-20 13:53:01 -05:00
|
|
|
$this->logActivity(ActivityType::USER_CREATE, $user);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2016-02-16 16:25:11 -05:00
|
|
|
return redirect('/settings/users');
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified user.
|
|
|
|
*/
|
2019-12-29 08:02:26 -05:00
|
|
|
public function edit(int $id, SocialAuthService $socialAuthService)
|
2015-08-08 15:05:30 -04:00
|
|
|
{
|
2019-04-27 09:18:00 -04:00
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $id);
|
2015-09-04 15:40:36 -04:00
|
|
|
|
2021-07-14 15:18:48 -04:00
|
|
|
/** @var User $user */
|
|
|
|
$user = $this->user->newQuery()->with(['apiTokens', 'mfaValues'])->findOrFail($id);
|
2016-09-29 07:43:46 -04:00
|
|
|
|
|
|
|
$authMethod = ($user->system_name) ? 'system' : config('auth.method');
|
|
|
|
|
2015-09-04 15:40:36 -04:00
|
|
|
$activeSocialDrivers = $socialAuthService->getActiveDrivers();
|
2021-07-14 15:18:48 -04:00
|
|
|
$mfaMethods = $user->mfaValues->groupBy('method');
|
2016-12-04 11:51:39 -05:00
|
|
|
$this->setPageTitle(trans('settings.user_profile'));
|
2016-09-29 07:43:46 -04:00
|
|
|
$roles = $this->userRepo->getAllRoles();
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-12-29 08:02:26 -05:00
|
|
|
return view('users.edit', [
|
2021-06-26 11:23:15 -04:00
|
|
|
'user' => $user,
|
2019-12-29 08:02:26 -05:00
|
|
|
'activeSocialDrivers' => $activeSocialDrivers,
|
2021-07-14 15:06:41 -04:00
|
|
|
'mfaMethods' => $mfaMethods,
|
2021-06-26 11:23:15 -04:00
|
|
|
'authMethod' => $authMethod,
|
|
|
|
'roles' => $roles,
|
2019-12-29 08:02:26 -05:00
|
|
|
]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified user in storage.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2018-12-30 11:11:58 -05:00
|
|
|
* @throws UserUpdateException
|
2021-03-09 18:06:12 -05:00
|
|
|
* @throws ImageUploadException
|
|
|
|
* @throws ValidationException
|
2015-08-08 15:05:30 -04:00
|
|
|
*/
|
2020-04-10 07:49:16 -04:00
|
|
|
public function update(Request $request, int $id)
|
2015-08-08 15:05:30 -04:00
|
|
|
{
|
2019-09-19 10:12:10 -04:00
|
|
|
$this->preventAccessInDemoMode();
|
2019-04-27 09:18:00 -04:00
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $id);
|
2015-12-31 12:57:34 -05:00
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$this->validate($request, [
|
2021-12-18 11:31:48 -05:00
|
|
|
'name' => ['min:2'],
|
2021-11-04 20:26:55 -04:00
|
|
|
'email' => ['min:2', 'email', 'unique:users,email,' . $id],
|
2021-12-18 11:31:48 -05:00
|
|
|
'password' => ['required_with:password_confirm', Password::default()],
|
2021-11-04 20:26:55 -04:00
|
|
|
'password-confirm' => ['same:password', 'required_with:password'],
|
2021-12-18 11:31:48 -05:00
|
|
|
'setting' => ['array'],
|
2021-11-04 20:26:55 -04:00
|
|
|
'profile_image' => array_merge(['nullable'], $this->getImageValidationRules()),
|
2015-08-08 15:05:30 -04:00
|
|
|
]);
|
|
|
|
|
2018-12-30 11:11:58 -05:00
|
|
|
$user = $this->userRepo->getById($id);
|
2019-08-06 16:29:42 -04:00
|
|
|
$user->fill($request->except(['email']));
|
|
|
|
|
|
|
|
// Email updates
|
|
|
|
if (userCan('users-manage') && $request->filled('email')) {
|
|
|
|
$user->email = $request->get('email');
|
|
|
|
}
|
2015-08-08 15:05:30 -04:00
|
|
|
|
2021-03-09 18:06:12 -05:00
|
|
|
// Refresh the slug if the user's name has changed
|
|
|
|
if ($user->isDirty('name')) {
|
|
|
|
$user->refreshSlug();
|
|
|
|
}
|
|
|
|
|
2016-01-13 17:22:30 -05:00
|
|
|
// Role updates
|
2017-11-19 10:56:06 -05:00
|
|
|
if (userCan('users-manage') && $request->filled('roles')) {
|
2016-02-27 14:24:42 -05:00
|
|
|
$roles = $request->get('roles');
|
2018-12-30 11:11:58 -05:00
|
|
|
$this->userRepo->setUserRoles($user, $roles);
|
2015-08-29 10:03:42 -04:00
|
|
|
}
|
|
|
|
|
2016-01-13 17:22:30 -05:00
|
|
|
// Password updates
|
2017-11-19 10:56:06 -05:00
|
|
|
if ($request->filled('password')) {
|
2015-08-08 15:05:30 -04:00
|
|
|
$password = $request->get('password');
|
2015-09-05 07:29:47 -04:00
|
|
|
$user->password = bcrypt($password);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
2016-01-02 09:48:35 -05:00
|
|
|
|
2016-01-13 17:22:30 -05:00
|
|
|
// External auth id updates
|
2019-09-19 19:18:28 -04:00
|
|
|
if (user()->can('users-manage') && $request->filled('external_auth_id')) {
|
2016-01-13 17:22:30 -05:00
|
|
|
$user->external_auth_id = $request->get('external_auth_id');
|
|
|
|
}
|
|
|
|
|
2017-01-15 11:27:24 -05:00
|
|
|
// Save an user-specific settings
|
2017-11-19 10:56:06 -05:00
|
|
|
if ($request->filled('setting')) {
|
2017-01-15 11:27:24 -05:00
|
|
|
foreach ($request->get('setting') as $key => $value) {
|
|
|
|
setting()->putUser($user, $key, $value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 10:48:15 -04:00
|
|
|
// Save profile image if in request
|
2020-03-03 18:08:01 -05:00
|
|
|
if ($request->hasFile('profile_image')) {
|
2019-05-04 10:48:15 -04:00
|
|
|
$imageUpload = $request->file('profile_image');
|
|
|
|
$this->imageRepo->destroyImage($user->avatar);
|
|
|
|
$image = $this->imageRepo->saveNew($imageUpload, 'user', $user->id);
|
|
|
|
$user->image_id = $image->id;
|
|
|
|
}
|
|
|
|
|
2020-12-08 18:46:38 -05:00
|
|
|
// Delete the profile image if reset option is in request
|
2019-05-04 10:48:15 -04:00
|
|
|
if ($request->has('profile_image_reset')) {
|
|
|
|
$this->imageRepo->destroyImage($user->avatar);
|
|
|
|
}
|
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$user->save();
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->showSuccessNotification(trans('settings.users_edit_success'));
|
2020-11-20 13:53:01 -05:00
|
|
|
$this->logActivity(ActivityType::USER_UPDATE, $user);
|
2016-03-13 11:37:46 -04:00
|
|
|
|
2019-05-04 10:48:15 -04:00
|
|
|
$redirectUrl = userCan('users-manage') ? '/settings/users' : ('/settings/users/' . $user->id);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2016-03-13 11:37:46 -04:00
|
|
|
return redirect($redirectUrl);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the user delete page.
|
|
|
|
*/
|
2020-04-10 07:49:16 -04:00
|
|
|
public function delete(int $id)
|
2015-08-08 15:05:30 -04:00
|
|
|
{
|
2019-04-27 09:18:00 -04:00
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $id);
|
2015-12-31 12:57:34 -05:00
|
|
|
|
2018-12-30 11:11:58 -05:00
|
|
|
$user = $this->userRepo->getById($id);
|
2016-12-04 11:51:39 -05:00
|
|
|
$this->setPageTitle(trans('settings.users_delete_named', ['userName' => $user->name]));
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-04-06 11:21:20 -04:00
|
|
|
return view('users.delete', ['user' => $user]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified user from storage.
|
2021-06-26 11:23:15 -04:00
|
|
|
*
|
2021-03-09 18:06:12 -05:00
|
|
|
* @throws Exception
|
2015-08-08 15:05:30 -04:00
|
|
|
*/
|
2021-01-01 13:31:01 -05:00
|
|
|
public function destroy(Request $request, int $id)
|
2015-08-08 15:05:30 -04:00
|
|
|
{
|
2019-09-19 10:12:10 -04:00
|
|
|
$this->preventAccessInDemoMode();
|
2019-04-27 09:18:00 -04:00
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $id);
|
2015-12-14 15:13:32 -05:00
|
|
|
|
2015-12-15 14:27:36 -05:00
|
|
|
$user = $this->userRepo->getById($id);
|
2021-01-01 13:31:01 -05:00
|
|
|
$newOwnerId = $request->get('new_owner_id', null);
|
2016-05-22 04:23:41 -04:00
|
|
|
|
2015-12-14 15:13:32 -05:00
|
|
|
if ($this->userRepo->isOnlyAdmin($user)) {
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->showErrorNotification(trans('errors.users_cannot_delete_only_admin'));
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2015-09-06 07:14:32 -04:00
|
|
|
return redirect($user->getEditUrl());
|
|
|
|
}
|
2016-05-22 04:23:41 -04:00
|
|
|
|
2016-09-29 12:07:58 -04:00
|
|
|
if ($user->system_name === 'public') {
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->showErrorNotification(trans('errors.users_cannot_delete_guest'));
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2016-09-29 12:07:58 -04:00
|
|
|
return redirect($user->getEditUrl());
|
|
|
|
}
|
|
|
|
|
2021-01-01 13:31:01 -05:00
|
|
|
$this->userRepo->destroy($user, $newOwnerId);
|
2019-10-05 07:55:01 -04:00
|
|
|
$this->showSuccessNotification(trans('settings.users_delete_success'));
|
2020-11-20 13:53:01 -05:00
|
|
|
$this->logActivity(ActivityType::USER_DELETE, $user);
|
2015-12-14 15:13:32 -05:00
|
|
|
|
2016-02-16 16:25:11 -05:00
|
|
|
return redirect('/settings/users');
|
|
|
|
}
|
|
|
|
|
2017-12-29 11:49:03 -05:00
|
|
|
/**
|
|
|
|
* Update the user's preferred book-list display setting.
|
|
|
|
*/
|
2020-04-10 07:49:16 -04:00
|
|
|
public function switchBooksView(Request $request, int $id)
|
2018-01-28 11:58:52 -05:00
|
|
|
{
|
2018-12-07 13:33:32 -05:00
|
|
|
return $this->switchViewType($id, $request, 'books');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the user's preferred shelf-list display setting.
|
|
|
|
*/
|
2020-04-10 07:49:16 -04:00
|
|
|
public function switchShelvesView(Request $request, int $id)
|
2018-12-07 13:33:32 -05:00
|
|
|
{
|
|
|
|
return $this->switchViewType($id, $request, 'bookshelves');
|
|
|
|
}
|
|
|
|
|
2020-04-10 07:49:16 -04:00
|
|
|
/**
|
|
|
|
* Update the user's preferred shelf-view book list display setting.
|
|
|
|
*/
|
|
|
|
public function switchShelfView(Request $request, int $id)
|
|
|
|
{
|
|
|
|
return $this->switchViewType($id, $request, 'bookshelf');
|
|
|
|
}
|
|
|
|
|
2018-12-07 13:33:32 -05:00
|
|
|
/**
|
|
|
|
* For a type of list, switch with stored view type for a user.
|
|
|
|
*/
|
2020-04-10 07:49:16 -04:00
|
|
|
protected function switchViewType(int $userId, Request $request, string $listName)
|
2018-12-07 13:33:32 -05:00
|
|
|
{
|
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $userId);
|
2017-12-26 02:08:16 -05:00
|
|
|
|
2018-08-27 09:18:09 -04:00
|
|
|
$viewType = $request->get('view_type');
|
2017-12-26 02:08:16 -05:00
|
|
|
if (!in_array($viewType, ['grid', 'list'])) {
|
|
|
|
$viewType = 'list';
|
|
|
|
}
|
|
|
|
|
2019-01-13 10:54:55 -05:00
|
|
|
$user = $this->userRepo->getById($userId);
|
2018-12-07 13:33:32 -05:00
|
|
|
$key = $listName . '_view_type';
|
|
|
|
setting()->putUser($user, $key, $viewType);
|
2017-12-26 02:08:16 -05:00
|
|
|
|
2018-12-07 13:33:32 -05:00
|
|
|
return redirect()->back(302, [], "/settings/users/$userId");
|
2017-12-26 02:08:16 -05:00
|
|
|
}
|
2018-08-27 09:18:09 -04:00
|
|
|
|
|
|
|
/**
|
2019-02-03 08:45:45 -05:00
|
|
|
* Change the stored sort type for a particular view.
|
2018-08-27 09:18:09 -04:00
|
|
|
*/
|
2019-09-15 13:53:30 -04:00
|
|
|
public function changeSort(Request $request, string $id, string $type)
|
2018-08-27 09:18:09 -04:00
|
|
|
{
|
2021-01-30 22:25:31 -05:00
|
|
|
$validSortTypes = ['books', 'bookshelves', 'shelf_books'];
|
2019-02-03 08:45:45 -05:00
|
|
|
if (!in_array($type, $validSortTypes)) {
|
|
|
|
return redirect()->back(500);
|
|
|
|
}
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2019-02-03 08:45:45 -05:00
|
|
|
return $this->changeListSort($id, $request, $type);
|
2018-12-07 13:33:32 -05:00
|
|
|
}
|
2018-08-27 09:18:09 -04:00
|
|
|
|
2020-04-11 15:37:51 -04:00
|
|
|
/**
|
|
|
|
* Toggle dark mode for the current user.
|
|
|
|
*/
|
|
|
|
public function toggleDarkMode()
|
|
|
|
{
|
|
|
|
$enabled = setting()->getForCurrentUser('dark-mode-enabled', false);
|
|
|
|
setting()->putUser(user(), 'dark-mode-enabled', $enabled ? 'false' : 'true');
|
2021-06-26 11:23:15 -04:00
|
|
|
|
2020-04-11 15:37:51 -04:00
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
2019-04-14 08:01:51 -04:00
|
|
|
/**
|
|
|
|
* Update the stored section expansion preference for the given user.
|
|
|
|
*/
|
2019-09-15 13:53:30 -04:00
|
|
|
public function updateExpansionPreference(Request $request, string $id, string $key)
|
2019-04-14 08:01:51 -04:00
|
|
|
{
|
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $id);
|
|
|
|
$keyWhitelist = ['home-details'];
|
|
|
|
if (!in_array($key, $keyWhitelist)) {
|
2021-06-26 11:23:15 -04:00
|
|
|
return response('Invalid key', 500);
|
2019-04-14 08:01:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$newState = $request->get('expand', 'false');
|
|
|
|
|
|
|
|
$user = $this->user->findOrFail($id);
|
|
|
|
setting()->putUser($user, 'section_expansion#' . $key, $newState);
|
2021-06-26 11:23:15 -04:00
|
|
|
|
|
|
|
return response('', 204);
|
2019-04-14 08:01:51 -04:00
|
|
|
}
|
|
|
|
|
2018-12-07 13:33:32 -05:00
|
|
|
/**
|
|
|
|
* Changed the stored preference for a list sort order.
|
|
|
|
*/
|
|
|
|
protected function changeListSort(int $userId, Request $request, string $listName)
|
|
|
|
{
|
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $userId);
|
|
|
|
|
|
|
|
$sort = $request->get('sort');
|
2021-03-21 19:06:15 -04:00
|
|
|
if (!in_array($sort, ['name', 'created_at', 'updated_at', 'default'])) {
|
2018-12-07 13:33:32 -05:00
|
|
|
$sort = 'name';
|
2018-08-27 09:18:09 -04:00
|
|
|
}
|
|
|
|
|
2018-12-07 13:33:32 -05:00
|
|
|
$order = $request->get('order');
|
|
|
|
if (!in_array($order, ['asc', 'desc'])) {
|
|
|
|
$order = 'asc';
|
|
|
|
}
|
2018-08-27 09:18:09 -04:00
|
|
|
|
2018-12-07 13:33:32 -05:00
|
|
|
$user = $this->user->findOrFail($userId);
|
|
|
|
$sortKey = $listName . '_sort';
|
|
|
|
$orderKey = $listName . '_sort_order';
|
|
|
|
setting()->putUser($user, $sortKey, $sort);
|
|
|
|
setting()->putUser($user, $orderKey, $order);
|
|
|
|
|
|
|
|
return redirect()->back(302, [], "/settings/users/$userId");
|
2018-08-27 09:18:09 -04:00
|
|
|
}
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|