121 lines
6.1 KiB
PHP

<?php
/**
* TELVERO USER MANAGEMENT (ENV VERSION)
*/
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Load bootstrap (session, WordPress, autoload, env)
require_once __DIR__ . '/api/bootstrap.php';
// Load configuration (database)
require_once __DIR__ . '/api/config.php';
// Load authentication middleware
require_once __DIR__ . '/api/middleware/auth.php';
// Get database connection
$db = getDatabase();
// Ensure role column exists (this function also adds it if missing)
roleColumnExists($db);
// Require administrator role - will log unauthorized access and exit if not admin
requireAdmin($db, 'users.php');
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$user = $_POST['username'];
$pass = password_hash($_POST['password'], PASSWORD_DEFAULT);
$name = $_POST['full_name'];
$role = $_POST['role'] ?? 'agent';
$stmt = $db->prepare("INSERT INTO sales_users (username, password, full_name, role) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $user, $pass, $name, $role);
if ($stmt->execute()) {
$msg = "Gebruiker $user succesvol aangemaakt met rol: $role!";
} else {
$msg = "Fout bij aanmaken: " . $db->error;
}
}
// Get existing users for display
$users_result = $db->query("SELECT id, username, full_name, COALESCE(role, 'agent') as role FROM sales_users ORDER BY id DESC");
$existing_users = [];
if ($users_result) {
while ($row = $users_result->fetch_assoc()) {
$existing_users[] = $row;
}
}
?>
<!DOCTYPE html>
<html lang="nl">
<head>
<meta charset="UTF-8">
<title>User Management</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-slate-100 min-h-screen p-6">
<div class="max-w-4xl mx-auto">
<header class="flex justify-between items-center mb-8 bg-white p-6 rounded-3xl shadow-sm border-b-4 border-blue-600">
<h1 class="text-2xl font-black italic tracking-tighter">TELVERO <span class="text-blue-600">USERS</span></h1>
<a href="index.html" class="bg-slate-100 border px-6 py-2 rounded-2xl text-[10px] font-black uppercase tracking-widest hover:bg-slate-50 transition">Panel</a>
</header>
<div class="grid grid-cols-1 lg:grid-cols-2 gap-8">
<!-- Create User Form -->
<div class="bg-white p-10 rounded-[2.5rem] shadow-2xl">
<h2 class="text-xl font-black mb-8 italic text-center text-slate-800">NIEUWE <span class="text-blue-600">AGENT</span></h2>
<?php if(isset($msg)) echo "<div class='mb-6 p-4 bg-green-50 text-green-600 rounded-2xl text-sm font-bold border border-green-100 text-center'>$msg</div>"; ?>
<form method="POST" class="space-y-4">
<div>
<label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest mb-2 px-2">Gebruikersnaam</label>
<input type="text" name="username" placeholder="Bijv. agent_jan" class="w-full border-2 border-slate-50 p-4 rounded-2xl outline-none focus:border-blue-500 bg-slate-50 transition-all" required>
</div>
<div>
<label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest mb-2 px-2">Wachtwoord</label>
<input type="password" name="password" placeholder="••••••••" class="w-full border-2 border-slate-50 p-4 rounded-2xl outline-none focus:border-blue-500 bg-slate-50 transition-all" required>
</div>
<div>
<label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest mb-2 px-2">Volledige Naam</label>
<input type="text" name="full_name" placeholder="Jan de Vries" class="w-full border-2 border-slate-50 p-4 rounded-2xl outline-none focus:border-blue-500 bg-slate-50 transition-all" required>
</div>
<div>
<label class="block text-[10px] font-black text-slate-400 uppercase tracking-widest mb-2 px-2">Rol</label>
<select name="role" class="w-full border-2 border-slate-50 p-4 rounded-2xl outline-none focus:border-blue-500 bg-slate-50 transition-all font-bold">
<option value="agent">Agent</option>
<option value="administrator">Administrator</option>
</select>
</div>
<button type="submit" class="w-full bg-blue-600 text-white p-5 rounded-2xl font-black shadow-lg hover:bg-blue-700 transition active:scale-95 uppercase tracking-tighter">Agent Opslaan</button>
</form>
</div>
<!-- Existing Users List -->
<div class="bg-white p-10 rounded-[2.5rem] shadow-2xl">
<h2 class="text-xl font-black mb-8 italic text-center text-slate-800">BESTAANDE <span class="text-blue-600">AGENTS</span></h2>
<div class="space-y-3 max-h-[500px] overflow-y-auto">
<?php foreach($existing_users as $u): ?>
<div class="flex items-center justify-between p-4 bg-slate-50 rounded-2xl border border-slate-100">
<div>
<p class="font-black text-sm text-slate-800"><?php echo htmlspecialchars($u['full_name']); ?></p>
<p class="text-[10px] text-slate-400 font-bold">@<?php echo htmlspecialchars($u['username']); ?></p>
</div>
<span class="px-3 py-1 rounded-full text-[9px] font-black uppercase <?php echo $u['role'] === 'administrator' ? 'bg-purple-100 text-purple-600' : 'bg-blue-100 text-blue-600'; ?>">
<?php echo htmlspecialchars($u['role'] ?? 'agent'); ?>
</span>
</div>
<?php endforeach; ?>
<?php if(empty($existing_users)): ?>
<p class="text-center text-slate-400 text-sm italic">Geen gebruikers gevonden</p>
<?php endif; ?>
</div>
</div>
</div>
</div>
</body>
</html>