feat(roles): tambah fitur relasi posisi pada role
- Tambahkan relasi posisi dengan menambahkan kolom `position_id` pada tabel roles melalui migrasi. - Perbarui fungsi pada `RolesController` untuk menyertakan posisi dalam proses CRUD. - Gunakan model `Position` untuk mendapatkan daftar posisi baik saat membuat maupun mengedit role. - Sesuaikan nama permission dari `roles.view` ke `roles.read`, `roles.store` ke `roles.create`, dan `roles.edit` ke `roles.update` agar konsisten. - Perbarui validasi di `RoleRequest` untuk mendukung input `position_id`. - Tambahkan properti `position_id` ke atribut `fillable` di model Role untuk mendukung mass assignment. - Buat fungsi relasi `position()` pada model Role untuk mereferensikan ke model Position. - Perbarui tampilan form role (`create.blade.php`): - Tambahkan dropdown untuk memilih posisi dalam form input. - Tampilkan informasi level posisi bersama dengan nama posisi dalam dropdown. - Sinkronisasi validasi dan nilai default sesuai dengan pengaturan posisi. - Perbaikan minor pada query pencarian data roles, menggunakan `whereRaw` untuk pencarian case-insensitive.
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
use Modules\Usermanagement\Http\Requests\RoleRequest;
|
||||
use Modules\Usermanagement\Models\Permission;
|
||||
use Modules\Usermanagement\Models\PermissionGroup;
|
||||
use Modules\Usermanagement\Models\Position;
|
||||
use Modules\Usermanagement\Models\Role;
|
||||
|
||||
/**
|
||||
@@ -48,7 +49,7 @@
|
||||
public function index()
|
||||
{
|
||||
// Check if the authenticated user has the required permission to view roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.view')) {
|
||||
if (is_null($this->user) || !$this->user->can('roles.read')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view roles.');
|
||||
}
|
||||
|
||||
@@ -70,7 +71,7 @@
|
||||
public function store(RoleRequest $request)
|
||||
{
|
||||
// Check if the authenticated user has the required permission to store roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.store')) {
|
||||
if (is_null($this->user) || !$this->user->can('roles.create')) {
|
||||
//abort(403, 'Sorry! You are not allowed to store roles.');
|
||||
}
|
||||
|
||||
@@ -115,8 +116,9 @@
|
||||
}
|
||||
|
||||
$permissiongroups = PermissionGroup::all();
|
||||
$positions = Position::all();
|
||||
// Return the view for creating a new role
|
||||
return view('usermanagement::roles.create',compact('permissiongroups'));
|
||||
return view('usermanagement::roles.create', compact('permissiongroups', 'positions'));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -130,7 +132,7 @@
|
||||
public function show($id)
|
||||
{
|
||||
// Check if the authenticated user has the required permission to view roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.view')) {
|
||||
if (is_null($this->user) || !$this->user->can('roles.read')) {
|
||||
abort(403, 'Sorry! You are not allowed to view roles.');
|
||||
}
|
||||
|
||||
@@ -154,7 +156,7 @@
|
||||
public function edit($id)
|
||||
{
|
||||
// Check if the authenticated user has the required permission to edit roles
|
||||
if (is_null($this->user) || !$this->user->can('roles.edit')) {
|
||||
if (is_null($this->user) || !$this->user->can('roles.update')) {
|
||||
//abort(403, 'Sorry! You are not allowed to edit roles.');
|
||||
}
|
||||
|
||||
@@ -162,8 +164,9 @@
|
||||
$role = Role::find($id);
|
||||
$permissions = Permission::all();
|
||||
$permissiongroups = PermissionGroup::all();
|
||||
$positions = Position::all();
|
||||
// Return the view for editing the role
|
||||
return view('usermanagement::roles.create', compact('role','permissions','permissiongroups'));
|
||||
return view('usermanagement::roles.create', compact('role', 'permissions', 'permissiongroups', 'positions'));
|
||||
}
|
||||
|
||||
|
||||
@@ -272,7 +275,7 @@
|
||||
*/
|
||||
public function dataForDatatables(Request $request)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('roles.view')) {
|
||||
if (is_null($this->user) || !$this->user->can('roles.read')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view users.');
|
||||
}
|
||||
|
||||
@@ -283,7 +286,7 @@
|
||||
if ($request->has('search') && !empty($request->get('search'))) {
|
||||
$search = $request->get('search');
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('name', 'LIKE', "%$search%");
|
||||
$q->whereRaw('LOWER(name) LIKE ?', ['%' . strtolower($search) . '%']);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
$rules = [
|
||||
'guard_names' => 'required|string|in:web,api',
|
||||
'position_id' => 'nullable|exists:positions,id',
|
||||
];
|
||||
|
||||
if ($this->method() === 'PUT') {
|
||||
@@ -41,6 +42,3 @@
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -11,6 +11,17 @@
|
||||
{
|
||||
use softDeletes, LogsActivity;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'guard_name',
|
||||
'position_id',
|
||||
];
|
||||
|
||||
/**
|
||||
* Retrieve the activity log options for this role.
|
||||
*
|
||||
@@ -22,4 +33,11 @@
|
||||
return LogOptions::defaults()->logAll()->useLogName('User Management|Roles : ');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the position that owns the role.
|
||||
*/
|
||||
public function position()
|
||||
{
|
||||
return $this->belongsTo(Position::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
$tableNames = config('permission.table_names');
|
||||
|
||||
if (empty($tableNames)) {
|
||||
throw new \Exception('Error: config/permission.php not loaded. Run [php artisan config:clear] and try again.');
|
||||
}
|
||||
|
||||
Schema::table($tableNames['roles'], function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('position_id')->nullable()->after('guard_name');
|
||||
$table->foreign('position_id')
|
||||
->references('id')
|
||||
->on('positions')
|
||||
->onDelete('set null');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
$tableNames = config('permission.table_names');
|
||||
|
||||
if (empty($tableNames)) {
|
||||
throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding, or drop the tables manually.');
|
||||
}
|
||||
|
||||
Schema::table($tableNames['roles'], function (Blueprint $table) {
|
||||
$table->dropForeign(['position_id']);
|
||||
$table->dropColumn('position_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -6,81 +6,97 @@
|
||||
|
||||
@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" id="role_form">
|
||||
<form action="{{ isset($role->id) ? route('users.roles.update', $role->id) : route('users.roles.store') }}" method="POST" id="role_form">
|
||||
@csrf
|
||||
@if(isset($role->id))
|
||||
<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">
|
||||
<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
|
||||
|
||||
<span class="switch-label">
|
||||
{{ ucwords($permission_name[1]) }}
|
||||
</span>
|
||||
</label>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
<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">
|
||||
Position
|
||||
</label>
|
||||
<div class="flex flex-wrap items-baseline w-full">
|
||||
<select class="select @error('position_id') border-danger @enderror" name="position_id">
|
||||
<option value="">Select Position</option>
|
||||
@foreach($positions as $position)
|
||||
<option value="{{ $position->id }}" {{ (isset($role) && $role->position_id == $position->id) ? 'selected' : '' }}>
|
||||
{{ $position->name }} (Level: {{ $position->level }})
|
||||
</option>
|
||||
@endforeach
|
||||
</select>
|
||||
@error('position_id')
|
||||
<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 justify-end">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
Save
|
||||
</button>
|
||||
</div>
|
||||
<span class="switch-label">
|
||||
{{ ucwords($permission_name[1]) }}
|
||||
</span>
|
||||
</label>
|
||||
@endforeach
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
@endforeach
|
||||
|
||||
<div class="flex justify-end">
|
||||
<button type="submit" class="btn btn-primary">
|
||||
Save
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
@endsection
|
||||
|
||||
Reference in New Issue
Block a user