feat: implement super admin role with spatie rbac and categorize sidebar by management
This commit is contained in:
@@ -267,5 +267,8 @@ class DatabaseSeeder extends Seeder
|
||||
'stnk_document' => 'STNK-BH-8412-AA',
|
||||
'kir_document' => 'KIR-2026/11',
|
||||
]);
|
||||
|
||||
// 6. Seed RBAC Roles, Permissions & Super Admin
|
||||
$this->call(RolePermissionSeeder::class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
|
||||
class RolePermissionSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// 1. Reset cache permission Spatie
|
||||
app()[PermissionRegistrar::class]->forgetCachedPermissions();
|
||||
|
||||
// 2. Baca seluruh daftar permission dari konfigurasi rbac_permissions
|
||||
$modules = config('rbac_permissions.modules', []);
|
||||
$allPermissions = [];
|
||||
|
||||
foreach ($modules as $moduleKey => $moduleData) {
|
||||
foreach ($moduleData['permissions'] as $permKey => $permMeta) {
|
||||
$allPermissions[] = $permKey;
|
||||
Permission::firstOrCreate([
|
||||
'name' => $permKey,
|
||||
'guard_name' => 'web',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Pemetaan Default Permission per Role
|
||||
$rolesWithPermissions = [
|
||||
'super_admin' => $allPermissions,
|
||||
'hse_admin' => [
|
||||
'hse.employees.view',
|
||||
'hse.employees.create',
|
||||
'hse.employees.edit',
|
||||
],
|
||||
'qma_admin' => [
|
||||
'qma.vehicles.view',
|
||||
'qma.vehicles.create',
|
||||
'qma.vehicles.edit',
|
||||
'qma.vehicles.delete',
|
||||
'qma.logs.view',
|
||||
'qma.summary.view',
|
||||
],
|
||||
'pg_admin' => [
|
||||
'pg.guards.view',
|
||||
'pg.guards.create',
|
||||
'pg.guards.edit',
|
||||
'pg.guards.toggle',
|
||||
'pg.guards.delete',
|
||||
'pg.logs.view',
|
||||
],
|
||||
'gate_officer' => [
|
||||
'gate.inspect',
|
||||
'gate.approve',
|
||||
'gate.reject',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($rolesWithPermissions as $roleName => $perms) {
|
||||
$role = Role::firstOrCreate(['name' => $roleName, 'guard_name' => 'web']);
|
||||
$role->syncPermissions($perms);
|
||||
}
|
||||
|
||||
// 4. Buat Akun Default Super Admin
|
||||
$superAdmin = User::firstOrCreate(
|
||||
['username' => 'superadmin'],
|
||||
[
|
||||
'name' => 'Super Administrator',
|
||||
'email' => '[email protected]',
|
||||
'password' => Hash::make('password'),
|
||||
'role' => 'super_admin',
|
||||
]
|
||||
);
|
||||
|
||||
if (!$superAdmin->hasRole('super_admin')) {
|
||||
$superAdmin->assignRole('super_admin');
|
||||
}
|
||||
|
||||
// 5. Sinkronisasi Akun Existing ke Spatie Roles
|
||||
$existingUsers = User::all();
|
||||
foreach ($existingUsers as $user) {
|
||||
if ($user->role && in_array($user->role, array_keys($rolesWithPermissions))) {
|
||||
if (!$user->hasRole($user->role)) {
|
||||
$user->assignRole($user->role);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user