0

私はこれをやっています。

例 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

今私のコントローラーコンストラクターにあります。MY_CONTROLLER コンストラクターが最初に呼び出されるため。したがって、$action_user オブジェクトを取得する必要があります。またはすでにログインしようとしています。

ページを制限したい場合は、追加するだけです

$this->auth();
//or $this->auth('admin');

そのコンストラクターに、ユーザーが許可されていない場合、ページは終了し、リダイレクトなしで私のビューページを彼に送信します。

このようなアプローチを使用する理由は、ユーザーが任意のコントローラーからログイン、ログアウトできるようにするためです。彼が訪問した場合、http://localhost/RANDOMECONTROLLER/logout彼はまだログアウトします..ログインについても同じです。

また、ajax でページのパーシャルを取得するときに役立つこともあります。ログインフォームを使用してランディングページをこの div に返すだけです。

統計ページには 4 つのウィジェットがあり、そのうちの 1 つは管理者のみが表示できます。次に、ajaxが4つのウィジェットをフィッティングすると、3と、ログインするには管理者である必要があるというログインフォーム付きのdivが表示されます..

...

これはこれを行う良い方法だと思いますか?それともバグゲーブルスパゲッティですか* * ?

4

1 に答える 1