ifnex/04_Laravel/database/seeders/RoleAndPermissionSeeder.php
Kazem Alghasi a0bc871c8d feat(auth): implement role-based access control using spatie/laravel-permission
Integrate Spatie Laravel Permission to replace the legacy role system.
This includes:
- Adding `spatie/laravel-permission` dependency.
- Implementing `Role` and `User` model updates with `HasRoles` trait.
- Adding migrations for permission and role tables.
- Creating `RoleResource` and `UserResource` for Filament administration.
- Adding a `RoleAndPermissionSeeder` for initial setup.
- Updating `User` model helper methods to utilize role checks.
2026-08-10 02:44:23 +03:30

136 lines
4.8 KiB
PHP

<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\Models\User;
class RoleAndPermissionSeeder extends Seeder
{
public function run(): void
{
// پاک کردن cache
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
// ─── تعریف Permissions ───
$permissions = [
// مدیریت کاربران
'view_users',
'create_users',
'edit_users',
'delete_users',
// مدیریت مرسولات
'view_shipments',
'create_shipments',
'edit_shipments',
'delete_shipments',
'export_shipments',
// مدیریت کیف پول
'view_wallets',
'adjust_wallets',
'freeze_wallets',
'view_transactions',
// گزارش‌های مالی
'view_financial_reports',
'export_financial_reports',
// تنظیمات سیستم
'manage_settings',
'adjust_exchange_rates',
// کدهای تخفیف
'manage_discount_codes',
// سفارشات مشتریان
'view_customer_orders',
'update_order_status',
];
foreach ($permissions as $permission) {
Permission::firstOrCreate([
'name' => $permission,
'guard_name' => 'web'
]);
}
// ─── تعریف Roles ───
// Super Admin: دسترسی کامل (guard all permissions)
$superAdmin = Role::firstOrCreate([
'name' => 'super_admin',
'guard_name' => 'web'
]);
$superAdmin->syncPermissions(Permission::all());
$this->command->info('✅ Super Admin role created with all permissions');
// Admin: دسترسی بالا (بدون مدیریت کاربران حساس)
$admin = Role::firstOrCreate([
'name' => 'admin',
'guard_name' => 'web'
]);
$admin->syncPermissions([
'view_users',
'view_shipments', 'create_shipments', 'edit_shipments', 'delete_shipments', 'export_shipments',
'view_wallets', 'adjust_wallets', 'freeze_wallets', 'view_transactions',
'view_financial_reports', 'export_financial_reports',
'manage_discount_codes',
'adjust_exchange_rates',
'view_customer_orders', 'update_order_status',
]);
$this->command->info('✅ Admin role created');
// Staff: فقط مشاهده و عملیات روزمره
$staff = Role::firstOrCreate([
'name' => 'staff',
'guard_name' => 'web'
]);
$staff->syncPermissions([
'view_shipments', 'create_shipments', 'edit_shipments', 'export_shipments',
'view_wallets', 'view_transactions',
'view_customer_orders', 'update_order_status',
]);
$this->command->info('✅ Staff role created');
// Customer: بدون دسترسی به پنل ادمین (فقط API)
Role::firstOrCreate([
'name' => 'customer',
'guard_name' => 'web'
]);
$this->command->info('✅ Customer role created');
// ─── اختصاص role به کاربران موجود ───
$this->command->info('');
$this->command->info('📋 Assigning roles to existing users...');
// کاربر اصلی را super_admin کن
$mainAdmin = User::where('email', 'kazem@vernasoft.group')->first();
if ($mainAdmin) {
$mainAdmin->assignRole('super_admin');
$this->command->info("{$mainAdmin->name} ({$mainAdmin->email}) → super_admin");
} else {
// اگر کاربر اصلی نبود، اولین کاربر را super_admin کن
$firstUser = User::first();
if ($firstUser) {
$firstUser->assignRole('super_admin');
$this->command->info("{$firstUser->name} ({$firstUser->email}) → super_admin (first user)");
}
}
// سایر کاربران موجود را customer کن
User::where('email', '!=', 'kazem@vernasoft.group')
->whereDoesntHave('roles')
->get()
->each(function ($user) {
$user->assignRole('customer');
$this->command->info("{$user->name} ({$user->email}) → customer");
});
$this->command->info('');
$this->command->info('🎉 All roles and permissions seeded successfully!');
}
}