user.class.php 8.1 KB

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