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-22 12:53:00 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-06-10 06:25:21 +00:00
|
|
|
|
2020-06-08 13:08:46 +00:00
|
|
|
|
|
|
|
class UserController extends Controller
|
|
|
|
{
|
2020-06-15 06:59:15 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2020-06-16 06:28:37 +00:00
|
|
|
// $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-15 06:59:15 +00:00
|
|
|
}
|
|
|
|
|
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-22 12:53:00 +00:00
|
|
|
$users = User::query()->paginate($request->query("page", 20));
|
2020-06-10 06:25:21 +00:00
|
|
|
|
2020-06-19 06:15:24 +00:00
|
|
|
return view("admin.users.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-19 06:15:24 +00:00
|
|
|
return view("admin.users.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-22 12:53:00 +00:00
|
|
|
Log::debug("STORE FUNCTION");
|
|
|
|
|
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",
|
|
|
|
|
|
|
|
]);
|
|
|
|
|
2020-06-22 12:53:00 +00:00
|
|
|
Log::debug("FINISHED VALIDATION?");
|
|
|
|
|
2020-06-10 06:25:21 +00:00
|
|
|
$user = new User($data);
|
2020-06-22 12:53:00 +00:00
|
|
|
|
|
|
|
Log::debug("CREATED USER [NOT PERSISTED YET]");
|
|
|
|
|
2020-06-10 06:25:21 +00:00
|
|
|
$user->save();
|
|
|
|
|
2020-06-22 12:53:00 +00:00
|
|
|
Log::debug("SAVED USER");
|
|
|
|
|
2020-06-19 06:15:24 +00:00
|
|
|
return view("admin.users.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);
|
|
|
|
|
2020-06-19 06:15:24 +00:00
|
|
|
return view("admin.users.show", [
|
2020-06-10 06:25:21 +00:00
|
|
|
"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);
|
|
|
|
|
2020-06-19 06:15:24 +00:00
|
|
|
return view("admin.users.edit", [
|
2020-06-10 06:25:21 +00:00
|
|
|
"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();
|
|
|
|
}
|
|
|
|
|
2020-06-19 06:15:24 +00:00
|
|
|
return view("admin.users.edit", [
|
2020-06-10 10:30:44 +00:00
|
|
|
"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();
|
|
|
|
}
|
|
|
|
|
2020-06-19 06:15:24 +00:00
|
|
|
return view("admin.users.delete");
|
2020-06-08 13:08:46 +00:00
|
|
|
}
|
2020-06-10 06:25:21 +00:00
|
|
|
|
|
|
|
/*******************************************/
|
|
|
|
/* Authentication */
|
|
|
|
/*******************************************/
|
|
|
|
|
2020-06-16 06:13:24 +00:00
|
|
|
public function showLogin() {
|
2020-06-19 06:15:24 +00:00
|
|
|
return view("admin.users.login");
|
2020-06-16 06:13:24 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 06:25:21 +00:00
|
|
|
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-22 12:53:00 +00:00
|
|
|
return redirect()->route("users.index");
|
2020-06-10 06:25:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 06:13:24 +00:00
|
|
|
return redirect()->back(303);
|
2020-06-10 10:30:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|