diff --git a/skolehjem/app/Http/Controllers/RolesController.php b/skolehjem/app/Http/Controllers/RolesController.php index baf642b..29aca2c 100644 --- a/skolehjem/app/Http/Controllers/RolesController.php +++ b/skolehjem/app/Http/Controllers/RolesController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Support\Facades\DB; +use Spatie\Permission\Models\Permission; use Spatie\Permission\Models\Role; use Illuminate\Database\Eloquent\Collection; @@ -117,9 +118,31 @@ class RolesController extends Controller */ public function update(Request $request, $id) { - $data = $request->all(); + $data = $request->validate([ + "name" => "required|max:255", + "description" => "required|max:255" + ]); $role = Role::find($id); + $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); + } + } + $allRoles = Role::query()->where('name', '=', $request->name)->where('id', '!=', $id)->get(); if(count($allRoles) > 0){ @@ -127,6 +150,7 @@ class RolesController extends Controller }else{ $role->update($data); $role->save(); + $roles = Role::query()->paginate($request->input("limit", 20)); return redirect()->route("roles.index", ['roles' => $roles]); } diff --git a/skolehjem/resources/views/admin/contacts/create.blade.php b/skolehjem/resources/views/admin/contacts/create.blade.php index 2244a48..e46e6ba 100644 --- a/skolehjem/resources/views/admin/contacts/create.blade.php +++ b/skolehjem/resources/views/admin/contacts/create.blade.php @@ -36,7 +36,7 @@ @endsection