|
Server : LiteSpeed System : Linux terra.hostitbro.com 5.14.0-611.54.3.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Thu May 7 16:31:24 EDT 2026 x86_64 User : outerorb ( 1091) PHP Version : 8.1.34 Disable Function : mail Directory : /home2/outerorb/.trash/survey_system/ | |
|
Path: /home2/outerorb/.trash/survey_system/user_create.php
Size: 7.78 KB
Permissions: 0666
<?php
require_once 'config.php';
// Check if user is logged in and is admin
if (!is_logged_in()) {
header("Location: index.php");
exit;
}
$user2 = current_user();
if ($user2['role'] !== 'admin') {
header("Location: dashboard.php");
exit;
}
$errors = [];
$username = '';
$full_name = '';
$role = 'agent';
// Fetch unique team names from users and teams tables
$teams = [];
$teams_stmt = $pdo->query('SELECT name FROM teams UNION SELECT DISTINCT team FROM users WHERE team IS NOT NULL AND team != "" ORDER BY name ASC');
if ($teams_stmt) {
$teams = $teams_stmt->fetchAll(PDO::FETCH_COLUMN);
}
$team = $teams[0] ?? '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$full_name = trim($_POST['full_name'] ?? '');
$password = $_POST['password'] ?? '';
$role = $_POST['role'] ?? 'agent';
$team = $_POST['team'] ?? 'BBSR';
if ($username === '') {
$errors[] = "Username is required.";
}
if ($full_name === '') {
$errors[] = "Full name is required.";
}
if ($password === '') {
$errors[] = "Password is required.";
}
if (!in_array($role, ['admin', 'agent', 'manager'])) {
$errors[] = "Invalid role selected.";
}
if (!in_array($team, $teams)) {
$errors[] = "Invalid team selected.";
}
if (empty($errors)) {
// Check if username already exists
$stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = ?");
$stmt->execute([$username]);
if ($stmt->fetchColumn() > 0) {
$errors[] = "Username already exists.";
require_once 'inc/eventlog.php';
log_event('user_create_failed', ['reason' => 'username_exists', 'username' => $username], 'failure');
} else {
// Insert new user
$stmt = $pdo->prepare("INSERT INTO users (username, full_name, password, role, team) VALUES (?, ?, ?, ?, ?)");
$stmt->execute([$username, $full_name, $password, $role, $team]);
require_once 'inc/eventlog.php';
log_event('user_created', ['new_username' => $username, 'role' => $role, 'team' => $team]);
header("Location: users.php?msg=User+created+successfully");
exit;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<?php include __DIR__ . '/inc/header.php'; ?>
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-lg-7 col-md-10">
<div class="card shadow-sm" style="background: #eaf4fb; border-radius: 18px;">
<div class="card-header d-flex align-items-center" style="background: transparent; color: #343a40; border-top-left-radius: 1rem; border-top-right-radius: 1rem; border-bottom: none;">
<i class="bi bi-person-plus-fill me-2 fs-4" style="color: #343a40;"></i>
<span style="font-size: 1.25rem; font-weight: 600; color: #343a40;">Create New User</span>
</div>
<div class="card-body p-4" style="border-bottom-left-radius: 1rem; border-bottom-right-radius: 1rem; color: #6c757d; font-size: 0.93rem;">
<?php if (!empty($errors)): ?>
<div class="alert alert-danger fs-5">
<ul class="mb-0">
<?php foreach ($errors as $error): ?>
<li><?= htmlspecialchars($error) ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<form method="post" action="user_create.php">
<div class="row g-3">
<div class="col-md-6">
<label for="username" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Username</label>
<div class="input-group input-group-sm">
<span class="input-group-text" style="font-size: 0.93rem;"><i class="bi bi-person-fill"></i></span>
<input type="text" class="form-control" style="font-size: 0.93rem;" id="username" name="username" value="<?= htmlspecialchars($username) ?>" required>
</div>
</div>
<div class="col-md-6">
<label for="full_name" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Full Name</label>
<div class="input-group input-group-sm">
<span class="input-group-text" style="font-size: 0.93rem;"><i class="bi bi-card-text"></i></span>
<input type="text" class="form-control" style="font-size: 0.93rem;" id="full_name" name="full_name" value="<?= htmlspecialchars($full_name) ?>" required>
</div>
</div>
<div class="col-md-6">
<label for="password" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Password</label>
<div class="input-group input-group-sm">
<span class="input-group-text" style="font-size: 0.93rem;"><i class="bi bi-key-fill"></i></span>
<input type="password" class="form-control" style="font-size: 0.93rem;" id="password" name="password" required>
</div>
</div>
<div class="col-md-6">
<label for="role" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Role</label>
<select class="form-select form-select-sm" style="font-size: 0.93rem;" id="role" name="role" required>
<option value="agent" <?= $role === 'agent' ? 'selected' : '' ?>>Agent</option>
<option value="admin" <?= $role === 'admin' ? 'selected' : '' ?>>Admin</option>
<option value="manager" <?= $role === 'manager' ? 'selected' : '' ?>>Manager</option>
</select>
</div>
<div class="col-md-6">
<label for="team" class="form-label fw-bold mb-1" style="font-size: 0.93rem;">Team</label>
<select class="form-select form-select-sm" style="font-size: 0.93rem;" id="team" name="team" required>
<?php foreach ($teams as $t): ?>
<option value="<?= htmlspecialchars($t) ?>" <?= $team === $t ? 'selected' : '' ?>><?= htmlspecialchars($t) ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-12 text-center mt-3">
<button type="submit" class="btn" style="background: #2563eb; color: #fff; font-size: 0.97rem; border-radius: 8px; font-weight: 500; padding: 0.45rem 1.1rem;"><i class="bi bi-person-plus me-2"></i>Create User</button>
<a href="users.php" class="btn btn-secondary ms-2" style="font-size: 0.97rem; border-radius: 8px; font-weight: 500; padding: 0.45rem 1.1rem; background: #e3eaf3; color: #2563eb; border: none;">Cancel</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php include __DIR__ . '/inc/footer.php'; ?>
</html>