ログインがうまくいかないので色々見ていたらハッシュ化ができていないことに気づきました。
しかし命名規則も守れているようだし原因がわかりません。お力添えをお願いします。
php
1<?php 2namespace App\Model\Entity; 3 4use Cake\ORM\Entity; 5use Cake\Auth\DefaultPasswordHasher; 6 7/** 8 * User Entity 9 * 10 * @property int $id 11 * @property string $username 12 * @property string $password 13 * @property string $role 14 * 15 * @property \App\Model\Entity\Bidinfo[] $bidinfo 16 * @property \App\Model\Entity\Biditem[] $biditems 17 * @property \App\Model\Entity\Bidmessage[] $bidmessages 18 */ 19class User extends Entity 20{ 21 /** 22 * Fields that can be mass assigned using newEntity() or patchEntity(). 23 * 24 * Note that when '*' is set to true, this allows all unspecified fields to 25 * be mass assigned. For security purposes, it is advised to set '*' to false 26 * (or remove it), and explicitly make individual fields accessible as needed. 27 * 28 * @var array 29 */ 30 protected $_accessible = [ 31 'username' => true, 32 'password' => true, 33 'role' => true, 34 'bidinfo' => true, 35 'biditems' => true, 36 'bidmessages' => true 37 ]; 38 39 /** 40 * Fields that are excluded from JSON versions of the entity. 41 * 42 * @var array 43 */ 44 protected $_hidden = [ 45 'password' 46 ]; 47 48 protected function _setPassword($password) { 49 return (new DefaultPasswordHasher)->hash($password); 50 } 51} 52
php
1//UserController.php 2<?php 3namespace App\Controller; 4 5use App\Controller\AppController; 6use Cake\Auth\DefaultPasswordHasher; 7use Cake\Event\Event; 8 9/** 10 * Users Controller 11 * 12 * @property \App\Model\Table\UsersTable $Users 13 * 14 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = []) 15 */ 16 17class UsersController extends AppController 18{ 19 /** 20 * Index method 21 * 22 * @return \Cake\Http\Response|null 23 */ 24 public function index() 25 { 26 $users = $this->paginate($this->Users); 27 28 $this->set(compact('users')); 29 } 30 31 /** 32 * View method 33 * 34 * @param string|null $id User id. 35 * @return \Cake\Http\Response|null 36 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 37 */ 38 public function view($id = null) 39 { 40 $user = $this->Users->get($id, [ 41 'contain' => ['Bidinfo', 'Biditems', 'Bidmessages'] 42 ]); 43 44 $this->set('user', $user); 45 } 46 47 /** 48 * Add method 49 * 50 * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. 51 */ 52 public function add() 53 { 54 $user = $this->Users->newEntity(); 55 if ($this->request->is('post')) { 56 $user = $this->Users->patchEntity($user, $this->request->getData()); 57 if ($this->Users->save($user)) { 58 $this->Flash->success(__('The user has been saved.')); 59 60 return $this->redirect(['action' => 'index']); 61 } 62 $this->Flash->error(__('The user could not be saved. Please, try again.')); 63 } 64 $this->set(compact('user')); 65 } 66 67 /** 68 * Edit method 69 * 70 * @param string|null $id User id. 71 * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. 72 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 73 */ 74 public function edit($id = null) 75 { 76 $user = $this->Users->get($id, [ 77 'contain' => [] 78 ]); 79 if ($this->request->is(['patch', 'post', 'put'])) { 80 $user = $this->Users->patchEntity($user, $this->request->getData()); 81 if ($this->Users->save($user)) { 82 $this->Flash->success(__('The user has been saved.')); 83 84 return $this->redirect(['action' => 'index']); 85 } 86 $this->Flash->error(__('The user could not be saved. Please, try again.')); 87 } 88 $this->set(compact('user')); 89 } 90 91 /** 92 * Delete method 93 * 94 * @param string|null $id User id. 95 * @return \Cake\Http\Response|null Redirects to index. 96 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 97 */ 98 public function delete($id = null) 99 { 100 $this->request->allowMethod(['post', 'delete']); 101 $user = $this->Users->get($id); 102 if ($this->Users->delete($user)) { 103 $this->Flash->success(__('The user has been deleted.')); 104 } else { 105 $this->Flash->error(__('The user could not be deleted. Please, try again.')); 106 } 107 return $this->redirect(['action' => 'index']); 108 } 109 110 public function initialize() 111 { 112 parent::initialize(); 113 //各種コンポーネントのロード 114 $this->loadComponent('RequestHandler'); 115 $this->loadComponent('Flash'); 116 $this->loadComponent('Auth', [ 117 'authorize'=>['Controller'], 118 'authenticate'=>[ 119 'Form'=>[ 120 'fields'=>[ 121 'username'=>'username', 122 'password'=>'password' 123 ] 124 ] 125 ], 126 'loginRedirect'=>[ 127 'controller'=>'Users', 128 'action'=>'index' 129 ], 130 'logoutRedirect'=>[ 131 'controller'=>'Users', 132 'action'=>'login', 133 ], 134 'authError'=>'ログインしてください。', 135 ]); 136 } 137 138 //ログイン処理 139 function login(){ 140 if ($this->request->isPost()) { 141 $user = $this->Auth->identify(); 142 if (!empty($user)) { 143 $this->Auth->setUser($user); 144 return $this->redirect($this->Auth->redirectUrl()); 145 } else { 146 $this->Flash->error('ユーザー名かパスワードが間違っています。'); 147 } 148 } 149 } 150 151 //ログアウト処理 152 public function logout() { 153 $this->request->session()->destroy(); 154 return $this->redirect($this->Auth->logout()); 155 } 156 157 //認証を使わないページの設定 158 public function beforeFilter(Event $event) { 159 parent::beforeFilter($event); 160 $this->Auth->allow(['login', 'index', 'add']); 161 } 162 163 //認証時のロールのチェック 164 public function isAuthorized($user = null) { 165 if ($user['role'] === 'admin') { 166 return true; 167 } 168 if ($user['role'] === 'user') { 169 return false; 170 } 171 return false; 172 } 173}
php
1//login.ctp 2<div class="users form"> 3<?= $this->Flash->render('auth') ?> 4<?= $this->Form->create() ?> 5 <fieldset> 6 <legend>アカウント名とパスワードを入力してください。</legend> 7 <?= $this->Form->input('username') ?> 8 <?= $this->Form->input('password') ?> 9 </fieldset> 10 <?= $this->Form->button(__('login')); ?> 11 <?= $this->Form->end() ?> 12</div>
回答1件
あなたの回答
tips
プレビュー