Files
Webgis-Poverty/api/config/setup_users.php
T
2026-06-11 12:20:46 +07:00

52 lines
1.7 KiB
PHP

<?php
// api/config/setup_users.php
require_once __DIR__ . '/database.php';
$db = getDB();
// Create users table
$sql = "CREATE TABLE IF NOT EXISTS `users` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(50) NOT NULL UNIQUE,
`password` VARCHAR(255) NOT NULL,
`role` ENUM('admin', 'petugas_lapangan', 'guest') NOT NULL,
`rumah_ibadah_id` INT DEFAULT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (`rumah_ibadah_id`) REFERENCES `rumah_ibadah` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
if ($db->query($sql)) {
echo "Tabel 'users' berhasil dibuat atau sudah ada.\n";
} else {
echo "Gagal membuat tabel 'users': " . $db->error . "\n";
exit(1);
}
// Insert default users if they don't exist
$defaultUsers = [
[
'username' => 'admin',
'password' => password_hash('password', PASSWORD_BCRYPT),
'role' => 'admin'
]
];
foreach ($defaultUsers as $user) {
$stmt = $db->prepare("SELECT id FROM users WHERE username = ?");
$stmt->bind_param('s', $user['username']);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows === 0) {
$stmtInsert = $db->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)");
$stmtInsert->bind_param('sss', $user['username'], $user['password'], $user['role']);
if ($stmtInsert->execute()) {
echo "User '" . $user['username'] . "' berhasil ditambahkan.\n";
} else {
echo "Gagal menambahkan user '" . $user['username'] . "': " . $stmtInsert->error . "\n";
}
} else {
echo "User '" . $user['username'] . "' sudah ada.\n";
}
}
echo "Setup database selesai.\n";