mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-11-25 17:57:28 +08:00
154 lines
3.8 KiB
PHP
154 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace Oxbow\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Oxbow\Http\Requests;
|
|
use Oxbow\User;
|
|
|
|
class UserController extends Controller
|
|
{
|
|
|
|
protected $user;
|
|
|
|
/**
|
|
* UserController constructor.
|
|
* @param $user
|
|
*/
|
|
public function __construct(User $user)
|
|
{
|
|
$this->user = $user;
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the users.
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function index()
|
|
{
|
|
$users = $this->user->all();
|
|
return view('users/index', ['users' => $users]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new user.
|
|
*
|
|
* @return Response
|
|
*/
|
|
public function create()
|
|
{
|
|
$this->checkPermission('user-create');
|
|
return view('users/create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created user in storage.
|
|
*
|
|
* @param Request $request
|
|
* @return Response
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$this->checkPermission('user-create');
|
|
$this->validate($request, [
|
|
'name' => 'required',
|
|
'email' => 'required|email',
|
|
'password' => 'required|min:5',
|
|
'password-confirm' => 'required|same:password',
|
|
'role' => 'required|exists:roles,id'
|
|
]);
|
|
|
|
$user = $this->user->fill($request->all());
|
|
$user->password = Hash::make($request->get('password'));
|
|
$user->save();
|
|
|
|
$user->attachRoleId($request->get('role'));
|
|
return redirect('/users');
|
|
}
|
|
|
|
|
|
/**
|
|
* Show the form for editing the specified user.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$this->checkPermissionOr('user-update', function () use ($id) {
|
|
return $this->currentUser->id == $id;
|
|
});
|
|
$user = $this->user->findOrFail($id);
|
|
return view('users/edit', ['user' => $user]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified user in storage.
|
|
*
|
|
* @param Request $request
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$this->checkPermissionOr('user-update', function () use ($id) {
|
|
return $this->currentUser->id == $id;
|
|
});
|
|
$this->validate($request, [
|
|
'name' => 'required',
|
|
'email' => 'required|email',
|
|
'password' => 'min:5',
|
|
'password-confirm' => 'same:password',
|
|
'role' => 'exists:roles,id'
|
|
]);
|
|
|
|
$user = $this->user->findOrFail($id);
|
|
$user->fill($request->all());
|
|
|
|
if ($this->currentUser->can('user-update') && $request->has('role')) {
|
|
$user->attachRoleId($request->get('role'));
|
|
}
|
|
|
|
if ($request->has('password') && $request->get('password') != '') {
|
|
$password = $request->get('password');
|
|
$user->password = Hash::make($password);
|
|
}
|
|
$user->save();
|
|
return redirect('/users');
|
|
}
|
|
|
|
/**
|
|
* Show the user delete page.
|
|
* @param $id
|
|
* @return \Illuminate\View\View
|
|
*/
|
|
public function delete($id)
|
|
{
|
|
$this->checkPermissionOr('user-delete', function () use ($id) {
|
|
return $this->currentUser->id == $id;
|
|
});
|
|
$user = $this->user->findOrFail($id);
|
|
return view('users/delete', ['user' => $user]);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified user from storage.
|
|
*
|
|
* @param int $id
|
|
* @return Response
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
$this->checkPermissionOr('user-delete', function () use ($id) {
|
|
return $this->currentUser->id == $id;
|
|
});
|
|
$user = $this->user->findOrFail($id);
|
|
$user->delete();
|
|
return redirect('/users');
|
|
}
|
|
}
|