Files

34 lines
1.1 KiB
PHP

<?php
// Silencing warnings for CLI run
error_reporting(E_ERROR);
require_once __DIR__ . '/config/bootstrap.php';
$pdo = Database::get();
try {
$pdo->beginTransaction();
$hash = password_hash('password', PASSWORD_DEFAULT);
// Update all passwords
$pdo->exec("UPDATE users SET password_hash = '{$hash}'");
// Update admin
$pdo->exec("UPDATE users SET email = 'admin' WHERE role = 'admin'");
// Update petugas (handle multiple to avoid UNIQUE constraint violation)
$stmt = $pdo->query("SELECT id FROM users WHERE role IN ('field_officer', 'staff', 'petugas') ORDER BY id ASC");
$petugas = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($petugas as $index => $p) {
// If there's only one, it will be 'petugas'. If more, 'petugas', 'petugas2', etc.
$username = $index === 0 ? 'petugas' : 'petugas' . ($index + 1);
$pdo->exec("UPDATE users SET email = '{$username}' WHERE id = " . $p['id']);
}
$pdo->commit();
echo "Database updated successfully.\n";
} catch (Exception $e) {
$pdo->rollBack();
echo "Error updating database: " . $e->getMessage() . "\n";
}