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->user->all();
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('user-create');
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('user-create');
64 'email' => 'required|email|unique:users,email',
65 'role' => 'required|exists:roles,id'
68 $authMethod = config('auth.method');
69 if ($authMethod === 'standard') {
70 $validationRules['password'] = 'required|min:5';
71 $validationRules['password-confirm'] = 'required|same:password';
72 } elseif ($authMethod === 'ldap') {
73 $validationRules['external_auth_id'] = 'required';
75 $this->validate($request, $validationRules);
78 $user = $this->user->fill($request->all());
80 if ($authMethod === 'standard') {
81 $user->password = bcrypt($request->get('password'));
82 } elseif ($authMethod === 'ldap') {
83 $user->external_auth_id = $request->get('external_auth_id');
87 $user->attachRoleId($request->get('role'));
89 // Get avatar from gravatar and save
90 if (!config('services.disable_services')) {
91 $avatar = \Images::saveUserGravatar($user);
92 $user->avatar()->associate($avatar);
96 return redirect('/settings/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, [
134 'email' => 'min:2|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('/settings/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('/settings/users');
204 * Show the user profile page
206 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
208 public function showProfilePage($id)
210 $user = $this->userRepo->getById($id);
211 $userActivity = $this->userRepo->getActivity($user);
212 $recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5, 0);
213 $assetCounts = $this->userRepo->getAssetCounts($user);
214 return view('users/profile', [
216 'activity' => $userActivity,
217 'recentlyCreated' => $recentlyCreated,
218 'assetCounts' => $assetCounts