raspisms/controllers/publics/Account.php

163 lines
7.0 KiB
PHP
Raw Normal View History

2019-10-29 14:57:13 +01:00
<?php
namespace controllers\publics;
2019-10-29 18:36:25 +01:00
class Account extends \descartes\Controller
2019-10-29 14:57:13 +01:00
{
public $internal_user;
2019-10-29 18:36:25 +01:00
public function __construct()
2019-10-29 14:57:13 +01:00
{
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
2019-10-29 14:57:13 +01:00
$this->internal_user = new \controllers\internals\User($bdd);
\controllers\internals\Tool::verifyconnect();
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
2019-10-29 14:57:13 +01:00
* Show profile page
2019-10-29 18:36:25 +01:00
*/
public function show()
2019-10-29 14:57:13 +01:00
{
$this->render('account/show');
2019-10-29 18:36:25 +01:00
}
2019-10-29 14:57:13 +01:00
2019-10-29 18:36:25 +01:00
/**
2019-10-29 14:57:13 +01:00
* Update connected user password
* @param $csrf : Le jeton CSRF
* @param string $_POST['password'] : The new password
* @return void;
*/
2019-10-29 18:36:25 +01:00
public function update_password($csrf)
2019-10-29 14:57:13 +01:00
{
2019-10-29 18:36:25 +01:00
$password = $_POST['password'] ?? false;
2019-10-29 14:57:13 +01:00
if (!$this->verify_csrf($csrf)) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
if (!$password) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous devez renseigner un mot de passe.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
$update_password_result = $this->internal_user->update_password($_SESSION['user']['id'], $password);
2019-10-29 18:36:25 +01:00
if (!$update_password_result) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Impossible de mettre à jour le mot de passe.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'Le mot de passe a bien été mis à jour.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
/**
* Update user mail transfer property
* @param $csrf : CSRF token
* @param string $_POST['transfer'] : New transfer property value
*/
2019-10-29 18:36:25 +01:00
public function update_transfer($csrf)
2019-10-29 14:57:13 +01:00
{
$transfer = $_POST['transfer'] ?? false;
if (!$this->verify_csrf($csrf)) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
if ($transfer === false) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous devez choisir une option parmis celles de la liste déroulante.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
$transfer_update_result = $this->internal_user->update_transfer($_SESSION['user']['id'], $transfer);
2019-10-29 18:36:25 +01:00
if (!$transfer_update_result) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Impossible de mettre à jour.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
$_SESSION['user']['transfer'] = $transfer;
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'Le transfert a bien été ' . ($transfer ? 'activé' : 'désactivé') . '.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
/**
* Update user email
* @param $csrf : Le jeton CSRF
* @param string $_POST['email'] : User new email
* @param string $_POST['verif_email'] : Verif email
*/
2019-10-29 18:36:25 +01:00
public function update_email($csrf)
2019-10-29 14:57:13 +01:00
{
if (!$this->verify_csrf($csrf)) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
$email = $_POST['email'] ?? false;
2019-10-29 18:36:25 +01:00
if (!$email) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Vous devez fournir une adresse e-mail !');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'L\'adresse e-mail n\'est pas une adresse valide.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
$update_email_result = $this->internal_user->update_email($_SESSION['user']['id'], $email);
2019-10-29 18:36:25 +01:00
if (!$update_email_result) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Impossible de mettre à jour.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
;
2019-10-29 14:57:13 +01:00
}
$_SESSION['user']['email'] = $email;
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('success', 'L\'email a bien été mis à jour.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
/**
2019-10-29 14:57:13 +01:00
* Delete a user
* @param string $_POST['delete_account'] : Boolean to see if we want to delete
* @return boolean;
*/
2019-10-29 18:36:25 +01:00
public function delete($csrf)
2019-10-29 14:57:13 +01:00
{
if (!$this->verify_csrf($csrf)) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Jeton CSRF invalid !');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
$delete_account = $_POST['delete_account'] ?? false;
2019-10-29 18:36:25 +01:00
if (!$delete_account) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Pour supprimer le compte, vous devez cocher la case correspondante.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
$delete_account_result = $this->internal_user->delete($_SESSION['user']['id']);
2019-10-29 18:36:25 +01:00
if (!$delete_account_result) {
\modules\DescartesSessionMessages\internals\DescartesSessionMessages::push('danger', 'Impossible de supprimer le compte.');
return $this->redirect(\descartes\Router::url('Account', 'show'));
2019-10-29 14:57:13 +01:00
}
return $this->logout();
}
/**
* Logout a user and redirect to login page
* @return null
2019-10-29 14:57:13 +01:00
*/
public function logout()
{
session_unset();
session_destroy();
return $this->redirect(\descartes\Router::url('Connect', 'login'));
2019-10-29 14:57:13 +01:00
}
2019-10-29 18:36:25 +01:00
}