<?php namespace BookStack\Http\Controllers;
use BookStack\Auth\Access\SocialAuthService;
+use BookStack\Auth\Access\UserInviteService;
use BookStack\Auth\User;
use BookStack\Auth\UserRepo;
use BookStack\Exceptions\UserUpdateException;
use BookStack\Uploads\ImageRepo;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
+use Illuminate\Support\Str;
class UserController extends Controller
{
protected $user;
protected $userRepo;
+ protected $inviteService;
protected $imageRepo;
/**
* UserController constructor.
* @param User $user
* @param UserRepo $userRepo
+ * @param UserInviteService $inviteService
* @param ImageRepo $imageRepo
*/
- public function __construct(User $user, UserRepo $userRepo, ImageRepo $imageRepo)
+ public function __construct(User $user, UserRepo $userRepo, UserInviteService $inviteService, ImageRepo $imageRepo)
{
$this->user = $user;
$this->userRepo = $userRepo;
+ $this->inviteService = $inviteService;
$this->imageRepo = $imageRepo;
parent::__construct();
}
];
$authMethod = config('auth.method');
- if ($authMethod === 'standard') {
- $validationRules['password'] = 'required|min:5';
+ $sendInvite = ($request->get('send_invite', 'false') === 'true');
+
+ if ($authMethod === 'standard' && !$sendInvite) {
+ $validationRules['password'] = 'required|min:6';
$validationRules['password-confirm'] = 'required|same:password';
} elseif ($authMethod === 'ldap') {
$validationRules['external_auth_id'] = 'required';
$user = $this->user->fill($request->all());
if ($authMethod === 'standard') {
- $user->password = bcrypt($request->get('password'));
+ $user->password = bcrypt($request->get('password', Str::random(32)));
} elseif ($authMethod === 'ldap') {
$user->external_auth_id = $request->get('external_auth_id');
}
$user->save();
+ if ($sendInvite) {
+ $this->inviteService->sendInvitation($user);
+ }
+
if ($request->filled('roles')) {
$roles = $request->get('roles');
$this->userRepo->setUserRoles($user, $roles);
/**
* Show the form for editing the specified user.
- * @param int $id
- * @param \BookStack\Auth\Access\SocialAuthService $socialAuthService
- * @return Response
*/
- public function edit($id, SocialAuthService $socialAuthService)
+ public function edit(int $id, SocialAuthService $socialAuthService)
{
$this->checkPermissionOrCurrentUser('users-manage', $id);
- $user = $this->user->findOrFail($id);
+ $user = $this->user->newQuery()->with(['apiTokens'])->findOrFail($id);
$authMethod = ($user->system_name) ? 'system' : config('auth.method');
$activeSocialDrivers = $socialAuthService->getActiveDrivers();
$this->setPageTitle(trans('settings.user_profile'));
$roles = $this->userRepo->getAllRoles();
- return view('users.edit', ['user' => $user, 'activeSocialDrivers' => $activeSocialDrivers, 'authMethod' => $authMethod, 'roles' => $roles]);
+ return view('users.edit', [
+ 'user' => $user,
+ 'activeSocialDrivers' => $activeSocialDrivers,
+ 'authMethod' => $authMethod,
+ 'roles' => $roles
+ ]);
}
/**
*/
public function update(Request $request, $id)
{
- $this->preventAccessForDemoUsers();
+ $this->preventAccessInDemoMode();
$this->checkPermissionOrCurrentUser('users-manage', $id);
$this->validate($request, [
'name' => 'min:2',
'email' => 'min:2|email|unique:users,email,' . $id,
- 'password' => 'min:5|required_with:password_confirm',
+ 'password' => 'min:6|required_with:password_confirm',
'password-confirm' => 'same:password|required_with:password',
'setting' => 'array',
'profile_image' => $this->imageRepo->getImageValidationRules(),
]);
$user = $this->userRepo->getById($id);
- $user->fill($request->all());
+ $user->fill($request->except(['email']));
+
+ // Email updates
+ if (userCan('users-manage') && $request->filled('email')) {
+ $user->email = $request->get('email');
+ }
// Role updates
if (userCan('users-manage') && $request->filled('roles')) {
}
// External auth id updates
- if ($this->currentUser->can('users-manage') && $request->filled('external_auth_id')) {
+ if (user()->can('users-manage') && $request->filled('external_auth_id')) {
$user->external_auth_id = $request->get('external_auth_id');
}
}
$user->save();
- session()->flash('success', trans('settings.users_edit_success'));
+ $this->showSuccessNotification(trans('settings.users_edit_success'));
$redirectUrl = userCan('users-manage') ? '/settings/users' : ('/settings/users/' . $user->id);
return redirect($redirectUrl);
*/
public function destroy($id)
{
- $this->preventAccessForDemoUsers();
+ $this->preventAccessInDemoMode();
$this->checkPermissionOrCurrentUser('users-manage', $id);
$user = $this->userRepo->getById($id);
if ($this->userRepo->isOnlyAdmin($user)) {
- session()->flash('error', trans('errors.users_cannot_delete_only_admin'));
+ $this->showErrorNotification(trans('errors.users_cannot_delete_only_admin'));
return redirect($user->getEditUrl());
}
if ($user->system_name === 'public') {
- session()->flash('error', trans('errors.users_cannot_delete_guest'));
+ $this->showErrorNotification(trans('errors.users_cannot_delete_guest'));
return redirect($user->getEditUrl());
}
$this->userRepo->destroy($user);
- session()->flash('success', trans('settings.users_delete_success'));
+ $this->showSuccessNotification(trans('settings.users_delete_success'));
return redirect('/settings/users');
}
$user = $this->userRepo->getById($id);
$userActivity = $this->userRepo->getActivity($user);
- $recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5, 0);
+ $recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5);
$assetCounts = $this->userRepo->getAssetCounts($user);
return view('users.profile', [
/**
* Update the user's preferred book-list display setting.
- * @param $id
* @param Request $request
+ * @param $id
* @return \Illuminate\Http\RedirectResponse
*/
- public function switchBookView($id, Request $request)
+ public function switchBookView(Request $request, $id)
{
return $this->switchViewType($id, $request, 'books');
}
/**
* Update the user's preferred shelf-list display setting.
- * @param $id
* @param Request $request
+ * @param $id
* @return \Illuminate\Http\RedirectResponse
*/
- public function switchShelfView($id, Request $request)
+ public function switchShelfView(Request $request, $id)
{
return $this->switchViewType($id, $request, 'bookshelves');
}
/**
* Change the stored sort type for a particular view.
+ * @param Request $request
* @param string $id
* @param string $type
- * @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
- public function changeSort(string $id, string $type, Request $request)
+ public function changeSort(Request $request, string $id, string $type)
{
$validSortTypes = ['books', 'bookshelves'];
if (!in_array($type, $validSortTypes)) {
/**
* Update the stored section expansion preference for the given user.
+ * @param Request $request
* @param string $id
* @param string $key
- * @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
*/
- public function updateExpansionPreference(string $id, string $key, Request $request)
+ public function updateExpansionPreference(Request $request, string $id, string $key)
{
$this->checkPermissionOrCurrentUser('users-manage', $id);
$keyWhitelist = ['home-details'];