2014-10-29 30 views
2

我正在使用cakephp构建应用程序。Cakephp 2.3:身份验证组件,在OR中的两列进行身份验证用户

我选择了cakephp2.3。

用户将使用“email”或“用户名”进行身份验证。

我发现一个选项与Auth组件“范围”,但与此我们可以设置静态条件。

LIKE: if user is active,, is_active => 1 

但我希望,虽然身份验证组件应检查'电子邮件'或'用户名'字段和其他密码。

有什么办法吗?

回答

1

这需要一些代码。您可以通过Users/CakeDC插件轻松找到它here

此插件使用下面的auth组件用于多列登录。

https://github.com/CakeDC/users/blob/master/Controller/Component/Auth/MultiColumnAuthenticate.php

它还包括如何使用的例子。如果你不想要整个插件,你可以将MultiColumnAuthenticate.php复制到文件夹app/Controllers/Components/Auth /。

,如果你只复制文件,然后在你的AppController中beforeFilter方法你必须写:

class AppController extends Controller { 

    public function beforeFilter() { 
     parent::beforeFilter(); 
     $this->Auth->authenticate = array(
      'MultiColumn' => array( //With no plugin 
       'fields' => array(
        'username' => 'username', 
        'password' => 'password' 
       ), 
       'columns' => array('username', 'email'), 
      ) 
     ); 
    } 
} 
+0

我一直搞不明白,为什么你已经加入此:: **回报parent :: beforeFilter(); ** – 2014-10-29 12:35:57

+0

我编辑了这篇文章。 – gmponos 2014-10-29 12:42:19

+0

@PankajBadukale这与这个答案没有关系,但是在cakephp的约定的一部分。 – 2017-03-30 19:12:23

0

根据CakePHP的文档。

FormAuthenticate允许您基于表单POST数据对用户进行身份验证。通常这是用户将信息输入到

默认情况下AuthComponent登录表单使用FormAuthenticate

你可以试试这个:

// Pass settings in $components array 
    public $components = array(
    'Auth' => array(
    'authenticate' => array(
     'Form' => array(
      'fields' => array('username' => 'email', 'password' => 'password') 
     ) 
    ) 
    ) 
); 
+0

你能指出那个文档吗?因为这不是用户名和密码,它会要求用户名和电子邮件。 – gmponos 2014-10-29 12:45:46

+0

你可以访问以下链接:http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html – Ali 2014-10-29 12:51:53

+0

检查我编辑的ans – Ali 2014-10-29 12:54:27