| 
									
										
										
										
											2017-01-16 00:27:24 +08:00
										 |  |  | <?php namespace BookStack\Http\Controllers; | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | use BookStack\Auth\Access\SocialAuthService; | 
					
						
							| 
									
										
										
										
											2019-08-18 20:11:30 +08:00
										 |  |  | use BookStack\Auth\Access\UserInviteService; | 
					
						
							| 
									
										
										
										
											2018-09-25 19:30:50 +08:00
										 |  |  | use BookStack\Auth\User; | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | use BookStack\Auth\UserRepo; | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  | use BookStack\Exceptions\UserUpdateException; | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  | use BookStack\Uploads\ImageRepo; | 
					
						
							| 
									
										
										
										
											2018-09-25 23:58:03 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2019-09-14 06:58:40 +08:00
										 |  |  | use Illuminate\Support\Str; | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class UserController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected $user; | 
					
						
							| 
									
										
										
										
											2015-09-06 19:14:32 +08:00
										 |  |  |     protected $userRepo; | 
					
						
							| 
									
										
										
										
											2019-08-18 20:11:30 +08:00
										 |  |  |     protected $inviteService; | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |     protected $imageRepo; | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * UserController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-08-18 20:11:30 +08:00
										 |  |  |     public function __construct(User $user, UserRepo $userRepo, UserInviteService $inviteService, ImageRepo $imageRepo) | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							| 
									
										
										
										
											2015-09-06 19:14:32 +08:00
										 |  |  |         $this->userRepo = $userRepo; | 
					
						
							| 
									
										
										
										
											2019-08-18 20:11:30 +08:00
										 |  |  |         $this->inviteService = $inviteService; | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         $this->imageRepo = $imageRepo; | 
					
						
							| 
									
										
										
										
											2015-08-29 22:03:42 +08:00
										 |  |  |         parent::__construct(); | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2016-05-22 17:44:31 +08:00
										 |  |  |         $listDetails = [ | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  |             'order' => $request->get('order', 'asc'), | 
					
						
							|  |  |  |             'search' => $request->get('search', ''), | 
					
						
							|  |  |  |             'sort' => $request->get('sort', 'name'), | 
					
						
							| 
									
										
										
										
											2016-05-22 17:44:31 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  |         $users = $this->userRepo->getAllUsersPaginatedAndSorted(20, $listDetails); | 
					
						
							| 
									
										
										
										
											2016-12-05 00:51:39 +08:00
										 |  |  |         $this->setPageTitle(trans('settings.users')); | 
					
						
							| 
									
										
										
										
											2016-05-22 17:44:31 +08:00
										 |  |  |         $users->appends($listDetails); | 
					
						
							| 
									
										
										
										
											2019-04-06 23:21:20 +08:00
										 |  |  |         return view('users.index', ['users' => $users, 'listDetails' => $listDetails]); | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2016-09-29 19:43:46 +08:00
										 |  |  |         $roles = $this->userRepo->getAllRoles(); | 
					
						
							| 
									
										
										
										
											2019-04-06 23:21:20 +08:00
										 |  |  |         return view('users.create', ['authMethod' => $authMethod, 'roles' => $roles]); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Store a newly created user in storage. | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  |      * @throws UserUpdateException | 
					
						
							| 
									
										
										
										
											2020-04-10 19:49:16 +08:00
										 |  |  |      * @throws \Illuminate\Validation\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
										 |  |  |         $validationRules = [ | 
					
						
							| 
									
										
										
										
											2020-08-04 21:55:01 +08:00
										 |  |  |             'name'  => 'required', | 
					
						
							|  |  |  |             'email' => 'required|email|unique:users,email' | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($authMethod === 'standard' && !$sendInvite) { | 
					
						
							|  |  |  |             $validationRules['password'] = 'required|min:6'; | 
					
						
							| 
									
										
										
										
											2016-01-17 23:20:07 +08:00
										 |  |  |             $validationRules['password-confirm'] = 'required|same:password'; | 
					
						
							| 
									
										
										
										
											2020-04-26 01:28:07 +08:00
										 |  |  |         } elseif ($authMethod === 'ldap' || $authMethod === 'saml2') { | 
					
						
							| 
									
										
										
										
											2016-01-17 23:20:07 +08:00
										 |  |  |             $validationRules['external_auth_id'] = 'required'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $this->validate($request, $validationRules); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |         $user = $this->user->fill($request->all()); | 
					
						
							| 
									
										
										
										
											2015-08-29 22:03:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-17 23:20:07 +08:00
										 |  |  |         if ($authMethod === 'standard') { | 
					
						
							| 
									
										
										
										
											2019-09-14 06:58:40 +08:00
										 |  |  |             $user->password = bcrypt($request->get('password', Str::random(32))); | 
					
						
							| 
									
										
										
										
											2020-04-26 01:28:07 +08:00
										 |  |  |         } elseif ($authMethod === 'ldap' || $authMethod === 'saml2') { | 
					
						
							| 
									
										
										
										
											2016-01-17 23:20:07 +08:00
										 |  |  |             $user->external_auth_id = $request->get('external_auth_id'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $user->save(); | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-18 20:11:30 +08:00
										 |  |  |         if ($sendInvite) { | 
					
						
							|  |  |  |             $this->inviteService->sendInvitation($user); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  |         if ($request->filled('roles')) { | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  |             $roles = $request->get('roles'); | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  |             $this->userRepo->setUserRoles($user, $roles); | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-12-15 04:13:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-23 03:29:19 +08:00
										 |  |  |         $this->userRepo->downloadAndAssignUserAvatar($user); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the form for editing the specified user. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |     public function edit(int $id, SocialAuthService $socialAuthService) | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |         $this->checkPermissionOrCurrentUser('users-manage', $id); | 
					
						
							| 
									
										
										
										
											2015-09-05 03:40:36 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |         $user = $this->user->newQuery()->with(['apiTokens'])->findOrFail($id); | 
					
						
							| 
									
										
										
										
											2016-09-29 19:43:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $authMethod = ($user->system_name) ? 'system' : config('auth.method'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-05 03:40:36 +08:00
										 |  |  |         $activeSocialDrivers = $socialAuthService->getActiveDrivers(); | 
					
						
							| 
									
										
										
										
											2016-12-05 00:51:39 +08:00
										 |  |  |         $this->setPageTitle(trans('settings.user_profile')); | 
					
						
							| 
									
										
										
										
											2016-09-29 19:43:46 +08:00
										 |  |  |         $roles = $this->userRepo->getAllRoles(); | 
					
						
							| 
									
										
										
										
											2019-12-29 21:02:26 +08:00
										 |  |  |         return view('users.edit', [ | 
					
						
							|  |  |  |             'user' => $user, | 
					
						
							|  |  |  |             'activeSocialDrivers' => $activeSocialDrivers, | 
					
						
							|  |  |  |             'authMethod' => $authMethod, | 
					
						
							|  |  |  |             'roles' => $roles | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Update the specified user in storage. | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  |      * @throws UserUpdateException | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |      * @throws \BookStack\Exceptions\ImageUploadException | 
					
						
							| 
									
										
										
										
											2020-04-10 19:49:16 +08:00
										 |  |  |      * @throws \Illuminate\Validation\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(); | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |         $this->checkPermissionOrCurrentUser('users-manage', $id); | 
					
						
							| 
									
										
										
										
											2016-01-01 01:57:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |         $this->validate($request, [ | 
					
						
							| 
									
										
										
										
											2016-02-09 04:35:23 +08:00
										 |  |  |             'name'             => 'min:2', | 
					
						
							|  |  |  |             'email'            => 'min:2|email|unique:users,email,' . $id, | 
					
						
							| 
									
										
										
										
											2019-08-18 20:11:30 +08:00
										 |  |  |             'password'         => 'min:6|required_with:password_confirm', | 
					
						
							| 
									
										
										
										
											2017-01-16 00:27:24 +08:00
										 |  |  |             'password-confirm' => 'same:password|required_with:password', | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |             'setting'          => 'array', | 
					
						
							| 
									
										
										
										
											2020-03-04 07:05:47 +08:00
										 |  |  |             'profile_image'    => 'nullable|' . $this->getImageValidationRules(), | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |         ]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  |         $user = $this->userRepo->getById($id); | 
					
						
							| 
									
										
										
										
											2019-08-07 04:29:42 +08:00
										 |  |  |         $user->fill($request->except(['email'])); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Email updates
 | 
					
						
							|  |  |  |         if (userCan('users-manage') && $request->filled('email')) { | 
					
						
							|  |  |  |             $user->email = $request->get('email'); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 06:22:30 +08:00
										 |  |  |         // Role updates
 | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  |         if (userCan('users-manage') && $request->filled('roles')) { | 
					
						
							| 
									
										
										
										
											2016-02-28 03:24:42 +08:00
										 |  |  |             $roles = $request->get('roles'); | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  |             $this->userRepo->setUserRoles($user, $roles); | 
					
						
							| 
									
										
										
										
											2015-08-29 22:03:42 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 06:22:30 +08:00
										 |  |  |         // Password updates
 | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  |         if ($request->filled('password')) { | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |             $password = $request->get('password'); | 
					
						
							| 
									
										
										
										
											2015-09-05 19:29:47 +08:00
										 |  |  |             $user->password = bcrypt($password); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-01-02 22:48:35 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-14 06:22:30 +08:00
										 |  |  |         // External auth id updates
 | 
					
						
							| 
									
										
										
										
											2019-09-20 07:18:28 +08:00
										 |  |  |         if (user()->can('users-manage') && $request->filled('external_auth_id')) { | 
					
						
							| 
									
										
										
										
											2016-01-14 06:22:30 +08:00
										 |  |  |             $user->external_auth_id = $request->get('external_auth_id'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-16 00:27:24 +08:00
										 |  |  |         // Save an user-specific settings
 | 
					
						
							| 
									
										
										
										
											2017-11-19 23:56:06 +08:00
										 |  |  |         if ($request->filled('setting')) { | 
					
						
							| 
									
										
										
										
											2017-01-16 00:27:24 +08:00
										 |  |  |             foreach ($request->get('setting') as $key => $value) { | 
					
						
							|  |  |  |                 setting()->putUser($user, $key, $value); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Delete the profile image if set to
 | 
					
						
							|  |  |  |         if ($request->has('profile_image_reset')) { | 
					
						
							|  |  |  |             $this->imageRepo->destroyImage($user->avatar); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |         $user->save(); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $this->showSuccessNotification(trans('settings.users_edit_success')); | 
					
						
							| 
									
										
										
										
											2016-03-13 23:37:46 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-04 22:48:15 +08:00
										 |  |  |         $redirectUrl = userCan('users-manage') ? '/settings/users' : ('/settings/users/' . $user->id); | 
					
						
							| 
									
										
										
										
											2016-03-13 23:37:46 +08:00
										 |  |  |         return redirect($redirectUrl); | 
					
						
							| 
									
										
										
										
											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
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |         $this->checkPermissionOrCurrentUser('users-manage', $id); | 
					
						
							| 
									
										
										
										
											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])); | 
					
						
							| 
									
										
										
										
											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. | 
					
						
							| 
									
										
										
										
											2018-12-31 00:11:58 +08:00
										 |  |  |      * @throws \Exception | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-10 19:49:16 +08:00
										 |  |  |     public function destroy(int $id) | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-09-19 22:12:10 +08:00
										 |  |  |         $this->preventAccessInDemoMode(); | 
					
						
							| 
									
										
										
										
											2019-04-27 21:18:00 +08:00
										 |  |  |         $this->checkPermissionOrCurrentUser('users-manage', $id); | 
					
						
							| 
									
										
										
										
											2015-12-15 04:13:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 03:27:36 +08:00
										 |  |  |         $user = $this->userRepo->getById($id); | 
					
						
							| 
									
										
										
										
											2016-05-22 16:23:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 04:13:32 +08:00
										 |  |  |         if ($this->userRepo->isOnlyAdmin($user)) { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |             $this->showErrorNotification(trans('errors.users_cannot_delete_only_admin')); | 
					
						
							| 
									
										
										
										
											2015-09-06 19:14:32 +08:00
										 |  |  |             return redirect($user->getEditUrl()); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2016-05-22 16:23:41 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |         if ($user->system_name === 'public') { | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |             $this->showErrorNotification(trans('errors.users_cannot_delete_guest')); | 
					
						
							| 
									
										
										
										
											2016-09-30 00:07:58 +08:00
										 |  |  |             return redirect($user->getEditUrl()); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 03:27:36 +08:00
										 |  |  |         $this->userRepo->destroy($user); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $this->showSuccessNotification(trans('settings.users_delete_success')); | 
					
						
							| 
									
										
										
										
											2015-12-15 04:13:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 05:25:11 +08:00
										 |  |  |         return redirect('/settings/users'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Show the user profile page | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function showProfilePage($id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $user = $this->userRepo->getById($id); | 
					
						
							| 
									
										
										
										
											2019-04-06 23:21:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-17 05:25:11 +08:00
										 |  |  |         $userActivity = $this->userRepo->getActivity($user); | 
					
						
							| 
									
										
										
										
											2019-10-05 19:55:01 +08:00
										 |  |  |         $recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5); | 
					
						
							| 
									
										
										
										
											2016-02-18 06:11:48 +08:00
										 |  |  |         $assetCounts = $this->userRepo->getAssetCounts($user); | 
					
						
							| 
									
										
										
										
											2019-04-06 23:21:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return view('users.profile', [ | 
					
						
							| 
									
										
										
										
											2016-02-18 06:11:48 +08:00
										 |  |  |             'user' => $user, | 
					
						
							|  |  |  |             'activity' => $userActivity, | 
					
						
							|  |  |  |             'recentlyCreated' => $recentlyCreated, | 
					
						
							|  |  |  |             'assetCounts' => $assetCounts | 
					
						
							|  |  |  |         ]); | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-12-26 15:08:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-30 00:49:03 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update the user's preferred book-list display setting. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-10 19:49:16 +08:00
										 |  |  |     public function switchBooksView(Request $request, int $id) | 
					
						
							| 
									
										
										
										
											2018-01-29 00:58:52 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |         return $this->switchViewType($id, $request, 'books'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Update the user's preferred shelf-list display setting. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-10 19:49:16 +08:00
										 |  |  |     public function switchShelvesView(Request $request, int $id) | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         return $this->switchViewType($id, $request, 'bookshelves'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-10 19:49:16 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update the user's preferred shelf-view book list display setting. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function switchShelfView(Request $request, int $id) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->switchViewType($id, $request, 'bookshelf'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * For a type of list, switch with stored view type for a user. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2020-04-10 19:49:16 +08:00
										 |  |  |     protected function switchViewType(int $userId, Request $request, string $listName) | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->checkPermissionOrCurrentUser('users-manage', $userId); | 
					
						
							| 
									
										
										
										
											2017-12-26 15:08:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |         $viewType = $request->get('view_type'); | 
					
						
							| 
									
										
										
										
											2017-12-26 15:08:16 +08:00
										 |  |  |         if (!in_array($viewType, ['grid', 'list'])) { | 
					
						
							|  |  |  |             $viewType = 'list'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-13 23:54:55 +08:00
										 |  |  |         $user = $this->userRepo->getById($userId); | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |         $key = $listName . '_view_type'; | 
					
						
							|  |  |  |         setting()->putUser($user, $key, $viewType); | 
					
						
							| 
									
										
										
										
											2017-12-26 15:08:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |         return redirect()->back(302, [], "/settings/users/$userId"); | 
					
						
							| 
									
										
										
										
											2017-12-26 15:08:16 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2019-02-03 21:45:45 +08:00
										 |  |  |      * Change the stored sort type for a particular view. | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-09-16 01:53:30 +08:00
										 |  |  |     public function changeSort(Request $request, string $id, string $type) | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-02-03 21:45:45 +08:00
										 |  |  |         $validSortTypes = ['books', 'bookshelves']; | 
					
						
							|  |  |  |         if (!in_array($type, $validSortTypes)) { | 
					
						
							|  |  |  |             return redirect()->back(500); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $this->changeListSort($id, $request, $type); | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-12 03:37:51 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Toggle dark mode for the current user. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function toggleDarkMode() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $enabled = setting()->getForCurrentUser('dark-mode-enabled', false); | 
					
						
							|  |  |  |         setting()->putUser(user(), 'dark-mode-enabled', $enabled ? 'false' : 'true'); | 
					
						
							|  |  |  |         return redirect()->back(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Update the stored section expansion preference for the given user. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-09-16 01:53:30 +08:00
										 |  |  |     public function updateExpansionPreference(Request $request, string $id, string $key) | 
					
						
							| 
									
										
										
										
											2019-04-14 20:01:51 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $this->checkPermissionOrCurrentUser('users-manage', $id); | 
					
						
							|  |  |  |         $keyWhitelist = ['home-details']; | 
					
						
							|  |  |  |         if (!in_array($key, $keyWhitelist)) { | 
					
						
							|  |  |  |             return response("Invalid key", 500); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $newState = $request->get('expand', 'false'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $user = $this->user->findOrFail($id); | 
					
						
							|  |  |  |         setting()->putUser($user, 'section_expansion#' . $key, $newState); | 
					
						
							|  |  |  |         return response("", 204); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Changed the stored preference for a list sort order. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function changeListSort(int $userId, Request $request, string $listName) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->checkPermissionOrCurrentUser('users-manage', $userId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $sort = $request->get('sort'); | 
					
						
							|  |  |  |         if (!in_array($sort, ['name', 'created_at', 'updated_at'])) { | 
					
						
							|  |  |  |             $sort = 'name'; | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |         $order = $request->get('order'); | 
					
						
							|  |  |  |         if (!in_array($order, ['asc', 'desc'])) { | 
					
						
							|  |  |  |             $order = 'asc'; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-08 02:33:32 +08:00
										 |  |  |         $user = $this->user->findOrFail($userId); | 
					
						
							|  |  |  |         $sortKey = $listName . '_sort'; | 
					
						
							|  |  |  |         $orderKey = $listName . '_sort_order'; | 
					
						
							|  |  |  |         setting()->putUser($user, $sortKey, $sort); | 
					
						
							|  |  |  |         setting()->putUser($user, $orderKey, $order); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect()->back(302, [], "/settings/users/$userId"); | 
					
						
							| 
									
										
										
										
											2018-08-27 21:18:09 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-09 03:05:30 +08:00
										 |  |  | } |