feat(admin): sync flexible unit kerja, employee type, and static backdrops with super admin user management

This commit is contained in:
Ray1111-aa
2026-10-27 21:38:04 +07:00
parent 4d24a98c41
commit 0642caebc1
5 changed files with 144 additions and 44 deletions
+12 -1
View File
@@ -60,7 +60,11 @@ class UserController extends Controller
$gates = Gate::where('is_active', true)->orderBy('name')->get();
$modules = config('rbac_permissions.modules', []);
return view('admin.users.index', compact('users', 'roles', 'gates', 'modules', 'rolePermissionsMap'));
$distinctUnits = Employee::query()->whereNotNull('unit_kerja')->where('unit_kerja', '!=', '')->distinct()->pluck('unit_kerja')->toArray();
$defaultUnits = ['Pembangkit', 'Bendungan', 'Teknik', 'Umum', 'Logistik', 'HSE', 'QMA'];
$unitKerjaList = array_values(array_unique(array_filter(array_merge($defaultUnits, $distinctUnits))));
return view('admin.users.index', compact('users', 'roles', 'gates', 'modules', 'rolePermissionsMap', 'unitKerjaList'));
}
/**
@@ -76,6 +80,7 @@ class UserController extends Controller
'role' => ['required', 'string', 'exists:roles,name'],
'gate_id' => ['nullable', 'exists:gates,id'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
'employee_type' => ['nullable', 'string', 'in:karyawan,sukon,Karyawan,Sukon'],
'unit_kerja' => ['nullable', 'string', 'max:100'],
'division' => ['nullable', 'string', 'max:100'],
'position' => ['nullable', 'string', 'max:100'],
@@ -134,6 +139,7 @@ class UserController extends Controller
'user_id' => $user->id,
'nik' => $user->username,
'name' => $user->name,
'employee_type' => !empty($validated['employee_type']) ? ucfirst(strtolower($validated['employee_type'])) : ($emp?->employee_type ?? 'Karyawan'),
'unit_kerja' => $validated['unit_kerja'] ?? ($emp?->unit_kerja ?? 'Umum'),
'division' => $validated['division'] ?? ($emp?->division ?? '-'),
'position' => $validated['position'] ?? ($emp?->position ?? ($isEmployeeRole ? 'Driver Operasional' : '-')),
@@ -184,6 +190,7 @@ class UserController extends Controller
'role' => ['required', 'string', 'exists:roles,name'],
'gate_id' => ['nullable', 'exists:gates,id'],
'photo' => ['nullable', 'image', 'mimes:jpeg,png,jpg,webp', 'max:10240'],
'employee_type' => ['nullable', 'string', 'in:karyawan,sukon,Karyawan,Sukon'],
'unit_kerja' => ['nullable', 'string', 'max:100'],
'division' => ['nullable', 'string', 'max:100'],
'position' => ['nullable', 'string', 'max:100'],
@@ -249,6 +256,9 @@ class UserController extends Controller
'nik' => $user->username,
'photo_path' => $user->photo_path ?: $emp?->photo_path,
];
if (array_key_exists('employee_type', $validated) && $validated['employee_type'] !== null) {
$empData['employee_type'] = ucfirst(strtolower($validated['employee_type']));
}
if (array_key_exists('unit_kerja', $validated) && $validated['unit_kerja'] !== null) {
$empData['unit_kerja'] = $validated['unit_kerja'];
}
@@ -269,6 +279,7 @@ class UserController extends Controller
}
} else {
$empData['user_id'] = $user->id;
$empData['employee_type'] = $empData['employee_type'] ?? 'Karyawan';
$empData['unit_kerja'] = $empData['unit_kerja'] ?? 'Umum';
$empData['division'] = $empData['division'] ?? '-';
$empData['position'] = $empData['position'] ?? ($isEmployeeRole ? 'Driver Operasional' : '-');
@@ -737,10 +737,18 @@ class EmployeeController extends Controller
if (!$employee->user_id) {
$employee->update(['user_id' => $linkedUser->id]);
}
$linkedUser->update([
$userData = [
'name' => $employee->name,
'username' => $employee->nik,
'photo_path' => $photoPath,
]);
];
if (str_ends_with($linkedUser->email, '@kmh.co.id')) {
$newCleanEmail = strtolower(str_replace(['-', ' '], '', $employee->nik)) . '@kmh.co.id';
if (!User::where('email', $newCleanEmail)->where('id', '!=', $linkedUser->id)->exists()) {
$userData['email'] = $newCleanEmail;
}
}
$linkedUser->update($userData);
}
return back()->with('success', "Data SIMPER untuk {$employee->name} berhasil diperbarui.");
+4 -1
View File
@@ -25,10 +25,13 @@ class AppServiceProvider extends ServiceProvider
// Super Admin bypass: secara otomatis memiliki izin untuk semua ability
Gate::before(function ($user, $ability) {
if ($user->role === 'super_admin' || $user->role === 'admin') {
if (in_array($user->role, ['super_admin', 'superadmin', 'admin'])) {
return true;
}
try {
if (method_exists($user, 'isSuperAdmin') && $user->isSuperAdmin()) {
return true;
}
return $user->hasRole('super_admin') ? true : null;
} catch (\Throwable $e) {
return null;
+46 -28
View File
@@ -290,7 +290,7 @@
<!-- ========================================================================= -->
<!-- MODAL: TAMBAH PENGGUNA BARU -->
<!-- ========================================================================= -->
<div id="createUserModal" class="fixed inset-0 z-50 hidden overflow-y-auto bg-slate-900/60 backdrop-blur-sm flex items-center justify-center p-4">
<div id="createUserModal" data-backdrop="static" class="fixed inset-0 z-50 hidden overflow-y-auto bg-slate-900/60 backdrop-blur-sm flex items-center justify-center p-4">
<div class="bg-white rounded-2xl max-w-lg w-full shadow-2xl overflow-hidden animate-scale-in">
<div class="px-6 py-4 border-b border-slate-100 flex items-center justify-between">
<h3 class="text-sm font-bold text-slate-800 flex items-center gap-2">
@@ -368,29 +368,33 @@
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Unit Kerja</label>
<select name="unit_kerja" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700">
<option value="">-- Pilih Unit Kerja --</option>
<option value="Pembangkit">Pembangkit</option>
<option value="Bendungan">Bendungan</option>
<option value="Teknik">Teknik</option>
<option value="Umum">Umum</option>
<option value="Logistik">Logistik</option>
<option value="HSE">HSE</option>
<option value="QMA">QMA</option>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Jenis Personil</label>
<select name="employee_type" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700">
<option value="karyawan">Karyawan (Internal KMH)</option>
<option value="sukon">Sukon (Subkontraktor)</option>
</select>
</div>
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Divisi</label>
<input type="text" name="division" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Operasi / Keuangan">
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Unit Kerja</label>
<input type="text"
name="unit_kerja"
list="adminUnitKerjaSuggestions"
class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700"
placeholder="Ketik atau pilih unit kerja...">
</div>
</div>
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Divisi</label>
<input type="text" name="division" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Operasi / Keuangan">
</div>
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Posisi / Jabatan</label>
<input type="text" name="position" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Driver / Staff Finance">
</div>
</div>
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Operator / Driver</label>
<select name="is_operator" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700">
@@ -399,7 +403,6 @@
</select>
</div>
</div>
</div>
<div class="pt-4 border-t border-slate-100 flex items-center justify-end gap-2.5">
<button type="button" onclick="closeCreateUserModal()" class="px-4 py-2 bg-slate-100 hover:bg-slate-200 text-slate-600 rounded-xl text-xs font-bold transition">
@@ -416,7 +419,7 @@
<!-- ========================================================================= -->
<!-- MODAL: EDIT PENGGUNA -->
<!-- ========================================================================= -->
<div id="editUserModal" class="fixed inset-0 z-50 hidden overflow-y-auto bg-slate-900/60 backdrop-blur-sm flex items-center justify-center p-4">
<div id="editUserModal" data-backdrop="static" class="fixed inset-0 z-50 hidden overflow-y-auto bg-slate-900/60 backdrop-blur-sm flex items-center justify-center p-4">
<div class="bg-white rounded-2xl max-w-lg w-full shadow-2xl overflow-hidden animate-scale-in">
<div class="px-6 py-4 border-b border-slate-100 flex items-center justify-between">
<h3 class="text-sm font-bold text-slate-800 flex items-center gap-2">
@@ -495,29 +498,34 @@
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Unit Kerja</label>
<select id="edit_unit_kerja" name="unit_kerja" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700">
<option value="">-- Pilih Unit Kerja --</option>
<option value="Pembangkit">Pembangkit</option>
<option value="Bendungan">Bendungan</option>
<option value="Teknik">Teknik</option>
<option value="Umum">Umum</option>
<option value="Logistik">Logistik</option>
<option value="HSE">HSE</option>
<option value="QMA">QMA</option>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Jenis Personil</label>
<select id="edit_employee_type" name="employee_type" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700">
<option value="karyawan">Karyawan (Internal KMH)</option>
<option value="sukon">Sukon (Subkontraktor)</option>
</select>
</div>
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Divisi</label>
<input type="text" id="edit_division" name="division" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Operasi / Keuangan">
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Unit Kerja</label>
<input type="text"
id="edit_unit_kerja"
name="unit_kerja"
list="adminUnitKerjaSuggestions"
class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700"
placeholder="Ketik atau pilih unit kerja...">
</div>
</div>
<div class="grid grid-cols-2 gap-3">
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Divisi</label>
<input type="text" id="edit_division" name="division" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Operasi / Keuangan">
</div>
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Posisi / Jabatan</label>
<input type="text" id="edit_position" name="position" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700" placeholder="Contoh: Driver / Staff Finance">
</div>
</div>
<div>
<label class="block text-[11px] font-semibold text-slate-600 mb-1">Operator / Driver</label>
<select id="edit_is_operator" name="is_operator" class="w-full px-3 py-2 bg-slate-50 border border-slate-200 rounded-xl text-xs focus:ring-2 focus:ring-indigo-500 focus:bg-white text-slate-700">
@@ -526,7 +534,6 @@
</select>
</div>
</div>
</div>
<!-- Accordion: Izin Tambahan Khusus (Direct Permissions) -->
<div class="border border-slate-200 rounded-xl overflow-hidden bg-slate-50/50">
@@ -773,6 +780,8 @@
if (editGateEl) editGateEl.value = user.gate_id || '';
const emp = user.employee || {};
const editEmployeeType = document.getElementById('edit_employee_type');
if (editEmployeeType) editEmployeeType.value = (emp.employee_type ? emp.employee_type.toLowerCase() : 'karyawan');
const editUnitKerja = document.getElementById('edit_unit_kerja');
if (editUnitKerja) editUnitKerja.value = emp.unit_kerja || '';
const editDivision = document.getElementById('edit_division');
@@ -866,4 +875,13 @@
}
});
</script>
<!-- Datalist Rekomendasi Unit Kerja (Sinkron Master HSE) -->
<datalist id="adminUnitKerjaSuggestions">
@if(isset($unitKerjaList))
@foreach($unitKerjaList as $uk)
<option value="{{ $uk }}"></option>
@endforeach
@endif
</datalist>
@endsection
+60
View File
@@ -496,4 +496,64 @@ class SuperAdminRbacTest extends TestCase
$this->assertTrue($targetUser->fresh()->can('employee.dashboard'));
$this->assertTrue($targetUser->fresh()->can('qma.vehicles.view'));
}
public function test_super_admin_user_management_syncs_flexible_unit_kerja_and_employee_type(): void
{
$superAdmin = User::where('username', 'superadmin')->first();
// 1. Cek UserController@index menyediakan $unitKerjaList dan datalist suggestions
$indexResponse = $this->actingAs($superAdmin)->get(route('admin.users.index'));
$indexResponse->assertOk();
$indexResponse->assertViewHas('unitKerjaList');
$indexResponse->assertSee('adminUnitKerjaSuggestions');
$indexResponse->assertSee('data-backdrop="static"', false);
// 2. Super Admin membuat user baru dengan unit_kerja fleksibel dan employee_type sukon
$response = $this->actingAs($superAdmin)->post(route('admin.users.store'), [
'name' => 'Budi Santoso',
'username' => 'BUDI-SUKON-001',
'email' => '[email protected]',
'password' => 'password123',
'role' => 'employee',
'employee_type' => 'sukon',
'unit_kerja' => 'Subkontraktor Sipil PT ABC',
'division' => 'Proyek Terowongan',
'position' => 'Driver Mixer',
'is_operator' => '1',
]);
$response->assertRedirect(route('admin.users.index'));
$response->assertSessionHas('success');
$user = User::where('username', 'BUDI-SUKON-001')->first();
$this->assertNotNull($user);
// Verifikasi data Employee terkait tersinkron otomatis
$employee = \App\Models\Employee::where('user_id', $user->id)->first();
$this->assertNotNull($employee);
$this->assertEquals('Subkontraktor Sipil PT ABC', $employee->unit_kerja);
$this->assertEquals('Sukon', $employee->employee_type);
$this->assertEquals('Driver Mixer', $employee->position);
$this->assertTrue((bool)$employee->is_operator);
// 3. Super Admin memperbarui unit_kerja ke nilai kustom lain
$updateResponse = $this->actingAs($superAdmin)->post(route('admin.users.update', $user->id), [
'name' => 'Budi Santoso Baru',
'username' => 'BUDI-SUKON-001',
'email' => '[email protected]',
'role' => 'employee',
'employee_type' => 'karyawan',
'unit_kerja' => 'Pembangkit Unit 2 Baru',
'division' => 'Operasi Generator',
'position' => 'Operator Turbin',
'is_operator' => '0',
]);
$updateResponse->assertRedirect(route('admin.users.index'));
$employee->refresh();
$this->assertEquals('Pembangkit Unit 2 Baru', $employee->unit_kerja);
$this->assertEquals('Karyawan', $employee->employee_type);
$this->assertEquals('Operator Turbin', $employee->position);
$this->assertFalse((bool)$employee->is_operator);
}
}