普通网友 2013-06-21 14:47
浏览 69
已采纳

使用电子邮件进行身份验

I followed the basic Authentication tutorial of CakePHP. If I add a user, I can't login with him. The views with the inputs should be alright. Background: Users HASANDBELONGSTOMANY Podcasts with a join table.


Controller/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());
        }
    }
}

Model/User.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);
  }
}
  • 写回答

1条回答 默认 最新

  • douyou1960 2013-06-21 15:05
    关注

    Solution: I'd like to authenticate by email, which I have to declare according to this answer.

    So my AppController.php looks like this:

    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')
            )
        );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计