php - Codeigniter customized user roles open specific pages only -
how define user privileges in ci opening specific pages? thinking calling database value every time open page , see if has right open particular page. if has no right show up, directly logged out. there many pages do. create function , check everytime open page. easiest way?
thanks.
you're on right track. query database on every page load, that's fine, , ideal if values change. alternatively, save privileges data session, , query session on page load (similar how check if user logged in).
but there many pages do
you can extend default ci_controller , add own code runs on every page. example, create new file called my_controller.php in application/core/ directory, , put code privileges check in __construct() function, runs automatically on every page load.
application/core/my_controller.php
class my_controller extends ci_controller {     function __construct()     {         parent::__construct();          // check privileges here. like..          $this->load->helper('url');         $this->load->model('privileges_model');          $current_uri = uri_string();              // if url http://some-site.com/blog/comments/123             // return "/blog/comments/123"          $user_id = $this->session->userdata('user_id');          if($this->privileges_model->check($user_id, $current_uri) == false)         {             // user not have correct permissions.             // log them out, redirect them somewhere else, etc etc.         }     } }   then in controllers, instead of extending ci_controller, can extend custom my_controller, inheriting methods every page.
application/controllers/secret.php
class secret extends my_controller {     function __construct()     {         parent::__construct();     }      function index()      {         // secret page code here..     } }   this article phil sturgeon goes more detail: http://philsturgeon.co.uk/blog/2010/02/codeigniter-base-classes-keeping-it-dry
Comments
Post a Comment