| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | namespace BookStack\Users\Controllers; | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 21:49:53 +08:00
										 |  |  | use BookStack\Access\SocialDriverManager; | 
					
						
							| 
									
										
										
										
											2021-03-10 07:06:12 +08:00
										 |  |  | use BookStack\Exceptions\ImageUploadException; | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  | use BookStack\Exceptions\UserUpdateException; | 
					
						
							| 
									
										
										
										
											2023-05-19 03:53:39 +08:00
										 |  |  | use BookStack\Http\Controller; | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  | use BookStack\Uploads\ImageRepo; | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Users\Models\Role; | 
					
						
							|  |  |  | use BookStack\Users\Queries\UsersAllPaginatedAndSorted; | 
					
						
							|  |  |  | use BookStack\Users\UserRepo; | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  | use BookStack\Util\SimpleListOptions; | 
					
						
							| 
									
										
										
										
											2021-03-10 07:06:12 +08:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2022-01-20 03:46:38 +08:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2021-12-19 00:31:48 +08:00
										 |  |  | use Illuminate\Validation\Rules\Password; | 
					
						
							| 
									
										
										
										
											2021-03-10 07:06:12 +08:00
										 |  |  | use Illuminate\Validation\ValidationException; | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class UserController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-06-12 23:45:30 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected UserRepo $userRepo, | 
					
						
							|  |  |  |         protected ImageRepo $imageRepo | 
					
						
							|  |  |  |     ) { | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Display a listing of the users. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2016-05-22 17:44:31 +08:00
										 |  |  |     public function index(Request $request) | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-03-05 20:09:09 +08:00
										 |  |  |         $this->checkPermission('users-manage'); | 
					
						
							| 
									
										
										
										
											2022-02-13 20:56:26 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |         $listOptions = SimpleListOptions::fromRequest($request, 'users')->withSortOptions([ | 
					
						
							|  |  |  |             'name' => trans('common.sort_name'), | 
					
						
							|  |  |  |             'email' => trans('auth.email'), | 
					
						
							|  |  |  |             'created_at' => trans('common.sort_created_at'), | 
					
						
							|  |  |  |             'updated_at' => trans('common.sort_updated_at'), | 
					
						
							|  |  |  |             'last_activity_at' => trans('settings.users_latest_activity'), | 
					
						
							|  |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $users = (new UsersAllPaginatedAndSorted())->run(20, $listOptions); | 
					
						
							| 
									
										
										
										
											2021-01-11 06:43:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-05 00:51:39 +08:00
										 |  |  |         $this->setPageTitle(trans('settings.users')); | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |         $users->appends($listOptions->getPaginationAppends()); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-13 20:56:26 +08:00
										 |  |  |         return view('users.index', [ | 
					
						
							| 
									
										
										
										
											2022-02-13 21:16:43 +08:00
										 |  |  |             'users'       => $users, | 
					
						
							| 
									
										
										
										
											2022-10-30 23:16:06 +08:00
										 |  |  |             'listOptions' => $listOptions, | 
					
						
							| 
									
										
										
										
											2022-02-13 20:56:26 +08:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the form for creating a new user. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function create() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  |         $this->checkPermission('users-manage'); | 
					
						
							| 
									
										
										
										
											2016-01-14 06:22:30 +08:00
										 |  |  |         $authMethod = config('auth.method'); | 
					
						
							| 
									
										
										
										
											2022-02-13 20:56:26 +08:00
										 |  |  |         $roles = Role::query()->orderBy('display_name', 'asc')->get(); | 
					
						
							| 
									
										
										
										
											2022-02-01 06:15:21 +08:00
										 |  |  |         $this->setPageTitle(trans('settings.users_add_new')); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-06 23:21:20 +08:00
										 |  |  |         return view('users.create', ['authMethod' => $authMethod, 'roles' => $roles]); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |      * Store a new user in storage. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-03-10 07:06:12 +08:00
										 |  |  |      * @throws ValidationException | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function store(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  |         $this->checkPermission('users-manage'); | 
					
						
							| 
									
										
										
										
											2016-01-17 23:20:07 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $authMethod = config('auth.method'); | 
					
						
							| 
									
										
										
										
											2019-08-18 20:11:30 +08:00
										 |  |  |         $sendInvite = ($request->get('send_invite', 'false') === 'true'); | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |         $externalAuth = $authMethod === 'ldap' || $authMethod === 'saml2' || $authMethod === 'oidc'; | 
					
						
							|  |  |  |         $passwordRequired = ($authMethod === 'standard' && !$sendInvite); | 
					
						
							| 
									
										
										
										
											2019-08-18 20:11:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |         $validationRules = [ | 
					
						
							| 
									
										
										
										
											2022-08-09 19:40:59 +08:00
										 |  |  |             'name'             => ['required', 'max:100'], | 
					
						
							| 
									
										
										
										
											2022-02-08 23:29:58 +08:00
										 |  |  |             'email'            => ['required', 'email', 'unique:users,email'], | 
					
						
							| 
									
										
										
										
											2022-08-05 00:24:04 +08:00
										 |  |  |             'language'         => ['string', 'max:15', 'alpha_dash'], | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |             'roles'            => ['array'], | 
					
						
							|  |  |  |             'roles.*'          => ['integer'], | 
					
						
							| 
									
										
										
										
											2022-02-08 23:29:58 +08:00
										 |  |  |             'password'         => $passwordRequired ? ['required', Password::default()] : null, | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |             'password-confirm' => $passwordRequired ? ['required', 'same:password'] : null, | 
					
						
							|  |  |  |             'external_auth_id' => $externalAuth ? ['required'] : null, | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2022-01-25 04:55:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |         $validated = $this->validate($request, array_filter($validationRules)); | 
					
						
							| 
									
										
										
										
											2022-01-25 04:55:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 08:26:19 +08:00
										 |  |  |         DB::transaction(function () use ($validated, $sendInvite) { | 
					
						
							|  |  |  |             $this->userRepo->create($validated, $sendInvite); | 
					
						
							| 
									
										
										
										
											2022-01-20 03:46:38 +08:00
										 |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 05:25:11 +08:00
										 |  |  |         return redirect('/settings/users'); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the form for editing the specified user. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-12-06 21:49:53 +08:00
										 |  |  |     public function edit(int $id, SocialDriverManager $socialDriverManager) | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-19 17:20:04 +08:00
										 |  |  |         $this->checkPermission('users-manage'); | 
					
						
							| 
									
										
										
										
											2015-09-05 03:40:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-30 23:25:02 +08:00
										 |  |  |         $user = $this->userRepo->getById($id); | 
					
						
							|  |  |  |         $user->load(['apiTokens', 'mfaValues']); | 
					
						
							| 
									
										
										
										
											2016-09-29 19:43:46 +08:00
										 |  |  |         $authMethod = ($user->system_name) ? 'system' : config('auth.method'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-06 21:49:53 +08:00
										 |  |  |         $activeSocialDrivers = $socialDriverManager->getActive(); | 
					
						
							| 
									
										
										
										
											2021-07-15 03:18:48 +08:00
										 |  |  |         $mfaMethods = $user->mfaValues->groupBy('method'); | 
					
						
							| 
									
										
										
										
											2016-12-05 00:51:39 +08:00
										 |  |  |         $this->setPageTitle(trans('settings.user_profile')); | 
					
						
							| 
									
										
										
										
											2022-02-13 20:56:26 +08:00
										 |  |  |         $roles = Role::query()->orderBy('display_name', 'asc')->get(); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |         return view('users.edit', [ | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'user'                => $user, | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |             'activeSocialDrivers' => $activeSocialDrivers, | 
					
						
							| 
									
										
										
										
											2021-07-15 03:06:41 +08:00
										 |  |  |             'mfaMethods'          => $mfaMethods, | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |             'authMethod'          => $authMethod, | 
					
						
							|  |  |  |             'roles'               => $roles, | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Update the specified user in storage. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  |      * @throws UserUpdateException | 
					
						
							| 
									
										
										
										
											2021-03-10 07:06:12 +08:00
										 |  |  |      * @throws ImageUploadException | 
					
						
							|  |  |  |      * @throws ValidationException | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-10 19:49:16 +08:00
										 |  |  |     public function update(Request $request, int $id) | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-19 22:12:10 +08:00
										 |  |  |         $this->preventAccessInDemoMode(); | 
					
						
							| 
									
										
										
										
											2023-10-19 17:20:04 +08:00
										 |  |  |         $this->checkPermission('users-manage'); | 
					
						
							| 
									
										
										
										
											2016-01-01 01:57:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-04 00:52:28 +08:00
										 |  |  |         $validated = $this->validate($request, [ | 
					
						
							| 
									
										
										
										
											2022-08-09 19:40:59 +08:00
										 |  |  |             'name'             => ['min:2', 'max:100'], | 
					
						
							| 
									
										
										
										
											2021-11-05 08:26:55 +08:00
										 |  |  |             'email'            => ['min:2', 'email', 'unique:users,email,' . $id], | 
					
						
							| 
									
										
										
										
											2021-12-19 00:31:48 +08:00
										 |  |  |             'password'         => ['required_with:password_confirm', Password::default()], | 
					
						
							| 
									
										
										
										
											2021-11-05 08:26:55 +08:00
										 |  |  |             'password-confirm' => ['same:password', 'required_with:password'], | 
					
						
							| 
									
										
										
										
											2022-08-05 00:24:04 +08:00
										 |  |  |             'language'         => ['string', 'max:15', 'alpha_dash'], | 
					
						
							| 
									
										
										
										
											2022-02-04 00:52:28 +08:00
										 |  |  |             'roles'            => ['array'], | 
					
						
							|  |  |  |             'roles.*'          => ['integer'], | 
					
						
							| 
									
										
										
										
											2022-02-04 09:02:13 +08:00
										 |  |  |             'external_auth_id' => ['string'], | 
					
						
							| 
									
										
										
										
											2021-11-05 08:26:55 +08:00
										 |  |  |             'profile_image'    => array_merge(['nullable'], $this->getImageValidationRules()), | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  |         $user = $this->userRepo->getById($id); | 
					
						
							| 
									
										
										
										
											2023-10-19 17:20:04 +08:00
										 |  |  |         $this->userRepo->update($user, $validated, true); | 
					
						
							| 
									
										
										
										
											2017-01-16 00:27:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         // Save profile image if in request
 | 
					
						
							| 
									
										
										
										
											2020-03-04 07:08:01 +08:00
										 |  |  |         if ($request->hasFile('profile_image')) { | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |             $imageUpload = $request->file('profile_image'); | 
					
						
							|  |  |  |             $this->imageRepo->destroyImage($user->avatar); | 
					
						
							|  |  |  |             $image = $this->imageRepo->saveNew($imageUpload, 'user', $user->id); | 
					
						
							|  |  |  |             $user->image_id = $image->id; | 
					
						
							| 
									
										
										
										
											2022-02-04 00:52:28 +08:00
										 |  |  |             $user->save(); | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-09 07:46:38 +08:00
										 |  |  |         // Delete the profile image if reset option is in request
 | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         if ($request->has('profile_image_reset')) { | 
					
						
							|  |  |  |             $this->imageRepo->destroyImage($user->avatar); | 
					
						
							| 
									
										
										
										
											2023-01-27 01:15:09 +08:00
										 |  |  |             $user->image_id = 0; | 
					
						
							|  |  |  |             $user->save(); | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-19 17:20:04 +08:00
										 |  |  |         return redirect('/settings/users'); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the user delete page. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-10 19:49:16 +08:00
										 |  |  |     public function delete(int $id) | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-10-19 17:20:04 +08:00
										 |  |  |         $this->checkPermission('users-manage'); | 
					
						
							| 
									
										
										
										
											2016-01-01 01:57:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  |         $user = $this->userRepo->getById($id); | 
					
						
							| 
									
										
										
										
											2016-12-05 00:51:39 +08:00
										 |  |  |         $this->setPageTitle(trans('settings.users_delete_named', ['userName' => $user->name])); | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-06 23:21:20 +08:00
										 |  |  |         return view('users.delete', ['user' => $user]); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Remove the specified user from storage. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-03-10 07:06:12 +08:00
										 |  |  |      * @throws Exception | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-01-02 02:31:01 +08:00
										 |  |  |     public function destroy(Request $request, int $id) | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-19 22:12:10 +08:00
										 |  |  |         $this->preventAccessInDemoMode(); | 
					
						
							| 
									
										
										
										
											2023-10-19 17:20:04 +08:00
										 |  |  |         $this->checkPermission('users-manage'); | 
					
						
							| 
									
										
										
										
											2015-12-15 04:13:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 03:27:36 +08:00
										 |  |  |         $user = $this->userRepo->getById($id); | 
					
						
							| 
									
										
										
										
											2023-04-07 22:57:21 +08:00
										 |  |  |         $newOwnerId = intval($request->get('new_owner_id')) ?: null; | 
					
						
							| 
									
										
										
										
											2016-05-22 16:23:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-02 02:31:01 +08:00
										 |  |  |         $this->userRepo->destroy($user, $newOwnerId); | 
					
						
							| 
									
										
										
										
											2015-12-15 04:13:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 05:25:11 +08:00
										 |  |  |         return redirect('/settings/users'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  | } |