3 namespace BookStack\Http\Controllers;
5 use Illuminate\Http\Request;
7 use Illuminate\Http\Response;
8 use BookStack\Http\Requests;
9 use BookStack\Repos\UserRepo;
10 use BookStack\Services\SocialAuthService;
13 class UserController extends Controller
20 * UserController constructor.
22 * @param UserRepo $userRepo
24 public function __construct(User $user, UserRepo $userRepo)
27 $this->userRepo = $userRepo;
28 parent::__construct();
32 * Display a listing of the users.
35 public function index()
37 $users = $this->user->all();
38 $this->setPageTitle('Users');
39 return view('users/index', ['users' => $users]);
43 * Show the form for creating a new user.
46 public function create()
48 $this->checkPermission('user-create');
49 $authMethod = config('auth.method');
50 return view('users/create', ['authMethod' => $authMethod]);
54 * Store a newly created user in storage.
55 * @param Request $request
58 public function store(Request $request)
60 $this->checkPermission('user-create');
63 'email' => 'required|email|unique:users,email',
64 'role' => 'required|exists:roles,id'
67 $authMethod = config('auth.method');
68 if ($authMethod === 'standard') {
69 $validationRules['password'] = 'required|min:5';
70 $validationRules['password-confirm'] = 'required|same:password';
71 } elseif ($authMethod === 'ldap') {
72 $validationRules['external_auth_id'] = 'required';
74 $this->validate($request, $validationRules);
77 $user = $this->user->fill($request->all());
79 if ($authMethod === 'standard') {
80 $user->password = bcrypt($request->get('password'));
81 } elseif ($authMethod === 'ldap') {
82 $user->external_auth_id = $request->get('external_auth_id');
86 $user->attachRoleId($request->get('role'));
88 // Get avatar from gravatar and save
89 if (!config('services.disable_services')) {
90 $avatar = \Images::saveUserGravatar($user);
91 $user->avatar()->associate($avatar);
95 return redirect('/users');
100 * Show the form for editing the specified user.
102 * @param SocialAuthService $socialAuthService
105 public function edit($id, SocialAuthService $socialAuthService)
107 $this->checkPermissionOr('user-update', function () use ($id) {
108 return $this->currentUser->id == $id;
111 $authMethod = config('auth.method');
113 $user = $this->user->findOrFail($id);
114 $activeSocialDrivers = $socialAuthService->getActiveDrivers();
115 $this->setPageTitle('User Profile');
116 return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers, 'authMethod' => $authMethod]);
120 * Update the specified user in storage.
121 * @param Request $request
125 public function update(Request $request, $id)
127 $this->preventAccessForDemoUsers();
128 $this->checkPermissionOr('user-update', function () use ($id) {
129 return $this->currentUser->id == $id;
132 $this->validate($request, [
133 'name' => 'required',
134 'email' => 'required|email|unique:users,email,' . $id,
135 'password' => 'min:5|required_with:password_confirm',
136 'password-confirm' => 'same:password|required_with:password',
137 'role' => 'exists:roles,id'
139 'password-confirm.required_with' => 'Password confirmation required'
142 $user = $this->user->findOrFail($id);
143 $user->fill($request->all());
146 if ($this->currentUser->can('user-update') && $request->has('role')) {
147 $user->attachRoleId($request->get('role'));
151 if ($request->has('password') && $request->get('password') != '') {
152 $password = $request->get('password');
153 $user->password = bcrypt($password);
156 // External auth id updates
157 if ($this->currentUser->can('user-update') && $request->has('external_auth_id')) {
158 $user->external_auth_id = $request->get('external_auth_id');
162 return redirect('/users');
166 * Show the user delete page.
168 * @return \Illuminate\View\View
170 public function delete($id)
172 $this->checkPermissionOr('user-delete', function () use ($id) {
173 return $this->currentUser->id == $id;
176 $user = $this->user->findOrFail($id);
177 $this->setPageTitle('Delete User ' . $user->name);
178 return view('users/delete', ['user' => $user]);
182 * Remove the specified user from storage.
186 public function destroy($id)
188 $this->preventAccessForDemoUsers();
189 $this->checkPermissionOr('user-delete', function () use ($id) {
190 return $this->currentUser->id == $id;
193 $user = $this->userRepo->getById($id);
194 if ($this->userRepo->isOnlyAdmin($user)) {
195 session()->flash('error', 'You cannot delete the only admin');
196 return redirect($user->getEditUrl());
198 $this->userRepo->destroy($user);
200 return redirect('/users');