|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Admin; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Http\Requests\admin\roles\StoreRoleRequest; |
| 7 | +use App\Http\Requests\admin\roles\UpdateRoleRequest; |
| 8 | +use App\Models\User; |
| 9 | +use Illuminate\Http\RedirectResponse; |
| 10 | +use Illuminate\Http\Request; |
| 11 | +use Illuminate\Support\Facades\Auth; |
| 12 | +use Illuminate\Support\Facades\Gate; |
| 13 | +use Inertia\Inertia; |
| 14 | +use Spatie\Permission\Models\Permission; |
| 15 | +use Spatie\Permission\Models\Role; |
| 16 | +use Symfony\Component\HttpFoundation\Response; |
| 17 | + |
| 18 | +class RolesController extends Controller |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Display a listing of the resource. |
| 22 | + * |
| 23 | + * @return \Inertia\Response |
| 24 | + */ |
| 25 | + public function index() |
| 26 | + { |
| 27 | + abort_if(!Auth::user()->can('role_access'), Response::HTTP_FORBIDDEN, '403 Forbidden'); |
| 28 | + |
| 29 | + $roles = Role::with(['permissions'])->get(); |
| 30 | + return Inertia::render('admin/roles/index', [ |
| 31 | + 'roles' => $roles |
| 32 | + ]); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Show the form for creating a new resource. |
| 38 | + * |
| 39 | + * @return \Illuminate\Http\Response |
| 40 | + */ |
| 41 | + public function create() |
| 42 | + { |
| 43 | + abort_if(!Auth::user()->can('user_create'), Response::HTTP_FORBIDDEN, '403 Forbidden'); |
| 44 | + |
| 45 | + $permissions = Permission::select('id AS value' , 'name AS label')->get(); |
| 46 | + |
| 47 | + return Inertia::render('admin/roles/create', [ |
| 48 | + 'permissions' => $permissions |
| 49 | + ]); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Store a newly created resource in storage. |
| 54 | + * |
| 55 | + * @param StoreRoleRequest $request |
| 56 | + * @return RedirectResponse |
| 57 | + */ |
| 58 | + public function store(StoreRoleRequest $request) |
| 59 | + { |
| 60 | + $permissions = $request->get('permissions'); |
| 61 | + $role = Role::create(['name' => $request->get('name')]); |
| 62 | + $role->syncPermissions($permissions); |
| 63 | + return redirect()->route('admin.roles.index'); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Display the specified resource. |
| 68 | + * |
| 69 | + * @param int $id |
| 70 | + * @return \Illuminate\Http\Response |
| 71 | + */ |
| 72 | + public function show($id) |
| 73 | + { |
| 74 | + abort_if(!Auth::user()->can('role_show'), Response::HTTP_FORBIDDEN, '403 Forbidden'); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Show the form for editing the specified resource. |
| 79 | + * |
| 80 | + * @param int $id |
| 81 | + * @return \Inertia\Response |
| 82 | + */ |
| 83 | + public function edit(Role $role) |
| 84 | + { |
| 85 | + abort_if(!Auth::user()->can('role_edit'), Response::HTTP_FORBIDDEN, '403 Forbidden'); |
| 86 | + $permissions = Permission::select('id AS value' , 'name AS label')->get(); |
| 87 | + |
| 88 | + $role->load('permissions'); |
| 89 | + return Inertia::render('admin/roles/edit',[ |
| 90 | + 'role' => $role, |
| 91 | + 'permissions' => $permissions, |
| 92 | + ]); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Update the specified resource in storage. |
| 97 | + * |
| 98 | + * @param Request $request |
| 99 | + * @param int $id |
| 100 | + * @return \Illuminate\Http\Response |
| 101 | + */ |
| 102 | + public function update(UpdateRoleRequest $request, Role $role) |
| 103 | + { |
| 104 | + $permissions = $request->get('permissions'); |
| 105 | + $role->update([ |
| 106 | + 'name' => $request->get('name'), |
| 107 | + ]); |
| 108 | + $role->syncPermissions($permissions); |
| 109 | + return redirect()->route('admin.roles.index'); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Remove the specified resource from storage. |
| 114 | + * |
| 115 | + * @param int $id |
| 116 | + * @return \Illuminate\Http\Response |
| 117 | + */ |
| 118 | + public function destroy(Role $role) |
| 119 | + { |
| 120 | + abort_if(!Auth::user()->can('role_delete'), Response::HTTP_FORBIDDEN, '403 Forbidden'); |
| 121 | + $users = User::role($role->name)->count(); |
| 122 | + |
| 123 | + if ($users) { |
| 124 | + return back()->with('error', 'This role assined to users , please change users role before delete it'); |
| 125 | + } |
| 126 | + $role->delete(); |
| 127 | + return back(); |
| 128 | + } |
| 129 | +} |
0 commit comments