feat(security): automatically generate user login accounts with default password 123456
This commit is contained in:
@@ -4,8 +4,11 @@ namespace App\Console\Commands;
|
|||||||
|
|
||||||
use App\Models\Gate;
|
use App\Models\Gate;
|
||||||
use App\Models\SecurityGuard;
|
use App\Models\SecurityGuard;
|
||||||
|
use App\Models\User;
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Spatie\Permission\Models\Role;
|
||||||
|
|
||||||
class ImportSecurityGuardsCommand extends Command
|
class ImportSecurityGuardsCommand extends Command
|
||||||
{
|
{
|
||||||
@@ -14,14 +17,14 @@ class ImportSecurityGuardsCommand extends Command
|
|||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $signature = 'security:import-plotingan';
|
protected $signature = 'security:import-plotingan {--password=123456 : Password default untuk akun user login satpam}';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The console command description.
|
* The console command description.
|
||||||
*
|
*
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $description = 'Import data personil Satpam lapangan dari berkas Plotingan Jaga Perpos ke database';
|
protected $description = 'Import data personil Satpam lapangan dari berkas Plotingan Jaga Perpos ke database beserta akun login User';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the console command.
|
* Execute the console command.
|
||||||
@@ -38,11 +41,18 @@ class ImportSecurityGuardsCommand extends Command
|
|||||||
$catalog = json_decode(File::get($jsonPath), true);
|
$catalog = json_decode(File::get($jsonPath), true);
|
||||||
$personnel = $catalog['personnel'] ?? [];
|
$personnel = $catalog['personnel'] ?? [];
|
||||||
|
|
||||||
|
$defaultPassword = (string) $this->option('password');
|
||||||
|
$defaultPasswordHash = Hash::make($defaultPassword);
|
||||||
|
|
||||||
$this->info("Ditemukan " . count($personnel) . " personil satpam dalam katalog.");
|
$this->info("Ditemukan " . count($personnel) . " personil satpam dalam katalog.");
|
||||||
|
$this->info("Password akun login default: {$defaultPassword}");
|
||||||
|
|
||||||
|
$gateOfficerRole = Role::firstOrCreate(['name' => 'gate_officer', 'guard_name' => 'web']);
|
||||||
|
|
||||||
$gateCache = [];
|
$gateCache = [];
|
||||||
$inserted = 0;
|
$insertedGuards = 0;
|
||||||
$updated = 0;
|
$updatedGuards = 0;
|
||||||
|
$usersCreated = 0;
|
||||||
|
|
||||||
$bar = $this->output->createProgressBar(count($personnel));
|
$bar = $this->output->createProgressBar(count($personnel));
|
||||||
$bar->start();
|
$bar->start();
|
||||||
@@ -57,20 +67,42 @@ class ImportSecurityGuardsCommand extends Command
|
|||||||
$gateCache[$gateCode] = $gate->id;
|
$gateCache[$gateCode] = $gate->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 1. Buat / Hubungkan Akun User untuk login di pos jaga
|
||||||
|
$username = strtoupper(trim($p['badge_id']));
|
||||||
|
$user = User::firstOrCreate(
|
||||||
|
['username' => $username],
|
||||||
|
[
|
||||||
|
'name' => $p['name'],
|
||||||
|
'email' => strtolower($username) . '@kmh.co.id',
|
||||||
|
'password' => $defaultPasswordHash,
|
||||||
|
'role' => 'gate_officer',
|
||||||
|
'gate_id' => $gateCache[$gateCode],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($user->wasRecentlyCreated) {
|
||||||
|
$usersCreated++;
|
||||||
|
if (!$user->hasRole('gate_officer')) {
|
||||||
|
$user->assignRole($gateOfficerRole);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Simpan / Hubungkan Data Master Satpam
|
||||||
$guard = SecurityGuard::updateOrCreate(
|
$guard = SecurityGuard::updateOrCreate(
|
||||||
['badge_id' => $p['badge_id']],
|
['badge_id' => $p['badge_id']],
|
||||||
[
|
[
|
||||||
'name' => $p['name'],
|
'name' => $p['name'],
|
||||||
'team' => $p['team'],
|
'team' => $p['team'],
|
||||||
'gate_id' => $gateCache[$gateCode],
|
'gate_id' => $gateCache[$gateCode],
|
||||||
|
'user_id' => $user->id,
|
||||||
'is_active' => $p['is_active'] ?? true,
|
'is_active' => $p['is_active'] ?? true,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($guard->wasRecentlyCreated) {
|
if ($guard->wasRecentlyCreated) {
|
||||||
$inserted++;
|
$insertedGuards++;
|
||||||
} else {
|
} else {
|
||||||
$updated++;
|
$updatedGuards++;
|
||||||
}
|
}
|
||||||
|
|
||||||
$bar->advance();
|
$bar->advance();
|
||||||
@@ -81,14 +113,16 @@ class ImportSecurityGuardsCommand extends Command
|
|||||||
|
|
||||||
$this->info("=== SINKRONISASI SELESAI ===");
|
$this->info("=== SINKRONISASI SELESAI ===");
|
||||||
$this->table(
|
$this->table(
|
||||||
['Metrik', 'Jumlah'],
|
['Metrik', 'Keterangan'],
|
||||||
[
|
[
|
||||||
['Total Personil Masuk', count($personnel)],
|
['Total Personil', count($personnel)],
|
||||||
['Data Baru Ditambahkan', $inserted],
|
['Satpam Ditambahkan / Diupdate', "{$insertedGuards} baru / {$updatedGuards} diupdate"],
|
||||||
['Data Diperbarui', $updated],
|
['Akun Login User Dibuat', $usersCreated],
|
||||||
['Regu 1 (R1)', $catalog['teams']['team_1'] ?? 0],
|
['Password Default Akun', $defaultPassword],
|
||||||
['Regu 2 (R2)', $catalog['teams']['team_2'] ?? 0],
|
['Role Akun Login', 'gate_officer (Petugas Pos Gerbang)'],
|
||||||
['Regu 3 (R3)', $catalog['teams']['team_3'] ?? 0],
|
['Regu 1 (R1)', ($catalog['teams']['team_1'] ?? 0) . ' Personil (R1-001 s/d R1-029)'],
|
||||||
|
['Regu 2 (R2)', ($catalog['teams']['team_2'] ?? 0) . ' Personil (R2-001 s/d R2-029)'],
|
||||||
|
['Regu 3 (R3)', ($catalog['teams']['team_3'] ?? 0) . ' Personil (R3-001 s/d R3-029)'],
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -4,14 +4,18 @@ namespace Database\Seeders;
|
|||||||
|
|
||||||
use App\Models\Gate;
|
use App\Models\Gate;
|
||||||
use App\Models\SecurityGuard;
|
use App\Models\SecurityGuard;
|
||||||
|
use App\Models\User;
|
||||||
use Illuminate\Database\Seeder;
|
use Illuminate\Database\Seeder;
|
||||||
use Illuminate\Support\Facades\File;
|
use Illuminate\Support\Facades\File;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
|
use Spatie\Permission\Models\Role;
|
||||||
|
|
||||||
class SecurityGuardSeeder extends Seeder
|
class SecurityGuardSeeder extends Seeder
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Run the database seeds.
|
* Run the database seeds.
|
||||||
* Mengimpor data personil satpam lapangan resmi dari Plotingan Jaga Perpos.
|
* Mengimpor data personil satpam lapangan resmi dari Plotingan Jaga Perpos
|
||||||
|
* sekaligus membuatkan akun login User (role: gate_officer) dengan password default 123456.
|
||||||
*/
|
*/
|
||||||
public function run(): void
|
public function run(): void
|
||||||
{
|
{
|
||||||
@@ -25,11 +29,15 @@ class SecurityGuardSeeder extends Seeder
|
|||||||
$data = json_decode(File::get($jsonPath), true);
|
$data = json_decode(File::get($jsonPath), true);
|
||||||
$personnel = $data['personnel'] ?? [];
|
$personnel = $data['personnel'] ?? [];
|
||||||
|
|
||||||
$this->command?->info("Memulai sinkronisasi " . count($personnel) . " personil Satpam...");
|
$this->command?->info("Memulai sinkronisasi " . count($personnel) . " personil Satpam beserta akun login...");
|
||||||
|
|
||||||
|
$gateOfficerRole = Role::firstOrCreate(['name' => 'gate_officer', 'guard_name' => 'web']);
|
||||||
|
$defaultPasswordHash = Hash::make('123456');
|
||||||
|
|
||||||
$gateCache = [];
|
$gateCache = [];
|
||||||
$inserted = 0;
|
$insertedGuards = 0;
|
||||||
$updated = 0;
|
$updatedGuards = 0;
|
||||||
|
$usersCreated = 0;
|
||||||
|
|
||||||
foreach ($personnel as $p) {
|
foreach ($personnel as $p) {
|
||||||
$gateCode = $p['gate_code'];
|
$gateCode = $p['gate_code'];
|
||||||
@@ -41,23 +49,45 @@ class SecurityGuardSeeder extends Seeder
|
|||||||
$gateCache[$gateCode] = $gate->id;
|
$gateCache[$gateCode] = $gate->id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 1. Buat / Ambil Akun Login User untuk Satpam
|
||||||
|
$username = strtoupper(trim($p['badge_id']));
|
||||||
|
$user = User::firstOrCreate(
|
||||||
|
['username' => $username],
|
||||||
|
[
|
||||||
|
'name' => $p['name'],
|
||||||
|
'email' => strtolower($username) . '@kmh.co.id',
|
||||||
|
'password' => $defaultPasswordHash,
|
||||||
|
'role' => 'gate_officer',
|
||||||
|
'gate_id' => $gateCache[$gateCode],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($user->wasRecentlyCreated) {
|
||||||
|
$usersCreated++;
|
||||||
|
if (!$user->hasRole('gate_officer')) {
|
||||||
|
$user->assignRole($gateOfficerRole);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Simpan Data Satpam
|
||||||
$guard = SecurityGuard::updateOrCreate(
|
$guard = SecurityGuard::updateOrCreate(
|
||||||
['badge_id' => $p['badge_id']],
|
['badge_id' => $p['badge_id']],
|
||||||
[
|
[
|
||||||
'name' => $p['name'],
|
'name' => $p['name'],
|
||||||
'team' => $p['team'],
|
'team' => $p['team'],
|
||||||
'gate_id' => $gateCache[$gateCode],
|
'gate_id' => $gateCache[$gateCode],
|
||||||
|
'user_id' => $user->id,
|
||||||
'is_active' => $p['is_active'] ?? true,
|
'is_active' => $p['is_active'] ?? true,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($guard->wasRecentlyCreated) {
|
if ($guard->wasRecentlyCreated) {
|
||||||
$inserted++;
|
$insertedGuards++;
|
||||||
} else {
|
} else {
|
||||||
$updated++;
|
$updatedGuards++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->command?->info("Selesai! {$inserted} personil baru ditambahkan, {$updated} personil diperbarui.");
|
$this->command?->info("Selesai! {$insertedGuards} satpam baru, {$updatedGuards} satpam diperbarui, {$usersCreated} akun login user dibuat (password default: 123456).");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user