BookStack/app/Http/Controllers/UserController.php

241 lines
7.4 KiB
PHP
Raw Normal View History

2015-08-09 03:05:30 +08:00
<?php
namespace BookStack\Http\Controllers;
2015-08-09 03:05:30 +08:00
2016-02-17 05:25:11 +08:00
use BookStack\Activity;
2015-08-09 03:05:30 +08:00
use Illuminate\Http\Request;
2015-12-15 04:13:32 +08:00
use Illuminate\Http\Response;
use BookStack\Http\Requests;
use BookStack\Repos\UserRepo;
use BookStack\Services\SocialAuthService;
use BookStack\User;
2015-08-09 03:05:30 +08:00
class UserController extends Controller
{
protected $user;
protected $userRepo;
2015-08-09 03:05:30 +08:00
/**
* UserController constructor.
* @param User $user
* @param UserRepo $userRepo
2015-08-09 03:05:30 +08:00
*/
public function __construct(User $user, UserRepo $userRepo)
2015-08-09 03:05:30 +08:00
{
$this->user = $user;
$this->userRepo = $userRepo;
2015-08-29 22:03:42 +08:00
parent::__construct();
2015-08-09 03:05:30 +08:00
}
/**
* Display a listing of the users.
* @param Request $request
2015-08-09 03:05:30 +08:00
* @return Response
*/
public function index(Request $request)
2015-08-09 03:05:30 +08:00
{
$this->checkPermission('users-manage');
$listDetails = [
'order' => $request->has('order') ? $request->get('order') : 'asc',
'search' => $request->has('search') ? $request->get('search') : '',
'sort' => $request->has('sort') ? $request->get('sort') : 'name',
];
$users = $this->userRepo->getAllUsersPaginatedAndSorted(20, $listDetails);
$this->setPageTitle('Users');
$users->appends($listDetails);
return view('users/index', ['users' => $users, 'listDetails' => $listDetails]);
2015-08-09 03:05:30 +08:00
}
/**
* Show the form for creating a new user.
* @return Response
*/
public function create()
{
$this->checkPermission('users-manage');
$authMethod = config('auth.method');
$roles = $this->userRepo->getAssignableRoles();
return view('users/create', ['authMethod' => $authMethod, 'roles' => $roles]);
2015-08-09 03:05:30 +08:00
}
/**
* Store a newly created user in storage.
2015-08-29 22:03:42 +08:00
* @param Request $request
2015-08-09 03:05:30 +08:00
* @return Response
*/
public function store(Request $request)
{
$this->checkPermission('users-manage');
$validationRules = [
2015-08-29 22:03:42 +08:00
'name' => 'required',
'email' => 'required|email|unique:users,email'
];
$authMethod = config('auth.method');
if ($authMethod === 'standard') {
$validationRules['password'] = 'required|min:5';
$validationRules['password-confirm'] = 'required|same:password';
} elseif ($authMethod === 'ldap') {
$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
if ($authMethod === 'standard') {
$user->password = bcrypt($request->get('password'));
} elseif ($authMethod === 'ldap') {
$user->external_auth_id = $request->get('external_auth_id');
}
$user->save();
if ($request->has('roles')) {
$roles = $request->get('roles');
$user->roles()->sync($roles);
}
2015-12-15 04:13:32 +08:00
// Get avatar from gravatar and save
if (!config('services.disable_services')) {
2015-12-15 04:13:32 +08:00
$avatar = \Images::saveUserGravatar($user);
$user->avatar()->associate($avatar);
$user->save();
}
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.
2015-09-05 03:40:36 +08:00
* @param int $id
* @param SocialAuthService $socialAuthService
2015-08-09 03:05:30 +08:00
* @return Response
*/
2015-09-05 03:40:36 +08:00
public function edit($id, SocialAuthService $socialAuthService)
2015-08-09 03:05:30 +08:00
{
$this->checkPermissionOr('users-manage', function () use ($id) {
2015-08-29 22:03:42 +08:00
return $this->currentUser->id == $id;
});
2015-09-05 03:40:36 +08:00
$authMethod = config('auth.method');
2015-08-09 03:05:30 +08:00
$user = $this->user->findOrFail($id);
2015-09-05 03:40:36 +08:00
$activeSocialDrivers = $socialAuthService->getActiveDrivers();
$this->setPageTitle('User Profile');
$roles = $this->userRepo->getAssignableRoles();
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.
2015-08-29 22:03:42 +08:00
* @param Request $request
* @param int $id
2015-08-09 03:05:30 +08:00
* @return Response
*/
public function update(Request $request, $id)
{
$this->preventAccessForDemoUsers();
$this->checkPermissionOr('users-manage', function () use ($id) {
2015-08-29 22:03:42 +08:00
return $this->currentUser->id == $id;
});
2015-08-09 03:05:30 +08:00
$this->validate($request, [
'name' => 'min:2',
'email' => 'min:2|email|unique:users,email,' . $id,
'password' => 'min:5|required_with:password_confirm',
'password-confirm' => 'same:password|required_with:password'
], [
'password-confirm.required_with' => 'Password confirmation required'
2015-08-09 03:05:30 +08:00
]);
$user = $this->user->findOrFail($id);
$user->fill($request->all());
2015-08-09 03:05:30 +08:00
// Role updates
if (userCan('users-manage') && $request->has('roles')) {
$roles = $request->get('roles');
$user->roles()->sync($roles);
2015-08-29 22:03:42 +08:00
}
// Password updates
2015-08-29 22:03:42 +08:00
if ($request->has('password') && $request->get('password') != '') {
2015-08-09 03:05:30 +08:00
$password = $request->get('password');
$user->password = bcrypt($password);
2015-08-09 03:05:30 +08:00
}
// External auth id updates
if ($this->currentUser->can('users-manage') && $request->has('external_auth_id')) {
$user->external_auth_id = $request->get('external_auth_id');
}
2015-08-09 03:05:30 +08:00
$user->save();
session()->flash('success', 'User successfully updated');
$redirectUrl = userCan('users-manage') ? '/settings/users' : '/settings/users/' . $user->id;
return redirect($redirectUrl);
2015-08-09 03:05:30 +08:00
}
/**
* Show the user delete page.
* @param $id
* @return \Illuminate\View\View
*/
public function delete($id)
{
$this->checkPermissionOr('users-manage', function () use ($id) {
2015-08-29 22:03:42 +08:00
return $this->currentUser->id == $id;
});
2015-08-09 03:05:30 +08:00
$user = $this->user->findOrFail($id);
$this->setPageTitle('Delete User ' . $user->name);
2015-08-09 03:05:30 +08:00
return view('users/delete', ['user' => $user]);
}
/**
* Remove the specified user from storage.
2015-08-29 22:03:42 +08:00
* @param int $id
2015-08-09 03:05:30 +08:00
* @return Response
*/
public function destroy($id)
{
$this->preventAccessForDemoUsers();
$this->checkPermissionOr('users-manage', function () use ($id) {
2015-08-29 22:03:42 +08:00
return $this->currentUser->id == $id;
});
2015-12-15 04:13:32 +08:00
$user = $this->userRepo->getById($id);
2015-12-15 04:13:32 +08:00
if ($this->userRepo->isOnlyAdmin($user)) {
session()->flash('error', 'You cannot delete the only admin');
return redirect($user->getEditUrl());
}
$this->userRepo->destroy($user);
session()->flash('success', 'User successfully removed');
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
* @param $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showProfilePage($id)
{
$user = $this->userRepo->getById($id);
$userActivity = $this->userRepo->getActivity($user);
2016-02-18 06:11:48 +08:00
$recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5, 0);
$assetCounts = $this->userRepo->getAssetCounts($user);
return view('users/profile', [
'user' => $user,
'activity' => $userActivity,
'recentlyCreated' => $recentlyCreated,
'assetCounts' => $assetCounts
]);
2015-08-09 03:05:30 +08:00
}
}