タイトル通りの質問です。cakephpのauthコンポーネントを使用してログイン機能を入れようとしています。
登録は出来るのですが、ログインすることができません。
cakephpのチュートリアルとあまり変わったことはせず、以前に練習でチュートリアル通りに試してみた時にはきちんと機能していました。
なので、今回も前回のものを元にコードを書いてみたのですが、上手く機能しません。
teratailで似た内容の質問なども確認して試してみたのですが、解決には至りませんでした。
もしかすると、saltやcipherSeedあたりが原因かもしれません。赤いnoticeが出たので変更はしました。
登録時とログイン時でsaltやcipherSeedの値が変わってしまったからとも思い、変更後に新たに登録したものでログインも試みましたが、結果は変わりませんでした。
ログイン周りのコードを以下に記載します。具体的にどこが駄目なのか説明してくださると助かります。よろしくお願いします。
php
1//controller/UsersController.php 2<?php 3App::uses('AppController', 'Controller'); 4class UsersController extends AppController { 5 6 public $components = array('Paginator', 'Flash', 'Session', 'Auth'); 7 8 public function beforeFilter() { 9 parent::beforeFilter(); 10 // ユーザー自身による登録とログアウトを許可する 11 $this->Auth->allow('add', 'logout'); 12 } 13 14 public function isAuthorized($user) { 15 if (in_array($this->action, array('view', 'edit', 'delete'))) { 16 $postId = (int) $this->request->params['pass'][0]; 17 if ($this->Auth->user('id') == $postId) { 18 return true; 19 } 20 } 21 return parent::isAuthorized($user); 22 } 23 24 public function index() { 25 $this->User->recursive = 0; 26 $this->set('users', $this->Paginator->paginate()); 27 } 28 29 public function view($id = null) { 30 if (!$this->User->exists($id)) { 31 throw new NotFoundException(__('Invalid user')); 32 } 33 $options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); 34 $this->set('user', $this->User->find('first', $options)); 35 } 36 37 public function add() { 38 if ($this->request->is('post')) { 39 $this->User->create(); 40 if ($this->User->save($this->request->data)) { 41 $this->Flash->success(__('The user has been saved.')); 42 return $this->redirect(array('controller' => 'sites', 'action' => 'index')); 43 } else { 44 $this->Flash->error(__('The user could not be saved. Please, try again.')); 45 } 46 } 47 } 48 49 public function edit($id = null) { 50 if (!$this->User->exists($id)) { 51 throw new NotFoundException(__('Invalid user')); 52 } 53 if ($this->request->is(array('post', 'put'))) { 54 if ($this->User->save($this->request->data)) { 55 $this->Flash->success(__('The user has been saved.')); 56 return $this->redirect(array('action' => 'index')); 57 } else { 58 $this->Flash->error(__('The user could not be saved. Please, try again.')); 59 } 60 } else { 61 $options = array('conditions' => array('User.' . $this->User->primaryKey => $id)); 62 $this->request->data = $this->User->find('first', $options); 63 } 64 } 65 66 public function delete($id = null) { 67 $this->User->id = $id; 68 if (!$this->User->exists()) { 69 throw new NotFoundException(__('Invalid user')); 70 } 71 $this->request->allowMethod('post', 'delete'); 72 if ($this->User->delete()) { 73 $this->Flash->success(__('The user has been deleted.')); 74 } else { 75 $this->Flash->error(__('The user could not be deleted. Please, try again.')); 76 } 77 return $this->redirect(array('action' => 'index')); 78 } 79 80 public function login() { 81 if ($this->request->is('post')) { 82 if ($this->Auth->login()) { 83 $this->redirect($this->Auth->redirect()); 84 } else { 85 $this->Flash->error(__('Invalid username or password, try again')); 86 } 87 } 88 } 89 90 public function logout() { 91 $this->redirect($this->Auth->logout()); 92 } 93} 94
php
1//Controller/AppController.php 2<?php 3 4App::uses('Controller', 'Controller'); 5 6class AppController extends Controller { 7 public $components = array( 8 'Flash', 9 'Auth' => array( 10 'loginRedirect' => array( 11 'controller' => 'sites', 12 'action' => 'index' 13 ), 14 'logoutRedirect' => array( 15 'controller' => 'sites'/*'pages'*/, 16 'action' => 'index'/*'display', 17 'home'*/ 18 ), 19 'authenticate' => array( 20 'Form' => array( 21 'passwordHasher' => 'Blowfish' 22 ) 23 ), 24 'authorize' => array('Controller') 25 ) 26 ); 27 28 public function beforeFilter() { 29 $this->Auth->allow('index', 'view', 'edit', 'search'); 30 $this->set('auth',$this->Auth);//これがないとログインログアウトの表示切替ができない 31 } 32 33 public function isAuthorized($user) { 34 return false; 35 } 36}
php
1//model/User.php 2<?php 3App::uses('AppModel', 'Model'); 4App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth'); 5 6/** 7 * User Model 8 * 9 * @property Plan $Plan 10 */ 11class User extends AppModel { 12 13/** 14 * Validation rules 15 * 16 * @var array 17 */ 18 public $validate = array( 19 'user_name' => array( 20 'notBlank' => array( 21 'rule' => array('notBlank'), 22 //'message' => 'Your custom message here', 23 //'allowEmpty' => false, 24 //'required' => false, 25 //'last' => false, // Stop validation after this rule 26 //'on' => 'create', // Limit validation to 'create' or 'update' operations 27 ), 28 ), 29 'password' => array( 30 'notBlank' => array( 31 'rule' => array('notBlank'), 32 //'message' => 'Your custom message here', 33 //'allowEmpty' => false, 34 //'required' => false, 35 //'last' => false, // Stop validation after this rule 36 //'on' => 'create', // Limit validation to 'create' or 'update' operations 37 ), 38 ), 39 'e-mail' => array( 40 'email' => array( 41 'rule' => array('email'), 42 //'message' => 'Your custom message here', 43 //'allowEmpty' => false, 44 //'required' => false, 45 //'last' => false, // Stop validation after this rule 46 //'on' => 'create', // Limit validation to 'create' or 'update' operations 47 ), 48 ), 49 ); 50 51 public function beforeSave($options = array()) { 52 if (isset($this->data[$this->alias]['password'])) { 53 $passwordHasher = new BlowfishPasswordHasher(); 54 $this->data[$this->alias]['password'] = $passwordHasher->hash( 55 $this->data[$this->alias]['password'] 56 ); 57 } 58 return true; 59 } 60 61 // The Associations below have been created with all possible keys, those that are not needed can be removed 62 63/** 64 * hasAndBelongsToMany associations 65 * 66 * @var array 67 */ 68 public $hasAndBelongsToMany = array( 69 'Plan' => array( 70 'className' => 'Plan', 71 'joinTable' => 'users_plans', 72 'foreignKey' => 'user_id', 73 'associationForeignKey' => 'plan_id', 74 'unique' => 'keepExisting', 75 'conditions' => '', 76 'fields' => '', 77 'order' => '', 78 'limit' => '', 79 'offset' => '', 80 'finderQuery' => '', 81 ) 82 ); 83 84}
php
1//view/login.ctp 2<div class="users form"> 3<?php echo $this->Flash->render('auth'); ?> 4<?php echo $this->Form->create('User'); ?> 5 <fieldset> 6 <legend> 7 <?php echo __('Please enter your username and password'); ?> 8 </legend> 9 <?php 10 echo $this->Form->input('username'); 11 echo $this->Form->input('password'); 12 ?> 13 </fieldset> 14<?php echo $this->Form->end(__('Login')); ?> 15</div>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/27 14:57