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) {
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.');

View File

@ -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.');
}
}
}
/**

View File

@ -47,7 +47,6 @@
$data = [];
if ($permission) {
$roles = Role::all();
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) {
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',

View File

@ -7,42 +7,80 @@
@section('content')
<div class="w-full grid gap-5 lg:gap-7.5 mx-auto">
@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 }}">
@method('PUT')
@else
<form method="POST" action="{{ route('users.roles.store') }}">
@endif
@csrf
<div class="card pb-2.5">
<div class="card-header" id="basic_settings">
<h3 class="card-title">
{{ isset($role->id) ? 'Edit' : 'Add' }} Role
</h3>
<div class="flex items-center gap-2">
<a href="{{ route('users.roles.index') }}" class="btn btn-xs btn-info">Back</a>
</div>
</div>
<div class="card-body grid gap-5">
@method('PUT')
@else
<form method="POST" action="{{ route('users.roles.store') }}">
@endif
@csrf
<div class="card pb-2.5">
<div class="card-header" id="basic_settings">
<h3 class="card-title">
{{ isset($role->id) ? 'Edit' : 'Add' }} Role
</h3>
<div class="flex items-center gap-2">
<a href="{{ route('users.roles.index') }}" class="btn btn-xs btn-info">Back</a>
</div>
</div>
<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">
<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
<span class="switch-label">
{{ ucwords($permission_name[1]) }}
</span>
</label>
@endforeach
</div>
</div>
@endforeach
<div class="flex justify-end">
<button type="submit" class="btn btn-primary">
Save
</button>
</div>
</div>
</div>
</div>
<div class="flex justify-end">
<button type="submit" class="btn btn-primary">
Save
</button>
</div>
</div>
</div>
</form>
</form>
</div>
@endsection

View File

@ -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',

View File

@ -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',