user.class.php 9.0 KB

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