【问题标题】:Authentication with email not username使用电子邮件而不是用户名进行身份验证
【发布时间】:2013-06-21 14:47:12
【问题描述】:

我遵循 CakePHP 的基本 Authentication tutorial。如果我添加一个用户,我不能用他登录。带有输入的视图应该没问题。 背景用户 HASANDBELONGSTOMANY 播客具有连接表。


控制器/Userscontroller.php

public function add() {
    if ($this->request->is('post')) {
        $this->User->create();
        if ($this->User->saveAssociated($this->request->data)) { //saveAssociatied() is described below
            $this->redirect(array('action' => 'index'));
        }

    }
    $podcasts = $this->User->Podcast->find('list');
    $this->set(compact('podcasts'));
}

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        }
    }
}

模型/用户.php

public $hasAndBelongsToMany = array(
    'Podcast' => array(
        'className' => 'Podcast',
        'joinTable' => 'podcasts_users',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'podcast_id',
        'unique' => false,
    )
);

//password encryption
public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
        $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
}

AppModel.php (according to this)

class AppModel extends Model {
public function saveAssociated($data = null, $options = array()) {
    foreach ($data as $alias => $modelData) {
        if (!empty($this->hasAndBelongsToMany[$alias])) {
            $habtm = array();
            $Model = ClassRegistry::init($this->hasAndBelongsToMany[$alias]['className']);
            foreach ($modelData as $modelDatum) {
                if (empty($modelDatum['id'])) {
                    $Model->create();
                }
                $Model->save($modelDatum);
                $habtm[] = empty($modelDatum['id']) ? $Model->getInsertID() : $modelDatum['id'];                    
            }
            $data[$alias] = array($alias => $habtm);
        }
    }
    return parent::saveAssociated($data, $options);
  }
}

【问题讨论】:

  • 那么你到底想要什么
  • 显然我想登录。

标签: php cakephp authentication cakephp-2.0 has-and-belongs-to-many


【解决方案1】:

解决方法:我想通过邮件认证,我必须按照this answer.声明

所以我的 AppController.php 看起来像这样:

public $components = array(
        'Session',
        'Auth' => array(
            'authenticate' => array(
                'Form' => array(
                    'fields' => array('username' => 'email')
                )
            ),
            'loginRedirect' => array('controller' => 'xxx', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
        )
    );

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 1970-01-01
    • 2010-10-16
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多