2015-08-08 21:05:30 +02:00
|
|
|
<?php
|
|
|
|
|
2015-09-10 20:31:09 +02:00
|
|
|
namespace BookStack\Http\Controllers;
|
2015-08-08 21:05:30 +02:00
|
|
|
|
2016-02-16 22:25:11 +01:00
|
|
|
use BookStack\Activity;
|
2015-08-08 21:05:30 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
2015-12-14 21:13:32 +01:00
|
|
|
use Illuminate\Http\Response;
|
2015-09-10 20:31:09 +02:00
|
|
|
use BookStack\Http\Requests;
|
|
|
|
use BookStack\Repos\UserRepo;
|
|
|
|
use BookStack\Services\SocialAuthService;
|
|
|
|
use BookStack\User;
|
2015-08-08 21:05:30 +02:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
protected $user;
|
2015-09-06 13:14:32 +02:00
|
|
|
protected $userRepo;
|
2015-08-08 21:05:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UserController constructor.
|
2015-12-08 23:04:59 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param UserRepo $userRepo
|
2015-08-08 21:05:30 +02:00
|
|
|
*/
|
2015-09-06 13:14:32 +02:00
|
|
|
public function __construct(User $user, UserRepo $userRepo)
|
2015-08-08 21:05:30 +02:00
|
|
|
{
|
|
|
|
$this->user = $user;
|
2015-09-06 13:14:32 +02:00
|
|
|
$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.
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2016-03-05 13:09:09 +01:00
|
|
|
$this->checkPermission('users-manage');
|
2016-02-27 20:24:42 +01:00
|
|
|
$users = $this->userRepo->getAllUsers();
|
2015-12-05 15:41:51 +01:00
|
|
|
$this->setPageTitle('Users');
|
2015-08-29 16:03:42 +02:00
|
|
|
return view('users/index', ['users' => $users]);
|
2015-08-08 21:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new user.
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2016-02-27 20:24:42 +01:00
|
|
|
$this->checkPermission('users-manage');
|
2016-01-13 23:22:30 +01:00
|
|
|
$authMethod = config('auth.method');
|
2016-05-01 20:36:53 +02:00
|
|
|
$roles = $this->userRepo->getAssignableRoles();
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2016-02-27 20:24:42 +01:00
|
|
|
$this->checkPermission('users-manage');
|
2016-01-17 16:20:07 +01:00
|
|
|
$validationRules = [
|
2015-08-29 16:03:42 +02:00
|
|
|
'name' => 'required',
|
2016-02-27 20:24:42 +01:00
|
|
|
'email' => 'required|email|unique:users,email'
|
2016-01-17 16:20:07 +01: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 21:05:30 +02:00
|
|
|
|
|
|
|
$user = $this->user->fill($request->all());
|
2015-08-29 16:03:42 +02:00
|
|
|
|
2016-01-17 16:20:07 +01: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 20:24:42 +01:00
|
|
|
|
|
|
|
if ($request->has('roles')) {
|
|
|
|
$roles = $request->get('roles');
|
|
|
|
$user->roles()->sync($roles);
|
|
|
|
}
|
2015-12-14 21:13:32 +01:00
|
|
|
|
|
|
|
// Get avatar from gravatar and save
|
2016-01-09 20:23:35 +01:00
|
|
|
if (!config('services.disable_services')) {
|
2015-12-14 21:13:32 +01:00
|
|
|
$avatar = \Images::saveUserGravatar($user);
|
|
|
|
$user->avatar()->associate($avatar);
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
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 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
|
|
|
{
|
2016-02-27 20:24:42 +01:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($id) {
|
2015-08-29 16:03:42 +02:00
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-09-04 21:40:36 +02:00
|
|
|
|
2016-01-13 23:22:30 +01:00
|
|
|
$authMethod = config('auth.method');
|
|
|
|
|
2015-08-08 21:05:30 +02:00
|
|
|
$user = $this->user->findOrFail($id);
|
2015-09-04 21:40:36 +02:00
|
|
|
$activeSocialDrivers = $socialAuthService->getActiveDrivers();
|
2015-12-05 15:41:51 +01:00
|
|
|
$this->setPageTitle('User Profile');
|
2016-05-01 20:36:53 +02:00
|
|
|
$roles = $this->userRepo->getAssignableRoles();
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
2015-12-31 18:57:34 +01:00
|
|
|
$this->preventAccessForDemoUsers();
|
2016-02-27 20:24:42 +01:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($id) {
|
2015-08-29 16:03:42 +02:00
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-12-31 18:57:34 +01:00
|
|
|
|
2015-08-08 21:05:30 +02:00
|
|
|
$this->validate($request, [
|
2016-02-08 21:35:23 +01:00
|
|
|
'name' => 'min:2',
|
|
|
|
'email' => 'min:2|email|unique:users,email,' . $id,
|
2016-01-02 15:48:35 +01:00
|
|
|
'password' => 'min:5|required_with:password_confirm',
|
2016-02-27 20:24:42 +01:00
|
|
|
'password-confirm' => 'same:password|required_with:password'
|
2016-01-02 15:48:35 +01:00
|
|
|
], [
|
|
|
|
'password-confirm.required_with' => 'Password confirmation required'
|
2015-08-08 21:05:30 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $this->user->findOrFail($id);
|
2016-01-13 23:22:30 +01:00
|
|
|
$user->fill($request->all());
|
2015-08-08 21:05:30 +02:00
|
|
|
|
2016-01-13 23:22:30 +01:00
|
|
|
// Role updates
|
2016-02-27 20:24:42 +01:00
|
|
|
if (userCan('users-manage') && $request->has('roles')) {
|
|
|
|
$roles = $request->get('roles');
|
|
|
|
$user->roles()->sync($roles);
|
2015-08-29 16:03:42 +02:00
|
|
|
}
|
|
|
|
|
2016-01-13 23:22:30 +01:00
|
|
|
// Password updates
|
2015-08-29 16:03:42 +02:00
|
|
|
if ($request->has('password') && $request->get('password') != '') {
|
2015-08-08 21:05:30 +02:00
|
|
|
$password = $request->get('password');
|
2015-09-05 13:29:47 +02:00
|
|
|
$user->password = bcrypt($password);
|
2015-08-08 21:05:30 +02:00
|
|
|
}
|
2016-01-02 15:48:35 +01:00
|
|
|
|
2016-01-13 23:22:30 +01:00
|
|
|
// External auth id updates
|
2016-02-27 20:24:42 +01:00
|
|
|
if ($this->currentUser->can('users-manage') && $request->has('external_auth_id')) {
|
2016-01-13 23:22:30 +01:00
|
|
|
$user->external_auth_id = $request->get('external_auth_id');
|
|
|
|
}
|
|
|
|
|
2015-08-08 21:05:30 +02:00
|
|
|
$user->save();
|
2016-02-27 20:24:42 +01:00
|
|
|
session()->flash('success', 'User successfully updated');
|
2016-03-13 16:37:46 +01:00
|
|
|
|
|
|
|
$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 $id
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
2016-02-27 20:24:42 +01:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($id) {
|
2015-08-29 16:03:42 +02:00
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-12-31 18:57:34 +01:00
|
|
|
|
2015-08-08 21:05:30 +02:00
|
|
|
$user = $this->user->findOrFail($id);
|
2015-12-05 15:41:51 +01:00
|
|
|
$this->setPageTitle('Delete User ' . $user->name);
|
2015-08-08 21:05:30 +02:00
|
|
|
return view('users/delete', ['user' => $user]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2015-12-31 18:57:34 +01:00
|
|
|
$this->preventAccessForDemoUsers();
|
2016-02-27 20:24:42 +01:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($id) {
|
2015-08-29 16:03:42 +02:00
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-12-14 21:13:32 +01:00
|
|
|
|
2015-12-15 20:27:36 +01:00
|
|
|
$user = $this->userRepo->getById($id);
|
2015-12-14 21:13:32 +01:00
|
|
|
if ($this->userRepo->isOnlyAdmin($user)) {
|
2015-09-06 13:14:32 +02:00
|
|
|
session()->flash('error', 'You cannot delete the only admin');
|
|
|
|
return redirect($user->getEditUrl());
|
|
|
|
}
|
2015-12-15 20:27:36 +01:00
|
|
|
$this->userRepo->destroy($user);
|
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);
|
|
|
|
$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', [
|
|
|
|
'user' => $user,
|
|
|
|
'activity' => $userActivity,
|
|
|
|
'recentlyCreated' => $recentlyCreated,
|
|
|
|
'assetCounts' => $assetCounts
|
|
|
|
]);
|
2015-08-08 21:05:30 +02:00
|
|
|
}
|
|
|
|
}
|