我已經使用電子郵件而不是用戶名設置了摘要式身份驗證,並嘗試根據食譜正確進行配置。不幸的是有些事情並不完全清楚。因此,試圖在我的cakephp3應用程序編輯的用戶,當它返回以下錯誤:cakephp 3使用電子郵件進行身份驗證聲明用戶名列不存在
Error: SQLSTATE[42703]: Undefined column: 7 ERROR: Column users.username doesn't exist LINE 1: SELECT Users.id AS "Users__id", Users.username AS "Users__us...^
的其他操作界面污物工作,雖然:創建用戶,列出用戶和查看用戶不會返回一個錯誤。但登錄和刪除不工作,要麼和我pgAdmin的存在似乎沒有數據行插入,雖然數據顯示
我這樣的配置它(也許與我的elasticsearch插件有問題?):
調用createUsers遷移:
public function change()
{
$table = $this->table('users', ['id' => false, 'primary_key' => ['id']]);
$table->addColumn('id', 'uuid');
$table->addColumn('email', 'string', [
'default' => null,
'limit' => 254,
'null' => false,
]);
$table->addColumn('password', 'binary', [
'default' => null,
'null' => false,
]);
$table->addColumn('created', 'datetime', [
'default' => null,
'null' => false,
]);
$table->addColumn('modified', 'datetime', [
'default' => null,
'null' => false,
]);
$table->create();
}
的AppController:
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->loadComponent('Auth', [
'authenticate' => [
'Digest' => [
'fields' => ['username' => 'email'],
'userModel' => 'Users',
'finder' => 'auth'
],
],
'loginAction' => [
'controller' => 'Users',
'action' => 'login'
],
'authError' => 'Dazu hast du keine Rechte...',
'storage' => 'Memory',
'unauthorizedRedirect' => false
]);
}
UsersController:
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
// Load the Type using the 'Elastic' provider.
$this->loadModel('Users', 'Elastic');
$this->Auth->allow(['index', 'view']);
if (!$this->Auth->user()){
$this->Auth->allow('add');
}
}
public function edit($id = null)
{
$user = $this->Users->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
用戶實體:
class User extends Entity
{
protected function _setPassword($password)
{
if (strlen($password) > 0) {
return (new DefaultPasswordHasher)->hash($password);
}
}
}
UsersTable:
public function initialize(array $config)
{
$this->addBehavior('Timestamp', [
'events' => [
'Model.beforeSave' => [
'created_at' => 'new',
'modified_at' => 'always'
]
]
]);
}
public function beforeSave(Event $event)
{
$entity = $event->data['entity'];
// Make a password for digest auth.
$entity->password = DigestAuthenticate::password(
$entity->email, //maybe change to username
$entity->plain_password,
env('SERVER_NAME')
);
$entity->created = Time::now();
return true;
}
public function findAuth(\Cake\ORM\Query $query, array $options)
{
$query
->select(['id', 'username', 'password'])
->where(['Users.active' => 1]);
return $query;
}
模板 「用戶編輯」 觀點:
<nav class="large-3 medium-4 columns" id="actions-sidebar">
<ul class="side-nav">
<li class="heading"><?= __('Actions') ?></li>
<li><?= $this->Form->postLink(
__('Delete'),
['action' => 'delete', $user->id],
['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]
)
?></li>
<li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?> </li>
</ul>
</nav>
<div class="users form large-9 medium-8 columns content">
<?= $this->Form->create($user) ?>
<fieldset>
<legend><?= __('Edit User') ?></legend>
<?php
echo $this->Form->input('email');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
</div>
我可以從錯誤信息中看到的唯一的事情就是這可能是cakephp中的核心問題,我不會硬編碼任何更改並且文檔沒有明確提到使用電子郵件而不是用戶名列的步驟。
當註釋掉這部分內容時(也是在配置auth組件時)它工作正常!謝謝。 – hardking