feat(auth): set default employee password to 123456 and sync employee profile in super admin
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\AdminActivityLog;
|
||||
use App\Models\Employee;
|
||||
use App\Models\Gate;
|
||||
use App\Models\User;
|
||||
use App\Services\ImageCompressionService;
|
||||
@@ -23,7 +24,7 @@ class UserController extends Controller
|
||||
*/
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$query = User::with(['roles', 'gate']);
|
||||
$query = User::with(['roles', 'gate', 'employee']);
|
||||
|
||||
if ($request->filled('search')) {
|
||||
$search = trim($request->input('search'));
|
||||
@@ -75,6 +76,10 @@ 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'],
|
||||
'unit_kerja' => ['nullable', 'string', 'max:100'],
|
||||
'division' => ['nullable', 'string', 'max:100'],
|
||||
'position' => ['nullable', 'string', 'max:100'],
|
||||
'is_operator' => ['nullable', 'in:0,1'],
|
||||
], [
|
||||
'name.required' => 'Nama lengkap wajib diisi.',
|
||||
'username.required' => 'Username wajib diisi.',
|
||||
@@ -116,11 +121,29 @@ class UserController extends Controller
|
||||
// Reset permission cache Spatie
|
||||
app()[PermissionRegistrar::class]->forgetCachedPermissions();
|
||||
|
||||
// Jika user berstatus employee (role employee atau memiliki wewenang operasional karyawan), pastikan profil master Employee ada
|
||||
if ($user->isEmployee()) {
|
||||
$emp = $user->getOrCreateEmployee();
|
||||
if ($emp && $photoPath && empty($emp->photo_path)) {
|
||||
$emp->update(['photo_path' => $photoPath]);
|
||||
// Sinkronisasi profil master Employee jika role karyawan atau ada isian data karyawan
|
||||
$isEmployeeRole = $user->isEmployee() || $validated['role'] === 'employee';
|
||||
$hasEmployeeFields = !empty($validated['unit_kerja']) || !empty($validated['division']) || !empty($validated['position']) || (isset($validated['is_operator']) && $validated['is_operator'] !== '');
|
||||
|
||||
if ($isEmployeeRole || $hasEmployeeFields) {
|
||||
$isOperator = (isset($validated['is_operator']) && $validated['is_operator'] !== '')
|
||||
? (bool) $validated['is_operator']
|
||||
: ($isEmployeeRole ? true : false);
|
||||
$emp = Employee::where('user_id', $user->id)->orWhere('nik', $user->username)->first();
|
||||
$empData = [
|
||||
'user_id' => $user->id,
|
||||
'nik' => $user->username,
|
||||
'name' => $user->name,
|
||||
'unit_kerja' => $validated['unit_kerja'] ?? ($emp?->unit_kerja ?? 'Umum'),
|
||||
'division' => $validated['division'] ?? ($emp?->division ?? '-'),
|
||||
'position' => $validated['position'] ?? ($emp?->position ?? ($isEmployeeRole ? 'Driver Operasional' : '-')),
|
||||
'is_operator' => $isOperator,
|
||||
'photo_path' => $photoPath ?: $emp?->photo_path,
|
||||
];
|
||||
if ($emp) {
|
||||
$emp->update($empData);
|
||||
} else {
|
||||
Employee::create($empData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,6 +184,10 @@ 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'],
|
||||
'unit_kerja' => ['nullable', 'string', 'max:100'],
|
||||
'division' => ['nullable', 'string', 'max:100'],
|
||||
'position' => ['nullable', 'string', 'max:100'],
|
||||
'is_operator' => ['nullable', 'in:0,1'],
|
||||
], [
|
||||
'name.required' => 'Nama lengkap wajib diisi.',
|
||||
'username.required' => 'Username wajib diisi.',
|
||||
@@ -211,17 +238,42 @@ class UserController extends Controller
|
||||
// Reset permission cache Spatie
|
||||
app()[PermissionRegistrar::class]->forgetCachedPermissions();
|
||||
|
||||
// Sinkronkan data master Employee jika pengguna berstatus employee
|
||||
if ($user->employee) {
|
||||
$user->employee->update([
|
||||
// Sinkronkan data master Employee
|
||||
$emp = $user->employee ?: Employee::where('nik', $user->username)->first();
|
||||
$isEmployeeRole = $user->isEmployee() || $validated['role'] === 'employee';
|
||||
$hasEmployeeFields = !empty($validated['unit_kerja']) || !empty($validated['division']) || !empty($validated['position']) || (isset($validated['is_operator']) && $validated['is_operator'] !== '');
|
||||
|
||||
if ($emp || $isEmployeeRole || $hasEmployeeFields) {
|
||||
$empData = [
|
||||
'name' => $user->name,
|
||||
'nik' => $user->username,
|
||||
'photo_path' => $user->photo_path ?: $user->employee->photo_path,
|
||||
]);
|
||||
} elseif ($user->isEmployee()) {
|
||||
$emp = $user->getOrCreateEmployee();
|
||||
if ($emp && $user->photo_path) {
|
||||
$emp->update(['photo_path' => $user->photo_path]);
|
||||
'photo_path' => $user->photo_path ?: $emp?->photo_path,
|
||||
];
|
||||
if (array_key_exists('unit_kerja', $validated) && $validated['unit_kerja'] !== null) {
|
||||
$empData['unit_kerja'] = $validated['unit_kerja'];
|
||||
}
|
||||
if (array_key_exists('division', $validated) && $validated['division'] !== null) {
|
||||
$empData['division'] = $validated['division'];
|
||||
}
|
||||
if (array_key_exists('position', $validated) && $validated['position'] !== null) {
|
||||
$empData['position'] = $validated['position'];
|
||||
}
|
||||
if (array_key_exists('is_operator', $validated) && $validated['is_operator'] !== null && $validated['is_operator'] !== '') {
|
||||
$empData['is_operator'] = (bool) $validated['is_operator'];
|
||||
}
|
||||
|
||||
if ($emp) {
|
||||
$emp->update($empData);
|
||||
if (!$emp->user_id) {
|
||||
$emp->update(['user_id' => $user->id]);
|
||||
}
|
||||
} else {
|
||||
$empData['user_id'] = $user->id;
|
||||
$empData['unit_kerja'] = $empData['unit_kerja'] ?? 'Umum';
|
||||
$empData['division'] = $empData['division'] ?? '-';
|
||||
$empData['position'] = $empData['position'] ?? ($isEmployeeRole ? 'Driver Operasional' : '-');
|
||||
$empData['is_operator'] = $empData['is_operator'] ?? ($isEmployeeRole ? true : false);
|
||||
Employee::create($empData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -468,7 +468,7 @@ class EmployeeController extends Controller
|
||||
[
|
||||
'name' => $name,
|
||||
'email' => $cleanEmail,
|
||||
'password' => Hash::make('password'),
|
||||
'password' => Hash::make('123456'),
|
||||
'role' => 'employee',
|
||||
]
|
||||
);
|
||||
@@ -556,7 +556,7 @@ class EmployeeController extends Controller
|
||||
[
|
||||
'name' => $validated['name'],
|
||||
'email' => strtolower(str_replace(['-', ' '], '', $validated['nik'])) . '@kmh.co.id',
|
||||
'password' => Hash::make('password'),
|
||||
'password' => Hash::make('123456'),
|
||||
'role' => 'employee',
|
||||
]
|
||||
);
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace App\Models;
|
||||
use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
@@ -218,7 +218,13 @@
|
||||
@endif
|
||||
<div>
|
||||
<p class="font-bold text-slate-900 text-xs">{{ $user->name }}</p>
|
||||
<p class="text-[11px] text-slate-400 font-mono">@<span>{{ $user->username }}</span></p>
|
||||
<div class="flex items-center gap-1.5 text-[11px] text-slate-400">
|
||||
<span class="font-mono">@<span>{{ $user->username }}</span></span>
|
||||
@if($user->employee && ($user->employee->position || $user->employee->unit_kerja))
|
||||
<span>•</span>
|
||||
<span class="text-slate-500 font-medium">{{ $user->employee->position ?: $user->employee->unit_kerja }}</span>
|
||||
@endif
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
@@ -349,6 +355,51 @@
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Section: Data Profil Karyawan & Penugasan (Selaras Master HSE) -->
|
||||
<div class="pt-3 border-t border-slate-100 space-y-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<label class="text-xs font-bold text-slate-700 flex items-center gap-1.5">
|
||||
<i class="fa-solid fa-id-badge text-indigo-600"></i>
|
||||
<span>Data Profil Karyawan & Penugasan</span>
|
||||
</label>
|
||||
<span class="text-[10px] text-slate-400 font-medium">(Opsional)</span>
|
||||
</div>
|
||||
|
||||
<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>
|
||||
</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">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-2 gap-3">
|
||||
<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>
|
||||
<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">
|
||||
<option value="0">TIDAK (Bukan Driver)</option>
|
||||
<option value="1">YA (Aktif Mengemudi)</option>
|
||||
</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">
|
||||
Batal
|
||||
@@ -430,6 +481,51 @@
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<!-- Section: Data Profil Karyawan & Penugasan (Selaras Master HSE) -->
|
||||
<div class="pt-3 border-t border-slate-100 space-y-3">
|
||||
<div class="flex items-center justify-between">
|
||||
<label class="text-xs font-bold text-slate-700 flex items-center gap-1.5">
|
||||
<i class="fa-solid fa-id-badge text-indigo-600"></i>
|
||||
<span>Data Profil Karyawan & Penugasan</span>
|
||||
</label>
|
||||
<span class="text-[10px] text-slate-400 font-medium">(Opsional)</span>
|
||||
</div>
|
||||
|
||||
<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>
|
||||
</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">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid grid-cols-2 gap-3">
|
||||
<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>
|
||||
<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">
|
||||
<option value="0">TIDAK (Bukan Driver)</option>
|
||||
<option value="1">YA (Aktif Mengemudi)</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Accordion: Izin Tambahan Khusus (Direct Permissions) -->
|
||||
<div class="border border-slate-200 rounded-xl overflow-hidden bg-slate-50/50">
|
||||
<button type="button"
|
||||
@@ -674,6 +770,16 @@
|
||||
const editGateEl = document.getElementById('edit_gate_id');
|
||||
if (editGateEl) editGateEl.value = user.gate_id || '';
|
||||
|
||||
const emp = user.employee || {};
|
||||
const editUnitKerja = document.getElementById('edit_unit_kerja');
|
||||
if (editUnitKerja) editUnitKerja.value = emp.unit_kerja || '';
|
||||
const editDivision = document.getElementById('edit_division');
|
||||
if (editDivision) editDivision.value = emp.division || '';
|
||||
const editPosition = document.getElementById('edit_position');
|
||||
if (editPosition) editPosition.value = emp.position || '';
|
||||
const editIsOperator = document.getElementById('edit_is_operator');
|
||||
if (editIsOperator) editIsOperator.value = (emp.is_operator ? '1' : '0');
|
||||
|
||||
const editPhotoPreview = document.getElementById('editUserPhotoPreview');
|
||||
const editPhotoPlaceholder = document.getElementById('editUserPhotoPlaceholder');
|
||||
const editPhotoInput = document.getElementById('editUserPhotoInput');
|
||||
|
||||
@@ -287,6 +287,13 @@
|
||||
<form action="{{ route('hse.employees.store') }}" method="POST" enctype="multipart/form-data" class="mt-5 space-y-4">
|
||||
@csrf
|
||||
|
||||
<div class="p-3 rounded-2xl bg-emerald-50/70 border border-emerald-100 flex items-start gap-2.5">
|
||||
<i class="fa-solid fa-circle-info text-emerald-600 text-xs mt-0.5"></i>
|
||||
<div class="text-[11px] text-emerald-900 leading-relaxed">
|
||||
Akun login karyawan otomatis dibuatkan dengan <strong>Username NIK</strong> dan <strong>Password default: 123456</strong> (Role: Karyawan).
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label class="block text-xs font-bold text-slate-800 mb-1.5">Foto Profil Karyawan (Opsional)</label>
|
||||
<div class="flex items-center space-x-3.5 p-3 rounded-2xl border border-slate-200 bg-slate-50/50">
|
||||
@@ -408,6 +415,13 @@
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="mt-3.5 p-3 rounded-2xl bg-slate-50 border border-slate-100 flex items-start gap-2.5">
|
||||
<i class="fa-solid fa-shield-halved text-emerald-600 text-xs mt-0.5"></i>
|
||||
<div class="text-[11px] text-slate-600 leading-relaxed">
|
||||
Setiap data karyawan yang diimpor otomatis dibuatkan akun login sistem dengan <strong>Username NIK</strong>, <strong>Role Karyawan</strong>, dan <strong>Password default: 123456</strong>.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form action="{{ route('hse.employees.import') }}" method="POST" enctype="multipart/form-data" class="mt-4 space-y-4">
|
||||
@csrf
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@ use App\Models\User;
|
||||
use App\Services\SimpleXlsxExporter;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Spatie\Permission\Models\Permission;
|
||||
use Spatie\Permission\Models\Role;
|
||||
use Tests\TestCase;
|
||||
@@ -105,6 +106,11 @@ class HseEmployeeImportTest extends TestCase
|
||||
'username' => 'KMH-IMP-001',
|
||||
'name' => 'Budi Kurniawan',
|
||||
]);
|
||||
$importedUser = User::where('username', 'KMH-IMP-001')->first();
|
||||
$this->assertNotNull($importedUser);
|
||||
$this->assertTrue(Hash::check('123456', $importedUser->password));
|
||||
$this->assertTrue($importedUser->hasRole('employee'));
|
||||
|
||||
$this->assertDatabaseHas('users', [
|
||||
'username' => 'KMH-IMP-002',
|
||||
'name' => 'Sari Agustin',
|
||||
@@ -233,6 +239,10 @@ class HseEmployeeImportTest extends TestCase
|
||||
'username' => 'KMH-MANUAL-001',
|
||||
'name' => 'Dimas Anggara',
|
||||
]);
|
||||
$manualUser = User::where('username', 'KMH-MANUAL-001')->first();
|
||||
$this->assertNotNull($manualUser);
|
||||
$this->assertTrue(Hash::check('123456', $manualUser->password));
|
||||
$this->assertTrue($manualUser->hasRole('employee'));
|
||||
}
|
||||
|
||||
public function test_authorized_user_can_import_employees_with_only_basic_columns(): void
|
||||
|
||||
@@ -74,6 +74,10 @@ class SuperAdminRbacTest extends TestCase
|
||||
'email' => '[email protected]',
|
||||
'password' => 'password123',
|
||||
'role' => 'gate_officer',
|
||||
'unit_kerja' => 'Pembangkit',
|
||||
'division' => 'Operasi',
|
||||
'position' => 'Driver LV',
|
||||
'is_operator' => '1',
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route('admin.users.index'));
|
||||
@@ -83,6 +87,15 @@ class SuperAdminRbacTest extends TestCase
|
||||
$this->assertNotNull($newUser);
|
||||
$this->assertTrue($newUser->hasRole('gate_officer'));
|
||||
$this->assertEquals('gate_officer', $newUser->role);
|
||||
|
||||
$this->assertDatabaseHas('employees', [
|
||||
'nik' => 'operator.baru',
|
||||
'name' => 'Operator Baru',
|
||||
'unit_kerja' => 'Pembangkit',
|
||||
'division' => 'Operasi',
|
||||
'position' => 'Driver LV',
|
||||
'is_operator' => 1,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_super_admin_can_update_user_and_change_role(): void
|
||||
@@ -101,6 +114,10 @@ class SuperAdminRbacTest extends TestCase
|
||||
'username' => '1171',
|
||||
'email' => '[email protected]',
|
||||
'role' => 'hse_admin',
|
||||
'unit_kerja' => 'HSE',
|
||||
'division' => 'Safety',
|
||||
'position' => 'Officer HSE',
|
||||
'is_operator' => '0',
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route('admin.users.index'));
|
||||
@@ -108,6 +125,14 @@ class SuperAdminRbacTest extends TestCase
|
||||
$user->refresh();
|
||||
$this->assertEquals('Pinto Yuliandra Updated', $user->name);
|
||||
$this->assertTrue($user->hasRole('hse_admin'));
|
||||
|
||||
$this->assertDatabaseHas('employees', [
|
||||
'nik' => '1171',
|
||||
'unit_kerja' => 'HSE',
|
||||
'division' => 'Safety',
|
||||
'position' => 'Officer HSE',
|
||||
'is_operator' => 0,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_super_admin_cannot_delete_own_account_or_main_superadmin(): void
|
||||
|
||||
Reference in New Issue
Block a user