BookStack/app/Http/Controllers/UserController.php

354 lines
11 KiB
PHP
Raw Normal View History

<?php namespace BookStack\Http\Controllers;
2015-08-08 21:05:30 +02:00
use BookStack\Auth\Access\SocialAuthService;
use BookStack\Auth\User;
use BookStack\Auth\UserRepo;
use BookStack\Exceptions\UserUpdateException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2015-08-08 21:05:30 +02:00
class UserController extends Controller
{
protected $user;
protected $userRepo;
2015-08-08 21:05:30 +02:00
/**
* UserController constructor.
* @param User $user
* @param UserRepo $userRepo
2015-08-08 21:05:30 +02:00
*/
public function __construct(User $user, UserRepo $userRepo)
2015-08-08 21:05:30 +02:00
{
$this->user = $user;
$this->userRepo = $userRepo;
2015-08-29 16:03:42 +02:00
parent::__construct();
2015-08-08 21:05:30 +02:00
}
/**
* Display a listing of the users.
* @param Request $request
2015-08-08 21:05:30 +02:00
* @return Response
*/
public function index(Request $request)
2015-08-08 21:05:30 +02:00
{
$this->checkPermission('users-manage');
$listDetails = [
2017-11-19 16:56:06 +01:00
'order' => $request->get('order', 'asc'),
'search' => $request->get('search', ''),
'sort' => $request->get('sort', 'name'),
];
$users = $this->userRepo->getAllUsersPaginatedAndSorted(20, $listDetails);
2016-12-04 17:51:39 +01:00
$this->setPageTitle(trans('settings.users'));
$users->appends($listDetails);
return view('users.index', ['users' => $users, 'listDetails' => $listDetails]);
2015-08-08 21:05:30 +02:00
}
/**
* Show the form for creating a new user.
* @return Response
*/
public function create()
{
$this->checkPermission('users-manage');
$authMethod = config('auth.method');
$roles = $this->userRepo->getAllRoles();
return view('users.create', ['authMethod' => $authMethod, 'roles' => $roles]);
2015-08-08 21:05:30 +02:00
}
/**
* Store a newly created user in storage.
2015-08-29 16:03:42 +02:00
* @param Request $request
2015-08-08 21:05:30 +02:00
* @return Response
* @throws UserUpdateException
2015-08-08 21:05:30 +02:00
*/
public function store(Request $request)
{
$this->checkPermission('users-manage');
$validationRules = [
2015-08-29 16:03:42 +02:00
'name' => 'required',
'email' => 'required|email|unique:users,email'
];
$authMethod = config('auth.method');
if ($authMethod === 'standard') {
$validationRules['password'] = 'required|min:5';
$validationRules['password-confirm'] = 'required|same:password';
} elseif ($authMethod === 'ldap') {
$validationRules['external_auth_id'] = 'required';
}
$this->validate($request, $validationRules);
2015-08-08 21:05:30 +02:00
$user = $this->user->fill($request->all());
2015-08-29 16:03:42 +02:00
if ($authMethod === 'standard') {
$user->password = bcrypt($request->get('password'));
} elseif ($authMethod === 'ldap') {
$user->external_auth_id = $request->get('external_auth_id');
}
$user->save();
2017-11-19 16:56:06 +01:00
if ($request->filled('roles')) {
$roles = $request->get('roles');
$this->userRepo->setUserRoles($user, $roles);
}
2015-12-14 21:13:32 +01:00
$this->userRepo->downloadAndAssignUserAvatar($user);
2015-12-14 21:13:32 +01:00
2016-02-16 22:25:11 +01:00
return redirect('/settings/users');
2015-08-08 21:05:30 +02:00
}
/**
* Show the form for editing the specified user.
2015-09-04 21:40:36 +02:00
* @param int $id
* @param \BookStack\Auth\Access\SocialAuthService $socialAuthService
2015-08-08 21:05:30 +02:00
* @return Response
*/
2015-09-04 21:40:36 +02:00
public function edit($id, SocialAuthService $socialAuthService)
2015-08-08 21:05:30 +02:00
{
$this->checkPermissionOrCurrentUser('users-manage', $id);
2015-09-04 21:40:36 +02:00
2015-08-08 21:05:30 +02:00
$user = $this->user->findOrFail($id);
$authMethod = ($user->system_name) ? 'system' : config('auth.method');
2015-09-04 21:40:36 +02:00
$activeSocialDrivers = $socialAuthService->getActiveDrivers();
2016-12-04 17:51:39 +01:00
$this->setPageTitle(trans('settings.user_profile'));
$roles = $this->userRepo->getAllRoles();
return view('users.edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers, 'authMethod' => $authMethod, 'roles' => $roles]);
2015-08-08 21:05:30 +02:00
}
/**
* Update the specified user in storage.
2015-08-29 16:03:42 +02:00
* @param Request $request
* @param int $id
2015-08-08 21:05:30 +02:00
* @return Response
* @throws UserUpdateException
2015-08-08 21:05:30 +02:00
*/
public function update(Request $request, $id)
{
$this->preventAccessForDemoUsers();
$this->checkPermissionOrCurrentUser('users-manage', $id);
2015-08-08 21:05:30 +02:00
$this->validate($request, [
'name' => 'min:2',
'email' => 'min:2|email|unique:users,email,' . $id,
'password' => 'min:5|required_with:password_confirm',
'password-confirm' => 'same:password|required_with:password',
'setting' => 'array'
2015-08-08 21:05:30 +02:00
]);
$user = $this->userRepo->getById($id);
$user->fill($request->all());
2015-08-08 21:05:30 +02:00
// Role updates
2017-11-19 16:56:06 +01:00
if (userCan('users-manage') && $request->filled('roles')) {
$roles = $request->get('roles');
$this->userRepo->setUserRoles($user, $roles);
2015-08-29 16:03:42 +02:00
}
// Password updates
2017-11-19 16:56:06 +01:00
if ($request->filled('password')) {
2015-08-08 21:05:30 +02:00
$password = $request->get('password');
$user->password = bcrypt($password);
2015-08-08 21:05:30 +02:00
}
// External auth id updates
2017-11-19 16:56:06 +01:00
if ($this->currentUser->can('users-manage') && $request->filled('external_auth_id')) {
$user->external_auth_id = $request->get('external_auth_id');
}
// Save an user-specific settings
2017-11-19 16:56:06 +01:00
if ($request->filled('setting')) {
foreach ($request->get('setting') as $key => $value) {
setting()->putUser($user, $key, $value);
}
}
2015-08-08 21:05:30 +02:00
$user->save();
2016-12-04 17:51:39 +01:00
session()->flash('success', trans('settings.users_edit_success'));
$redirectUrl = userCan('users-manage') ? '/settings/users' : '/settings/users/' . $user->id;
return redirect($redirectUrl);
2015-08-08 21:05:30 +02:00
}
/**
* Show the user delete page.
* @param int $id
2015-08-08 21:05:30 +02:00
* @return \Illuminate\View\View
*/
public function delete($id)
{
$this->checkPermissionOrCurrentUser('users-manage', $id);
$user = $this->userRepo->getById($id);
2016-12-04 17:51:39 +01:00
$this->setPageTitle(trans('settings.users_delete_named', ['userName' => $user->name]));
return view('users.delete', ['user' => $user]);
2015-08-08 21:05:30 +02:00
}
/**
* Remove the specified user from storage.
2015-08-29 16:03:42 +02:00
* @param int $id
2015-08-08 21:05:30 +02:00
* @return Response
* @throws \Exception
2015-08-08 21:05:30 +02:00
*/
public function destroy($id)
{
$this->preventAccessForDemoUsers();
$this->checkPermissionOrCurrentUser('users-manage', $id);
2015-12-14 21:13:32 +01:00
$user = $this->userRepo->getById($id);
2015-12-14 21:13:32 +01:00
if ($this->userRepo->isOnlyAdmin($user)) {
2016-12-04 17:51:39 +01:00
session()->flash('error', trans('errors.users_cannot_delete_only_admin'));
return redirect($user->getEditUrl());
}
if ($user->system_name === 'public') {
2016-12-04 17:51:39 +01:00
session()->flash('error', trans('errors.users_cannot_delete_guest'));
return redirect($user->getEditUrl());
}
$this->userRepo->destroy($user);
2016-12-04 17:51:39 +01:00
session()->flash('success', trans('settings.users_delete_success'));
2015-12-14 21:13:32 +01:00
2016-02-16 22:25:11 +01:00
return redirect('/settings/users');
}
/**
* Show the user profile page
* @param $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showProfilePage($id)
{
$user = $this->userRepo->getById($id);
2016-02-16 22:25:11 +01:00
$userActivity = $this->userRepo->getActivity($user);
2016-02-17 23:11:48 +01:00
$recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5, 0);
$assetCounts = $this->userRepo->getAssetCounts($user);
return view('users.profile', [
2016-02-17 23:11:48 +01:00
'user' => $user,
'activity' => $userActivity,
'recentlyCreated' => $recentlyCreated,
'assetCounts' => $assetCounts
]);
2015-08-08 21:05:30 +02:00
}
/**
* Update the user's preferred book-list display setting.
* @param $id
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function switchBookView($id, Request $request)
{
return $this->switchViewType($id, $request, 'books');
}
/**
* Update the user's preferred shelf-list display setting.
* @param $id
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function switchShelfView($id, Request $request)
{
return $this->switchViewType($id, $request, 'bookshelves');
}
/**
* For a type of list, switch with stored view type for a user.
* @param integer $userId
* @param Request $request
* @param string $listName
* @return \Illuminate\Http\RedirectResponse
*/
protected function switchViewType($userId, Request $request, string $listName)
{
$this->checkPermissionOrCurrentUser('users-manage', $userId);
$viewType = $request->get('view_type');
if (!in_array($viewType, ['grid', 'list'])) {
$viewType = 'list';
}
$user = $this->userRepo->getById($userId);
$key = $listName . '_view_type';
setting()->putUser($user, $key, $viewType);
return redirect()->back(302, [], "/settings/users/$userId");
}
/**
* Change the stored sort type for a particular view.
* @param string $id
* @param string $type
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function changeSort(string $id, string $type, Request $request)
{
$validSortTypes = ['books', 'bookshelves'];
if (!in_array($type, $validSortTypes)) {
return redirect()->back(500);
}
return $this->changeListSort($id, $request, $type);
}
/**
* Update the stored section expansion preference for the given user.
* @param string $id
* @param string $key
* @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
*/
public function updateExpansionPreference(string $id, string $key, Request $request)
{
$this->checkPermissionOrCurrentUser('users-manage', $id);
$keyWhitelist = ['home-details'];
if (!in_array($key, $keyWhitelist)) {
return response("Invalid key", 500);
}
$newState = $request->get('expand', 'false');
$user = $this->user->findOrFail($id);
setting()->putUser($user, 'section_expansion#' . $key, $newState);
return response("", 204);
}
/**
* Changed the stored preference for a list sort order.
* @param int $userId
* @param Request $request
* @param string $listName
* @return \Illuminate\Http\RedirectResponse
*/
protected function changeListSort(int $userId, Request $request, string $listName)
{
$this->checkPermissionOrCurrentUser('users-manage', $userId);
$sort = $request->get('sort');
if (!in_array($sort, ['name', 'created_at', 'updated_at'])) {
$sort = 'name';
}
$order = $request->get('order');
if (!in_array($order, ['asc', 'desc'])) {
$order = 'asc';
}
$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");
}
2015-08-08 21:05:30 +02:00
}