0

我是 cakephp 2.x 的新手,所以我不知道该怎么做.. 我想从他的电子邮件地址和电话号码登录用户..如果数据库中的号码是这个“12345”和用户,我的意图是什么正在尝试通过此号码“+12345”登录他可以登录系统..我已经编写了一个代码,但我不知道如何使用它或在 auth 组件中调整我的代码,因为 auth 组件正在自动记录用户 ..

这是我的控制器

 public function beforeFilter() {
    parent::beforeFilter();


    $this->Auth->authenticate = array(
        'Authenticate.Cookie' => array(
            'fields' => array(
                'username' => 'email',
                'password' => 'password'
            ),
            'userModel' => 'User',
            'scope' => array('User.active' => 1)
        ),
        'Authenticate.MultiColumn' => array(
            'fields' => array(
                'username' => 'email',
                'password' => 'password'
            ),
            'columns' => array('email', 'mobileNo'),
            'userModel' => 'User',
        )
    );
 }




public function login() {


    if ($this->Auth->login() || $this->Auth->loggedIn()) {
        $this->redirect('/users/dashboard');
    }else{
    $this->layout='logindefault';
    $this->set('title_for_layout', 'Account Login');
    /*$this->Auth->logout();
     $cookie = $this->Cookie->read('Auth.User'); */


    if ($this->request->is('post')) {




        if ($this->Auth->login() || $this->Auth->loggedIn()) {
            if ($this->Session->check('Auth.User')){


            $this->_setCookie($this->Auth->user('idUser'));
            $this->redirect('/users/dashboard');

        } }else {
            $this->Session->setFlash('Incorrect Email/Password Combination');
        }
    }}
}

这是我要添加的代码..

    $mobileNo='+123456789';
   if (strpos($mobileNo,'+') !== false) {
      $mobileNo=str_replace("+", "",$mobileNo);
   }

?>

4

2 回答 2

0

我已经做到了,它有效

我在 $this->Auth->login() $this->request->data['User']['email'] = $mobileNo; 之前添加了这一行

来源:用户登录 Cakephp 时的字符串连接

于 2013-06-28T04:55:15.700 回答
0

文档 ( http://book.cakephp.org/2.0/en/models/data-validation.html#custom-validation-rules ) 有关于自定义验证规则的示例。

但是您的用例与验证无关(对于登录,没有正常的验证,只能应用“范围”)。它关于自定义进入登录的数据。您可以使用 login() 方法在 Auth->login() 调用之前修改传入数据,也可以使用新的 2.x 方法:使用自定义身份验证适配器。

// in your AppController::beforeFilter() method
$this->Auth->authorize = array('PhoneNumber');

然后您的 PhoneNumber 适配器应称为 PhoneNumberAuthenticate 并放置在 /Controller/Component/Auth 中。

有关其他适配器如何工作并将其应用于您自己的适配器的详细信息,请参阅https://github.com/ceeram/Authenticate 。

但由于您还使用“电子邮件”登录,您最好在登录方法中使用替换选项:

public function login() {
    $this->request->data['User']['email'] = $this->_replace($this->request->data['User']['email']);
    if ($this->Auth->login()) {...}

_replace() 包含您的替换代码。

于 2013-06-27T10:16:23.120 回答