2020-06-30 09:35:32 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Http\Response;
|
2020-07-27 14:03:49 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-09-02 06:56:02 +00:00
|
|
|
use Spatie\Permission\Models\Permission;
|
2020-06-30 09:35:32 +00:00
|
|
|
use Spatie\Permission\Models\Role;
|
2020-08-27 13:29:59 +00:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2020-06-30 09:35:32 +00:00
|
|
|
|
2020-08-31 07:36:10 +00:00
|
|
|
class RolesController extends Controller
|
2020-06-30 09:35:32 +00:00
|
|
|
{
|
2020-07-29 09:53:37 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware([ "auth" ]);
|
2020-08-06 13:31:38 +00:00
|
|
|
$this->middleware([ "lang" ]);
|
2020-07-29 09:53:37 +00:00
|
|
|
|
2020-08-31 07:36:10 +00:00
|
|
|
$this->middleware([ "check.auth:roles.show" ])->only("show", "index");
|
2020-07-29 09:53:37 +00:00
|
|
|
$this->middleware([ "check.auth:roles.create" ])->only("create", "store");
|
|
|
|
$this->middleware([ "check.auth:roles.edit" ])->only("edit", "update");
|
|
|
|
$this->middleware([ "check.auth:roles.delete" ])->only("delete");
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:35:32 +00:00
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2020-09-18 08:04:33 +00:00
|
|
|
$roles = Role::query()->where("id", "!=", 1)->where("id", "!=", 2)->get();
|
2020-06-30 09:35:32 +00:00
|
|
|
|
2020-06-30 09:51:20 +00:00
|
|
|
return Response::detect("roles.index", [ "roles" => $roles]);
|
2020-06-30 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for creating a new resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
return Response::detect("roles.create");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$requestRole = $request->validate([
|
2020-06-30 09:55:11 +00:00
|
|
|
"name" => "required|max:255",
|
|
|
|
"description" => "required|max:255"
|
2020-06-30 09:35:32 +00:00
|
|
|
]);
|
|
|
|
|
2020-08-27 13:29:59 +00:00
|
|
|
$brugerPermissions = new Collection();
|
2020-08-28 06:28:32 +00:00
|
|
|
if ($request->value !== null) {
|
2020-08-27 13:29:59 +00:00
|
|
|
foreach ($request->value as $valuee) {
|
|
|
|
$brugerPermissions->add(\Spatie\Permission\Models\Permission::query()->where("name", "=", $valuee)->first());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:35:32 +00:00
|
|
|
$role = new Role($requestRole);
|
2020-07-01 07:16:53 +00:00
|
|
|
|
2020-08-04 07:34:56 +00:00
|
|
|
$allRoles = Role::query()->where('name', '=', $request->name)->get();
|
|
|
|
|
|
|
|
// If there already is a role with that name, then don't create
|
|
|
|
if(count($allRoles) > 0)
|
2020-08-17 08:21:02 +00:00
|
|
|
return redirect()->route("roles.store");
|
2020-08-04 07:34:56 +00:00
|
|
|
else { // Else - Create it
|
|
|
|
$role->save();
|
2020-08-27 13:29:59 +00:00
|
|
|
|
|
|
|
if (!$brugerPermissions->isEmpty()) {
|
|
|
|
foreach ($brugerPermissions as $permission) {
|
|
|
|
\Spatie\Permission\Models\Role::query()->find($role->id)->givePermissionTo($permission);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 08:45:27 +00:00
|
|
|
$roles = Role::query()->get();
|
2020-08-04 07:34:56 +00:00
|
|
|
return redirect()->route("roles.index", ['roles' => $roles]);
|
2020-07-01 07:16:53 +00:00
|
|
|
}
|
2020-06-30 09:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
return Response::detect("roles.show", [ "role" => $id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form for editing the specified resource.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function edit($id)
|
|
|
|
{
|
2020-09-22 06:56:03 +00:00
|
|
|
//Disables editing of roles: Administrator & Bruger
|
2020-09-10 08:39:07 +00:00
|
|
|
if($id == 1 || $id == 2)
|
2020-09-07 06:28:49 +00:00
|
|
|
return redirect()->route("roles.index");
|
|
|
|
|
2020-06-30 09:35:32 +00:00
|
|
|
$role = Role::find($id);
|
|
|
|
return Response::detect("roles.edit", ["role" => $role]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
2020-09-10 08:39:07 +00:00
|
|
|
if($id == 1 || $id == 2)
|
2020-09-07 06:28:49 +00:00
|
|
|
return redirect()->route("roles.index");
|
|
|
|
|
2020-09-02 06:56:02 +00:00
|
|
|
$data = $request->validate([
|
|
|
|
"name" => "required|max:255",
|
|
|
|
"description" => "required|max:255"
|
|
|
|
]);
|
2020-06-30 10:02:32 +00:00
|
|
|
$role = Role::find($id);
|
2020-07-01 07:16:53 +00:00
|
|
|
|
2020-09-02 06:56:02 +00:00
|
|
|
$brugerPermissions = new Collection();
|
|
|
|
if ($request->value !== null) {
|
|
|
|
foreach ($request->value as $valuee) {
|
|
|
|
$brugerPermissions->add(Permission::query()->where("name", "=", $valuee)->first());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete all permission to the role you're editing, so you can give new ones
|
|
|
|
foreach (Permission::query()->get() as $permission) {
|
|
|
|
Role::query()->find($id)->revokePermissionTo($permission);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give new permissions to the tole you're editing
|
|
|
|
if (!$brugerPermissions->isEmpty()) {
|
|
|
|
foreach ($brugerPermissions as $permission) {
|
|
|
|
Role::query()->find($id)->givePermissionTo($permission);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 07:34:56 +00:00
|
|
|
$allRoles = Role::query()->where('name', '=', $request->name)->where('id', '!=', $id)->get();
|
|
|
|
|
2020-09-22 06:56:03 +00:00
|
|
|
//Check if role already exist if not save
|
2020-08-04 07:34:56 +00:00
|
|
|
if(count($allRoles) > 0){
|
2020-08-17 08:21:02 +00:00
|
|
|
return redirect()->route("roles.index", [ "role" => $role ]);
|
2020-07-01 07:16:53 +00:00
|
|
|
}else{
|
2020-08-04 07:34:56 +00:00
|
|
|
$role->update($data);
|
|
|
|
$role->save();
|
2020-09-02 06:56:02 +00:00
|
|
|
|
2020-09-22 08:45:27 +00:00
|
|
|
$roles = Role::query()->get();
|
2020-08-04 07:34:56 +00:00
|
|
|
return redirect()->route("roles.index", ['roles' => $roles]);
|
2020-07-01 07:16:53 +00:00
|
|
|
}
|
2020-06-30 09:35:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @param int $id
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
2020-09-22 06:56:03 +00:00
|
|
|
//Disables deletion of roles: Administrator & Bruger
|
2020-09-10 08:39:07 +00:00
|
|
|
if($id == 1 || $id == 2)
|
2020-09-07 06:28:49 +00:00
|
|
|
return redirect()->route("roles.index");
|
|
|
|
|
2020-06-30 10:02:32 +00:00
|
|
|
$role = Role::find($id);
|
2020-06-30 09:35:32 +00:00
|
|
|
$role->delete();
|
|
|
|
return redirect()->route("roles.index");
|
|
|
|
}
|
2020-07-27 14:03:49 +00:00
|
|
|
|
2020-09-22 06:56:03 +00:00
|
|
|
//Used for checking if the currently typed role name is unique. Create version
|
2020-08-14 09:22:51 +00:00
|
|
|
public function nameCheck(Request $request){
|
|
|
|
$roles = Role::query()->where('name', 'LIKE',$request->nameCheck)->get();
|
|
|
|
if(count($roles) > 0 && $request->nameCheck !== NULL){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 06:56:03 +00:00
|
|
|
//Used for checking if the currently typed role name is unique. Edit version
|
2020-08-17 08:21:02 +00:00
|
|
|
public function nameCheckUpdate(Request $request){
|
|
|
|
$roles = Role::query()->where('name', 'LIKE',$request->nameCheck)->where('id', '!=', $request->id)->get();
|
|
|
|
if(count($roles) > 0 && $request->nameCheck !== NULL){
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 09:35:32 +00:00
|
|
|
}
|