# Espace Admin avec gestion des rôles
Bonjour, j'essaye en vins d'afficher les liens du menu selon la session dans laquelle on se trouve,
mais tous liens s'affichent que l'on soit sur Admin, éditeur ou visiteur.
Merci pour votre aide.
1. class/userManager.php
class userManager {
private $db;
public function __construct($db) {
$this->setDb($db);
}
public function setDb(PDO $dbh) {
return $this->db = $dbh;
}
public function displayMenu($user_role_id) {
try {
$sql = 'SELECT A.name, A.slug
FROM user_action AS A
INNER JOIN user_permission AS P
ON A.actionID = P.action_id
AND min_role_id < :user_role_id';
echo $sql;
$stmnt = $this->db->prepare($sql);
$stmnt->execute(array(
':user_role_id' => $user_role_id
));
while($row = $stmnt->fetch(PDO::FETCH_ASSOC)) {
$menu_data[] = $row;
}
if(isset($menu_data)) {
return $menu_data;
}
else {
return false;
}
}
catch(PDOException $e) {
echo 'Une erreur est survenue lors de la récupération d\'une des données dans la base.';
echo '<br>';
echo 'Message d\'erreur : ' . $e->getMessage();
}
}
public function checkUserPermission($action_slug, $user_role_id) {
try {
$sql = 'SELECT P.action_id, min_role_id
FROM user_permission AS P
INNER JOIN user_action AS A
ON P.action_id = A.actionID
AND A.slug = :action_slug';
$stmnt = $this->db->prepare($sql);
$stmnt->execute(array(
':action_slug' => $action_slug
));
while($row = $stmnt->fetch(PDO::FETCH_ASSOC)) {
$min_role_id = $row['min_role_id'];
if($min_role_id > $user_role_id) {
return false;
}
else {
return true;
}
}
}
catch(PDOException $e) {
echo 'Une erreur est survenue lors de la récupération d\'une des données dans la base.';
echo '<br>';
echo 'Message d\'erreur : ' . $e->getMessage();
}
}
}
2. process/process-display-menu.php
$role = isset($_SESSION['role']) ? $_SESSION['role'] : TRUE;
$manager = new userManager($db);
$menu_items = $manager->displayMenu($role);
$menu_html = '';
foreach($menu_items as $menu_item) {
$name = $menu_item['name'];
$slug = $menu_item['slug'];
$menu_html .= '<li><a href="' . $slug . '.php">' . $name . '</a></li>' . "\n";
}
3. process/process-user-permission.php
$action_slug = substr($filename, 0, 4);
$level = isset($_SESSION['level']) ? $_SESSION['level'] : TRUE;
$manager = new userManager($db);
$userPermission = $manager->checkUserPermission($action_slug, $level);
if($userPermission === false) {
echo 'Erreur. Cette page n\'existe pas.';
exit;
}
4. admin/menu.php
require('../process/process-display-menu.php');
?>
<ul class="menu">
<?php echo $menu_html; ?>
<li><a href="../index.php">Site web</a></li>
<li><a href="logout.php">Déconnexion</a></li>
</ul>
5. admin/index.php
require('../include/inc-connexion.php');
require('../include/inc-config.php');
require('../include/inc-identification-user.php');
$filename = basename(__FILE__);
require('../process/process-user-permission.php');
require('menu.php');
https://imagizer.imageshack.com/img922/3593/sLwld7.png https://imagizer.imageshack.com/img923/2979/AIY5lj.png https://imagizer.imageshack.com/img922/6676/OTbRws.png
+0
-0