3 namespace BookStack\Http\Controllers;
5 use BookStack\Activity;
6 use Illuminate\Http\Request;
8 use Illuminate\Http\Response;
9 use BookStack\Http\Requests;
10 use BookStack\Repos\UserRepo;
11 use BookStack\Services\SocialAuthService;
14 class UserController extends Controller
21 * UserController constructor.
23 * @param UserRepo $userRepo
25 public function __construct(User $user, UserRepo $userRepo)
28 $this->userRepo = $userRepo;
29 parent::__construct();
33 * Display a listing of the users.
36 public function index()
38 $this->checkPermission('users-manage');
39 $users = $this->userRepo->getAllUsers();
40 $this->setPageTitle('Users');
41 return view('users/index', ['users' => $users]);
45 * Show the form for creating a new user.
48 public function create()
50 $this->checkPermission('users-manage');
51 $authMethod = config('auth.method');
52 $roles = $this->userRepo->getAssignableRoles();
53 return view('users/create', ['authMethod' => $authMethod, 'roles' => $roles]);
57 * Store a newly created user in storage.
58 * @param Request $request
61 public function store(Request $request)
63 $this->checkPermission('users-manage');
66 'email' => 'required|email|unique:users,email'
69 $authMethod = config('auth.method');
70 if ($authMethod === 'standard') {
71 $validationRules['password'] = 'required|min:5';
72 $validationRules['password-confirm'] = 'required|same:password';
73 } elseif ($authMethod === 'ldap') {
74 $validationRules['external_auth_id'] = 'required';
76 $this->validate($request, $validationRules);
79 $user = $this->user->fill($request->all());
81 if ($authMethod === 'standard') {
82 $user->password = bcrypt($request->get('password'));
83 } elseif ($authMethod === 'ldap') {
84 $user->external_auth_id = $request->get('external_auth_id');
89 if ($request->has('roles')) {
90 $roles = $request->get('roles');
91 $user->roles()->sync($roles);
94 // Get avatar from gravatar and save
95 if (!config('services.disable_services')) {
96 $avatar = \Images::saveUserGravatar($user);
97 $user->avatar()->associate($avatar);
101 return redirect('/settings/users');
105 * Show the form for editing the specified user.
107 * @param SocialAuthService $socialAuthService
110 public function edit($id, SocialAuthService $socialAuthService)
112 $this->checkPermissionOr('users-manage', function () use ($id) {
113 return $this->currentUser->id == $id;
116 $authMethod = config('auth.method');
118 $user = $this->user->findOrFail($id);
119 $activeSocialDrivers = $socialAuthService->getActiveDrivers();
120 $this->setPageTitle('User Profile');
121 $roles = $this->userRepo->getAssignableRoles();
122 return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers, 'authMethod' => $authMethod, 'roles' => $roles]);
126 * Update the specified user in storage.
127 * @param Request $request
131 public function update(Request $request, $id)
133 $this->preventAccessForDemoUsers();
134 $this->checkPermissionOr('users-manage', function () use ($id) {
135 return $this->currentUser->id == $id;
138 $this->validate($request, [
140 'email' => 'min:2|email|unique:users,email,' . $id,
141 'password' => 'min:5|required_with:password_confirm',
142 'password-confirm' => 'same:password|required_with:password'
144 'password-confirm.required_with' => 'Password confirmation required'
147 $user = $this->user->findOrFail($id);
148 $user->fill($request->all());
151 if (userCan('users-manage') && $request->has('roles')) {
152 $roles = $request->get('roles');
153 $user->roles()->sync($roles);
157 if ($request->has('password') && $request->get('password') != '') {
158 $password = $request->get('password');
159 $user->password = bcrypt($password);
162 // External auth id updates
163 if ($this->currentUser->can('users-manage') && $request->has('external_auth_id')) {
164 $user->external_auth_id = $request->get('external_auth_id');
168 session()->flash('success', 'User successfully updated');
170 $redirectUrl = userCan('users-manage') ? '/settings/users' : '/settings/users/' . $user->id;
171 return redirect($redirectUrl);
175 * Show the user delete page.
177 * @return \Illuminate\View\View
179 public function delete($id)
181 $this->checkPermissionOr('users-manage', function () use ($id) {
182 return $this->currentUser->id == $id;
185 $user = $this->user->findOrFail($id);
186 $this->setPageTitle('Delete User ' . $user->name);
187 return view('users/delete', ['user' => $user]);
191 * Remove the specified user from storage.
195 public function destroy($id)
197 $this->preventAccessForDemoUsers();
198 $this->checkPermissionOr('users-manage', function () use ($id) {
199 return $this->currentUser->id == $id;
202 $user = $this->userRepo->getById($id);
203 if ($this->userRepo->isOnlyAdmin($user)) {
204 session()->flash('error', 'You cannot delete the only admin');
205 return redirect($user->getEditUrl());
207 $this->userRepo->destroy($user);
209 return redirect('/settings/users');
213 * Show the user profile page
215 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
217 public function showProfilePage($id)
219 $user = $this->userRepo->getById($id);
220 $userActivity = $this->userRepo->getActivity($user);
221 $recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5, 0);
222 $assetCounts = $this->userRepo->getAssetCounts($user);
223 return view('users/profile', [
225 'activity' => $userActivity,
226 'recentlyCreated' => $recentlyCreated,
227 'assetCounts' => $assetCounts