feat: initialize role-based access control (RBAC) system with multi-module UI and administrative management controllers

This commit is contained in:
Ajjj
2026-08-05 04:58:22 +08:00
parent 80fa96097c
commit 089e421716
19 changed files with 405 additions and 119 deletions

View File

@@ -66,7 +66,17 @@ class UserController extends Controller
$nextNumber = $last ? ((int) substr($last, 4)) + 1 : 1;
$nextCode = 'EMP-' . str_pad($nextNumber, 4, '0', STR_PAD_LEFT);
$roles = \Spatie\Permission\Models\Role::orderBy('name')->pluck('name');
$roleOrder = [
'Super Admin',
'admin',
'Main Contractor Admin',
'Project Manager',
'Construction Supervisor',
'Site Technical',
];
$roles = collect($roleOrder)->filter(
fn (string $role) => \Spatie\Permission\Models\Role::where('name', $role)->exists()
)->values();
return Inertia::render('UserManagement::Users/Create', [
'isSuperAdmin' => $isSuperAdmin,
@@ -108,7 +118,7 @@ class UserController extends Controller
'company_name' => ['nullable', 'string', 'max:255'],
'contact_person' => ['nullable', 'string', 'max:255'],
'tin_number' => ['nullable', 'string', 'max:255'],
'spatie_role' => ['nullable', 'string', Rule::in(['Super Admin', 'admin', 'Contractor', 'Contractor Admin', 'Project Manager', 'Designer', 'Site Technical', 'Construction Supervisor'])],
'spatie_role' => ['nullable', 'string', Rule::in(['Super Admin', 'admin', 'Main Contractor Admin', 'Project Manager', 'Site Technical', 'Construction Supervisor'])],
]);
// Resolve contractor_id: contractor admins always use their own;
@@ -133,13 +143,11 @@ class UserController extends Controller
]);
// Determine Spatie role to assign
$roleToAssign = $validated['spatie_role'] ?? $validated['user_type'];
if ($roleToAssign === 'Contractor Admin') {
$roleToAssign = 'Contractor';
} elseif ($roleToAssign === 'admin') {
$roleToAssign = 'Main Contractor Admin';
}
$roleToAssign = $validated['spatie_role'] ?? match ($validated['user_type']) {
'admin' => 'admin',
'contractor' => 'Main Contractor Admin',
default => 'Site Technical',
};
// Explicitly assign the spatie role matching the user type or selected role
if ($roleToAssign) {
$user->assignRole($roleToAssign);
@@ -209,7 +217,7 @@ class UserController extends Controller
'company_name' => ['nullable', 'string', 'max:255'],
'contact_person'=> ['nullable', 'string', 'max:255'],
'tin_number' => ['nullable', 'string', 'max:255'],
'spatie_role' => ['nullable', 'string', Rule::in(['Super Admin', 'Contractor', 'Project Manager', 'Designer', 'Site Technical', 'Construction Supervisor'])],
'spatie_role' => ['nullable', 'string', Rule::in(['Super Admin', 'admin', 'Main Contractor Admin', 'Project Manager', 'Site Technical', 'Construction Supervisor'])],
]);
$userData = [
@@ -225,10 +233,11 @@ class UserController extends Controller
$user->update($userData);
$roleToAssign = $validated['spatie_role'] ?? $validated['user_type'];
if ($roleToAssign === 'admin') {
$roleToAssign = 'Super Admin';
}
$roleToAssign = $validated['spatie_role'] ?? match ($validated['user_type']) {
'admin' => 'admin',
'contractor' => 'Main Contractor Admin',
default => 'Site Technical',
};
$user->syncRoles([$roleToAssign]);
@@ -376,12 +385,12 @@ class UserController extends Controller
continue;
}
$allowedRoles = ['Super Admin', 'Contractor', 'Project Manager', 'Designer', 'Site Technical', 'Construction Supervisor'];
$resolvedRole = in_array($role, $allowedRoles) ? $role : 'Designer';
$allowedRoles = ['Super Admin', 'admin', 'Main Contractor Admin', 'Project Manager', 'Site Technical', 'Construction Supervisor'];
$resolvedRole = in_array($role, $allowedRoles) ? $role : 'Site Technical';
// Map the Spatie role to the base user_type
$userType = $resolvedRole;
if (in_array($resolvedRole, ['Contractor', 'Project Manager', 'Designer', 'Site Technical', 'Construction Supervisor'])) {
if (in_array($resolvedRole, ['Main Contractor Admin', 'Project Manager', 'Site Technical', 'Construction Supervisor'])) {
$userType = 'contractor';
}