94 lines
2.9 KiB
PHP
94 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Database\Seeder;
|
|
use Modules\UserManagement\Models\EmployeeProfile;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
class SuperAdminSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run the database seeds.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
// Reset cached roles and permissions
|
|
app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions();
|
|
|
|
// 1. Ensure all system permissions are seeded
|
|
$permissions = [
|
|
// User Management
|
|
'users.view',
|
|
'users.create',
|
|
'users.edit',
|
|
'users.delete',
|
|
'users.manage-status',
|
|
|
|
// System Administration
|
|
'manage system settings',
|
|
'manage main contractors',
|
|
'manage sub contractors',
|
|
'manage users',
|
|
'assign roles',
|
|
'view all data',
|
|
'delete data',
|
|
'view reports',
|
|
|
|
// Project Management
|
|
'manage projects',
|
|
'approve_mr',
|
|
'approve_po',
|
|
];
|
|
|
|
foreach ($permissions as $permissionName) {
|
|
Permission::firstOrCreate(['name' => $permissionName]);
|
|
}
|
|
|
|
// 2. Ensure "Super Admin" role exists
|
|
$superAdminRole = Role::firstOrCreate(['name' => 'Super Admin']);
|
|
|
|
// Give all permissions to Super Admin role
|
|
$allPermissions = Permission::all();
|
|
$superAdminRole->syncPermissions($allPermissions);
|
|
|
|
// 3. Create or Update Super Admin User
|
|
$superAdmin = User::firstOrCreate(
|
|
['email' => 'superadmin@gsb-cons.com'],
|
|
[
|
|
'name' => 'Super Administrator',
|
|
'password' => bcrypt('password'), // You can change this in production!
|
|
'user_type' => 'admin',
|
|
'status' => 'active',
|
|
'contractor_id' => null, // Platform owner/bypasses tenant scope
|
|
]
|
|
);
|
|
|
|
// Assign Super Admin role to guarantee full Spatie coverage
|
|
$superAdmin->assignRole(['Super Admin']);
|
|
|
|
// 4. Create Employee Profile if not present to support full ERP dashboard workflows
|
|
$profile = EmployeeProfile::firstOrCreate(
|
|
['user_id' => $superAdmin->id],
|
|
[
|
|
'department' => 'Administration',
|
|
'position' => 'Super Administrator',
|
|
'employee_code' => 'SUPER-0001',
|
|
'hire_date' => now(),
|
|
]
|
|
);
|
|
|
|
// Link the user back to the profile
|
|
$superAdmin->update([
|
|
'userable_type' => EmployeeProfile::class,
|
|
'userable_id' => $profile->id,
|
|
]);
|
|
|
|
$this->command->info('Super Admin user created successfully:');
|
|
$this->command->info('Email: superadmin@gsb-cons.com');
|
|
$this->command->info('Password: password');
|
|
}
|
|
}
|