【问题标题】:cakephp2 Auth Component with 2 users tables带有 2 个用户表的 cakephp2 Auth 组件
【发布时间】:2013-06-16 23:04:10
【问题描述】:

我有一个带有“用户”和“管理员”的 cakephp 2.0 应用程序,但它们被组织成 2 个不同的表,我如何检查这 2 个表以使用 Auth 组件使用相同的表单登录?我试过这个solution,但你必须做两种不同的形式,每个模型一个,有没有办法强制认证查看两个表?

【问题讨论】:

    标签: php authentication cakephp-2.0


    【解决方案1】:

    好的,经过一些尝试,我解决了这个问题,首先我定义了 2 个 authenticate ,一个用于用户,一个用于管理员,如下所示:

    App::uses('FormAuthenticate', 'Controller/Component/Auth');
    class AdministratorAuthenticate extends FormAuthenticate {
    }
    

    App::uses('FormAuthenticate', 'Controller/Component/Auth');
    class UserAuthenticate extends FormAuthenticate {
    }
    

    现在我们可以像这样在 AppController 中定义 components 变量:

    public $components = array(
        'Session',
        'Auth' => array(
            'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
            'authenticate' => array(
                    'Administrator' => array(
                        'fields' => array('username' => 'email'),
                        'userModel' => 'Administrator'
                    ),
                    'User' => array(
                        'fields' => array('username' => 'email'),
                        'userModel' => 'User'
                    )
                    )
        )
    );
    

    然后在访问控制器的登录方法中我尝试管理员登录,然后用户以这种方式登录:

    if ($this->Auth->login()) {
    $this->redirect(array('controller'=>'administrator','action'=>'index'));
    } 
    else {
    $this->request->data['Users']=$this->request->data['Administrator'];
    unset($this->request->data['Administrator']);
    if ($this->Auth->login()) {
        $this->redirect(array('controller'=>'user','action'=>'index'));
    }
        else                            
          $this->Session->setFlash(__('Invalid username or password, try again'));
    }
    

    这里的关键是修改request->data数组用'User'替换userModel'Administrator'

    【讨论】:

      猜你喜欢
      • 2010-12-13
      • 2012-07-09
      • 2017-05-14
      • 2013-09-09
      • 1970-01-01
      • 2012-02-18
      • 2014-08-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多