Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter:在核心基础控制器中使用auth是一个好的实践吗?_Php_Codeigniter_Authentication_Codeigniter 3 - Fatal编程技术网

Php Codeigniter:在核心基础控制器中使用auth是一个好的实践吗?

Php Codeigniter:在核心基础控制器中使用auth是一个好的实践吗?,php,codeigniter,authentication,codeigniter-3,Php,Codeigniter,Authentication,Codeigniter 3,我这样做 示例core/MY_CONTROLLER.php private $action_user=null; public function __construct() { parent::__construct(); ##listen for post attempts; $this->validate(); ##set action_user; return null if no session else return user object

我这样做

示例core/MY_CONTROLLER.php

private $action_user=null;
public function __construct()
{
    parent::__construct();

    ##listen for post attempts;
    $this->validate();

    ##set action_user; return null if no session else return user object
    $this->action_user = $this->session->userdata('loged_user');

    ##extra check step
    if($this->user->pwd_has_changed($this->action_user)){
            $this->session->sess_destroy();
            alerts('error','the password you used to login has changed ! please relogin');
            return $this->failed_login();
    }
}

public function alerts(){return die(json_encode(alerts()));}#a helper function.. just ignore it for this example
public function logout(){$this->session->sess_destroy();redirect();}


#AUTH
private function failed_login(){
    //$callers=debug_backtrace();

    alerts('warning','failed login');//.' '.$callers[1]['function']);
    ob_clean();//clear flush just to make sure !

    if($this->input->is_ajax_request())$this->load->view('base/ajax/landing');
    else $this->load->view('base/landing');

    die($this->output->get_output());//kill request and load landing in same uri. this in case he attempt login again he will be at same url; also helps with partials views
}
private function success_login(){
    unset($_POST['login'],$_POST['password']);
    alerts('success','welcome '.$this->action_user->login);
    //nothin much to do.. just dont die
}
private function validate(){
    //listen to posts if so logout and relog in
    if( !$this->input->post('login') || !$this->input->post('password'))return FALSE;
    $this->session->sess_destroy();#destroy session
    #1. validation
    $this->form_validation->set_rules('login', 'User Login', 'required|min_length[4]|max_length[12]|xss_clean');
    $this->form_validation->set_rules('password', 'Password', 'required|min_length[4]|max_length[12]|xss_clean');

    #1.2 Failed validation
    if( ! $this->form_validation->run() )return alerts('error',$this->form_validation->error_string(),false);#set message and return false

    #2. Login
    $this->user->login(set_value('login'),set_value('password'));
    //i dont want it to return anything ! $this->user->login should set messages of success OR fail + set user session
}
public function auth($role = null){
    if(!isset($this->action_user->id))
        return alerts('error',"this is a users restricted area",$this->failed_login());

    //ACCESS LEVELS CONDITIONS
    if($this->user->in_group($this->action_user->id,$role))return $this->success_login();

    return alerts('error',"this is a {$role} restricted area",$this->failed_login());
}
#END AUTH
现在在我的控制器构造函数中;因为我的_控制器构造函数是先调用的;所以我应该检索$action\u用户对象;或者已经尝试让他登录

如果我想限制一个页面,我只需添加

$this->auth();
//or $this->auth('admin');
它的构造器,如果用户是不允许的页面将死亡,并向他发送我的查看页面没有重定向

im使用这种方法的原因是让用户能够从任何控制器登录、注销; 如果他访问
http://localhost/RANDOMECONTROLLER/logout
他仍将注销。。登录也一样

有时,当我通过ajax获得页面片段时,这也是很有帮助的;它将只返回一个带有登录表单的登录页到这个div中

范例

一个统计页面有4个小部件,其中1个只能由管理员查看; 然后当ajax fitch 4小部件出现时,它将显示3和一个div,带有登录表单,表示您需要成为管理员才能登录


那么你认为这是一个好方法吗?或者是虫墙式意大利面**?

这不是一个好做法。最佳做法是创建一个安全的\u控制器,扩展我的\u控制器。如果您有一个带有auth的控制器,您需要扩展Secure\u控制器,但是如果您有另一个没有auth的控制器,您需要扩展MY\u控制器


例如,codeigniter auth有很多库易于扩展和适应您的需求。

有什么原因不适合这种做法吗?为什么我要使用一个繁重的库,它利用4个表进行简单的身份验证?即使我用了一个;这将取代我的模型,但我的问题是关于核心基础控制器的原因是,如果您在我的\u控制器中设置了身份验证功能,您会假设您未来的所有控制器都需要身份验证,这不是一个好的设计,因为将来可能会创建一个没有身份验证功能但需要MY_控制器功能的控制器。使用“重”是很有用的lib,因为您必须使用它编写0行代码,而且它由搜索bug和新功能的用户维护。一个好的做法是创建具有确定角色的类,例如用于管理身份验证的类,用于数据库访问的类。。。Auth必须由专门为该任务创建的类或控制器管理;这就是为什么我有
$this->user
来处理auth;对于需要auth的页面,我必须包括
$this->auth()command是关于维护库的一个很好的观点。