Reputation: 415
I'm stuck on knowing what is the most efficient method of performing 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) would be comprised of what the user has permission to access.
I'm trying to figure out how I should go about putting this together. I have an Admin_Controller
that might be useful to put the logic into but need some help on figuring out how to do so.
To further explain what I want I have the following user's table and navigation set up. Lets say the first user (1) is a guest so they may only be able to view the dashboard and nothing else as that role. Maybe users with a role of 2 can view the dashboard and 2 more menu's. Admins can access all menus. Something else to ponder is what if say a user can have access only 2 of the three links from under Menu 2.
Here is an example of what I"m talking about.
Users 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');
//if (logged_in()) {
$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 {
// Make all controllers like roster, match_types, etc have access to the $current_user object.
$this -> data['current_user'] = $current_user;
}
if (in_array($current_class, $public_access)) {
redirect('dashboard', 'refresh');
}
}
}
}
}
Upvotes: 3
Views: 2071
Reputation: 44
<?php $user_id = $this->session->userdata('user_id'); ?>
<?php if( $user_id == '3' ):?>
<!-- HTML FOR ADMIN NAVIGATION -->
<?php elseif($user_id == '48' || $user_id == '49' || $user_id == '50'):?>
<!-- HTML FOR MANAGERS NAVIGATION -->
<?php else:?>
<!-- HTML FOR GENERAL NAVIGATION FOR EVERYBODY ELSE -->
<?php endif;?>
Upvotes: 0
Reputation: 2522
I dont know how "efficient" this is, but it works. What i did on my application which is a bit different but still the same principles was i had a table with all the possible things a user could do in a db. I then associate an id do each field, with each field being either a 1 or 0. so in the end the query results may look like:
my table would look like:
UserId | Add | remove | modify | view | change | delete
-------------------------------------------------------
23 | 1 | 0 | 1 | 1 | 1 | 0
I then set this to a session variable as an array
while($row = $qry->fetch())
{
$field = key($r);
$_SESSION['permissions'][$field] = $r[$field];
}
so then on my navigation page i have
foreach ($_SESSION['permission'] as $k => $v)
{
if ($v == "1")
{
echo "<li>" . $k . "</li>";
}
}
in this example you would end up with:
of course you need to add all the other code (ul,ol) or whatever...
I am typing this from memory so forgive me if there's any syntax error, i hope you get the general idea.
i see your update has alot of object data, im guessing you could use the same principle on those.
Upvotes: 2