67 lines
3.1 KiB
PHP
67 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Menu;
|
|
use App\Models\Role;
|
|
use App\Models\User;
|
|
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class UsersRoleMenuSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
// Fetch roles in a single query
|
|
$roles = Role::whereIn('name', ['superadmin', 'admin', 'operator'])->get()->keyBy('name');
|
|
|
|
// Fetch all menus in a single query and index by name
|
|
$menus = Menu::whereIn('name', [
|
|
'Dashboard', 'Master', 'Settings', 'Data Settings', 'Data', 'Laporan', 'Neng Bedas',
|
|
'Approval', 'Tools', 'Dashboard Pimpinan', 'Dashboard PBG', 'Users', 'Syncronize',
|
|
'Menu', 'Role', 'Setting Dashboard', 'PBG', 'Reklame', 'Usaha atau Industri', 'Pariwisata',
|
|
'Lap Pariwisata', 'UMKM', 'Dashboard Potensi', 'Tata Ruang', 'PDAM', 'PETA',
|
|
'Lap Pimpinan', 'Chat', 'Dalam Sistem', 'Luar Sistem', 'Google Sheets', 'TPA TPT',
|
|
'Approval Pejabat', 'Undangan', 'Rekap Pembayaran', 'Lap Rekap Data Pembayaran', 'Lap PBG (PTSP)'
|
|
])->get()->keyBy('name');
|
|
|
|
// Define access levels for each role
|
|
$permissions = [
|
|
'superadmin' => [
|
|
'Dashboard', 'Master', 'Settings', 'Data Settings', 'Data', 'Laporan', 'Neng Bedas',
|
|
'Approval', 'Tools', 'Dashboard Pimpinan', 'Dashboard PBG', 'Users', 'Syncronize',
|
|
'Menu', 'Role', 'Setting Dashboard', 'PBG', 'Reklame', 'Usaha atau Industri', 'Pariwisata',
|
|
'Lap Pariwisata', 'UMKM', 'Dashboard Potensi', 'Tata Ruang', 'PDAM', 'Dalam Sistem',
|
|
'Luar Sistem', 'Lap Pimpinan', 'Chat', 'Google Sheets', 'TPA TPT', 'Approval Pejabat',
|
|
'Undangan', 'Rekap Pembayaran', 'Lap Rekap Data Pembayaran', 'Lap PBG (PTSP)'
|
|
],
|
|
'admin' => ['Dashboard', 'Master', 'Settings'],
|
|
'operator' => ['Dashboard', 'Data', 'Laporan']
|
|
];
|
|
|
|
// Define permission levels
|
|
$superadminPermissions = ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true];
|
|
$adminPermissions = ["allow_show" => true, "allow_create" => true, "allow_update" => true, "allow_destroy" => true];
|
|
$operatorPermissions = ["allow_show" => true, "allow_create" => false, "allow_update" => false, "allow_destroy" => false];
|
|
|
|
// Assign menus to roles
|
|
foreach ($permissions as $roleName => $menuNames) {
|
|
$role = $roles[$roleName] ?? null;
|
|
if ($role) {
|
|
$role->menus()->sync(
|
|
collect($menuNames)->mapWithKeys(fn($menuName) => [
|
|
$menus[$menuName]->id => ($roleName === 'superadmin' ? $superadminPermissions :
|
|
($roleName === 'admin' ? $adminPermissions : $operatorPermissions))
|
|
])->toArray()
|
|
);
|
|
}
|
|
}
|
|
|
|
// Attach User to role super admin
|
|
User::findOrFail(1)->roles()->sync([$roles['superadmin']->id]);
|
|
}
|
|
}
|