使用CakePHP中的不同用户角色登录?

时间:2013-07-23 11:40:28

标签: php cakephp login login-control user-roles

我正在使用CakePHP制作一个系统,其中用户可以是A,B或C.像学生,老师和其他一些角色。是否有可能让他们通过1个链接登录?所以不是/ students / login和/ teachers / login,但所有这些都是www.somewebsite / login吗?

2 个答案:

答案 0 :(得分:4)

Read this tutorial,它完全涵盖了您所要求的内容。还read this section

对于不同类型的用户而言,拥有不同的控制器根本没有任何意义,您只需复制代码即可。如果您需要根据角色采取不同的操作,您可以在登录方法中通过从login()方法中调用afterStudentLogin()等其他方法来执行此操作,并在那里执行角色特定的操作。原因是单个方法应始终只执行一个任务,因此您可以在单独的方法中将特定于角色的代码与其分离。

public function login() {
    if ($this->Auth->user()) {
        /* ... */
        $callback = 'after' . $this->Auth->user('role') . 'Login');
        $this->{$callback}($this->Auth->user());
        /* ... */
    }
}

即使用户类型非常不同,他们都会共享一个共同点:登录。在这种情况下,有一个users表,例如student_profils表和teacher_profiles表。如果差异只是几个字段,我会将它们全部放在一个表中,如profiles

如果您想使用/ login而不是/ users / login,则应使用routing

Router::connect(
    '/login',
    array(
        'controller' => 'users',
        'action' => 'login'
    )
);

您还可以使用look at this Users plugin来涵盖许多与用户相关的常规任务。并here is a simple multi-role authorization adapter

答案 1 :(得分:1)

取决于用户组的简单基本登录功能如下所示

<?php
public function login() {       

        //if user already logged in call routing function...
        if($this->Session->read('Auth.User')) {
            $this->routing();
        }

        if ($this->request->is('post')) {

            if ($this->Auth->login()) { 

                //if user status is active...
                if ($this->Auth->user('status') == 1){ 

                    //redirect users based on his group id...
                    if($this->Auth->User('group_id')==1){
                        $this->redirect($this->Auth->redirect('/admins/dashboard'));
                    } 
                    else if($this->Auth->User('group_id')==2){
                        $this->redirect($this->Auth->redirect('/teachers/dashboard'));
                    } 
                    else if($this->Auth->User('group_id')==3){
                        $this->redirect($this->Auth->redirect('/students/dashboard'));
                    }
                } 
                else{

                    $this->Session->delete('User');
                    $this->Session->destroy();
                    $this->Session->setFlash('Your account is not yet activated. Please activate your account to login.', 'warning');
                }

            } 
            else {
                $this->Session->setFlash('Your username or password was incorrect.', 'error');
            }
        }
    }

//just route the loggedin users to his proper channel...
public function routing() {

        if($this->Session->read('Auth.User.Group.id') == 1) {
            $this->redirect('/admins/dashboard');
        } 
        else if($this->Session->read('Auth.User.Group.id') == 2) {
            $this->redirect('/teachers/dashboard');
        }
        else if($this->Session->read('Auth.User.Group.id') == 3) {
            $this->redirect('/students/dashboard');
        } 
        else {
            $this->Session->destroy();
            $this->redirect('/');
        }
    }   
?>