security(core): patch driver IDOR, protect employee core role, harden XLSX parser, and fix timesheet concurrency race condition
This commit is contained in:
@@ -101,7 +101,7 @@ class RolePermissionController extends Controller
|
||||
$role = Role::findOrFail($roleId);
|
||||
|
||||
// Proteksi role inti sistem
|
||||
$coreRoles = ['super_admin', 'hse_admin', 'qma_admin', 'pg_admin', 'gate_officer'];
|
||||
$coreRoles = ['super_admin', 'hse_admin', 'qma_admin', 'pg_admin', 'gate_officer', 'employee'];
|
||||
if (in_array($role->name, $coreRoles)) {
|
||||
return redirect()->route('admin.roles.index')
|
||||
->with('error', "Aksi Ditolak: Role inti sistem [{$role->name}] tidak dapat dihapus.");
|
||||
|
||||
@@ -163,17 +163,13 @@ class GateInspectionController extends Controller
|
||||
$guardId = $request->input('security_guard_id') ?? ($currentGuard ? $currentGuard->id : null);
|
||||
|
||||
if (!$guardId) {
|
||||
$fallbackGuard = SecurityGuard::where('is_active', true)->first() ?? SecurityGuard::first();
|
||||
if ($fallbackGuard) {
|
||||
$guardId = $fallbackGuard->id;
|
||||
} else {
|
||||
$request->validate([
|
||||
'security_guard_id' => ['required', 'exists:security_guards,id'],
|
||||
], [
|
||||
'security_guard_id.required' => 'Silakan pilih nama petugas Satpam yang melakukan pemeriksaan.',
|
||||
]);
|
||||
$guardId = $request->input('security_guard_id');
|
||||
}
|
||||
$request->validate([
|
||||
'security_guard_id' => ['required', 'exists:security_guards,id'],
|
||||
], [
|
||||
'security_guard_id.required' => 'Silakan pilih nama petugas Satpam yang melakukan pemeriksaan fisik.',
|
||||
'security_guard_id.exists' => 'Petugas Satpam yang dipilih tidak valid dalam sistem.',
|
||||
]);
|
||||
$guardId = $request->input('security_guard_id');
|
||||
}
|
||||
|
||||
$activity = VehicleActivity::with(['vehicle', 'gate'])->findOrFail($id);
|
||||
|
||||
@@ -284,21 +284,23 @@ class SupportTimesheetController extends Controller
|
||||
: null;
|
||||
$operatingHours = $endMeter !== null ? max(0, $endMeter - $startMeter) : null;
|
||||
$shift = $validated['shift'] ?: 'Shift Siang';
|
||||
|
||||
$timesheetNo = UnitSupportTimesheet::generateTimesheetNo(Carbon::parse($validated['date']));
|
||||
$updaterName = auth()->user()?->name ?? 'Admin QMA';
|
||||
$timesheetNo = null;
|
||||
|
||||
DB::transaction(function () use (
|
||||
$validated,
|
||||
$vehicle,
|
||||
$operator,
|
||||
$timesheetNo,
|
||||
$startMeter,
|
||||
$endMeter,
|
||||
$operatingHours,
|
||||
$shift,
|
||||
$updaterName
|
||||
$updaterName,
|
||||
&$timesheetNo
|
||||
) {
|
||||
// Generate nomor timesheet unik di dalam transaksi untuk meminimalkan bentrokan
|
||||
$timesheetNo = UnitSupportTimesheet::generateTimesheetNo(Carbon::parse($validated['date']));
|
||||
|
||||
// 1. Simpan Data Timesheet Heavy Equipment
|
||||
$timesheet = UnitSupportTimesheet::create([
|
||||
'timesheet_no' => $timesheetNo,
|
||||
|
||||
@@ -67,6 +67,20 @@ class StoreVehicleActivityRequest extends FormRequest
|
||||
// 1. Validasi SIMPER Pengemudi (BR-01 & BR-02)
|
||||
$driver = Employee::find($this->input('driver_id'));
|
||||
if ($driver) {
|
||||
// Proteksi IDOR: Karyawan biasa tidak boleh memalsukan perjalanan atas nama karyawan lain yang memiliki akun sendiri
|
||||
$user = $this->user();
|
||||
if ($user) {
|
||||
$userEmployee = $user->getOrCreateEmployee();
|
||||
$isStaffAdmin = $user->isSuperAdmin() || $user->isPgAdmin() || $user->isGateOfficer() || $user->isHseAdmin() || $user->isQmaAdmin();
|
||||
if (!$isStaffAdmin && $userEmployee && $driver->id !== $userEmployee->id && !empty($driver->user_id) && $driver->user_id !== $user->id) {
|
||||
$validator->errors()->add(
|
||||
'driver_id',
|
||||
"Akses Ditolak: Anda tidak diizinkan mencatatkan aktivitas kendaraan atas nama karyawan lain ({$driver->name}). Gunakan akun pribadi Anda."
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$driver->is_operator) {
|
||||
$validator->errors()->add(
|
||||
'driver_id',
|
||||
|
||||
@@ -62,6 +62,7 @@ class UnitSupportTimesheet extends Model
|
||||
|
||||
/**
|
||||
* Generate nomor timesheet unik harian (Format: TS-US-YYYYMMDD-0001).
|
||||
* Dilengkapi mitigasi tabrakan concurrency dan pengecekan soft deleted records.
|
||||
*/
|
||||
public static function generateTimesheetNo(?Carbon $date = null): string
|
||||
{
|
||||
@@ -69,7 +70,8 @@ class UnitSupportTimesheet extends Model
|
||||
$dateStr = $d->format('Ymd');
|
||||
$prefix = "TS-US-{$dateStr}-";
|
||||
|
||||
$last = self::where('timesheet_no', 'like', "{$prefix}%")
|
||||
$last = self::withTrashed()
|
||||
->where('timesheet_no', 'like', "{$prefix}%")
|
||||
->orderByDesc('id')
|
||||
->first();
|
||||
|
||||
@@ -78,6 +80,14 @@ class UnitSupportTimesheet extends Model
|
||||
$seq = ((int) $matches[1]) + 1;
|
||||
}
|
||||
|
||||
return $prefix . str_pad((string) $seq, 4, '0', STR_PAD_LEFT);
|
||||
$candidate = $prefix . str_pad((string) $seq, 4, '0', STR_PAD_LEFT);
|
||||
|
||||
// Mitigasi bentrokan concurrency: cek apakah candidate sudah pernah digunakan (termasuk soft deletes)
|
||||
while (self::withTrashed()->where('timesheet_no', $candidate)->exists()) {
|
||||
$seq++;
|
||||
$candidate = $prefix . str_pad((string) $seq, 4, '0', STR_PAD_LEFT);
|
||||
}
|
||||
|
||||
return $candidate;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,7 @@ class SimpleXlsxImporter
|
||||
$sharedStrings = [];
|
||||
$sharedXmlContent = $zip->getFromName('xl/sharedStrings.xml');
|
||||
if ($sharedXmlContent) {
|
||||
$xml = @simplexml_load_string($sharedXmlContent);
|
||||
$xml = @simplexml_load_string($sharedXmlContent, 'SimpleXMLElement', LIBXML_NONET);
|
||||
if ($xml && isset($xml->si)) {
|
||||
foreach ($xml->si as $si) {
|
||||
if (isset($si->t)) {
|
||||
@@ -112,7 +112,7 @@ class SimpleXlsxImporter
|
||||
return $rows;
|
||||
}
|
||||
|
||||
$xml = @simplexml_load_string($sheetXmlContent);
|
||||
$xml = @simplexml_load_string($sheetXmlContent, 'SimpleXMLElement', LIBXML_NONET);
|
||||
$zip->close();
|
||||
|
||||
if (!$xml || !isset($xml->sheetData->row)) {
|
||||
|
||||
@@ -166,7 +166,7 @@
|
||||
<nav class="space-y-1">
|
||||
@if(auth()->user()->isSuperAdmin())
|
||||
<a href="{{ route('admin.dashboard') }}"
|
||||
class="flex items-center space-x-3 px-3.5 py-2.5 text-sm {{ request()->routeIs('admin.dashboard') || request()->routeIs('superadmin.dashboard') ? 'sidebar-item-active' : 'sidebar-item-inactive' }}">
|
||||
class="flex items-center space-x-3 px-3.5 py-2.5 text-sm {{ request()->routeIs('admin.dashboard') ? 'sidebar-item-active' : 'sidebar-item-inactive' }}">
|
||||
<svg class="w-5 h-5 flex-shrink-0" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<rect x="3" y="3" width="7" height="7" rx="2"></rect>
|
||||
<rect x="14" y="3" width="7" height="7" rx="2"></rect>
|
||||
@@ -575,7 +575,7 @@
|
||||
<div>
|
||||
<nav class="space-y-1">
|
||||
@if(auth()->user()->isSuperAdmin())
|
||||
<a href="{{ route('admin.dashboard') }}" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl text-sm {{ request()->routeIs('admin.dashboard') || request()->routeIs('superadmin.dashboard') ? 'sidebar-item-active' : 'sidebar-item-inactive' }}">
|
||||
<a href="{{ route('admin.dashboard') }}" class="flex items-center space-x-3 px-3 py-2.5 rounded-xl text-sm {{ request()->routeIs('admin.dashboard') ? 'sidebar-item-active' : 'sidebar-item-inactive' }}">
|
||||
<svg class="w-5 h-5 flex-shrink-0" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round">
|
||||
<rect x="3" y="3" width="7" height="7" rx="2"></rect>
|
||||
<rect x="14" y="3" width="7" height="7" rx="2"></rect>
|
||||
@@ -940,7 +940,7 @@
|
||||
<i class="fa-solid fa-bars text-base"></i>
|
||||
</button>
|
||||
<h1 class="text-lg sm:text-xl font-bold text-slate-900 tracking-tight">
|
||||
@if(request()->routeIs('admin.dashboard') || request()->routeIs('superadmin.dashboard'))
|
||||
@if(request()->routeIs('admin.dashboard'))
|
||||
Dashboard Admin
|
||||
@elseif(request()->routeIs('admin.users.*'))
|
||||
Kelola Pengguna
|
||||
|
||||
+2
-9
@@ -48,13 +48,6 @@ Route::prefix('gate-activity')->name('vehicle.activity.')->group(function () {
|
||||
// Group Terproteksi Autentikasi Sistem
|
||||
Route::middleware('auth')->group(function () {
|
||||
|
||||
// Backward Compatibility: Rute Super Admin Legacy -> Admin Dashboard Baru
|
||||
Route::prefix('superadmin')->name('superadmin.')->middleware('role:super_admin')->group(function () {
|
||||
Route::get('/', fn() => redirect()->route('admin.dashboard'));
|
||||
Route::get('/dashboard', fn() => redirect()->route('admin.dashboard'))->name('dashboard');
|
||||
Route::get('/roles', fn() => redirect()->route('admin.roles.index'))->name('roles.index');
|
||||
});
|
||||
|
||||
// 2. Modul HSE: Admin HSE - Kelola Karyawan & SIMPER
|
||||
Route::prefix('hse')->name('hse.')->group(function () {
|
||||
Route::get('/dashboard', [EmployeeController::class, 'dashboard'])->name('dashboard')->middleware('permission:hse.employees.view');
|
||||
@@ -225,10 +218,10 @@ Route::middleware('auth')->group(function () {
|
||||
// Backup & Restore Sistem (Database & Media)
|
||||
Route::get('/backups', [BackupController::class, 'index'])->name('backups.index');
|
||||
Route::post('/backups', [BackupController::class, 'store'])->name('backups.store');
|
||||
Route::post('/backups/restore-file', [BackupController::class, 'restoreFromFile'])->name('backups.restore_file');
|
||||
Route::post('/backups/restore-file', [BackupController::class, 'restoreFromFile'])->name('backups.restore_file')->middleware('throttle:5,1');
|
||||
Route::get('/backups/{filename}/download', [BackupController::class, 'download'])->name('backups.download');
|
||||
Route::post('/backups/upload', [BackupController::class, 'upload'])->name('backups.upload');
|
||||
Route::post('/backups/{filename}/restore', [BackupController::class, 'restore'])->name('backups.restore');
|
||||
Route::post('/backups/{filename}/restore', [BackupController::class, 'restore'])->name('backups.restore')->middleware('throttle:5,1');
|
||||
Route::delete('/backups/{filename}', [BackupController::class, 'destroy'])->name('backups.destroy');
|
||||
|
||||
// Tong Sampah / Recycle Bin & Retensi Otomatis
|
||||
|
||||
@@ -556,4 +556,112 @@ class SuperAdminRbacTest extends TestCase
|
||||
$this->assertEquals('Operator Turbin', $employee->position);
|
||||
$this->assertFalse((bool)$employee->is_operator);
|
||||
}
|
||||
|
||||
public function test_super_admin_cannot_delete_employee_core_role(): void
|
||||
{
|
||||
$superAdmin = User::where('username', 'superadmin')->first();
|
||||
$employeeRole = Role::where('name', 'employee')->first();
|
||||
$this->assertNotNull($employeeRole);
|
||||
|
||||
$response = $this->actingAs($superAdmin)->delete(route('admin.roles.destroy', $employeeRole->id));
|
||||
$response->assertRedirect(route('admin.roles.index'));
|
||||
$response->assertSessionHas('error');
|
||||
$this->assertDatabaseHas('roles', ['name' => 'employee']);
|
||||
}
|
||||
|
||||
public function test_unauthorized_employee_cannot_impersonate_another_employee_trip(): void
|
||||
{
|
||||
$gate = Gate::create([
|
||||
'code' => 'GT-01',
|
||||
'name' => 'Main Gate',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$vehicle = \App\Models\Vehicle::create([
|
||||
'code' => 'LV-SEC-01',
|
||||
'category' => 'light_vehicle',
|
||||
'brand_type' => 'Toyota Hilux',
|
||||
'license_plate' => 'BH 1111 XX',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 1000,
|
||||
]);
|
||||
|
||||
// User A (Karyawan A)
|
||||
$userA = User::factory()->create([
|
||||
'username' => 'EMP-AAA',
|
||||
'name' => 'Karyawan A',
|
||||
'role' => 'employee',
|
||||
]);
|
||||
$userA->assignRole('employee');
|
||||
$empA = $userA->getOrCreateEmployee();
|
||||
|
||||
// User B (Karyawan B yang sah sebagai driver)
|
||||
$userB = User::factory()->create([
|
||||
'username' => 'EMP-BBB',
|
||||
'name' => 'Karyawan B',
|
||||
'role' => 'employee',
|
||||
]);
|
||||
$userB->assignRole('employee');
|
||||
$empB = $userB->getOrCreateEmployee();
|
||||
|
||||
// Karyawan A mencoba submit atas nama Karyawan B
|
||||
$response = $this->actingAs($userA)->post(route('vehicle.activity.store', ['code' => $vehicle->code]), [
|
||||
'driver_id' => $empB->id,
|
||||
'gate_id' => $gate->id,
|
||||
'direction' => 'out',
|
||||
'current_km' => 1010,
|
||||
'purpose' => 'Test Tampering',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('driver_id');
|
||||
$this->assertDatabaseMissing('vehicle_activities', [
|
||||
'vehicle_id' => $vehicle->id,
|
||||
'driver_id' => $empB->id,
|
||||
'current_km' => 1010,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_timesheet_concurrency_generator_avoids_duplicate_numbers(): void
|
||||
{
|
||||
$today = now();
|
||||
$dateStr = $today->format('Ymd');
|
||||
$expectedNo1 = "TS-US-{$dateStr}-0001";
|
||||
|
||||
$no1 = \App\Models\UnitSupportTimesheet::generateTimesheetNo($today);
|
||||
$this->assertEquals($expectedNo1, $no1);
|
||||
|
||||
// Simulasikan ada record yang sudah dibuat dengan nomor tersebut
|
||||
$vehicle = \App\Models\Vehicle::create([
|
||||
'code' => 'HE-CONC-01',
|
||||
'category' => 'unit_support',
|
||||
'brand_type' => 'Excavator PC200',
|
||||
'status' => 'Operation',
|
||||
'current_km' => 500,
|
||||
]);
|
||||
|
||||
$emp = \App\Models\Employee::create([
|
||||
'nik' => 'OP-CONC-01',
|
||||
'name' => 'Operator Concurrency',
|
||||
'is_operator' => true,
|
||||
]);
|
||||
|
||||
\App\Models\UnitSupportTimesheet::create([
|
||||
'timesheet_no' => $expectedNo1,
|
||||
'vehicle_id' => $vehicle->id,
|
||||
'operator_id' => $emp->id,
|
||||
'date' => $today->toDateString(),
|
||||
'shift' => 'Shift Siang',
|
||||
'meter_type' => 'HM',
|
||||
'start_meter' => 100,
|
||||
'end_meter' => 108,
|
||||
'operating_hours' => 8,
|
||||
'location' => 'Pit A',
|
||||
'activity_type' => 'Galian',
|
||||
'status' => 'Operation',
|
||||
]);
|
||||
|
||||
// Generator harus mendeteksi nomor 0001 sudah ada dan otomatis menghasilkan 0002
|
||||
$no2 = \App\Models\UnitSupportTimesheet::generateTimesheetNo($today);
|
||||
$this->assertEquals("TS-US-{$dateStr}-0002", $no2);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user