2
0

user.class.php 9.1 KB

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