jvbs
jvbs

Reputation: 436

User Permission System with CodeIgniter

I'm re-developing my system using CodeIgniter, which i'm new, and i'm trying to load a function from my Controller that will check if the user has or not permission to see/access that feature.

I'm using a TEXT in my DB with all the permissions that each user level has and then save the permissions into a session var do work with in my Controller.

In my Controller Users.php:

public function UserHasPermissions($permission){
    checkSession($this);

    $arrayPermissions = explode($this->sessions->userdata('session_permissions'));
    if(in_array($permission, $arrayPermissions)){
        return true;
    } else {
    return false;
    }
}

In my View home.php i'm doing the following try:

$this->load->view('commons/header');
if($this->Users->UserHasPermissions('ADD_MEETING')){
    echo 'content';
}
$this->load->view('commons/footer');

And it's returning this error:

An uncaught Exception was encountered
Type: Error

Message: Call to a member function UserHasPermissions() on null

Filename: /var/www/html/DF_CHECKER_CI/application/views/home.php

Line Number: 4

I took a look at these docs but nothing really helped me much.

https://www.codeigniter.com/user_guide/general/views.html#adding-dynamic-data-to-the-view

How to call codeigniter controller function from view

Upvotes: 0

Views: 5160

Answers (2)

Alex
Alex

Reputation: 9265

  1. you shouldn't be loading views in views unless you are using a templating engine like smarty
  2. you can't access a controller method as you would a library/model method (I think you may be able to if you are using HMVC but you clearly aren't)

You can either make a model or a helper or a library. Although a model/helper would be better suited. This is relatively easy, especially if you've used CI in any capacity before as it appears you have.

The only thing to note is that if you use a helper you have to access the CI instance like so in a function: $CI = &get_instance(); and then you can use it like $CI->load->database(); $CI->db->get('users')->result(); or whatever.

Upvotes: 0

Kisaragi
Kisaragi

Reputation: 2218

You are breaking MVC by attempting to call a controller from your view, you should instead determine what permissions a user has in the controller before you render your view, either in the method that renders the view or your class constructor. With that being said, you cannot call a controller from a view in CI unless you reference the singleton CI instance:

$ci = &get_instance();

$this->load->view('commons/header');
if($ci->UserHasPermissions('ADD_MEETING')){
    echo 'content';
}
$this->load->view('commons/footer')

This will produce an error however:

object of class Users could not be converted to a string

Here's one way:

class Users extends CI_Controller{ //or whatever you're doing here

   public function __construct()
   {
       // you can check all permissions here if you'd like
   }
   /**or**/
   /**whatever is responsible for calling your view**/
   public function index()
   {
      $data['has_permission'] = $this->userHasPermissions('ADD_MEETING');
      $this->load->view('home',$data); 
   }

   private function userHasPermissions($permission)
   {
       return in_array($permission, $this->sessions>userdata('session_permissions'));
   }
}

And in your view:

<?if($has_permission):?> 
<--show content-->
<?endif;?>

Upvotes: 1

Related Questions