user.class.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 . ".deleted, "
  17. . "" . DB_T_USER . ".id_type, "
  18. . "" . DB_T_USER_TYPE . ".type "
  19. . "FROM " . DB_T_USER . " "
  20. . "INNER JOIN " . DB_T_USER_TYPE . " ON " . DB_T_USER . ".id_type = " . DB_T_USER_TYPE . ".id "
  21. . "WHERE " . DB_T_USER . ".id = :id");
  22. db::bind(':id', $_id);
  23. return db::single();
  24. }
  25. public static function getUsers() {
  26. // Récupération des données de l'excel au format Json
  27. db::query("SELECT "
  28. . "" . DB_T_USER . ".id AS id, "
  29. . "" . DB_T_USER . ".email ,"
  30. . "" . DB_T_USER . ".prenom, "
  31. . "" . DB_T_USER . ".nom, "
  32. . "" . DB_T_USER . ".cree, "
  33. . "" . DB_T_USER . ".last_connect, "
  34. . "" . DB_T_USER . ".googleAuthenticator, "
  35. . "" . DB_T_USER . ".actif, "
  36. . "" . DB_T_USER . ".id_type, "
  37. . "" . DB_T_USER_TYPE . ".type "
  38. . "FROM " . DB_T_USER . " "
  39. . "INNER JOIN " . DB_T_USER_TYPE . " ON " . DB_T_USER . ".id_type = " . DB_T_USER_TYPE . ".id "
  40. . "WHERE " . DB_T_USER . ".deleted = 0");
  41. return db::resultset();
  42. }
  43. public static function getNameById(int $_id) {
  44. db::query("SELECT "
  45. . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'name' "
  46. . "FROM " . DB_T_USER . " "
  47. . "WHERE " . DB_T_USER . ".id = :id");
  48. db::bind(':id', $_id);
  49. return db::single()["name"];
  50. }
  51. public static function getMyGoogleAuthenticator(int $_id){
  52. db::query("SELECT "
  53. . "" . DB_T_USER . ".googleAuthenticatorSecret "
  54. . "FROM " . DB_T_USER . " "
  55. . "WHERE " . DB_T_USER . ".id = :id");
  56. db::bind(':id', $_id);
  57. return db::single()["googleAuthenticatorSecret"];
  58. }
  59. public static function connect(array $_input) {
  60. $return = NULL;
  61. if (isset($_input["email"]) AND isset($_input["password"])) {
  62. db::query("SELECT id, email, password, prenom, nom, id_type, googleAuthenticator, googleAuthenticatorSecret, actif FROM " . DB_T_USER . " WHERE email = :email AND deleted = 0");
  63. db::bind(':email', $_input["email"]);
  64. $row = db::single();
  65. if($row["googleAuthenticator"] == 1 AND DOMAIN_CONTROL != $_SERVER['SERVER_NAME']){
  66. if(googleAuthenticator::verifyCode($row["googleAuthenticatorSecret"], $_input["authenticator"], 1) == FALSE){
  67. $row["id"] = NULL;
  68. }
  69. }
  70. if (isset($row["id"])) {
  71. if ($row["actif"] == 0) {
  72. alert::recError("Votre compte est désactivé");
  73. return FALSE;
  74. } elseif (isset($_input["password"]) AND md5($_input["password"]) == $row["password"]) {
  75. $_SESSION["user"] = array(
  76. "id" => $row["id"],
  77. "prenom" => $row["prenom"],
  78. "nom" => $row["nom"],
  79. "googleAuthenticator" => $row["googleAuthenticator"],
  80. "idType" => $row["id_type"],
  81. "email" => $row["email"],
  82. "actif" => $row["actif"]
  83. );
  84. self::updateLastConnect($row["id"]);
  85. return TRUE;
  86. } else {
  87. alert::recError("Erreur d'authentification");
  88. return FALSE;
  89. }
  90. } else {
  91. alert::recError("Erreur d'authentification");
  92. return FALSE;
  93. }
  94. } else {
  95. alert::recError("Erreur d'authentification");
  96. return FALSE;
  97. }
  98. }
  99. private static function updateLastConnect(int $_id){
  100. db::query("UPDATE " . DB_T_USER . " SET `last_connect` = CURRENT_TIMESTAMP() WHERE id = :id");
  101. db::bind(':id', $_id);
  102. db::execute();
  103. }
  104. public static function add_user(array $_input){
  105. db::query("INSERT INTO " . DB_T_USER . " "
  106. . "(email, password, prenom, nom, id_type, actif) "
  107. . "VALUES (:email, :password, :prenom, :nom, :id_type, :actif)");
  108. db::bind(':email', $_input["email"]);
  109. db::bind(':password', md5($_input["password"]));
  110. db::bind(':prenom', $_input["prenom"]);
  111. db::bind(':nom', $_input["nom"]);
  112. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  113. db::bind(':id_type', $_input["id_type"]);
  114. db::bind(':actif', $_input["actif"]);
  115. try {
  116. db::execute();
  117. alert::recSuccess("La création a bien été prise en compte");
  118. } catch (Exception $ex) {
  119. alert::recError("Erreur lors de la création de l'utilisateur");
  120. header("Location: /add-user.html");
  121. exit();
  122. }
  123. }
  124. public static function lastUser(){
  125. db::query("SELECT MAX(id) AS id FROM ". DB_T_USER);
  126. return db::single()["id"];
  127. }
  128. public static function maj_user(array $_input){
  129. if($_input["password"] != ""){
  130. db::query("UPDATE " . DB_T_USER . " SET password = :password WHERE id = :id");
  131. db::bind(':password', md5($_input["password"]));
  132. db::bind(':id', $_input["id"]);
  133. try {
  134. db::execute();
  135. } catch (Exception $ex) {
  136. alert::recError("Erreur lors de la modification du mot de passe");
  137. header("Location: /user-" . $_input["id"] .".html");
  138. exit();
  139. }
  140. }
  141. if(self::getMyGoogleAuthenticator($_input["id"]) == NULL){
  142. db::query("UPDATE " . DB_T_USER . " SET googleAuthenticatorSecret = :googleAuthenticatorSecret WHERE id = :id");
  143. db::bind(':googleAuthenticatorSecret', googleAuthenticator::createSecret());
  144. db::bind(':id', $_input["id"]);
  145. try {
  146. db::execute();
  147. } catch (Exception $ex) {
  148. alert::recError("Erreur lors de la création du token de Google Authenticator");
  149. header("Location: /user-" . $_input["id"] .".html");
  150. exit();
  151. }
  152. }
  153. db::query("UPDATE " . DB_T_USER . " SET email = :email, prenom = :prenom, nom = :nom, id_type = :id_type, googleAuthenticator = :googleAuthenticator, actif = :actif WHERE id = :id");
  154. db::bind(':email', $_input["email"]);
  155. db::bind(':prenom', $_input["prenom"]);
  156. db::bind(':nom', $_input["nom"]);
  157. db::bind(':googleAuthenticator', $_input["googleAuthenticator"]);
  158. db::bind(':id_type', $_input["id_type"]);
  159. db::bind(':actif', $_input["actif"]);
  160. db::bind(':id', $_input["id"]);
  161. try {
  162. db::execute();
  163. alert::recSuccess("La modification a bien été prise en compte");
  164. } catch (Exception $ex) {
  165. alert::recError("Erreur lors de la modification de l'utilisateur");
  166. header("Location: /user-" . $_input["id"] . ".html");
  167. exit();
  168. }
  169. }
  170. public static function deleteUser(int $_id){
  171. db::query("UPDATE " . DB_T_USER . " SET deleted = 1 WHERE id = :id");
  172. db::bind(':id', $_id);
  173. try {
  174. db::execute();
  175. } catch (Exception $ex) {
  176. alert::recError("Erreur lors de la suppression");
  177. header("Location: /user-" . $_id .".html");
  178. exit();
  179. }
  180. }
  181. public static function restoreUser(int $_id){
  182. db::query("UPDATE " . DB_T_USER . " SET deleted = 0 WHERE id = :id");
  183. db::bind(':id', $_id);
  184. try {
  185. db::execute();
  186. } catch (Exception $ex) {
  187. alert::recError("Erreur lors de la restauration");
  188. header("Location: /user-" . $_id .".html");
  189. exit();
  190. }
  191. }
  192. }