From 18f0f68f1a605d109eeaab37431aaf917a6c3a80 Mon Sep 17 00:00:00 2001 From: Daeng Deni Mardaeni Date: Sat, 10 Aug 2024 21:08:03 +0700 Subject: [PATCH] Feature #2 : Role With Permission Selection --- .../Controllers/PermissionsController.php | 2 +- app/Http/Controllers/RolesController.php | 70 +++++++++--- app/Models/PermissionGroup.php | 1 - resources/assets/js/app.js | 13 +++ resources/views/permissions/index.blade.php | 2 +- resources/views/roles/create.blade.php | 106 ++++++++++++------ resources/views/roles/index.blade.php | 2 +- resources/views/users/index.blade.php | 2 +- 8 files changed, 146 insertions(+), 52 deletions(-) diff --git a/app/Http/Controllers/PermissionsController.php b/app/Http/Controllers/PermissionsController.php index 68a7caa..caface3 100644 --- a/app/Http/Controllers/PermissionsController.php +++ b/app/Http/Controllers/PermissionsController.php @@ -73,7 +73,7 @@ ]; foreach ($data as $permission) { - Permission::create(['name' => $permission,'guard_name' => 'web', 'group_id' => $group->id]); + Permission::create(['name' => $permission,'guard_name' => 'web', 'permission_group_id' => $group->id]); } return redirect()->route('users.permissions.index')->with('success', 'Permission created successfully.'); diff --git a/app/Http/Controllers/RolesController.php b/app/Http/Controllers/RolesController.php index e8bb944..93fd8ff 100644 --- a/app/Http/Controllers/RolesController.php +++ b/app/Http/Controllers/RolesController.php @@ -8,6 +8,8 @@ use Maatwebsite\Excel\Facades\Excel; use Modules\Usermanagement\Exports\RolesExport; use Modules\Usermanagement\Http\Requests\RoleRequest; + use Modules\Usermanagement\Models\Permission; + use Modules\Usermanagement\Models\PermissionGroup; use Modules\Usermanagement\Models\Role; /** @@ -72,11 +74,31 @@ //abort(403, 'Sorry! You are not allowed to store roles.'); } - // Create a new role using the provided request data - Role::create($request->all()); + $validated = $request->validated(); - // Redirect back to the roles index with a success message - return redirect()->route('users.roles.index')->with('success', 'Role created successfully.'); + if($validated){ + try{ + // If no errors, save the role to the database + $role = Role::create($validated); + + $permissions = $request->input('permissions'); + $permissions = Permission::whereIn('id', $permissions)->pluck('name')->toArray(); + if (!empty($permissions)) { + $role = Role::find($role->id); + try{ + $role->syncPermissions($permissions); + } catch (\Exception $e) { + echo json_encode(['message' => $e->getMessage(), 'success']);exit; + } + } + + // Redirect back to the roles index with a success message + return redirect()->route('users.roles.index')->with('success', 'Role created successfully.'); + } catch (\Exception $e) { + // Redirect back to the roles index with an error message + return redirect()->route('users.roles.index')->with('error', 'Failed to create role. Please try again.'); + } + } } /** @@ -92,8 +114,9 @@ //abort(403, 'Sorry! You are not allowed to create roles.'); } + $permissiongroups = PermissionGroup::all(); // Return the view for creating a new role - return view('usermanagement::roles.create'); + return view('usermanagement::roles.create',compact('permissiongroups')); } /** @@ -114,6 +137,8 @@ // Fetch the specified role from the database $role = Role::find($id); + + // Return the view for displaying the role return view('usermanagement::roles.show', compact('role')); } @@ -135,9 +160,10 @@ // Fetch the specified role from the database $role = Role::find($id); - + $permissions = Permission::all(); + $permissiongroups = PermissionGroup::all(); // Return the view for editing the role - return view('usermanagement::roles.create', compact('role')); + return view('usermanagement::roles.create', compact('role','permissions','permissiongroups')); } @@ -158,14 +184,32 @@ //abort(403, 'Sorry! You are not allowed to update roles.'); } - // Fetch the specified role from the database - $role = Role::find($id); + $validated = $request->validated(); + if($validated){ + try{ + // If no errors, update the role in the database + $role = Role::find($id); + $role->update($request->all()); - // Update the role using the provided request data - $role->update($request->all()); + $permissions = $request->input('permissions'); + $permissions = Permission::whereIn('id', $permissions)->pluck('name')->toArray(); + if (!empty($permissions)) { + $role = Role::find($role->id); + try{ + $role->syncPermissions($permissions); + } catch (\Exception $e) { + echo json_encode(['message' => $e->getMessage(), 'success']);exit; + } - // Redirect back to the roles index with a success message - return redirect()->route('users.roles.index')->with('success', 'Role updated successfully.'); + } + + // Redirect back to the roles index with a success message + return redirect()->route('users.roles.index')->with('success', 'Role updated successfully.'); + } catch (\Exception $e) { + // Redirect back to the roles index with an error message + return redirect()->route('users.roles.index')->with('error', 'Failed to update role. Please try again.'); + } + } } /** diff --git a/app/Models/PermissionGroup.php b/app/Models/PermissionGroup.php index 0d22b8f..ccea056 100644 --- a/app/Models/PermissionGroup.php +++ b/app/Models/PermissionGroup.php @@ -47,7 +47,6 @@ $data = []; if ($permission) { - $roles = Role::all(); foreach ($roles as $role) { diff --git a/resources/assets/js/app.js b/resources/assets/js/app.js index e69de29..a2198ab 100644 --- a/resources/assets/js/app.js +++ b/resources/assets/js/app.js @@ -0,0 +1,13 @@ +const selectAll = document.querySelector('#select_all'); + +if (selectAll) { + const allCheckboxes = document.querySelectorAll('[type="checkbox"]'); + + // Handle check state + selectAll.addEventListener('change', e => { + // Apply check state to all checkboxes + allCheckboxes.forEach(c => { + c.checked = e.target.checked; + }); + }); +} diff --git a/resources/views/permissions/index.blade.php b/resources/views/permissions/index.blade.php index 18e0f5c..55acb2b 100644 --- a/resources/views/permissions/index.blade.php +++ b/resources/views/permissions/index.blade.php @@ -92,7 +92,7 @@ function deleteData(data) { Swal.fire({ title: 'Are you sure?', - text: "You won't be able to revert this!" + data, + text: "You won't be able to revert this!" , icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', diff --git a/resources/views/roles/create.blade.php b/resources/views/roles/create.blade.php index 161061e..c9f0879 100644 --- a/resources/views/roles/create.blade.php +++ b/resources/views/roles/create.blade.php @@ -7,42 +7,80 @@ @section('content')
@if(isset($role->id)) -
+ - @method('PUT') - @else - - @endif - @csrf -
-
-

- {{ isset($role->id) ? 'Edit' : 'Add' }} Role -

-
- Back -
-
-
+ @method('PUT') + @else + + @endif + @csrf +
+
+

+ {{ isset($role->id) ? 'Edit' : 'Add' }} Role +

+
+ Back +
+
+
+
+ +
+ + @error('name') + {{ $message }} + @enderror +
+
+
+ +
+ +
+
+ @foreach($permissiongroups as $group) +
+ +
+ @foreach($group->getpermissionsByGroupId($group->id) as $permission) +
-
-
- -
-
-
- +
@endsection diff --git a/resources/views/roles/index.blade.php b/resources/views/roles/index.blade.php index 4020501..7a3fb09 100644 --- a/resources/views/roles/index.blade.php +++ b/resources/views/roles/index.blade.php @@ -88,7 +88,7 @@ function deleteData(data) { Swal.fire({ title: 'Are you sure?', - text: "You won't be able to revert this!" + data, + text: "You won't be able to revert this!" , icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6', diff --git a/resources/views/users/index.blade.php b/resources/views/users/index.blade.php index 7f52ba7..31d7268 100644 --- a/resources/views/users/index.blade.php +++ b/resources/views/users/index.blade.php @@ -92,7 +92,7 @@ function deleteData(data) { Swal.fire({ title: 'Are you sure?', - text: "You won't be able to revert this!" + data, + text: "You won't be able to revert this!" , icon: 'warning', showCancelButton: true, confirmButtonColor: '#3085d6',