access.class.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. class access
  3. {
  4. public static function check(string $_element)
  5. {
  6. // Eléments autorisé sans authentification
  7. if (self::checkAccessWhite($_element)) {
  8. return TRUE;
  9. } else {
  10. if (session::isConnect("salarie") and session::isEspaceSalaries()) { // Espaces spécifiques aux Salariés
  11. return TRUE;
  12. } elseif (session::isConnect() and session::getType() == 2 and session::isEspaceControleurs()) { // Espaces spécifiques aux Contrôleurs
  13. return TRUE;
  14. } else {
  15. return self::ifAccesss($_element);
  16. }
  17. }
  18. }
  19. public static function checkAccessOffLine(string $_string)
  20. {
  21. return in_array($_string, OFF_LINE);
  22. }
  23. public static function checkAccessWhite(string $_string)
  24. {
  25. return in_array($_string, WHITE_ACCESS);
  26. }
  27. public static function getAccessList(?int $_idType = NULL)
  28. {
  29. ($_idType == NULL) ? $idType = session::getType() : $idType = $_idType;
  30. $return["access"] = $return["noAccess"] = $return["exception"] = array();
  31. db::query("SELECT "
  32. . "" . DB_T_ACCESS . ".id, "
  33. . "" . DB_T_ACCESS . ".label, "
  34. . "" . DB_T_ACCESS . ".access, "
  35. . "" . DB_T_ACCESS . ".noAccess, "
  36. . "" . DB_T_TYPE_ACCESS . ".exception "
  37. . "FROM " . DB_T_TYPE_ACCESS . " "
  38. . "INNER JOIN " . DB_T_ACCESS . " ON " . DB_T_TYPE_ACCESS . ".id_access = " . DB_T_ACCESS . ".id "
  39. . "WHERE " . DB_T_TYPE_ACCESS . ".id_type = :id_type ");
  40. db::bind(':id_type', $idType);
  41. try {
  42. $tmp = db::resultset();
  43. foreach ($tmp as $access) {
  44. $return["access"] = self::addInArray($access["access"], $return["access"]);
  45. $return["noAccess"] = self::addInArray($access["noAccess"], $return["noAccess"]);
  46. if(isset($access["exception"])) { $return["exception"] = self::addInArray($access["exception"], $return["exception"]); }
  47. }
  48. // Je supprime les restriction d'accès en fonction des accès accordés
  49. $return["noAccess"] = array_diff($return["noAccess"], $return["access"]);
  50. return $return;
  51. } catch (Exception $e) {
  52. return FALSE;
  53. }
  54. }
  55. public static function getAccessByType()
  56. {
  57. $return = array();
  58. db::query("SELECT "
  59. . "" . DB_T_TYPE_USER . ".type, "
  60. . "" . DB_T_ACCESS . ".label, "
  61. . "" . DB_T_ACCESS . ".show, "
  62. . "" . DB_T_ACCESS . ".add "
  63. . "FROM " . DB_T_ACCESS . " "
  64. . "INNER JOIN " . DB_T_TYPE_USER . " ON " . DB_T_TYPE_USER . ".id = " . DB_T_TYPE_ACCESS . ".id_type "
  65. . "INNER JOIN " . DB_T_TYPE_ACCESS . " ON " . DB_T_ACCESS . ".id = " . DB_T_TYPE_ACCESS . ".id_access");
  66. try {
  67. $tmp = db::resultset();
  68. foreach ($tmp as $access) {
  69. $return[$access["type"]][$access["label"]] = array(
  70. "show" => $access["show"],
  71. "add" => $access["add"],
  72. );
  73. }
  74. return $return;
  75. } catch (Exception $e) {
  76. return FALSE;
  77. }
  78. }
  79. public static function getTypesAccess()
  80. {
  81. db::query("SELECT "
  82. . "" . DB_T_ACCESS . ".label, "
  83. . "" . DB_T_ACCESS . ".show, "
  84. . "" . DB_T_ACCESS . ".add "
  85. . "FROM " . DB_T_ACCESS);
  86. try {
  87. $tmp = db::resultset();
  88. return $tmp;
  89. } catch (Exception $e) {
  90. return FALSE;
  91. }
  92. }
  93. public static function getTypesUsers(bool $_expect = FALSE)
  94. {
  95. $except = ($_expect == FALSE) ? NULL : " WHERE " . DB_T_TYPE_USER . ".id != 1 AND " . DB_T_TYPE_USER . ".id != 2";
  96. db::query("SELECT * FROM " . DB_T_TYPE_USER . $except);
  97. try {
  98. $tmp = db::resultset();
  99. return $tmp;
  100. } catch (Exception $e) {
  101. return FALSE;
  102. }
  103. }
  104. public static function getArrayTypes()
  105. {
  106. $return = $final = array();
  107. $getTypesUsers = self::getTypesUsers(TRUE);
  108. db::query("SELECT "
  109. . "CONCAT(" . DB_T_ACCESS . ".label, '|', " . DB_T_ACCESS . ".show, " . DB_T_ACCESS . ".add) AS access, "
  110. . "" . DB_T_TYPE_ACCESS . ".id_type, "
  111. . "" . DB_T_TYPE_ACCESS . ".exception, "
  112. . "" . DB_T_TYPE_USER . ".type "
  113. . "FROM " . DB_T_ACCESS . " "
  114. . "LEFT JOIN " . DB_T_TYPE_ACCESS . " ON " . DB_T_TYPE_ACCESS . ".id_access = " . DB_T_ACCESS . ".id "
  115. . "LEFT JOIN " . DB_T_TYPE_USER . " ON " . DB_T_TYPE_ACCESS . ".id_type = " . DB_T_TYPE_USER . ".id "
  116. );
  117. try {
  118. $tmp = db::resultset();
  119. foreach ($tmp as $access) {
  120. $tmpaccess = explode("|", $access["access"]);
  121. if($tmpaccess[1] == "10"){
  122. $return[$access["access"]]["access"] = "Accès à " . $tmpaccess[0] . " en lecture";
  123. } elseif($tmpaccess[1] == "01"){
  124. $return[$access["access"]]["access"] = "Accès à " . $tmpaccess[0] . " en écriture";
  125. } else {
  126. $return[$access["access"]]["access"] = "Accès à " . $tmpaccess[0] . " en lecture et écriture";
  127. }
  128. foreach ($getTypesUsers as $type) {
  129. if (empty($return[$access["access"]][$type["type"]])) {
  130. if ($type["id"] == $access["id_type"]) {
  131. if(is_null($access["exception"])){
  132. $return[$access["access"]][$type["type"]] = 1;
  133. } else {
  134. $return[$access["access"]][$type["type"]] = 2;
  135. }
  136. }
  137. else {
  138. $return[$access["access"]][$type["type"]] = 0;
  139. }
  140. }
  141. }
  142. $return[$access["access"]]["Administrateur"] = 1;
  143. }
  144. foreach ($return as $value) {
  145. $final[] = $value;
  146. }
  147. return $final;
  148. } catch (Exception $e) {
  149. return FALSE;
  150. }
  151. }
  152. public static function ifLimitAccessException(string $_exception)
  153. {
  154. $accessList = self::getAccessList(session::getType());
  155. return in_array($_exception, $accessList["exception"]) ? TRUE : FALSE;
  156. }
  157. public static function ifAccesss(string $_accessAsk, ?int $_idType = NULL)
  158. {
  159. if (session::isConnect() == FALSE and self::checkAccessOffLine($_accessAsk)) {
  160. return TRUE;
  161. }
  162. // Si Admin OK
  163. $idType = $_idType == NULL ? session::getType() : $_idType;
  164. if ($idType == 1) {
  165. return TRUE;
  166. }
  167. // Si Admin OK
  168. $accessList = self::getAccessList($idType);
  169. $cheminGenrique = self::checkGenericAccess($_accessAsk, $accessList["access"]);
  170. if ($cheminGenrique != FALSE AND !in_array($_accessAsk, $accessList["noAccess"])) { // Si Accès générique
  171. return TRUE;
  172. } elseif (in_array($_accessAsk, $accessList["access"]) or self::checkAccessWhite($_accessAsk)) {
  173. return TRUE;
  174. } else {
  175. return FALSE;
  176. }
  177. }
  178. private static function checkGenericAccess(string $_string, array $_access_list)
  179. {
  180. $string = explode("-", $_string)[0];
  181. $check = in_array($string."*", $_access_list);
  182. return ($check == TRUE) ? $string : FALSE;
  183. }
  184. private static function getGenericAccess(string $_string)
  185. {
  186. return explode("*", $_string);
  187. }
  188. private static function splitAccess(string $_string)
  189. {
  190. $return = array();
  191. $tmp = array_filter(explode("\n", $_string));
  192. foreach ($tmp as $key => $value) {
  193. $return[$key] = trim($value);
  194. }
  195. return $return;
  196. }
  197. private static function addInArray(string $_string, array $_array)
  198. {
  199. return array_unique(array_merge(self::splitAccess($_string), $_array));
  200. }
  201. }