user.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. class user {
  3. public function __construct() {
  4. }
  5. public static function getUserById(int $_id) {
  6. // Récupération des données de l'excel au format Json
  7. db::query("SELECT "
  8. . "" . DB_T_USER . ".id AS id, "
  9. . "" . DB_T_USER . ".email ,"
  10. . "" . DB_T_USER . ".prenom, "
  11. . "" . DB_T_USER . ".nom, "
  12. . "" . DB_T_USER . ".cree, "
  13. . "" . DB_T_USER . ".last_connect, "
  14. . "" . DB_T_USER . ".googleAuthenticator, "
  15. . "" . DB_T_USER . ".actif, "
  16. . "" . DB_T_USER . ".id_type, "
  17. . "" . DB_T_USER_TYPE . ".type "
  18. . "FROM " . DB_T_USER . " "
  19. . "INNER JOIN " . DB_T_USER_TYPE . " ON " . DB_T_USER . ".id_type = " . DB_T_USER_TYPE . ".id "
  20. . "WHERE " . DB_T_USER . ".id = :id");
  21. db::bind(':id', $_id);
  22. return db::single();
  23. }
  24. public static function getUsers() {
  25. // Récupération des données de l'excel au format Json
  26. db::query("SELECT "
  27. . "" . DB_T_USER . ".id AS id, "
  28. . "" . DB_T_USER . ".email ,"
  29. . "" . DB_T_USER . ".prenom, "
  30. . "" . DB_T_USER . ".nom, "
  31. . "" . DB_T_USER . ".cree, "
  32. . "" . DB_T_USER . ".last_connect, "
  33. . "" . DB_T_USER . ".googleAuthenticator, "
  34. . "" . DB_T_USER . ".actif, "
  35. . "" . DB_T_USER . ".id_type, "
  36. . "" . DB_T_USER_TYPE . ".type "
  37. . "FROM " . DB_T_USER . " "
  38. . "INNER JOIN " . DB_T_USER_TYPE . " ON " . DB_T_USER . ".id_type = " . DB_T_USER_TYPE . ".id");
  39. return db::resultset();
  40. }
  41. public static function getMyGoogleAuthenticator(int $_id){
  42. db::query("SELECT "
  43. . "" . DB_T_USER . ".googleAuthenticatorSecret "
  44. . "FROM " . DB_T_USER . " "
  45. . "WHERE " . DB_T_USER . ".id = :id");
  46. db::bind(':id', $_id);
  47. return db::single()["googleAuthenticatorSecret"];
  48. }
  49. public static function connect(array $_input) {
  50. $return = NULL;
  51. if (isset($_input["email"]) AND isset($_input["password"])) {
  52. db::query("SELECT id, email, password, prenom, nom, id_type, googleAuthenticator, googleAuthenticatorSecret, actif FROM " . DB_T_USER . " WHERE email = :email");
  53. db::bind(':email', $_input["email"]);
  54. $row = db::single();
  55. if($row["googleAuthenticator"] == 1){
  56. if(googleAuthenticator::verifyCode($row["googleAuthenticatorSecret"], $_input["authenticator"], 1) == FALSE){
  57. $row["id"] = NULL;
  58. }
  59. }
  60. if (isset($row["id"])) {
  61. if ($row["actif"] == 0) {
  62. alert::recError("Votre compte est désactivé");
  63. return FALSE;
  64. } elseif (isset($_input["password"]) AND md5($_input["password"]) == $row["password"]) {
  65. $_SESSION["user"] = array(
  66. "id" => $row["id"],
  67. "prenom" => $row["prenom"],
  68. "nom" => $row["nom"],
  69. "googleAuthenticator" => $row["googleAuthenticator"],
  70. "idType" => $row["id_type"],
  71. "email" => $row["email"],
  72. "actif" => $row["actif"]
  73. );
  74. self::updateLastConnect($row["id"]);
  75. return TRUE;
  76. } else {
  77. alert::recError("Erreur d'authentification");
  78. return FALSE;
  79. }
  80. } else {
  81. alert::recError("Erreur d'authentification");
  82. return FALSE;
  83. }
  84. } else {
  85. alert::recError("Erreur d'authentification");
  86. return FALSE;
  87. }
  88. }
  89. private static function updateLastConnect(int $_id){
  90. db::query("UPDATE " . DB_T_USER . " SET `last_connect` = CURRENT_TIMESTAMP() WHERE id = :id");
  91. db::bind(':id', $_id);
  92. db::execute();
  93. }
  94. public static function add_user(array $_input){
  95. db::query("INSERT INTO " . DB_T_USER . " "
  96. . "(email, password, prenom, nom, id_type, actif) "
  97. . "VALUES (:email, :password, :prenom, :nom, :id_type, :actif)");
  98. db::bind(':email', $_input["email"]);
  99. db::bind(':password', md5($_input["password"]));
  100. db::bind(':prenom', $_input["prenom"]);
  101. db::bind(':nom', $_input["nom"]);
  102. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  103. db::bind(':id_type', $_input["id_type"]);
  104. db::bind(':actif', $_input["actif"]);
  105. try {
  106. db::execute();
  107. alert::recSuccess("La création a bien été prise en compte");
  108. } catch (Exception $ex) {
  109. alert::recError("Erreur lors de la création de l'utilisateur");
  110. header("Location: /add-user.html");
  111. exit();
  112. }
  113. }
  114. public static function lastUser(){
  115. db::query("SELECT MAX(id) AS id FROM ". DB_T_USER);
  116. return db::single()["id"];
  117. }
  118. public static function maj_user(array $_input){
  119. if($_input["password"] != ""){
  120. db::query("UPDATE " . DB_T_USER . " SET password = :password WHERE id = :id");
  121. db::bind(':password', md5($_input["password"]));
  122. db::bind(':id', $_input["id"]);
  123. try {
  124. db::execute();
  125. } catch (Exception $ex) {
  126. alert::recError("Erreur lors de la modification de l'utilisateur");
  127. header("Location: /user-" . $_input["id"] .".html");
  128. exit();
  129. }
  130. }
  131. db::query("UPDATE " . DB_T_USER . " SET email = :email, prenom = :prenom, nom = :nom, id_type = :id_type, googleAuthenticator = :googleAuthenticator, actif = :actif WHERE id = :id");
  132. db::bind(':email', $_input["email"]);
  133. db::bind(':prenom', $_input["prenom"]);
  134. db::bind(':nom', $_input["nom"]);
  135. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  136. db::bind(':id_type', $_input["id_type"]);
  137. db::bind(':actif', $_input["actif"]);
  138. db::bind(':id', $_input["id"]);
  139. try {
  140. db::execute();
  141. alert::recSuccess("La modification a bien été prise en compte");
  142. } catch (Exception $ex) {
  143. alert::recError("Erreur lors de la modification de l'utilisateur");
  144. header("Location: /user-" . $_input["id"] . ".html");
  145. exit();
  146. }
  147. }
  148. public static function deleteUser(int $_id){
  149. db::query("DELETE FROM ". DB_T_USER ." WHERE id = :id");
  150. db::bind(':id', $_id);
  151. return db::execute();
  152. }
  153. }