$value) { $return[$key] = trim($value); } return $return; } private static function addInArray(string $_string, array $_array) { return array_unique(array_merge(self::splitAccess($_string), $_array)); } public static function getListTypeUser(?array $_idExceptions = NULL ) { $return = array(); db::query("SELECT id, type FROM " . DB_T_TYPE_USER); try { $return = []; foreach (db::resultset() as $value) { if(is_null($_idExceptions) OR (!is_null($_idExceptions) AND !in_array($value["id"], $_idExceptions)) ){ $return[$value["id"]] = $value["type"]; } } return $return; } catch (Exception $e) { return FALSE; } } public static function printRenderAccessRow(array $valueAccess){ if (!is_null($valueAccess["exception"])) { $tooltip = ''; } else { $tooltip = ''; } echo ''; echo '' . $valueAccess["label"] . $tooltip . ''; $tmp = []; foreach ($valueAccess["access"] as $keyRole => $valueRole) { $tmp[$keyRole] = '
'; $tmp[$keyRole] .= self::getSelectAccess($valueAccess["access"][$keyRole], $valueAccess["exception"]); $tmp[$keyRole] .= '
'; } echo $tmp[1]; // Administrateur echo $tmp[4]; // Modérateur du CMS echo $tmp[5]; // Bureau du CSE echo $tmp[6]; // Elu du CSE echo $tmp[7]; // Comptable echo $tmp[3]; // Assistance sociale echo ''; } private static function getSelectAccess(?array $_access = NULL, ?string $_exception = NULL){ if (!is_null($_exception)) { $options = [ 1 => 'Autorisé', 0 => '-', 2 => 'Partiellement' ]; } else { $options = [ 1 => 'Autorisé', 0 => '-', ]; } $bgColors = [ 1 => 'background-color:#d4edda;', 0 => 'background-color:#f8d7da;', 2 => 'background-color:#ffeeba;' ]; $disabled = ($_access["id_type"] == 1 OR $_access["id_access"] == 3) ? ' disabled' : ''; $disabledStyle = ($_access["id_type"] == 1 OR $_access["id_access"] == 3) ? ' opacity: 0.5; cursor: not-allowed;' : ''; $style = isset($bgColors[$_access["access"]]) ? $bgColors[$_access["access"]] : ''; $return = ''; return $return; } private static function completeIdemAccess(?array $_access = NULL, string $_idAccess){ $roles = self::getListTypeUser([2]); // Tous les types sauf les contrôleurs foreach ($roles as $keyRole => $valueRole) { if(empty($_access["access"][$keyRole])){ $tmp = []; $tmp["id_type_access"] = $keyRole . "#" . $_access["id_access"]; $tmp["id_type"] = $keyRole; $tmp["id_access"] = $_idAccess; $tmp["type"] = $valueRole; $tmp["access"] = ($keyRole == 1) ? 1 : 0; $return[$keyRole] = $tmp; } else { $return[$keyRole] = $_access["access"][$keyRole]; } } return $return; } public static function finalCompletAccess(){ $access = access::getTypesAccessRecording(); $return = []; foreach ($access as $keyAccess => $valueAccess) { $completeAccess = access::completeIdemAccess($valueAccess, $valueAccess["id_access"]); unset($valueAccess["access"]); $valueAccess["access"] = $completeAccess; $return[] = $valueAccess; } return $return; } private static function getTypesAccess(){ db::query("SELECT " . DB_T_ACCESS . ".id, " . DB_T_ACCESS . ".label, " . DB_T_ACCESS . ".show, " . DB_T_ACCESS . ".add, " . DB_T_TYPE_ACCESS . ".id AS id_type_access, " . DB_T_TYPE_ACCESS . ".id_type, " . "exception1.exception AS exception_type, " . "exception2.exception AS exception, " . DB_T_TYPE_USER . ".type " . "FROM " . DB_T_ACCESS . " " . "LEFT JOIN " . DB_T_ACCESS_EXCEPTION . " AS exception1 ON exception1.id_access = " . DB_T_ACCESS . ".id " . "LEFT JOIN " . DB_T_TYPE_ACCESS . " ON " . DB_T_TYPE_ACCESS . ".id_access = " . DB_T_ACCESS . ".id " . "LEFT JOIN " . DB_T_TYPE_USER . " ON " . DB_T_TYPE_ACCESS . ".id_type = " . DB_T_TYPE_USER . ".id " . "LEFT JOIN " . DB_T_ACCESS_EXCEPTION . " AS exception2 ON " . DB_T_TYPE_ACCESS . ".id_exception = exception2.id " . "ORDER BY " . DB_T_ACCESS . ".id"); try { $tmp = db::resultset(); return $tmp; } catch (Exception $e) { return FALSE; } } private static function getTitleLabel(array $_array){ if ($_array["show"] == 1 AND $_array["add"] == 0) { return "Accès à " . $_array["label"] . " en lecture"; } elseif ($_array["show"] == 0 AND $_array["add"] == 1) { return "Accès à " . $_array["label"] . " en écriture"; } else { return "Accès à " . $_array["label"] . " en lecture et écriture"; } } public static function getTypesAccessRecording(){ $return = []; foreach (self::getTypesAccess() as $valueAccess) { $return[$valueAccess["id"]]["id_access"] = $valueAccess["id"]; $return[$valueAccess["id"]]["label"] = self::getTitleLabel($valueAccess); $return[$valueAccess["id"]]["show"] = $valueAccess["show"]; $return[$valueAccess["id"]]["add"] = $valueAccess["add"]; $return[$valueAccess["id"]]["exception"] = $valueAccess["exception_type"]; if(!empty($valueAccess["id_type_access"])) { $return[$valueAccess["id"]]["access"][$valueAccess["id_type"]] = [ "id_type_access" => $valueAccess["id_type_access"], "id_type" => $valueAccess["id_type"], "id_access" => $valueAccess["id"], "type" => $valueAccess["type"], "access" => (empty($valueAccess["exception"])) ? 1 : 2, ]; } } return $return; } public static function getAccessByRole(){ $getAccessByRole = self::getTypesAccessRecording(); $allTypeRole = self::getTypesUsers(); $return = []; foreach ($getAccessByRole as $valuesGetAccessByRole) { $tmp = []; $tmp["access"] = $valuesGetAccessByRole["label"]; $tmp["exception"] = $valuesGetAccessByRole["exception"]; foreach ($allTypeRole as $valueAllTypeRole) { if(!empty($valuesGetAccessByRole["access"][$valueAllTypeRole["id"]])){ $tmp[$valuesGetAccessByRole["access"][$valueAllTypeRole["id"]]["type"]] = $valuesGetAccessByRole["access"][$valueAllTypeRole["id"]]["access"]; } elseif($valueAllTypeRole["id"] == 1){ $tmp[$valueAllTypeRole["type"]] = 1; } else { $tmp[$valueAllTypeRole["type"]] = 0; } } $return[] = $tmp; } return $return; } private static function getIdException(int $_idAccess){ db::query("SELECT " . DB_T_ACCESS_EXCEPTION . ".id, " . DB_T_ACCESS_EXCEPTION . ".exception " . "FROM " . DB_T_ACCESS_EXCEPTION . " " . "WHERE " . DB_T_ACCESS_EXCEPTION . ".id_access = :id_access"); db::bind(':id_access', $_idAccess); try { $tmp = db::single(); return $tmp; } catch (Exception $e) { return FALSE; } } public static function recordAccess(){ $post = core::getPost(); $data = []; foreach ($post as $keyPost => $valuePost) { if($valuePost == 1 OR $valuePost == 2){ $tmp = explode("-", $keyPost); $data[$tmp[1] . "#" . $tmp[2]]["id_access"] = $tmp[2]; $data[$tmp[1] . "#" . $tmp[2]]["id_type"] = $tmp[1]; if($valuePost == 2){ $data[$tmp[1] . "#" . $tmp[2]]["id_exception"] = self::getIdException($tmp[2])["id"]; } else { $data[$tmp[1] . "#" . $tmp[2]]["id_exception"] = NULL; } } } // Vide la table db::query("TRUNCATE TABLE " . DB_T_TYPE_ACCESS); db::execute(); // Reconstruit la table foreach ($data as $keyData => $valueData) { db::query("INSERT INTO " . DB_T_TYPE_ACCESS . " (id, id_type, id_access, id_exception) VALUES (:id, :id_type, :id_access, :id_exception)"); db::bind(':id', $keyData); db::bind(':id_type', $valueData["id_type"]); db::bind(':id_access', $valueData["id_access"]); db::bind(':id_exception', $valueData["id_exception"]); try { db::execute(); } catch (Exception $ex) { alert::recError("Erreur à l'enregistrement des droits"); if(debug::isFile("debug")) { alert::recError("Stack : " . $ex); } return FALSE; } } return TRUE; } }