127 lines
3.4 KiB
PHP
127 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\StoreRoleRequest;
|
|
use App\Http\Requests\UpdateRoleRequest;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class RolesController extends Controller
|
|
{
|
|
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth');
|
|
$this->middleware('permission:create-role|edit-role|delete-role', ['only' => ['index', 'show']]);
|
|
$this->middleware('permission:create-role', ['only' => ['create', 'store']]);
|
|
$this->middleware('permission:edit-role', ['only' => ['edit', 'update']]);
|
|
$this->middleware('permission:delete-role', ['only' => ['destroy']]);
|
|
}
|
|
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
return view('roles.index', [
|
|
'roles' => Role::orderBy('id', 'DESC')->paginate(3)
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('roles.create', [
|
|
'permissions' => Permission::get()
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StoreRoleRequest $request)
|
|
{
|
|
$role = Role::create(['name' => $request->name]);
|
|
|
|
$permissions = Permission::whereIn('id', $request->permissions)->get(['name'])->toArray();
|
|
|
|
$role->syncPermissions($permissions);
|
|
|
|
return redirect()->route('roles.index')
|
|
->withSuccess('New role is added successfully.');
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Role $role)
|
|
{
|
|
$rolePermissions = Permission::join("role_has_permissions", "permission_id", "=", "id")
|
|
->where("role_id", $role->id)
|
|
->select('name')
|
|
->get();
|
|
return view('roles.show', [
|
|
'role' => $role,
|
|
'rolePermissions' => $rolePermissions
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Role $role)
|
|
{
|
|
if ($role->name == 'ADMIN') {
|
|
abort(403, 'ADMIN ROLE CAN NOT BE EDITED');
|
|
}
|
|
|
|
$rolePermissions = DB::table("role_has_permissions")->where("role_id", $role->id)
|
|
->pluck('permission_id')
|
|
->all();
|
|
|
|
return view('roles.edit', [
|
|
'role' => $role,
|
|
'permissions' => Permission::get(),
|
|
'rolePermissions' => $rolePermissions
|
|
]);
|
|
}
|
|
|
|
public function update(UpdateRoleRequest $request, Role $role)
|
|
{
|
|
$input = $request->only('name');
|
|
|
|
$role->update($input);
|
|
|
|
$permissions = Permission::whereIn('id', $request->permissions)->get(['name'])->toArray();
|
|
|
|
$role->syncPermissions($permissions);
|
|
|
|
return redirect()->back()
|
|
->withSuccess('Role is updated successfully.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Role $role)
|
|
{
|
|
if($role->name=='ADMIn'){
|
|
abort(403, 'ADMIN ROLE CAN NOT BE DELETED');
|
|
}
|
|
if(auth()->user()->hasRole($role->name)){
|
|
abort(403, 'CAN NOT DELETE SELF ASSIGNED ROLE');
|
|
}
|
|
$role->delete();
|
|
return redirect()->route('roles.index')
|
|
->withSuccess('Role is deleted successfully.');
|
|
}
|
|
}
|