How To Confine Controllers Functions Based On User Roles Inward Codeigniter?
To bound logged inwards users from an entire controller is fairly elementary inwards Codeigniter.
If you lot desire to bound controllers functions/methods based on user roles, you lot tin dismiss create the next way:
You should only create the banking concern tally inwards the constructor of the cast that you're calling.
Firstly, banking concern tally if the user is authenticated or not. If user is non an authenticated, organization redirect the user to Login page.
And utilization $this->router->method (it gives you lot the existent controller/method names, fifty-fifty if you lot reroute them) to encounter what method the user is trying to access.
Check that against an array of methods that required to a greater extent than or less specific roles to access the methods/functions.
In this example, role1 is the user that is non allowed to access protected methods 1,2 too 3.
If the user has role i.e. role1, it volition redirect to login page otherwise redirect to dwelling page. Sumber http://developer-paradize.blogspot.com
business office __construct(){ parent::__construct(); // to protect the controller to endure accessed alone past times registered users if(!$this->session->userdata('logged_in')){ redirect('login', 'refresh'); } }This plant great. The user is non allowed access to functions or methods inside the controller unless they are authenticated or registered users.
If you lot desire to bound controllers functions/methods based on user roles, you lot tin dismiss create the next way:
business office __construct(){ parent::__construct(); // to protect the controller to endure accessed alone past times registered users if(!$this->session->userdata('logged_in')){ redirect('login', 'refresh'); } //list of protected methods to access (for instance alone past times admin ) $protected_methods = array('METHOD1', 'METHOD2', 'METHOD3'); /*$this->session->userdata('logged_in') is the array containing user information such every bit name, email, user role etc.*/ if($this->session->userdata('logged_in')['user_role'] == 'role1'){ //grab the controller/method advert too compare alongside protected methods array if(in_array($this->router->method, $protected_methods)){ redirect('login', 'refresh'); } else { redirect('home', 'refresh'); } } }How it works:
You should only create the banking concern tally inwards the constructor of the cast that you're calling.
Firstly, banking concern tally if the user is authenticated or not. If user is non an authenticated, organization redirect the user to Login page.
And utilization $this->router->method (it gives you lot the existent controller/method names, fifty-fifty if you lot reroute them) to encounter what method the user is trying to access.
Check that against an array of methods that required to a greater extent than or less specific roles to access the methods/functions.
In this example, role1 is the user that is non allowed to access protected methods 1,2 too 3.
If the user has role i.e. role1, it volition redirect to login page otherwise redirect to dwelling page. Sumber http://developer-paradize.blogspot.com
Comments
Post a Comment