Cakephp - 2个不同用户的用户管理系统

时间:2014-10-04 04:02:11

标签: cakephp authentication

我正在使用CakePHP 2.3.6。在项目中,我有两种类型的用户:StudentsAdmin。因此,我为2种类型的用户创建了2 Controllers,即StudentsControllerAdminsController。我对这两个控制器有不同的Authentication配置,所以我在2个控制器中单独配置了AuthComponent。我希望为2种用户提供一个通用的login()函数实现,这样我就不必再编写两次相同的代码了。

这是我的代码:

AppController.php:

public $components=array('Session','RequestHandler','Acl','Auth'=>array('authorize'=>array('Actions'=>array('actionPath'=>'controllers'))));

StudentsController.php:

    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->loginRedirect=array('controller'=>'students','action'=>'editProfile');
        $this->Auth->logoutRedirect=array('controller'=>'students','action'=>'index');
        $this->Auth->authenticate=array('Form'=>array('scope'=>array('User.group_id'=>2),'userModel'=>'User','fields'=>array('username'=>'username','password'=>'password')));
        $this->Auth->unauthorizedRedirect=array('controller'=>'users','action'=>'login');
        $this->Auth->loginAction=array('controller'=>'users','action'=>'login');
        $this->Auth->allow('login','index','createProfile');
        $this->layout='student_layout';
    }

AdminsController.php:

    public function beforeFilter(){
        parent::beforeFilter();
        $this->Auth->loginRedirect=array('controller'=>'admins','action'=>'myJobs');
        $this->Auth->logoutRedirect=array('controller'=>'admins','action'=>'index');
        $this->Auth->authenticate=array('Form'=>array('scope'=>array('User.group_id'=>1),'userModel'=>'User','fields'=>array('username'=>'username','password'=>'password')));
        $this->Auth->authError='Did you really think you are allowed to see that ?';
        $this->Auth->unauthorizedRedirect=array('controller'=>'admin','action'=>'index');
        $this->Auth->loginAction=array('controller'=>'users','action'=>'login');
        $this->Auth->allow('index');
        $this->layout='admin_layout';
    }

UsersController.php:

public function login(){
    if($this->request->is('post'))
        if($this->Auth->login()){
            $welcome=($this->Auth->user('group_id')==2)?'Welcome '.$this->Student->field('name',array('Student.id'=>$this->Auth->user('id'))):(($this->Auth->user('group_id')==1)?"<p style='margin-left:20px;color:#366;'><strong>Welcome Admin, You have successfully entered to your Admin Panel!</strong></p>":"");
            $this->Session->setFlash($welcome);
            return $this->redirect($this->Auth->redirect());
        }else{
            $this->Session->setFlash('Invalid username or password, please try again');
            $this->set('title_for_layout','Error - Login');
        }
}

因此,对于两个用户,我希望在login中处理users/login。我知道,我的代码有点复杂。实际上,我的AdminsController index页面包含login form,其中提交了users/login

我的意思是,login逻辑应该在users/login中处理,但login page(login form)可能对两个用户都不同,唯一重要的是那些forms应提交给{ {1}}。

现在,通过这些配置,users/login无法访问StudentseditProfile无法访问Admins中的anything

我认为成功登录后问题出在Admin Panel上。这就是我在登录功能redirecting之前使用return的原因。

那么,问题出在哪里?我该怎么办?

请帮帮我。

感谢。

1 个答案:

答案 0 :(得分:1)

在core.php中修改此行:

Configure::write('Routing.prefixes', array('admin','student'));

在app controller中的beforeFilter函数中添加以下行:

if (isset($this->params['prefix']) && $this->params['prefix'] == 'admin') {
        AuthComponent::$sessionKey = 'Auth.Admin';
        $this->Auth->loginAction = array('plugin' => false, 'controller' => 'users', 'action' => 'login','admin'=>true);
        $this->Auth->logoutRedirect = array('plugin' => false, 'controller' => 'admin', 'action' => 'dashboard');
    } else {
        AuthComponent::$sessionKey = 'Auth.Front';
        $this->Auth->loginAction = array('plugin' => false, 'controller' => 'users', 'action' => 'login',$this->request->prefix=>false);
        $this->Auth->logoutRedirect = array('plugin' => false, 'controller' => 'users', 'action' => 'dashboard');
    }

为了获得管理员的会话,你可以像那样使用

$this->Session->read('Auth.Admin');

为前线(学生)会议
$this->Session->read('Auth.Front');