2
0

access.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 . ".access, "
  34. . "" . DB_T_ACCESS . ".noAccess, "
  35. . "" . DB_T_ACCESS_EXCEPTION . ".exception "
  36. . "FROM " . DB_T_TYPE_ACCESS . " "
  37. . "INNER JOIN " . DB_T_ACCESS . " ON " . DB_T_TYPE_ACCESS . ".id_access = " . DB_T_ACCESS . ".id "
  38. . "LEFT JOIN " . DB_T_ACCESS_EXCEPTION . " ON " . DB_T_TYPE_ACCESS . ".id_exception = " . DB_T_ACCESS_EXCEPTION . ".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 getTypesUsers(bool $_expect = FALSE)
  56. {
  57. $except = ($_expect == FALSE) ? NULL : " WHERE " . DB_T_TYPE_USER . ".id != 2"; // Sauf les contrôleurs
  58. db::query("SELECT * FROM " . DB_T_TYPE_USER . $except);
  59. try {
  60. $tmp = db::resultset();
  61. return $tmp;
  62. } catch (Exception $e) {
  63. return FALSE;
  64. }
  65. }
  66. public static function ifLimitAccessException(string $_exception)
  67. {
  68. $accessList = self::getAccessList(session::getType());
  69. return in_array($_exception, $accessList["exception"]) ? TRUE : FALSE;
  70. }
  71. public static function ifAccesss(string $_accessAsk, ?int $_idType = NULL)
  72. {
  73. if (session::isConnect() == FALSE and self::checkAccessOffLine($_accessAsk)) {
  74. return TRUE;
  75. }
  76. // Si Admin OK
  77. $idType = $_idType == NULL ? session::getType() : $_idType;
  78. if ($idType == 1) {
  79. return TRUE;
  80. }
  81. // Si Admin OK
  82. $accessList = self::getAccessList($idType);
  83. $cheminGenrique = self::checkGenericAccess($_accessAsk, $accessList["access"]);
  84. if ($cheminGenrique != FALSE AND !in_array($_accessAsk, $accessList["noAccess"])) { // Si Accès générique
  85. return TRUE;
  86. } elseif (in_array($_accessAsk, $accessList["access"]) or self::checkAccessWhite($_accessAsk)) {
  87. return TRUE;
  88. } else {
  89. return FALSE;
  90. }
  91. }
  92. private static function checkGenericAccess(string $_string, array $_access_list)
  93. {
  94. $string = explode("-", $_string)[0];
  95. $check = in_array($string."*", $_access_list);
  96. return ($check == TRUE) ? $string : FALSE;
  97. }
  98. private static function splitAccess(string $_string)
  99. {
  100. $return = array();
  101. $tmp = array_filter(explode("\n", $_string));
  102. foreach ($tmp as $key => $value) {
  103. $return[$key] = trim($value);
  104. }
  105. return $return;
  106. }
  107. private static function addInArray(string $_string, array $_array)
  108. {
  109. return array_unique(array_merge(self::splitAccess($_string), $_array));
  110. }
  111. public static function getListTypeUser(?array $_idExceptions = NULL )
  112. {
  113. $return = array();
  114. db::query("SELECT id, type FROM " . DB_T_TYPE_USER);
  115. try {
  116. $return = [];
  117. foreach (db::resultset() as $value) {
  118. if(is_null($_idExceptions) OR (!is_null($_idExceptions) AND !in_array($value["id"], $_idExceptions)) ){
  119. $return[$value["id"]] = $value["type"];
  120. }
  121. }
  122. return $return;
  123. } catch (Exception $e) {
  124. return FALSE;
  125. }
  126. }
  127. public static function printRenderAccessRow(array $valueAccess){
  128. if (!is_null($valueAccess["exception"])) {
  129. $tooltip = '<span class="ms-2" data-bs-toggle="tooltip" title="Possibilité d\'accès partiel avec les éléments cachés suivants : ' . htmlspecialchars($valueAccess["exception"]) . '"><i class="bi bi-info-circle-fill" style="color:#ffc107;"></i></span>';
  130. } else {
  131. $tooltip = '';
  132. }
  133. echo '<tr>';
  134. echo '<td style="vertical-align: middle; box-sizing: border-box;">' . $valueAccess["label"] . $tooltip . '</td>';
  135. $tmp = [];
  136. foreach ($valueAccess["access"] as $keyRole => $valueRole) {
  137. $tmp[$keyRole] = '<td style="width: 180px;"><div style="text-align:center;">';
  138. $tmp[$keyRole] .= self::getSelectAccess($valueAccess["access"][$keyRole], $valueAccess["exception"]);
  139. $tmp[$keyRole] .= '</div></td>';
  140. }
  141. echo $tmp[1]; // Administrateur
  142. echo $tmp[5]; // Bureau du CSE
  143. echo $tmp[6]; // Elu du CSE
  144. echo $tmp[7]; // Comptable
  145. echo $tmp[4]; // Modérateur du CMS
  146. echo $tmp[3]; // Assistance sociale
  147. echo '</tr>';
  148. }
  149. private static function getSelectAccess(?array $_access = NULL, ?string $_exception = NULL){
  150. if (!is_null($_exception)) {
  151. $options = [
  152. 1 => 'Autorisé',
  153. 0 => '-',
  154. 2 => 'Partiellement'
  155. ];
  156. } else {
  157. $options = [
  158. 1 => 'Autorisé',
  159. 0 => '-',
  160. ];
  161. }
  162. $bgColors = [
  163. 1 => 'background-color:#d4edda;',
  164. 0 => 'background-color:#f8d7da;',
  165. 2 => 'background-color:#ffeeba;'
  166. ];
  167. $disabled = ($_access["id_type"] == 1 OR $_access["id_access"] == 3) ? ' disabled' : '';
  168. $disabledStyle = ($_access["id_type"] == 1 OR $_access["id_access"] == 3) ? ' opacity: 0.5; cursor: not-allowed;' : '';
  169. $style = isset($bgColors[$_access["access"]]) ? $bgColors[$_access["access"]] : '';
  170. $return = '<select class="form-select form-select-sm" name="access-' . str_replace("#", "-", $_access["id_type_access"]) . '" style="' . $style . $disabledStyle . '"' . $disabled . '>';
  171. foreach ($options as $value => $label) {
  172. $selected = ($_access["access"] === $value) ? ' selected' : '';
  173. $return .= '<option value="' . $value . '"' . $selected . '>' . $label . '</option>';
  174. }
  175. $return .= '</select>';
  176. return $return;
  177. }
  178. private static function completeIdemAccess(?array $_access = NULL, string $_idAccess){
  179. $roles = self::getListTypeUser([2]); // Tous les types sauf les contrôleurs
  180. foreach ($roles as $keyRole => $valueRole) {
  181. if(empty($_access["access"][$keyRole])){
  182. $tmp = [];
  183. $tmp["id_type_access"] = $keyRole . "#" . $_access["id_access"];
  184. $tmp["id_type"] = $keyRole;
  185. $tmp["id_access"] = $_idAccess;
  186. $tmp["type"] = $valueRole;
  187. $tmp["access"] = ($keyRole == 1) ? 1 : 0;
  188. $return[$keyRole] = $tmp;
  189. } else {
  190. $return[$keyRole] = $_access["access"][$keyRole];
  191. }
  192. }
  193. return $return;
  194. }
  195. public static function finalCompletAccess(){
  196. $access = access::getTypesAccessRecording();
  197. $return = [];
  198. foreach ($access as $keyAccess => $valueAccess) {
  199. $completeAccess = access::completeIdemAccess($valueAccess, $valueAccess["id_access"]);
  200. unset($valueAccess["access"]);
  201. $valueAccess["access"] = $completeAccess;
  202. $return[] = $valueAccess;
  203. }
  204. return $return;
  205. }
  206. private static function getTypesAccess(){
  207. db::query("SELECT "
  208. . DB_T_ACCESS . ".id, "
  209. . DB_T_ACCESS . ".label, "
  210. . DB_T_ACCESS . ".show, "
  211. . DB_T_ACCESS . ".add, "
  212. . DB_T_TYPE_ACCESS . ".id AS id_type_access, "
  213. . DB_T_TYPE_ACCESS . ".id_type, "
  214. . "exception1.exception AS exception_type, "
  215. . "exception2.exception AS exception, "
  216. . DB_T_TYPE_USER . ".type "
  217. . "FROM " . DB_T_ACCESS . " "
  218. . "LEFT JOIN " . DB_T_ACCESS_EXCEPTION . " AS exception1 ON exception1.id_access = " . DB_T_ACCESS . ".id "
  219. . "LEFT JOIN " . DB_T_TYPE_ACCESS . " ON " . DB_T_TYPE_ACCESS . ".id_access = " . DB_T_ACCESS . ".id "
  220. . "LEFT JOIN " . DB_T_TYPE_USER . " ON " . DB_T_TYPE_ACCESS . ".id_type = " . DB_T_TYPE_USER . ".id "
  221. . "LEFT JOIN " . DB_T_ACCESS_EXCEPTION . " AS exception2 ON " . DB_T_TYPE_ACCESS . ".id_exception = exception2.id "
  222. );
  223. try {
  224. $tmp = db::resultset();
  225. return $tmp;
  226. } catch (Exception $e) {
  227. return FALSE;
  228. }
  229. }
  230. private static function getTitleLabel(array $_array){
  231. if ($_array["show"] == 1 AND $_array["add"] == 0) {
  232. return "Accès à <strong>" . $_array["label"] . "</strong> en lecture";
  233. } elseif ($_array["show"] == 0 AND $_array["add"] == 1) {
  234. return "Accès à <strong>" . $_array["label"] . "</strong> en écriture";
  235. } else {
  236. return "Accès à <strong>" . $_array["label"] . "</strong> en lecture et écriture";
  237. }
  238. }
  239. public static function getTypesAccessRecording(){
  240. $return = [];
  241. foreach (self::getTypesAccess() as $valueAccess) {
  242. $return[$valueAccess["id"]]["id_access"] = $valueAccess["id"];
  243. $return[$valueAccess["id"]]["label"] = self::getTitleLabel($valueAccess);
  244. $return[$valueAccess["id"]]["show"] = $valueAccess["show"];
  245. $return[$valueAccess["id"]]["add"] = $valueAccess["add"];
  246. $return[$valueAccess["id"]]["exception"] = $valueAccess["exception_type"];
  247. if(!empty($valueAccess["id_type_access"])) {
  248. $return[$valueAccess["id"]]["access"][$valueAccess["id_type"]] = [
  249. "id_type_access" => $valueAccess["id_type_access"],
  250. "id_type" => $valueAccess["id_type"],
  251. "id_access" => $valueAccess["id"],
  252. "type" => $valueAccess["type"],
  253. "access" => (empty($valueAccess["exception"])) ? 1 : 2,
  254. ];
  255. }
  256. }
  257. return $return;
  258. }
  259. public static function getAccessByRole(){
  260. $getAccessByRole = self::getTypesAccessRecording();
  261. $allTypeRole = self::getTypesUsers();
  262. $return = [];
  263. foreach ($getAccessByRole as $valuesGetAccessByRole) {
  264. $tmp = [];
  265. $tmp["access"] = $valuesGetAccessByRole["label"];
  266. $tmp["exception"] = $valuesGetAccessByRole["exception"];
  267. foreach ($allTypeRole as $valueAllTypeRole) {
  268. if(!empty($valuesGetAccessByRole["access"][$valueAllTypeRole["id"]])){
  269. $tmp[$valuesGetAccessByRole["access"][$valueAllTypeRole["id"]]["type"]] = $valuesGetAccessByRole["access"][$valueAllTypeRole["id"]]["access"];
  270. } elseif($valueAllTypeRole["id"] == 1){
  271. $tmp[$valueAllTypeRole["type"]] = 1;
  272. } else {
  273. $tmp[$valueAllTypeRole["type"]] = 0;
  274. }
  275. }
  276. $return[] = $tmp;
  277. }
  278. return $return;
  279. }
  280. private static function getIdException(int $_idAccess){
  281. db::query("SELECT "
  282. . DB_T_ACCESS_EXCEPTION . ".id, "
  283. . DB_T_ACCESS_EXCEPTION . ".exception "
  284. . "FROM " . DB_T_ACCESS_EXCEPTION . " "
  285. . "WHERE " . DB_T_ACCESS_EXCEPTION . ".id_access = :id_access");
  286. db::bind(':id_access', $_idAccess);
  287. try {
  288. $tmp = db::single();
  289. return $tmp;
  290. } catch (Exception $e) {
  291. return FALSE;
  292. }
  293. }
  294. public static function recordAccess(){
  295. $post = core::getPost();
  296. $data = [];
  297. foreach ($post as $keyPost => $valuePost) {
  298. if($valuePost == 1 OR $valuePost == 2){
  299. $tmp = explode("-", $keyPost);
  300. $data[$tmp[1] . "#" . $tmp[2]]["id_access"] = $tmp[2];
  301. $data[$tmp[1] . "#" . $tmp[2]]["id_type"] = $tmp[1];
  302. if($valuePost == 2){
  303. $data[$tmp[1] . "#" . $tmp[2]]["id_exception"] = self::getIdException($tmp[2])["id"];
  304. } else {
  305. $data[$tmp[1] . "#" . $tmp[2]]["id_exception"] = NULL;
  306. }
  307. }
  308. }
  309. // Vide la table
  310. db::query("TRUNCATE TABLE " . DB_T_TYPE_ACCESS);
  311. db::execute();
  312. // Reconstruit la table
  313. foreach ($data as $keyData => $valueData) {
  314. db::query("INSERT INTO " . DB_T_TYPE_ACCESS . " (id, id_type, id_access, id_exception) VALUES (:id, :id_type, :id_access, :id_exception)");
  315. db::bind(':id', $keyData);
  316. db::bind(':id_type', $valueData["id_type"]);
  317. db::bind(':id_access', $valueData["id_access"]);
  318. db::bind(':id_exception', $valueData["id_exception"]);
  319. try {
  320. db::execute();
  321. } catch (Exception $ex) {
  322. alert::recError("Erreur à l'enregistrement des droits");
  323. if(debug::isFile("debug")) { alert::recError("Stack : " . $ex); }
  324. return FALSE;
  325. }
  326. }
  327. return TRUE;
  328. }
  329. }