title: Roles Permissions and Navigation
body: <p>I'm stuck on knowing what is the most efficient method of performing the following:</p>
  
  <p>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.</p>
  
  <p>I'm trying to figure out how I should go about putting this together. I have an <code>Admin_Controller</code> that might be useful to put the logic into but need some help on figuring out how to do so.</p>
  
  <p>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. </p>
  
  <p>Here is an example of what I"m talking about.</p>
  
  <p><strong>Users Table</strong></p>
  
  <pre><code>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)
  </code></pre>
  
  <p><strong>Navigation</strong></p>
  
  <pre><code>&lt;ul&gt;
      &lt;li class="current"&gt;
      &lt;a class="current" href="&lt;?php echo base_url(); ?&gt;dashboard" data-toggle="tooltip" data-placement="right" title="" data-original-title="Dashboard"&gt; &lt;i class="fa fa-home"&gt;&lt;/i&gt; &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 1"&gt; &lt;i class="fa fa-user"&gt;&lt;/i&gt; &lt;/a&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a&gt;Test Link 1&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a&gt;Test Link 2&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a&gt;Test Link 3&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;                
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 2"&gt; &lt;i class="fa fa-pencil"&gt;&lt;/i&gt; &lt;/a&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a&gt;Test Link 1&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a&gt;Test Link 2&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a&gt;Test Link 3&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;   
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 3"&gt; &lt;i class="fa fa-calendar"&gt;&lt;/i&gt; &lt;/a&gt;
          &lt;ul&gt;
              &lt;li&gt;&lt;a&gt;Test Link 1&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a&gt;Test Link 2&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a&gt;Test Link 3&lt;/a&gt;&lt;/li&gt;
          &lt;/ul&gt;   
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 4"&gt; &lt;i class="fa fa-users"&gt;&lt;/i&gt; &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 5"&gt; &lt;i class="fa fa-briefcase"&gt;&lt;/i&gt; &lt;/a&gt;
      &lt;/li&gt;
      &lt;li&gt;
          &lt;a href="#" data-toggle="tooltip" data-placement="right" title="" data-original-title="Menu 6"&gt; &lt;i class="fa fa-sitemap"&gt;&lt;/i&gt; &lt;/a&gt;
      &lt;/li&gt;
  &lt;/ul&gt;    
  </code></pre>
  
  <p><strong>Admin Controller</strong></p>
  
  <pre><code>&lt;?php
  if (!defined('BASEPATH'))
      exit('No direct script access allowed');
  class Admin_Controller extends MY_Controller {
      protected $data;
      public function __construct() {
          parent::__construct();
          $this -&gt; has_access();
          $this -&gt; template -&gt; set_theme('saturn') -&gt; set_layout('default', 'admin') -&gt; set_partial('header', 'admin/partials/header') -&gt; set_partial('navigation', 'admin/partials/navigation');
          //if (logged_in()) {
              $menu_items = array();
              $this -&gt; template -&gt; menu_items = $menu_items;
          //}
      }
  
      public function has_access() {
          $public_access = array('login', 'registration');
          $current_class = $this -&gt; router -&gt; fetch_method();
          $user_id = $this -&gt; session -&gt; 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) &lt; 5)) {
                  $this -&gt; session -&gt; unset_userdata('user_id');
                  $this -&gt; session -&gt; sess_destroy();
                  redirect('login', 'refresh');
              }
              else {
                  $this -&gt; load -&gt; model('user_model', 'user');
                  $current_user = $this -&gt; user -&gt; get($user_id);
                  if (!is_object($current_user)) {
                      $this -&gt; session -&gt; unset_userdata('user_id');
                      $this -&gt; session -&gt; sess_destroy();
                      redirect('login', 'refresh');
                  }
                  else {
                      // Make all controllers like roster, match_types, etc have access to the $current_user object.
                      $this -&gt; data['current_user'] = $current_user;
                  }
                  if (in_array($current_class, $public_access)) {
                      redirect('dashboard', 'refresh');
                  }
              }
          }
      }
  }
  </code></pre>