You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1.3 KiB
46 lines
1.3 KiB
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use Illuminate\Database\Seeder;
|
|
use Spatie\Permission\Models\Role;
|
|
use Spatie\Permission\Models\Permission;
|
|
use App\Models\User;
|
|
|
|
class RolePermissionSeeder extends Seeder
|
|
{
|
|
public function run(): void
|
|
{
|
|
// Xóa cache phân quyền (nếu có)
|
|
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
|
|
|
|
// Tạo permissions
|
|
$permissions = [
|
|
'manage users',
|
|
'edit articles',
|
|
'delete articles',
|
|
'publish articles',
|
|
'view dashboard',
|
|
];
|
|
|
|
foreach ($permissions as $perm) {
|
|
Permission::firstOrCreate(['name' => $perm]);
|
|
}
|
|
|
|
// Tạo roles
|
|
$admin = Role::firstOrCreate(['name' => 'admin']);
|
|
$editor = Role::firstOrCreate(['name' => 'editor']);
|
|
$viewer = Role::firstOrCreate(['name' => 'viewer']);
|
|
|
|
// Gán permission cho role
|
|
$admin->syncPermissions($permissions);
|
|
$editor->syncPermissions(['edit articles', 'publish articles']);
|
|
$viewer->syncPermissions(['view dashboard']);
|
|
|
|
// Gán role cho user test (ID = 1)
|
|
$user = User::find(1);
|
|
if ($user) {
|
|
$user->assignRole('admin');
|
|
}
|
|
}
|
|
}
|
|
|