首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CakePHP 3.6 $this->Auth->identify()对新创建的数据返回FALSE

CakePHP 3.6 $this->Auth->identify()对新创建的数据返回FALSE
EN

Stack Overflow用户
提问于 2018-06-22 14:43:01
回答 1查看 686关注 0票数 0

我目前在解决我的问题时遇到了一些困难。我是cakePHP 3的新手,被指派创建一个博客系统(用户、博客、角色)来进一步培训。我做的第一件事是插入身份验证部分。最初,登录工作正常(我使用/users/add.ctp创建了3个帐户,并通过DefaultPasswordHasher使用散列密码,并尝试使用每个帐户登录)。经过几天的网站前端设计,我创建了两个新用户,并注意到当我尝试登录这两个新帐户时,$user = $this->Auth->identify()返回false。我尝试使用我的一个旧帐户登录,然后定义了$user。为了测试它,我编辑了旧帐户的一个密码,并尝试使用新密码登录,现在$user返回false。

下面是我的代码:

代码语言:javascript
运行
复制
//App Controller
$this->loadComponent('Auth', [
        'authenticate' => [
            'Form' => [
                'fields' => ['username' => 'username','password' => 'password'],

            ]
        ],
        'authError' => 'Please login before continuing.',
        'loginAction' => ['controller' => 'Users', 'action' => 'login'],
        'loginRedirect' => ['controller' => 'Blogs', 'action' => 'index'],
        'logoutRedirect' =>  ['controller' => 'Users', 'action' => 'login'],
        'unauthorizedRedirect' => ['controller' => 'Users', 'action' => 'login']
    ]);

public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    $this->Auth->allow();



    $this->set(compact('session_user'));
}

这是我的用户控制器登录:

代码语言:javascript
运行
复制
public function login()
{
    $this->viewBuilder()->setLayout('login');

    if ($this->request->is('post')) {
        $user = $this->Auth->identify();
        die(pr($user));  //<--To check if identify() catches login details
        if ($user) {
            $this->Auth->setUser($user);
            return $this->redirect($this->Auth->redirectUrl());
        }
        else {
            $this->Flash->error(__('Username/Password is invalid.'));
            return $this->redirect($this->Auth->config('loginAction'));
        }
    }
}

这是login.ctp

代码语言:javascript
运行
复制
<div class = "login-container">
<div class = "login-div">
    <div = "login-field">
        <h1>Login Page</h1>
        <?= $this->Form->create(); ?>
        <?= $this->Form->control('username') ?>
        <?= $this->Form->control('password', ['type' => 'password']) ?>
        <?= $this->Form->button(__('Login')) ?>
        <?= $this->Form->end() ?>
    </div>
</div>

这是我的用户实体:

代码语言:javascript
运行
复制
class User extends Entity
{

/**
 * Fields that can be mass assigned using newEntity() or patchEntity().
 *
 * Note that when '*' is set to true, this allows all unspecified fields to
 * be mass assigned. For security purposes, it is advised to set '*' to false
 * (or remove it), and explicitly make individual fields accessible as needed.
 *
 * @var array
 */
protected $_accessible = [
    'username' => true,
    'role_id' => true,
    'email' => true,
    'password' => true,
    'created' => true,
    'modified' => true,
    'role' => true,
    'blogs' => true
];

/**
 * Fields that are excluded from JSON versions of the entity.
 *
 * @var array
 */
protected $_hidden = [
    'password'
];

public function _setPassword($value) {
    $hasher = new DefaultPasswordHasher();
    return $hasher->hash('$value');
}

}

这是我的UsersTable:

代码语言:javascript
运行
复制
class UsersTable extends Table
{

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('users');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->addBehavior('Timestamp');

    $this->belongsTo('Roles', [
        'foreignKey' => 'role_id',
        'joinType' => 'INNER'
    ]);
    $this->hasMany('Blogs', [
        'foreignKey' => 'user_id'
    ]);
    $this->hasMany('Comments', [
        'foreignKey' => 'user_id'
    ]);
}

/**
 * Default validation rules.
 *
 * @param \Cake\Validation\Validator $validator Validator instance.
 * @return \Cake\Validation\Validator
 */
public function validationDefault(Validator $validator)
{
    $validator
        ->integer('id')
        ->allowEmpty('id', 'create');

    $validator
        ->scalar('username')
        ->maxLength('username', 255)
        ->requirePresence('username', 'create')
        ->notEmpty('username');


    $validator
        ->email('email')
        ->requirePresence('email', 'create')
        ->notEmpty('email');

    $validator
        ->scalar('password')
        ->maxLength('password', 255)
        ->requirePresence('password', 'create')
        ->notEmpty('password');

    return $validator;
}

/**
 * Returns a rules checker object that will be used for validating
 * application integrity.
 *
 * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
 * @return \Cake\ORM\RulesChecker
 */
public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->isUnique(['username']));
    $rules->add($rules->isUnique(['email']));
    $rules->add($rules->existsIn(['role_id'], 'Roles'));

    return $rules;
}

}

我在用户控制器的登录函数中的$this->Auth->identify();之后插入了骰子(pr($user))。通过在我的localhost/phpmyadmin和users/view.ctp上进行检查,我确信数据被正确地保存和散列,只是新创建/编辑的帐户似乎无法通过登录。

EN

回答 1

Stack Overflow用户

发布于 2018-06-22 15:38:14

我找出了我的错误所在。我在密码hasher的_setPassword函数中的$value上放了单引号。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50982102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档