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 $users = $this->userRepo->getAllUsers();
39 $this->setPageTitle('Users');
40 return view('users/index', ['users' => $users]);
44 * Show the form for creating a new user.
47 public function create()
49 $this->checkPermission('users-manage');
50 $authMethod = config('auth.method');
51 return view('users/create', ['authMethod' => $authMethod]);
55 * Store a newly created user in storage.
56 * @param Request $request
59 public function store(Request $request)
61 $this->checkPermission('users-manage');
64 'email' => 'required|email|unique:users,email'
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');
87 if ($request->has('roles')) {
88 $roles = $request->get('roles');
89 $user->roles()->sync($roles);
92 // Get avatar from gravatar and save
93 if (!config('services.disable_services')) {
94 $avatar = \Images::saveUserGravatar($user);
95 $user->avatar()->associate($avatar);
99 return redirect('/settings/users');
103 * Show the form for editing the specified user.
105 * @param SocialAuthService $socialAuthService
108 public function edit($id, SocialAuthService $socialAuthService)
110 $this->checkPermissionOr('users-manage', function () use ($id) {
111 return $this->currentUser->id == $id;
114 $authMethod = config('auth.method');
116 $user = $this->user->findOrFail($id);
117 $activeSocialDrivers = $socialAuthService->getActiveDrivers();
118 $this->setPageTitle('User Profile');
119 return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers, 'authMethod' => $authMethod]);
123 * Update the specified user in storage.
124 * @param Request $request
128 public function update(Request $request, $id)
130 $this->preventAccessForDemoUsers();
131 $this->checkPermissionOr('users-manage', function () use ($id) {
132 return $this->currentUser->id == $id;
135 $this->validate($request, [
137 'email' => 'min:2|email|unique:users,email,' . $id,
138 'password' => 'min:5|required_with:password_confirm',
139 'password-confirm' => 'same:password|required_with:password'
141 'password-confirm.required_with' => 'Password confirmation required'
144 $user = $this->user->findOrFail($id);
145 $user->fill($request->all());
148 if (userCan('users-manage') && $request->has('roles')) {
149 $roles = $request->get('roles');
150 $user->roles()->sync($roles);
154 if ($request->has('password') && $request->get('password') != '') {
155 $password = $request->get('password');
156 $user->password = bcrypt($password);
159 // External auth id updates
160 if ($this->currentUser->can('users-manage') && $request->has('external_auth_id')) {
161 $user->external_auth_id = $request->get('external_auth_id');
165 session()->flash('success', 'User successfully updated');
166 return redirect('/settings/users');
170 * Show the user delete page.
172 * @return \Illuminate\View\View
174 public function delete($id)
176 $this->checkPermissionOr('users-manage', function () use ($id) {
177 return $this->currentUser->id == $id;
180 $user = $this->user->findOrFail($id);
181 $this->setPageTitle('Delete User ' . $user->name);
182 return view('users/delete', ['user' => $user]);
186 * Remove the specified user from storage.
190 public function destroy($id)
192 $this->preventAccessForDemoUsers();
193 $this->checkPermissionOr('users-manage', function () use ($id) {
194 return $this->currentUser->id == $id;
197 $user = $this->userRepo->getById($id);
198 if ($this->userRepo->isOnlyAdmin($user)) {
199 session()->flash('error', 'You cannot delete the only admin');
200 return redirect($user->getEditUrl());
202 $this->userRepo->destroy($user);
204 return redirect('/settings/users');
208 * Show the user profile page
210 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
212 public function showProfilePage($id)
214 $user = $this->userRepo->getById($id);
215 $userActivity = $this->userRepo->getActivity($user);
216 $recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5, 0);
217 $assetCounts = $this->userRepo->getAssetCounts($user);
218 return view('users/profile', [
220 'activity' => $userActivity,
221 'recentlyCreated' => $recentlyCreated,
222 'assetCounts' => $assetCounts