2017-01-15 11:27:24 -05:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
2015-08-08 15:05:30 -04:00
|
|
|
|
2016-09-17 16:33:55 -04:00
|
|
|
use Exception;
|
2015-08-08 15:05:30 -04:00
|
|
|
use Illuminate\Http\Request;
|
2015-12-14 15:13:32 -05:00
|
|
|
use Illuminate\Http\Response;
|
2015-09-10 14:31:09 -04:00
|
|
|
use BookStack\Repos\UserRepo;
|
|
|
|
use BookStack\Services\SocialAuthService;
|
|
|
|
use BookStack\User;
|
2015-08-08 15:05:30 -04:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $user;
|
2015-09-06 07:14:32 -04:00
|
|
|
protected $userRepo;
|
2015-08-08 15:05:30 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserController constructor.
|
2015-12-08 17:04:59 -05:00
|
|
|
* @param User $user
|
|
|
|
* @param UserRepo $userRepo
|
2015-08-08 15:05:30 -04:00
|
|
|
*/
|
2015-09-06 07:14:32 -04:00
|
|
|
public function __construct(User $user, UserRepo $userRepo)
|
2015-08-08 15:05:30 -04:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
2015-09-06 07:14:32 -04:00
|
|
|
$this->userRepo = $userRepo;
|
2015-08-29 10:03:42 -04:00
|
|
|
parent::__construct();
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display a listing of the users.
|
2016-05-22 05:44:31 -04:00
|
|
|
* @param Request $request
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return Response
|
|
|
|
*/
|
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 = [
|
2017-11-19 10:56:06 -05:00
|
|
|
'order' => $request->get('order', 'asc'),
|
|
|
|
'search' => $request->get('search', ''),
|
|
|
|
'sort' => $request->get('sort', 'name'),
|
2016-05-22 05:44:31 -04:00
|
|
|
];
|
|
|
|
$users = $this->userRepo->getAllUsersPaginatedAndSorted(20, $listDetails);
|
2016-12-04 11:51:39 -05:00
|
|
|
$this->setPageTitle(trans('settings.users'));
|
2016-05-22 05:44:31 -04:00
|
|
|
$users->appends($listDetails);
|
|
|
|
return view('users/index', ['users' => $users, 'listDetails' => $listDetails]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new user.
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
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();
|
2016-05-01 14:36:53 -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.
|
2015-08-29 10:03:42 -04:00
|
|
|
* @param Request $request
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
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 = [
|
2015-08-29 10:03:42 -04:00
|
|
|
'name' => 'required',
|
2016-02-27 14:24:42 -05:00
|
|
|
'email' => 'required|email|unique:users,email'
|
2016-01-17 10:20:07 -05:00
|
|
|
];
|
|
|
|
|
|
|
|
$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 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') {
|
|
|
|
$user->password = bcrypt($request->get('password'));
|
|
|
|
} elseif ($authMethod === 'ldap') {
|
|
|
|
$user->external_auth_id = $request->get('external_auth_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
$user->save();
|
2016-02-27 14:24:42 -05:00
|
|
|
|
2017-11-19 10:56:06 -05:00
|
|
|
if ($request->filled('roles')) {
|
2016-02-27 14:24:42 -05:00
|
|
|
$roles = $request->get('roles');
|
|
|
|
$user->roles()->sync($roles);
|
|
|
|
}
|
2015-12-14 15:13:32 -05:00
|
|
|
|
2018-01-28 13:09:26 -05:00
|
|
|
$this->userRepo->downloadGravatarToUserAvatar($user);
|
2015-12-14 15:13:32 -05: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.
|
2015-09-04 15:40:36 -04:00
|
|
|
* @param int $id
|
|
|
|
* @param SocialAuthService $socialAuthService
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return Response
|
|
|
|
*/
|
2015-09-04 15:40:36 -04:00
|
|
|
public function edit($id, SocialAuthService $socialAuthService)
|
2015-08-08 15:05:30 -04:00
|
|
|
{
|
2016-02-27 14:24:42 -05:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($id) {
|
2015-08-29 10:03:42 -04:00
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-09-04 15:40:36 -04:00
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$user = $this->user->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();
|
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();
|
2016-05-01 14:36:53 -04:00
|
|
|
return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers, 'authMethod' => $authMethod, 'roles' => $roles]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified user in storage.
|
2015-08-29 10:03:42 -04:00
|
|
|
* @param Request $request
|
|
|
|
* @param int $id
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
2015-12-31 12:57:34 -05:00
|
|
|
$this->preventAccessForDemoUsers();
|
2016-02-27 14:24:42 -05:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($id) {
|
2015-08-29 10:03:42 -04:00
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-12-31 12:57:34 -05:00
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$this->validate($request, [
|
2016-02-08 15:35:23 -05:00
|
|
|
'name' => 'min:2',
|
|
|
|
'email' => 'min:2|email|unique:users,email,' . $id,
|
2016-01-02 09:48:35 -05:00
|
|
|
'password' => 'min:5|required_with:password_confirm',
|
2017-01-15 11:27:24 -05:00
|
|
|
'password-confirm' => 'same:password|required_with:password',
|
|
|
|
'setting' => 'array'
|
2015-08-08 15:05:30 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $this->user->findOrFail($id);
|
2016-01-13 17:22:30 -05:00
|
|
|
$user->fill($request->all());
|
2015-08-08 15:05:30 -04:00
|
|
|
|
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');
|
|
|
|
$user->roles()->sync($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
|
2017-11-19 10:56:06 -05:00
|
|
|
if ($this->currentUser->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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$user->save();
|
2016-12-04 11:51:39 -05:00
|
|
|
session()->flash('success', trans('settings.users_edit_success'));
|
2016-03-13 11:37:46 -04:00
|
|
|
|
|
|
|
$redirectUrl = userCan('users-manage') ? '/settings/users' : '/settings/users/' . $user->id;
|
|
|
|
return redirect($redirectUrl);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the user delete page.
|
2016-09-29 12:07:58 -04:00
|
|
|
* @param int $id
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
2016-02-27 14:24:42 -05:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($id) {
|
2015-08-29 10:03:42 -04:00
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-12-31 12:57:34 -05:00
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$user = $this->user->findOrFail($id);
|
2016-12-04 11:51:39 -05:00
|
|
|
$this->setPageTitle(trans('settings.users_delete_named', ['userName' => $user->name]));
|
2015-08-08 15:05:30 -04:00
|
|
|
return view('users/delete', ['user' => $user]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified user from storage.
|
2015-08-29 10:03:42 -04:00
|
|
|
* @param int $id
|
2015-08-08 15:05:30 -04:00
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2015-12-31 12:57:34 -05:00
|
|
|
$this->preventAccessForDemoUsers();
|
2016-02-27 14:24:42 -05:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($id) {
|
2015-08-29 10:03:42 -04:00
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-12-14 15:13:32 -05:00
|
|
|
|
2015-12-15 14:27:36 -05:00
|
|
|
$user = $this->userRepo->getById($id);
|
2016-05-22 04:23:41 -04:00
|
|
|
|
2015-12-14 15:13:32 -05:00
|
|
|
if ($this->userRepo->isOnlyAdmin($user)) {
|
2016-12-04 11:51:39 -05:00
|
|
|
session()->flash('error', trans('errors.users_cannot_delete_only_admin'));
|
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') {
|
2016-12-04 11:51:39 -05:00
|
|
|
session()->flash('error', trans('errors.users_cannot_delete_guest'));
|
2016-09-29 12:07:58 -04:00
|
|
|
return redirect($user->getEditUrl());
|
|
|
|
}
|
|
|
|
|
2015-12-15 14:27:36 -05:00
|
|
|
$this->userRepo->destroy($user);
|
2016-12-04 11:51:39 -05:00
|
|
|
session()->flash('success', trans('settings.users_delete_success'));
|
2015-12-14 15:13:32 -05:00
|
|
|
|
2016-02-16 16:25:11 -05: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);
|
|
|
|
$userActivity = $this->userRepo->getActivity($user);
|
2016-02-17 17:11:48 -05:00
|
|
|
$recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5, 0);
|
|
|
|
$assetCounts = $this->userRepo->getAssetCounts($user);
|
|
|
|
return view('users/profile', [
|
|
|
|
'user' => $user,
|
|
|
|
'activity' => $userActivity,
|
|
|
|
'recentlyCreated' => $recentlyCreated,
|
|
|
|
'assetCounts' => $assetCounts
|
|
|
|
]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
2017-12-26 02:08:16 -05:00
|
|
|
|
2017-12-29 11:49:03 -05:00
|
|
|
/**
|
|
|
|
* Update the user's preferred book-list display setting.
|
|
|
|
* @param $id
|
|
|
|
* @param Request $request
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2018-01-28 11:58:52 -05:00
|
|
|
public function switchBookView($id, Request $request)
|
|
|
|
{
|
2017-12-26 05:16:20 -05:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($id) {
|
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2017-12-26 02:08:16 -05:00
|
|
|
|
2017-12-29 11:49:03 -05:00
|
|
|
$viewType = $request->get('book_view_type');
|
2017-12-26 02:08:16 -05:00
|
|
|
if (!in_array($viewType, ['grid', 'list'])) {
|
|
|
|
$viewType = 'list';
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $this->user->findOrFail($id);
|
|
|
|
setting()->putUser($user, 'books_view_type', $viewType);
|
|
|
|
|
2017-12-29 11:49:03 -05:00
|
|
|
return redirect()->back(302, [], "/settings/users/$id");
|
2017-12-26 02:08:16 -05:00
|
|
|
}
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|