223 lines
7.4 KiB
PHP
223 lines
7.4 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;
|
|
use Spatie\Permission\PermissionRegistrar;
|
|
|
|
class ProductionSetupSeeder extends Seeder
|
|
{
|
|
/**
|
|
* Run all production-grade setup seeds for GSB Construction ERP.
|
|
*/
|
|
public function run(): void
|
|
{
|
|
// 1. Reset cached roles and permissions
|
|
app()[PermissionRegistrar::class]->forgetCachedPermissions();
|
|
$this->command->info('1. Permission cache reset successfully.');
|
|
|
|
// 2. Define and seed all system permissions
|
|
$permissions = [
|
|
// Core Access
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'contractors.access',
|
|
'bidding.access',
|
|
'users.access',
|
|
'materials-catalog.access',
|
|
'inventory.access',
|
|
'finance.access',
|
|
'documents.access',
|
|
'approvals.access',
|
|
'roles.access',
|
|
'labors.access',
|
|
'equipments.access',
|
|
|
|
// User & Role Management
|
|
'users.view',
|
|
'users.create',
|
|
'users.edit',
|
|
'users.delete',
|
|
'users.manage-status',
|
|
'users.link-contractor',
|
|
'roles.view',
|
|
'roles.create',
|
|
'roles.edit',
|
|
'roles.delete',
|
|
'roles.assign',
|
|
|
|
// Projects & Tasks
|
|
'create projects',
|
|
'edit projects',
|
|
'delete projects',
|
|
'create tasks',
|
|
'edit tasks',
|
|
'delete tasks',
|
|
'edit master schedule',
|
|
|
|
// Daily Reports & QC
|
|
'create daily reports',
|
|
'edit daily reports',
|
|
'delete daily reports',
|
|
'create inspections',
|
|
'approve inspections',
|
|
|
|
// Documents
|
|
'upload documents',
|
|
'approve documents',
|
|
|
|
// Settings & System
|
|
'manage settings',
|
|
'manage system settings',
|
|
'manage main contractors',
|
|
'manage sub contractors',
|
|
'view all data',
|
|
'delete data',
|
|
'view reports',
|
|
'approve_mr',
|
|
'approve_po',
|
|
];
|
|
|
|
foreach ($permissions as $permissionName) {
|
|
Permission::firstOrCreate(['name' => $permissionName, 'guard_name' => 'web']);
|
|
}
|
|
$this->command->info('2. Created / verified ' . count($permissions) . ' system permissions.');
|
|
|
|
// 3. Define Roles
|
|
$superAdmin = Role::firstOrCreate(['name' => 'Super Admin', 'guard_name' => 'web']);
|
|
$admin = Role::firstOrCreate(['name' => 'admin', 'guard_name' => 'web']);
|
|
$mainContractorAdmin = Role::firstOrCreate(['name' => 'Main Contractor Admin', 'guard_name' => 'web']);
|
|
$projectManager = Role::firstOrCreate(['name' => 'Project Manager', 'guard_name' => 'web']);
|
|
$siteTechnical = Role::firstOrCreate(['name' => 'Site Technical', 'guard_name' => 'web']);
|
|
$constructionSupervisor = Role::firstOrCreate(['name' => 'Construction Supervisor', 'guard_name' => 'web']);
|
|
|
|
// Give Super Admin & admin all permissions
|
|
$allPermissions = Permission::all();
|
|
$superAdmin->syncPermissions($allPermissions);
|
|
$admin->syncPermissions($allPermissions);
|
|
|
|
// Main Contractor Admin Permissions
|
|
$mainContractorAdmin->syncPermissions([
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'contractors.access',
|
|
'bidding.access',
|
|
'users.access',
|
|
'materials-catalog.access',
|
|
'inventory.access',
|
|
'finance.access',
|
|
'documents.access',
|
|
'approvals.access',
|
|
'create projects',
|
|
'edit projects',
|
|
'create tasks',
|
|
'edit tasks',
|
|
'view all data',
|
|
'view reports',
|
|
]);
|
|
|
|
// Project Manager Permissions
|
|
$projectManager->syncPermissions([
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'contractors.access',
|
|
'bidding.access',
|
|
'materials-catalog.access',
|
|
'inventory.access',
|
|
'finance.access',
|
|
'documents.access',
|
|
'approvals.access',
|
|
'labors.access',
|
|
'equipments.access',
|
|
'create tasks',
|
|
'edit tasks',
|
|
'delete tasks',
|
|
'upload documents',
|
|
'approve documents',
|
|
'approve inspections',
|
|
'edit master schedule',
|
|
'approve_mr',
|
|
'approve_po',
|
|
]);
|
|
|
|
// Site Technical Permissions
|
|
$siteTechnical->syncPermissions([
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'inventory.access',
|
|
'documents.access',
|
|
'edit tasks',
|
|
'create daily reports',
|
|
'create inspections',
|
|
'upload documents',
|
|
]);
|
|
|
|
// Construction Supervisor Permissions
|
|
$constructionSupervisor->syncPermissions([
|
|
'dashboard.access',
|
|
'projects.access',
|
|
'inventory.access',
|
|
'materials-catalog.access',
|
|
'documents.access',
|
|
'edit tasks',
|
|
'create daily reports',
|
|
]);
|
|
|
|
$this->command->info('3. Roles and permission mappings configured successfully.');
|
|
|
|
// 4. Ensure Super Admin Account
|
|
$superAdminUser = User::firstOrCreate(
|
|
['email' => 'superadmin@gsb-cons.com'],
|
|
[
|
|
'name' => 'Super Administrator',
|
|
'password' => bcrypt('password'),
|
|
'user_type' => 'admin',
|
|
'status' => 'active',
|
|
'contractor_id' => null,
|
|
]
|
|
);
|
|
|
|
// Update to guarantee proper user_type and status
|
|
$superAdminUser->update([
|
|
'user_type' => 'admin',
|
|
'status' => 'active',
|
|
]);
|
|
$superAdminUser->syncRoles(['Super Admin', 'admin']);
|
|
|
|
// Profile for Super Admin
|
|
$profile = EmployeeProfile::firstOrCreate(
|
|
['user_id' => $superAdminUser->id],
|
|
[
|
|
'department' => 'Administration',
|
|
'position' => 'Super Administrator',
|
|
'employee_code' => 'SUPER-0001',
|
|
'hire_date' => now(),
|
|
]
|
|
);
|
|
$superAdminUser->update([
|
|
'userable_type' => EmployeeProfile::class,
|
|
'userable_id' => $profile->id,
|
|
]);
|
|
|
|
// Also check if an admin@gsb-cons.com or admin@example.com exists and grant Super Admin
|
|
$otherAdmins = User::whereIn('email', ['admin@gsb-cons.com', 'admin@example.com'])->get();
|
|
foreach ($otherAdmins as $adm) {
|
|
$adm->update(['user_type' => 'admin', 'status' => 'active']);
|
|
$adm->syncRoles(['Super Admin', 'admin']);
|
|
}
|
|
|
|
// 5. Clear permission cache one final time
|
|
app()[PermissionRegistrar::class]->forgetCachedPermissions();
|
|
|
|
$this->command->info('----------------------------------------------------');
|
|
$this->command->info('✅ Production Setup Seeding Complete!');
|
|
$this->command->info('Account: superadmin@gsb-cons.com');
|
|
$this->command->info('Role: Super Admin / Executive Governance');
|
|
$this->command->info('----------------------------------------------------');
|
|
}
|
|
}
|