在CakePHP的验证组件使用电子邮件,而不是用户名 [英] Using email instead of username in CakePHP Auth Component

查看:305
本文介绍了在CakePHP的验证组件使用电子邮件,而不是用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作CakePHP的2.x的。我的问题是我不希望从数据库中用于记录用户名..我采取从用户的电子邮件和密码,并确认此电子邮件和密码
 我有一个表在我的数据库名称用户并有3场
ID
电子邮件
密码

这是我的code

型号

 < PHP
类用户扩展AppModel {
公共$ useTable ='用户';
}
?>

的AppController

 类AppController的扩展控制器{
   公共$组件=阵列(
   会话,
'验证'=>阵列(
    loginRedirect'=>阵列(控制器= GT;'用户','行动'=>管理),
    logoutRedirect'=>阵列(控制器= GT;'用户','行动'=>管理),
    authError'=>中你不能访问该页面,
    '授权'=>阵列('控制')
   )
);公共职能isAuthorized($用户){
}  公共职能beforeFilter(){
  $这个 - > Auth->允许('指数');

UserController中

 公共职能登录()
   {
     如果($这个 - >请求 - '是('后')){
       如果(这 - $> Auth->登录()){
        $这个 - >重定向($这个 - > Auth->重定向());
    }其他{
        $这个 - >会话级> setFlash('您的email /密码组合是不正确');
    }
}
}

login.ctp

 < PHP
   回声$这个 - >&形式 - GT;创建();  回声$这个 - >&形式 - GT;输入(电子邮件);
  回声$这个 - >&形式 - GT;输入('密码');
    回声$这个 - >&形式 - GT;结束('身份验证');
   ?>


解决方案

您可以配置它:

 公共$组件=阵列(
    '验证'=>阵列(
        '身份验证'=>阵列(
            形= GT;阵列(
                '域'=>阵列('用户名'= GT;电子邮件)
            )
        )
    )
);

又见<一个href=\"http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#configuring-authentication-handlers\">Configuring验证处理的在食谱。

I am working on cakephp 2.x . my problem is i dont want to use username for logging .. i am taking the email and password from the user and verify this email and password from the database i have a table in my database name user and it has 3 fields id, email and password

here is my code

Model

 <?php
class User extends AppModel {
public $useTable = 'user';
}
?>

AppController

 class AppController extends Controller {
   public $components = array(
   'Session',
'Auth'=>array(
    'loginRedirect'=>array('controller'=>'users', 'action'=>'admin'),
    'logoutRedirect'=>array('controller'=>'users', 'action'=>'admin'),
    'authError'=>"You can't access that page",
    'authorize'=>array('Controller') 
   )
);

public function isAuthorized($user) {
}

  public function beforeFilter() {
  $this->Auth->allow('index');

UserController

public function login()
   {
     if ($this->request->is('post')) {
       if ($this->Auth->login()) {
        $this->redirect($this->Auth->redirect());
    } else {
        $this->Session->setFlash('Your email/password combination was incorrect');
    }
}
}

login.ctp

 <?php


   echo $this->form->create();

  echo $this->form->input('email');
  echo $this->form->input('password');


    echo $this->form->end('Authenticate');
   ?>

解决方案

You can configure it with:

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'fields' => array('username' => 'email')
            )
        )
    )
);

See also Configuring Authentication handlers in the cookbook.

这篇关于在CakePHP的验证组件使用电子邮件,而不是用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆