1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-30 08:53:04 +00:00
plateforme-tfjm2/server_files/controllers/mon_compte.php

153 lines
4.6 KiB
PHP
Raw Normal View History

2019-09-06 11:48:50 +00:00
<?php
2019-09-07 15:26:30 +00:00
if (!isset($_SESSION["user_id"]))
require_once "server_files/403.php";
/** @var User $user */
$user = $_SESSION["user"];
$has_error = false;
$error_message = null;
2019-09-06 11:48:50 +00:00
if (isset($_POST["submitted"])) {
$my_account = new MyAccount($_POST);
try {
$my_account->makeVerifications();
$my_account->updateAccount();
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
}
2019-09-06 11:48:50 +00:00
}
if (isset($_POST["submitted_password"])) {
$new_password = new NewPassword($_POST);
try {
$new_password->makeVerifications();
$new_password->updatePassword();
2019-09-06 11:48:50 +00:00
}
catch (AssertionError $e) {
$has_error = true;
$error_message = $e->getMessage();
2019-09-06 11:48:50 +00:00
}
}
2019-09-06 11:48:50 +00:00
class MyAccount
{
public $email;
public $surname;
public $first_name;
public $birth_date;
public $gender;
public $address;
public $postal_code;
public $city;
public $country;
public $phone_number;
public $school;
public $class;
public $responsible_name;
public $responsible_phone;
public $responsible_email;
public $description;
private $user;
public function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
$this->user = $_SESSION["user"];
$keys = ["email", "surname", "first_name", "birth_date", "gender", "address", "postal_code", "city", "country", "phone_number",
"school", "class", "responsible_name", "responsible_phone", "responsible_email", "description"];
if ($this->user->getRole() == Role::PARTICIPANT)
$this->class = SchoolClass::fromName($this->class);
foreach ($keys as $key)
$this->$key = $this->$key != null && $this->$key != "" ? $this->$key : $this->user->$key;
2019-09-06 11:48:50 +00:00
}
public function makeVerifications()
{
global $YEAR;
ensure(filter_var($this->email, FILTER_VALIDATE_EMAIL), "L'adresse e-mail entrée est invalide.");
$this->email = strtolower($this->email);
ensure($this->email == $this->user->getEmail() || !userExists($this->email), "Un compte existe déjà avec cette adresse e-mail.");
ensure(dateWellFormed($this->birth_date), "La date de naissance est invalide.");
ensure($this->birth_date < $YEAR . "-01-01", "Vous devez être né.");
ensure($this->gender == "M" || $this->gender == "F", "Le sexe indiqué est invalide.");
ensure(preg_match("#^[0-9]{4}[0-9]?$#", $this->postal_code) && intval($this->postal_code) >= 01000 && intval($this->postal_code) <= 95999, "Le code postal est invalide.");
ensure(strlen($this->phone_number) >= 10, "Le numéro de téléphone est invalide.");
if ($this->user->getRole() == Role::PARTICIPANT) {
if ($this->birth_date > strval($YEAR - 18) . "04-01") {
ensure($this->responsible_name != "", "Veuillez spécifier un responsable légal.");
ensure(strlen($this->responsible_phone) >= 10, "Veuillez rentrer le numéro de téléphone de votre responsable légal.");
ensure(filter_var($this->responsible_email, FILTER_VALIDATE_EMAIL), "Veuillez spécifier un responsable légal.");
}
}
}
2019-09-06 11:48:50 +00:00
public function updateAccount()
{
$this->user->setSurname($this->surname);
$this->user->setFirstName($this->first_name);
$this->user->setBirthDate($this->birth_date);
$this->user->setGender($this->gender);
$this->user->setAddress($this->address);
$this->user->setPostalCode($this->postal_code);
$this->user->setCity($this->city);
$this->user->setCountry($this->country);
$this->user->setPhoneNumber($this->phone_number);
$this->user->setSchool($this->school);
$this->user->setClass($this->class);
$this->user->setResponsibleName($this->responsible_name);
$this->user->setResponsiblePhone($this->responsible_phone);
$this->user->setResponsibleEmail($this->responsible_email);
$this->user->setDescription($this->description);
if ($this->email != $this->user->getEmail()) {
$this->user->setEmail($this->email);
$this->user->setConfirmEmailToken(genRandomPhrase(64));
Mailer::sendChangeEmailAddressMail($this->user);
}
}
2019-09-06 11:48:50 +00:00
}
class NewPassword
2019-09-06 11:48:50 +00:00
{
private $user;
private $old_password;
private $new_password;
private $confirm_password;
2019-09-06 11:48:50 +00:00
public function __construct($data)
{
foreach ($data as $key => $value)
$this->$key = htmlspecialchars($value);
2019-09-06 11:48:50 +00:00
$this->user = $_SESSION["user"];
}
2019-09-06 11:48:50 +00:00
public function makeVerifications()
{
ensure($this->user->checkPassword($this->old_password), "L'ancien mot de passe est incorrect.");
ensure(strlen($this->new_password) >= 8, "Le mot de passe doit comporter au moins 8 caractères.");
ensure($this->new_password == $this->confirm_password, "Les deux mots de passe sont différents.");
}
2019-09-06 11:48:50 +00:00
public function updatePassword()
{
$this->user->setPassword($this->new_password);
2019-09-06 11:48:50 +00:00
Mailer::sendChangePasswordMail($this->user);
}
2019-09-06 11:48:50 +00:00
}
require_once "server_files/views/mon_compte.php";