access.class.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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"] = 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. . "FROM " . DB_T_TYPE_ACCESS . " "
  37. . "INNER JOIN " . DB_T_ACCESS . " ON " . DB_T_TYPE_ACCESS . ".id_access = " . DB_T_ACCESS . ".id "
  38. . "WHERE " . DB_T_TYPE_ACCESS . ".id_type = :id_type ");
  39. db::bind(':id_type', $idType);
  40. try {
  41. $tmp = db::resultset();
  42. foreach ($tmp as $access) {
  43. $return["access"] = self::addInArray($access["access"], $return["access"]);
  44. $return["noAccess"] = self::addInArray($access["noAccess"], $return["noAccess"]);
  45. }
  46. // Je supprime les restriction d'accès en fonction des accès accordés
  47. $return["noAccess"] = array_diff($return["noAccess"], $return["access"]);
  48. return $return;
  49. } catch (Exception $e) {
  50. return FALSE;
  51. }
  52. }
  53. public static function getAccessByType()
  54. {
  55. $return = array();
  56. db::query("SELECT "
  57. . "" . DB_T_USER_TYPE . ".type, "
  58. . "" . DB_T_ACCESS . ".label, "
  59. . "" . DB_T_ACCESS . ".show, "
  60. . "" . DB_T_ACCESS . ".add "
  61. . "FROM " . DB_T_ACCESS . " "
  62. . "INNER JOIN " . DB_T_USER_TYPE . " ON " . DB_T_USER_TYPE . ".id = " . DB_T_TYPE_ACCESS . ".id_type "
  63. . "INNER JOIN " . DB_T_TYPE_ACCESS . " ON " . DB_T_ACCESS . ".id = " . DB_T_TYPE_ACCESS . ".id_access");
  64. try {
  65. $tmp = db::resultset();
  66. foreach ($tmp as $access) {
  67. $return[$access["type"]][$access["label"]] = array(
  68. "show" => $access["show"],
  69. "add" => $access["add"],
  70. );
  71. }
  72. return $return;
  73. } catch (Exception $e) {
  74. return FALSE;
  75. }
  76. }
  77. public static function getTypesAccess()
  78. {
  79. db::query("SELECT "
  80. . "" . DB_T_ACCESS . ".label, "
  81. . "" . DB_T_ACCESS . ".show, "
  82. . "" . DB_T_ACCESS . ".add "
  83. . "FROM " . DB_T_ACCESS);
  84. try {
  85. $tmp = db::resultset();
  86. return $tmp;
  87. } catch (Exception $e) {
  88. return FALSE;
  89. }
  90. }
  91. public static function getTypesUsers(bool $_expect = FALSE)
  92. {
  93. $except = ($_expect == FALSE) ? NULL : " WHERE " . DB_T_USER_TYPE . ".id != 1 AND " . DB_T_USER_TYPE . ".id != 2";
  94. db::query("SELECT * FROM " . DB_T_USER_TYPE . $except);
  95. try {
  96. $tmp = db::resultset();
  97. return $tmp;
  98. } catch (Exception $e) {
  99. return FALSE;
  100. }
  101. }
  102. public static function getArrayTypes()
  103. {
  104. $return = $final = array();
  105. $getTypesUsers = self::getTypesUsers(TRUE);
  106. db::query("SELECT "
  107. . "CONCAT(" . DB_T_ACCESS . ".label, '|', " . DB_T_ACCESS . ".show, " . DB_T_ACCESS . ".add) AS access, "
  108. . "" . DB_T_TYPE_ACCESS . ".id_type, "
  109. . "" . DB_T_USER_TYPE . ".type "
  110. . "FROM " . DB_T_ACCESS . " "
  111. . "LEFT JOIN " . DB_T_TYPE_ACCESS . " ON " . DB_T_TYPE_ACCESS . ".id_access = " . DB_T_ACCESS . ".id "
  112. . "LEFT JOIN " . DB_T_USER_TYPE . " ON " . DB_T_TYPE_ACCESS . ".id_type = " . DB_T_USER_TYPE . ".id "
  113. );
  114. try {
  115. $tmp = db::resultset();
  116. foreach ($tmp as $access) {
  117. $tmpaccess = explode("|", $access["access"]);
  118. if($tmpaccess[1] == "10"){
  119. $return[$access["access"]]["access"] = "Accès à " . $tmpaccess[0] . " en lecture";
  120. } elseif($tmpaccess[1] == "01"){
  121. $return[$access["access"]]["access"] = "Accès à " . $tmpaccess[0] . " en écriture";
  122. } else {
  123. $return[$access["access"]]["access"] = "Accès à " . $tmpaccess[0] . " en lecture et écriture";
  124. }
  125. foreach ($getTypesUsers as $type) {
  126. if(isset($return[$access["access"]][$type["type"]]) AND $return[$access["access"]][$type["type"]] == 1)
  127. { $return[$access["access"]][$type["type"]] = 1;
  128. } elseif($type["id"] == $access["id_type"])
  129. { $return[$access["access"]][$type["type"]] = 1;
  130. } else
  131. { $return[$access["access"]][$type["type"]] = 0;
  132. }
  133. }
  134. $return[$access["access"]]["Administrateur"] = 1;
  135. }
  136. foreach ($return as $value) {
  137. $final[] = $value;
  138. }
  139. return $final;
  140. } catch (Exception $e) {
  141. return FALSE;
  142. }
  143. }
  144. public static function ifAccesss(string $_accessAsk, int $_idType = NULL)
  145. {
  146. if (session::isConnect() == FALSE and self::checkAccessOffLine($_accessAsk)) {
  147. return TRUE;
  148. }
  149. ($_idType == NULL) ? $idType = session::getType() : $idType = $_idType;
  150. if ($idType == 1) {
  151. return TRUE;
  152. } // Si Admin OK
  153. $accessList = self::getAccessList($idType);
  154. $cheminGenrique = self::checkGenericAccess($_accessAsk, $accessList["access"]);
  155. if ($cheminGenrique != FALSE AND !in_array($_accessAsk, $accessList["noAccess"])) { // Si Accès générique
  156. return TRUE;
  157. } elseif (in_array($_accessAsk, $accessList["access"]) or self::checkAccessWhite($_accessAsk)) {
  158. return TRUE;
  159. } else {
  160. return FALSE;
  161. }
  162. }
  163. private static function checkGenericAccess(string $_string, array $_access_list)
  164. {
  165. $string = explode("-", $_string)[0];
  166. $check = in_array($string."*", $_access_list);
  167. return ($check == TRUE) ? $string : FALSE;
  168. }
  169. private static function getGenericAccess(string $_string)
  170. {
  171. return explode("*", $_string);
  172. }
  173. private static function splitAccess(string $_string)
  174. {
  175. $return = array();
  176. $tmp = array_filter(explode("\n", $_string));
  177. foreach ($tmp as $key => $value) {
  178. $return[$key] = trim($value);
  179. }
  180. return $return;
  181. }
  182. private static function addInArray(string $_string, array $_array)
  183. {
  184. return array_unique(array_merge(self::splitAccess($_string), $_array));
  185. }
  186. }