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 return view('users/create');
53 * Store a newly created user in storage.
54 * @param Request $request
57 public function store(Request $request)
59 $this->checkPermission('user-create');
60 $this->validate($request, [
62 'email' => 'required|email|unique:users,email',
63 'password' => 'required|min:5',
64 'password-confirm' => 'required|same:password',
65 'role' => 'required|exists:roles,id'
68 $user = $this->user->fill($request->all());
69 $user->password = bcrypt($request->get('password'));
72 $user->attachRoleId($request->get('role'));
74 // Get avatar from gravatar and save
75 if (!env('DISABLE_EXTERNAL_SERVICES', false)) {
76 $avatar = \Images::saveUserGravatar($user);
77 $user->avatar()->associate($avatar);
81 return redirect('/users');
86 * Show the form for editing the specified user.
88 * @param SocialAuthService $socialAuthService
91 public function edit($id, SocialAuthService $socialAuthService)
93 $this->checkPermissionOr('user-update', function () use ($id) {
94 return $this->currentUser->id == $id;
97 $user = $this->user->findOrFail($id);
98 $activeSocialDrivers = $socialAuthService->getActiveDrivers();
99 $this->setPageTitle('User Profile');
100 return view('users/edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers]);
104 * Update the specified user in storage.
105 * @param Request $request
109 public function update(Request $request, $id)
111 $this->checkPermissionOr('user-update', function () use ($id) {
112 return $this->currentUser->id == $id;
114 $this->validate($request, [
115 'name' => 'required',
116 'email' => 'required|email|unique:users,email,' . $id,
117 'password' => 'min:5',
118 'password-confirm' => 'same:password',
119 'role' => 'exists:roles,id'
122 $user = $this->user->findOrFail($id);
123 $user->fill($request->except('password'));
125 if ($this->currentUser->can('user-update') && $request->has('role')) {
126 $user->attachRoleId($request->get('role'));
129 if ($request->has('password') && $request->get('password') != '') {
130 $password = $request->get('password');
131 $user->password = bcrypt($password);
134 return redirect('/users');
138 * Show the user delete page.
140 * @return \Illuminate\View\View
142 public function delete($id)
144 $this->checkPermissionOr('user-delete', function () use ($id) {
145 return $this->currentUser->id == $id;
147 $user = $this->user->findOrFail($id);
148 $this->setPageTitle('Delete User ' . $user->name);
149 return view('users/delete', ['user' => $user]);
153 * Remove the specified user from storage.
157 public function destroy($id)
159 $this->checkPermissionOr('user-delete', function () use ($id) {
160 return $this->currentUser->id == $id;
163 $user = $this->userRepo->getById($id);
164 if ($this->userRepo->isOnlyAdmin($user)) {
165 session()->flash('error', 'You cannot delete the only admin');
166 return redirect($user->getEditUrl());
168 $this->userRepo->destroy($user);
170 return redirect('/users');