问题
I'm looking to do log ins with my codeigniter project.
Some considerations:
1) There are two controllers that are outside that do not require authentication. One for information (splash_pages and such), and the other to create a logged in session.
2) All other controllers inherit from a master Controller that as part of its constructor, requires you to be logged in or it kicks you to the log in screen.
So far the above 2 works fine for logged in vs logged out.
In terms of code:
The master controller I was describing for 2) This is located in the Core folder of Codeigniter
class MY_Controller extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('cart');
$this->load->library('session');
$this->load->helper('form');
$this->load->library('form_validation');
if (!$this->session->userdata('loggedin')){
redirect('/sessions/log_in/','refresh');
}
}
A class using the login system:
class Records extends MY_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('some_model');
$this->load->library('some_library');
}
The idea is that on object construction, it will check whether the user is logged in or not and either construct the object correctly or redirect to the log in screen.
However, the needs of the project have changed a bit. The requirements now state that there are around 6 different user groups arranged whose privileges can be arranged into subsets. A can do I, B can do A + II, C can do B + III, and so on. There has been slight hints that there may be privileges that are not strictly subset (IE only B can do task IV), but this has not been confirmed yet so I want to keep my options open.
How I am envisioning doing it is having a bunch of Controllers from MY_Controller that inherit from MY_Controller.
For example in the Core folder:
class MY_AsController extends MY_Controller {
public function __construct(){
parent::__construct();
$accountType = $this->session->userdata('accountType');
if(!($accountType == declaredConstant)){
redirect('/someController/someMethod','refresh');
}
}
Then in the controllers folder:
class AControlPage extends MY_AsController {
//Insert page functions that only As have access to here
}
Unfortunately, applying it in practice doesn't generate any errors, only a blank page. I'm not sure what to after that though.
回答1:
Ended up not changing the parent constructor or using more inheritance at all:
Added the following to My_Controller:
public function allowedToView($userAccountType, $requiredAccountTypes){
//If user not in allowed userGroup
if(!(in_array($userAccountType,$requiredAccountTypes))){
redirect('/sessions/not_allowed/','refresh');
}
}
Changed child constructor to:
public function __construct() {
parent::__construct();
$accountType = $this->session->userdata('accountType');
$allowedTypes = array(declaredConstant1,declaredConstant2,...);
$this->allowedToView($accountType,$allowedTypes);
}
Thanks, Joseph for the insight leading me away from my craziness!
来源:https://stackoverflow.com/questions/7897742/codeigniter-controlling-log-in-privileges-with-inheritance