Feature #2 : Role With Permission Selection

This commit is contained in:
Daeng Deni Mardaeni 2024-08-10 21:08:03 +07:00
parent 25db8db2f8
commit 18f0f68f1a
8 changed files with 146 additions and 52 deletions

View File

@ -73,7 +73,7 @@
]; ];
foreach ($data as $permission) { 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.'); return redirect()->route('users.permissions.index')->with('success', 'Permission created successfully.');

View File

@ -8,6 +8,8 @@
use Maatwebsite\Excel\Facades\Excel; use Maatwebsite\Excel\Facades\Excel;
use Modules\Usermanagement\Exports\RolesExport; use Modules\Usermanagement\Exports\RolesExport;
use Modules\Usermanagement\Http\Requests\RoleRequest; use Modules\Usermanagement\Http\Requests\RoleRequest;
use Modules\Usermanagement\Models\Permission;
use Modules\Usermanagement\Models\PermissionGroup;
use Modules\Usermanagement\Models\Role; use Modules\Usermanagement\Models\Role;
/** /**
@ -72,11 +74,31 @@
//abort(403, 'Sorry! You are not allowed to store roles.'); //abort(403, 'Sorry! You are not allowed to store roles.');
} }
// Create a new role using the provided request data $validated = $request->validated();
Role::create($request->all());
// Redirect back to the roles index with a success message if($validated){
return redirect()->route('users.roles.index')->with('success', 'Role created successfully.'); 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.'); //abort(403, 'Sorry! You are not allowed to create roles.');
} }
$permissiongroups = PermissionGroup::all();
// Return the view for creating a new role // 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 // Fetch the specified role from the database
$role = Role::find($id); $role = Role::find($id);
// Return the view for displaying the role // Return the view for displaying the role
return view('usermanagement::roles.show', compact('role')); return view('usermanagement::roles.show', compact('role'));
} }
@ -135,9 +160,10 @@
// Fetch the specified role from the database // Fetch the specified role from the database
$role = Role::find($id); $role = Role::find($id);
$permissions = Permission::all();
$permissiongroups = PermissionGroup::all();
// Return the view for editing the role // 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.'); //abort(403, 'Sorry! You are not allowed to update roles.');
} }
// Fetch the specified role from the database $validated = $request->validated();
$role = Role::find($id); 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 $permissions = $request->input('permissions');
$role->update($request->all()); $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.');
}
}
} }
/** /**

View File

@ -47,7 +47,6 @@
$data = []; $data = [];
if ($permission) { if ($permission) {
$roles = Role::all(); $roles = Role::all();
foreach ($roles as $role) { foreach ($roles as $role) {

View File

@ -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;
});
});
}

View File

@ -92,7 +92,7 @@
function deleteData(data) { function deleteData(data) {
Swal.fire({ Swal.fire({
title: 'Are you sure?', 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', icon: 'warning',
showCancelButton: true, showCancelButton: true,
confirmButtonColor: '#3085d6', confirmButtonColor: '#3085d6',

View File

@ -7,42 +7,80 @@
@section('content') @section('content')
<div class="w-full grid gap-5 lg:gap-7.5 mx-auto"> <div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
@if(isset($role->id)) @if(isset($role->id))
<form action="{{ route('users.roles.update', $role->id) }}" method="POST"> <form action="{{ route('users.roles.update', $role->id) }}" method="POST" id="role_form">
<input type="hidden" name="id" value="{{ $role->id }}"> <input type="hidden" name="id" value="{{ $role->id }}">
@method('PUT') @method('PUT')
@else @else
<form method="POST" action="{{ route('users.roles.store') }}"> <form method="POST" action="{{ route('users.roles.store') }}">
@endif @endif
@csrf @csrf
<div class="card pb-2.5"> <div class="card pb-2.5">
<div class="card-header" id="basic_settings"> <div class="card-header" id="basic_settings">
<h3 class="card-title"> <h3 class="card-title">
{{ isset($role->id) ? 'Edit' : 'Add' }} Role {{ isset($role->id) ? 'Edit' : 'Add' }} Role
</h3> </h3>
<div class="flex items-center gap-2"> <div class="flex items-center gap-2">
<a href="{{ route('users.roles.index') }}" class="btn btn-xs btn-info">Back</a> <a href="{{ route('users.roles.index') }}" class="btn btn-xs btn-info">Back</a>
</div> </div>
</div> </div>
<div class="card-body grid gap-5"> <div class="card-body grid gap-5">
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
Name
</label>
<div class="flex flex-wrap items-baseline w-full">
<input class="input @error('name') border-danger @enderror" type="text" name="name" value="{{ $role->name ?? '' }}">
@error('name')
<em class="alert text-danger text-sm">{{ $message }}</em>
@enderror
</div>
</div>
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
Administrator/Superuser Access
</label>
<div class="flex flex-wrap items-baseline w-full">
<label class="switch">
<input name="check" id="select_all" type="checkbox" value="1"/>
<span class="switch-label">
Select All
</span>
</label>
</div>
</div>
@foreach($permissiongroups as $group)
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label class="form-label max-w-56">
{{ ucwords($group->name) }}
</label>
<div class="flex flex-wrap items-baseline w-full gap-2.5">
@foreach($group->getpermissionsByGroupId($group->id) as $permission)
<label class="switch">
@if(isset($role))
<input type="checkbox" value="{{ $permission->id }}" name="permissions[]" {{ $role->hasPermissionTo($permission->name) ? 'checked' : null }} />
@else
<input type="checkbox" value="{{ $permission->id }}" name="permissions[]"/>
@endif
@php
$permission_name = explode('.',$permission->name);
@endphp
<div class="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5"> <span class="switch-label">
<label class="form-label max-w-56"> {{ ucwords($permission_name[1]) }}
Name </span>
</label> </label>
<div class="flex flex-wrap items-baseline w-full"> @endforeach
<input class="input @error('name') border-danger @enderror" type="text" name="name" value="{{ $role->name ?? '' }}"> </div>
@error('name') </div>
<em class="alert text-danger text-sm">{{ $message }}</em> @endforeach
@enderror
<div class="flex justify-end">
<button type="submit" class="btn btn-primary">
Save
</button>
</div>
</div>
</div> </div>
</div> </form>
<div class="flex justify-end">
<button type="submit" class="btn btn-primary">
Save
</button>
</div>
</div>
</div>
</form>
</div> </div>
@endsection @endsection

View File

@ -88,7 +88,7 @@
function deleteData(data) { function deleteData(data) {
Swal.fire({ Swal.fire({
title: 'Are you sure?', 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', icon: 'warning',
showCancelButton: true, showCancelButton: true,
confirmButtonColor: '#3085d6', confirmButtonColor: '#3085d6',

View File

@ -92,7 +92,7 @@
function deleteData(data) { function deleteData(data) {
Swal.fire({ Swal.fire({
title: 'Are you sure?', 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', icon: 'warning',
showCancelButton: true, showCancelButton: true,
confirmButtonColor: '#3085d6', confirmButtonColor: '#3085d6',