180 lines
4.8 KiB
PHP
180 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\User;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
class UserController extends Controller
|
|
{
|
|
public function __construct()
|
|
{
|
|
$this->middleware([ "auth" ])->only("logout");
|
|
$this->middleware([ "guest" ])->only("login");
|
|
|
|
$this->middleware([ "permission:user.list", "role:admin" ])->only("index");
|
|
$this->middleware([ "permission:user.show", "role:admin" ])->only("show");
|
|
$this->middleware([ "permission:user.edit", "role:admin" ])->only([ "edit", "update" ]);
|
|
$this->middleware([ "permission:user.delete", "role:admin" ])->only("delete");
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$users = User::query()->paginate($request->query("page", 1));
|
|
|
|
return view("user.index", [ "users" => $users ]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function create()
|
|
{
|
|
return view("user.create");
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
"name_first" => "required|max:255",
|
|
"name_last" => "required|max:255",
|
|
"email" => "required|email|unique:users",
|
|
"password" => "required|max:60",
|
|
"phone" => "required|unique:users",
|
|
|
|
]);
|
|
|
|
$user = new User($data);
|
|
$user->save();
|
|
|
|
return view("user.store");
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function show($id)
|
|
{
|
|
$user = User::find($id);
|
|
|
|
return view("user.show", [
|
|
"user" => $user
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function edit($id)
|
|
{
|
|
$user = User::find($id);
|
|
|
|
return view("user.edit", [
|
|
"user" => $user
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*
|
|
* @param \Illuminate\Http\Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$data = $request->validate([
|
|
"name_first" => "max:255",
|
|
"name_last" => "max:255",
|
|
"email" => "email|unique:users",
|
|
"password" => "max:60",
|
|
"phone" => "unique:users",
|
|
]);
|
|
|
|
// Validates if the user is updating itself or another user.
|
|
if($id === Auth::id()) {
|
|
$user = Auth::user();
|
|
|
|
$user->update($data);
|
|
|
|
$user->save();
|
|
}
|
|
else if(Auth::user()->hasPermissionTo("user.edit")) {
|
|
$user = User::find($id);
|
|
|
|
/** @var User $user */
|
|
$user->update($data);
|
|
|
|
$user->save();
|
|
}
|
|
|
|
return view("user.edit", [
|
|
"user" => $user
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*
|
|
* @param int $id
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
|
|
*/
|
|
public function destroy($id)
|
|
{
|
|
if($id === Auth::id()) {
|
|
$user = Auth::user();
|
|
$user->delete();
|
|
}
|
|
else if(Auth::user()->hasPermissionTo("user.delete")) {
|
|
$user = User::find($id);
|
|
|
|
$user->delete();
|
|
}
|
|
|
|
return view("user.delete");
|
|
}
|
|
|
|
/*******************************************/
|
|
/* Authentication */
|
|
/*******************************************/
|
|
|
|
public function login(Request $request) {
|
|
$data = $request->only("email", "password");
|
|
|
|
if(Auth::attempt($data)) {
|
|
//TODO: Implement home?
|
|
return view("user.home", [ "user" => Auth::user() ]);
|
|
}
|
|
|
|
return redirect()->back(403);
|
|
}
|
|
|
|
public function logout(Request $request) {
|
|
Auth::logout();
|
|
|
|
return redirect()->to("/");
|
|
}
|
|
}
|