Add user registration page with default operator role

This commit is contained in:
2026-06-06 19:41:03 -04:00
parent 59aca54d42
commit 516ffbb579
5 changed files with 128 additions and 0 deletions

View File

@@ -80,6 +80,65 @@ class AuthController
$this->view->redirect($intended);
}
public function registerForm(): void
{
if (Session::has('user_id')) {
$this->view->redirect('/dashboard');
}
$this->view->setLayout('auth');
$this->view->display('auth.register', [
'title' => 'Register - ServerManager',
]);
}
public function register(): void
{
$validator = new Validator();
$rules = [
'username' => 'required|min:3|max:50',
'email' => 'required|email',
'password' => 'required|min:8',
'confirm_password' => 'required|match:password',
];
if (!$validator->validate($_POST, $rules)) {
Session::setFlash('error', $validator->getFirstError());
$this->view->redirect('/register');
}
$username = $_POST['username'];
$email = $_POST['email'];
$existing = $this->userModel->findByUsername($username);
if ($existing) {
Session::setFlash('error', 'Username already exists.');
$this->view->redirect('/register');
}
$existingEmail = $this->userModel->findByEmail($email);
if ($existingEmail) {
Session::setFlash('error', 'Email already registered.');
$this->view->redirect('/register');
}
$id = $this->userModel->create([
'username' => $username,
'email' => $email,
'password' => $_POST['password'],
'role' => 'operator',
'is_active' => 1,
]);
$this->auditService->log('user_registered', 'user', $id, [
'username' => $username,
'role' => 'operator',
]);
Session::setFlash('success', 'Account created. You can now log in.');
$this->view->redirect('/login');
}
public function logout(): void
{
$this->auditService->log(