user.class.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. . "WHERE " . DB_T_USER . ".deleted = 0");
  40. return db::resultset();
  41. }
  42. public static function getMyGoogleAuthenticator(int $_id){
  43. db::query("SELECT "
  44. . "" . DB_T_USER . ".googleAuthenticatorSecret "
  45. . "FROM " . DB_T_USER . " "
  46. . "WHERE " . DB_T_USER . ".id = :id");
  47. db::bind(':id', $_id);
  48. return db::single()["googleAuthenticatorSecret"];
  49. }
  50. public static function connect(array $_input) {
  51. $return = NULL;
  52. if (isset($_input["email"]) AND isset($_input["password"])) {
  53. db::query("SELECT id, email, password, prenom, nom, id_type, googleAuthenticator, googleAuthenticatorSecret, actif FROM " . DB_T_USER . " WHERE email = :email AND deleted = 0");
  54. db::bind(':email', $_input["email"]);
  55. $row = db::single();
  56. if($row["googleAuthenticator"] == 1 AND DOMAIN_CONTROL != $_SERVER['SERVER_NAME']){
  57. if(googleAuthenticator::verifyCode($row["googleAuthenticatorSecret"], $_input["authenticator"], 1) == FALSE){
  58. $row["id"] = NULL;
  59. }
  60. }
  61. if (isset($row["id"])) {
  62. if ($row["actif"] == 0) {
  63. alert::recError("Votre compte est désactivé");
  64. return FALSE;
  65. } elseif (isset($_input["password"]) AND md5($_input["password"]) == $row["password"]) {
  66. $_SESSION["user"] = array(
  67. "id" => $row["id"],
  68. "prenom" => $row["prenom"],
  69. "nom" => $row["nom"],
  70. "googleAuthenticator" => $row["googleAuthenticator"],
  71. "idType" => $row["id_type"],
  72. "email" => $row["email"],
  73. "actif" => $row["actif"]
  74. );
  75. self::updateLastConnect($row["id"]);
  76. return TRUE;
  77. } else {
  78. alert::recError("Erreur d'authentification");
  79. return FALSE;
  80. }
  81. } else {
  82. alert::recError("Erreur d'authentification");
  83. return FALSE;
  84. }
  85. } else {
  86. alert::recError("Erreur d'authentification");
  87. return FALSE;
  88. }
  89. }
  90. private static function updateLastConnect(int $_id){
  91. db::query("UPDATE " . DB_T_USER . " SET `last_connect` = CURRENT_TIMESTAMP() WHERE id = :id");
  92. db::bind(':id', $_id);
  93. db::execute();
  94. }
  95. public static function add_user(array $_input){
  96. db::query("INSERT INTO " . DB_T_USER . " "
  97. . "(email, password, prenom, nom, id_type, actif) "
  98. . "VALUES (:email, :password, :prenom, :nom, :id_type, :actif)");
  99. db::bind(':email', $_input["email"]);
  100. db::bind(':password', md5($_input["password"]));
  101. db::bind(':prenom', $_input["prenom"]);
  102. db::bind(':nom', $_input["nom"]);
  103. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  104. db::bind(':id_type', $_input["id_type"]);
  105. db::bind(':actif', $_input["actif"]);
  106. try {
  107. db::execute();
  108. alert::recSuccess("La création a bien été prise en compte");
  109. } catch (Exception $ex) {
  110. alert::recError("Erreur lors de la création de l'utilisateur");
  111. header("Location: /add-user.html");
  112. exit();
  113. }
  114. }
  115. public static function lastUser(){
  116. db::query("SELECT MAX(id) AS id FROM ". DB_T_USER);
  117. return db::single()["id"];
  118. }
  119. public static function maj_user(array $_input){
  120. if($_input["password"] != ""){
  121. db::query("UPDATE " . DB_T_USER . " SET password = :password WHERE id = :id");
  122. db::bind(':password', md5($_input["password"]));
  123. db::bind(':id', $_input["id"]);
  124. try {
  125. db::execute();
  126. } catch (Exception $ex) {
  127. alert::recError("Erreur lors de la modification du mot de passe");
  128. header("Location: /user-" . $_input["id"] .".html");
  129. exit();
  130. }
  131. }
  132. if(self::getMyGoogleAuthenticator($_input["id"]) == NULL){
  133. db::query("UPDATE " . DB_T_USER . " SET googleAuthenticatorSecret = :googleAuthenticatorSecret WHERE id = :id");
  134. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  135. db::bind(':id', $_input["id"]);
  136. try {
  137. db::execute();
  138. } catch (Exception $ex) {
  139. alert::recError("Erreur lors de la création du token de Google Authenticator");
  140. header("Location: /user-" . $_input["id"] .".html");
  141. exit();
  142. }
  143. }
  144. db::query("UPDATE " . DB_T_USER . " SET email = :email, prenom = :prenom, nom = :nom, id_type = :id_type, googleAuthenticator = :googleAuthenticator, actif = :actif WHERE id = :id");
  145. db::bind(':email', $_input["email"]);
  146. db::bind(':prenom', $_input["prenom"]);
  147. db::bind(':nom', $_input["nom"]);
  148. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  149. db::bind(':id_type', $_input["id_type"]);
  150. db::bind(':actif', $_input["actif"]);
  151. db::bind(':id', $_input["id"]);
  152. try {
  153. db::execute();
  154. alert::recSuccess("La modification a bien été prise en compte");
  155. } catch (Exception $ex) {
  156. alert::recError("Erreur lors de la modification de l'utilisateur");
  157. header("Location: /user-" . $_input["id"] . ".html");
  158. exit();
  159. }
  160. }
  161. public static function deleteUser(int $_id){
  162. db::query("UPDATE " . DB_T_USER . " SET deleted = 1 WHERE id = :id");
  163. db::bind(':id', $_id);
  164. try {
  165. db::execute();
  166. } catch (Exception $ex) {
  167. alert::recError("Erreur lors de la suppression");
  168. header("Location: /user-" . $_id .".html");
  169. exit();
  170. }
  171. }
  172. }