2015-08-08 15:05:30 -04:00
|
|
|
<?php
|
|
|
|
|
2015-09-10 14:31:09 -04:00
|
|
|
namespace BookStack\Http\Controllers;
|
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\Http\Requests;
|
|
|
|
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.
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
|
|
|
$users = $this->user->all();
|
2015-12-05 09:41:51 -05:00
|
|
|
$this->setPageTitle('Users');
|
2015-08-29 10:03:42 -04:00
|
|
|
return view('users/index', ['users' => $users]);
|
2015-08-08 15:05:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new user.
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermission('user-create');
|
2015-08-08 15:05:30 -04:00
|
|
|
return view('users/create');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermission('user-create');
|
2015-08-08 15:05:30 -04:00
|
|
|
$this->validate($request, [
|
2015-08-29 10:03:42 -04:00
|
|
|
'name' => 'required',
|
2015-12-09 17:30:55 -05:00
|
|
|
'email' => 'required|email|unique:users,email',
|
2015-08-29 10:03:42 -04:00
|
|
|
'password' => 'required|min:5',
|
|
|
|
'password-confirm' => 'required|same:password',
|
|
|
|
'role' => 'required|exists:roles,id'
|
2015-08-08 15:05:30 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $this->user->fill($request->all());
|
2015-09-05 07:29:47 -04:00
|
|
|
$user->password = bcrypt($request->get('password'));
|
2015-08-08 15:05:30 -04:00
|
|
|
$user->save();
|
2015-08-29 10:03:42 -04:00
|
|
|
|
|
|
|
$user->attachRoleId($request->get('role'));
|
2015-12-14 15:13:32 -05:00
|
|
|
|
|
|
|
// Get avatar from gravatar and save
|
2016-01-09 14:23:35 -05:00
|
|
|
if (!config('services.disable_services')) {
|
2015-12-14 15:13:32 -05:00
|
|
|
$avatar = \Images::saveUserGravatar($user);
|
|
|
|
$user->avatar()->associate($avatar);
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
return redirect('/users');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermissionOr('user-update', function () use ($id) {
|
|
|
|
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);
|
2015-09-04 15:40:36 -04:00
|
|
|
$activeSocialDrivers = $socialAuthService->getActiveDrivers();
|
2015-12-05 09:41:51 -05:00
|
|
|
$this->setPageTitle('User Profile');
|
2015-09-04 15:40:36 -04:00
|
|
|
return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers]);
|
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();
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermissionOr('user-update', function () use ($id) {
|
|
|
|
return $this->currentUser->id == $id;
|
|
|
|
});
|
2015-12-31 12:57:34 -05:00
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$this->validate($request, [
|
2015-08-29 10:03:42 -04:00
|
|
|
'name' => 'required',
|
2015-09-04 12:50:52 -04:00
|
|
|
'email' => 'required|email|unique:users,email,' . $id,
|
2016-01-02 09:48:35 -05:00
|
|
|
'password' => 'min:5|required_with:password_confirm',
|
|
|
|
'password-confirm' => 'same:password|required_with:password',
|
2015-08-29 10:03:42 -04:00
|
|
|
'role' => 'exists:roles,id'
|
2016-01-02 09:48:35 -05:00
|
|
|
], [
|
|
|
|
'password-confirm.required_with' => 'Password confirmation required'
|
2015-08-08 15:05:30 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
$user = $this->user->findOrFail($id);
|
2015-09-04 15:40:36 -04:00
|
|
|
$user->fill($request->except('password'));
|
2015-08-08 15:05:30 -04:00
|
|
|
|
2015-08-29 10:03:42 -04:00
|
|
|
if ($this->currentUser->can('user-update') && $request->has('role')) {
|
|
|
|
$user->attachRoleId($request->get('role'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($request->has('password') && $request->get('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
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
$user->save();
|
|
|
|
return redirect('/users');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the user delete page.
|
|
|
|
* @param $id
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function delete($id)
|
|
|
|
{
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermissionOr('user-delete', function () use ($id) {
|
|
|
|
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);
|
2015-12-05 09:41:51 -05:00
|
|
|
$this->setPageTitle('Delete User ' . $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();
|
2015-08-29 10:03:42 -04:00
|
|
|
$this->checkPermissionOr('user-delete', function () use ($id) {
|
|
|
|
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);
|
2015-12-14 15:13:32 -05:00
|
|
|
if ($this->userRepo->isOnlyAdmin($user)) {
|
2015-09-06 07:14:32 -04:00
|
|
|
session()->flash('error', 'You cannot delete the only admin');
|
|
|
|
return redirect($user->getEditUrl());
|
|
|
|
}
|
2015-12-15 14:27:36 -05:00
|
|
|
$this->userRepo->destroy($user);
|
2015-12-14 15:13:32 -05:00
|
|
|
|
2015-08-08 15:05:30 -04:00
|
|
|
return redirect('/users');
|
|
|
|
}
|
|
|
|
}
|