I am stuck in understanding what is the most effective method of doing the following:
I have a CMS with different types of users. All users have access to the same CMS, however, the links in the sidebar (navigation) will consist of the fact that the user has permission to access.
I'm trying to figure out how to work with this. I have Admin_Controllerone that can be useful for inputting logic, but you need help figuring out how to do this.
To clarify what I want, I have the following user table and navigation setting. Suppose that the first user (1) is a guest, so they can only view the control panel and nothing else like this role. Perhaps users with role 2 can view the toolbar and 2 more menus. Admins can access all menus. Something else to think about, if you say that the user can only access two three links from menu 2.
Here is an example of what I'm talking about.
User table
user_id username status_id role_id
-------------------------------------------
1 testuser1 1 (active) 1 (guest)
2 testuser2 1 2 (user)
3 testuser3 1 3 (editor)
4 testuser4 1 4 (admin)
Navigation
<ul>
<li class="current">
<a class="current" href="<?php echo base_url(); ?>dashboard" data-toggle="tooltip" data-placement="right" title="" data-original-title="Dashboard"> <i class="fa fa-home"></i> </a>
</li>
<li>
<a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 1"> <i class="fa fa-user"></i> </a>
<ul>
<li><a>Test Link 1</a></li>
<li><a>Test Link 2</a></li>
<li><a>Test Link 3</a></li>
</ul>
</li>
<li>
<a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 2"> <i class="fa fa-pencil"></i> </a>
<ul>
<li><a>Test Link 1</a></li>
<li><a>Test Link 2</a></li>
<li><a>Test Link 3</a></li>
</ul>
</li>
<li>
<a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 3"> <i class="fa fa-calendar"></i> </a>
<ul>
<li><a>Test Link 1</a></li>
<li><a>Test Link 2</a></li>
<li><a>Test Link 3</a></li>
</ul>
</li>
<li>
<a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 4"> <i class="fa fa-users"></i> </a>
</li>
<li>
<a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 5"> <i class="fa fa-briefcase"></i> </a>
</li>
<li>
<a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 6"> <i class="fa fa-sitemap"></i> </a>
</li>
</ul>
Admin controller
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Admin_Controller extends MY_Controller {
protected $data;
public function __construct() {
parent::__construct();
$this -> has_access();
$this -> template -> set_theme('saturn') -> set_layout('default', 'admin') -> set_partial('header', 'admin/partials/header') -> set_partial('navigation', 'admin/partials/navigation');
$menu_items = array();
$this -> template -> menu_items = $menu_items;
}
public function has_access() {
$public_access = array('login', 'registration');
$current_class = $this -> router -> fetch_method();
$user_id = $this -> session -> userdata('user_id');
if ($user_id == FALSE) {
if (!in_array($current_class, $public_access)) {
redirect('login', 'refresh');
}
}
else {
if ((!is_numeric($user_id)) || (strlen($user_id) < 5)) {
$this -> session -> unset_userdata('user_id');
$this -> session -> sess_destroy();
redirect('login', 'refresh');
}
else {
$this -> load -> model('user_model', 'user');
$current_user = $this -> user -> get($user_id);
if (!is_object($current_user)) {
$this -> session -> unset_userdata('user_id');
$this -> session -> sess_destroy();
redirect('login', 'refresh');
}
else {
$this -> data['current_user'] = $current_user;
}
if (in_array($current_class, $public_access)) {
redirect('dashboard', 'refresh');
}
}
}
}
}
source
share