Ekapp/skolehjem/app/Http/Controllers/UserController.php

180 lines
4.8 KiB
PHP
Raw Normal View History

2020-06-08 13:08:46 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
2020-06-10 06:25:21 +00:00
use App\User;
use Illuminate\Support\Facades\Auth;
2020-06-08 13:08:46 +00:00
class UserController extends Controller
{
2020-06-15 06:59:15 +00:00
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");
}
2020-06-08 13:08:46 +00:00
/**
* Display a listing of the resource.
*
2020-06-10 06:25:21 +00:00
* @param Request $request
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
2020-06-10 06:25:21 +00:00
public function index(Request $request)
2020-06-08 13:08:46 +00:00
{
2020-06-10 06:25:21 +00:00
$users = User::query()->paginate($request->query("page", 1));
return view("user.index", [ "users" => $users ]);
2020-06-08 13:08:46 +00:00
}
/**
* Show the form for creating a new resource.
*
2020-06-10 06:25:21 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function create()
{
2020-06-10 06:25:21 +00:00
return view("user.create");
2020-06-08 13:08:46 +00:00
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
2020-06-10 06:25:21 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function store(Request $request)
{
2020-06-10 06:25:21 +00:00
$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();
2020-06-10 10:30:44 +00:00
return view("user.store");
2020-06-08 13:08:46 +00:00
}
/**
* Display the specified resource.
*
* @param int $id
2020-06-10 06:25:21 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function show($id)
{
2020-06-10 06:25:21 +00:00
$user = User::find($id);
return view("user.show", [
"user" => $user
]);
2020-06-08 13:08:46 +00:00
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
2020-06-10 06:25:21 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function edit($id)
{
2020-06-10 06:25:21 +00:00
$user = User::find($id);
return view("user.edit", [
"user" => $user
]);
2020-06-08 13:08:46 +00:00
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
2020-06-10 10:30:44 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function update(Request $request, $id)
{
2020-06-10 10:30:44 +00:00
$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
]);
2020-06-08 13:08:46 +00:00
}
/**
* Remove the specified resource from storage.
*
* @param int $id
2020-06-10 10:30:44 +00:00
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
2020-06-08 13:08:46 +00:00
*/
public function destroy($id)
{
2020-06-10 10:30:44 +00:00
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");
2020-06-08 13:08:46 +00:00
}
2020-06-10 06:25:21 +00:00
/*******************************************/
/* Authentication */
/*******************************************/
public function login(Request $request) {
$data = $request->only("email", "password");
if(Auth::attempt($data)) {
2020-06-10 10:30:44 +00:00
//TODO: Implement home?
2020-06-10 06:25:21 +00:00
return view("user.home", [ "user" => Auth::user() ]);
}
2020-06-10 10:30:44 +00:00
return redirect()->back(403);
}
public function logout(Request $request) {
Auth::logout();
return redirect()->to("/");
2020-06-10 06:25:21 +00:00
}
2020-06-08 13:08:46 +00:00
}