1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-26 23:27:08 +00:00
plateforme-tfjm2/server_files/controllers/ajouter_organisateur.php
2019-09-08 12:45:48 +02:00

60 lines
1.8 KiB
PHP

<?php
if (!isset($_SESSION["role"]) || $_SESSION["role"] != Role::ADMIN)
require_once "server_files/403.php";
$has_error = false;
$error_message = null;
if (isset($_POST["submitted"])) {
$orga = new NewOrganizer($_POST);
try {
$orga->makeVerifications();
$orga->register();
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
}
class NewOrganizer {
public $surname;
public $first_name;
public $email;
public $admin;
public $password;
public function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
}
public function makeVerifications()
{
ensure($this->surname != null && $this->surname != "", "Le nom est invalide.");
ensure($this->first_name != null && $this->first_name != "", "Le prénom est invalide.");
ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse e-mail est invalide.");
$this->email = strtolower($this->email);
ensure(!userExists($this->email), "Cette adresse e-mail est déjà utilisée.");
$this->admin = $this->admin == "on" ? true : false;
}
public function register() {
global $DB, $YEAR;
$alphabet = "0123456789abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$this->password = "";
for ($i = 0; $i < 16; ++$i)
$this->password .= $alphabet[rand(0, strlen($alphabet) - 1)];
$req = $DB->prepare("INSERT INTO `users`(`email`, `pwd_hash`, `surname`, `first_name`, `role`, `year`)
VALUES (?, ?, ?, ?, ?, ?);");
$req->execute([$this->email, password_hash($this->password, PASSWORD_BCRYPT), $this->surname, $this->first_name, $this->admin ? "ADMIN" : "ORGANIZER", $YEAR]);
Mailer::sendAddOrganizerMail($this);
}
}
require_once "server_files/views/ajouter_organisateur.php";