Cakephp 2:有两种不同的认证功能

Cakephp 2 : Have two different authentication functions

我在蛋糕 php 中使用自定义身份验证对象。 我在 component/Auth/LdapAuthenticate.php 中创建了一个文件。在这个文件中,我有一个使用 LDAP 进行身份验证的函数。它看起来像这样:

App::uses('BaseAuthenticate', 'Controller/Component/Auth');
class LdapAuthenticate extends BaseAuthenticate {
public function authenticate(CakeRequest $request, CakeResponse $response) {
    $username=$request->data["Users"]["username"];
    $pwd=$request->data["Users"]["password"];
    $ldap = ldap_connect("ldap:........");
    ldap_set_option ($ldap, LDAP_OPT_REFERRALS, 0);
    ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);

    $bind = @ldap_bind($ldap, "TEST\".$username, $pwd);
    if ($bind && $pwd!="") {
        //CakeLog::write('debug', "loggé");
        $ldap_dn ="DC=world,DC=pcm,DC=local";
        $filter = "(&(objectClass=user)(samaccountname=".$username.")(cn=*))";
        $justthese = array("cn","mail","givenname","distinguishedname","memberof");
        $sr=ldap_search($ldap, $ldap_dn, $filter,$justthese);
        $info = ldap_get_entries($ldap, $sr);
        ldap_close($ldap);
        return $info;
    } else {
        ldap_close($ldap);
        return false;
    }
}

}

它在用户控制器中像这样记录我:

function login(){
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
               return $this->redirect($this->Auth->redirectUrl());
             }else{
                 $this->Session->setFlash(__('Username ou password incorrect'), 'default', array('class'=>'error-message'), 'auth');
             }

我现在想创建第二个登录控制器,它将我的用户登录到数据库。我的问题是如何创建第二个自定义身份验证对象并在正确的位置调用它?我想在函数 logindist() 中使用它。认证会有2个页面,一个是ldap连接,一个是数据库连接。

我不明白你的问题。但无论如何我都会盲目拍摄。 我想你想支持 multiply cakephp auth。 auth 对象就是为了这个目的。你可以附加很多对象,cakephp会依次检查它们,如果有任何一个可以识别请求,将允许访问。

$this->Auth->authenticate = array(
    'databaseAuth',
     'Ldap'
);

如果您所有的 auth 对象都在它们的 authenticate() 方法中进行标识,即不是无状态的,那么您不需要任何设置,只需按正确的顺序包含 auth 对象,cakephp 将从那里接管。 相反,如果您需要首先根据数据库对您的用户进行身份验证,那么上面的设置就可以了。

但请记住,如果您需要像上面所说的那样在您的应用程序中进行并行验证,您将需要手动调用 identify() 方法或在 auth 对象中实现 getUser() 方法以使 cakephp Auth 正常运行

public function logindist(){

      $user = $this->Auth->identify();
      if($user){
        $this->Auth->allow();
       }
     // throw 403 exception
   }