質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

Q&A

解決済

1回答

4710閲覧

auth認証で登録は出来るがログインできない

tacchan

総合スコア60

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

0グッド

0クリップ

投稿2016/05/24 14:53

タイトル通りの質問です。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>

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

AppController内の $componentsの定義で、PasswordHasherにBlowfishを使用するように定義しているのですが、これが上手く働かないようです
そのため、DB上はBlowfishでハッシュ化されたパスワードが登録されるのですが、ログイン時のパスワードチェックでは初期値(Simple)のハッシュアルゴリズムが使用されてしまうようです
ですので、以下のようにbeforeFilter()内で再度定義してやることで、ログイン処理時のPasswordHasherをBlowfishに設定することができます

CakePHP

1// AppController.php 2 3 public function beforeFilter() { 4 $this->Auth->allow('index', 'view', 'edit', 'search'); 5 $this->Auth->authenticate = array( 6 'Form' => array('userModel' => 'User','passwordHasher' => 'Blowfish') 7 ); 8 $this->set('auth',$this->Auth);//これがないとログインログアウトの表示切替ができない 9 }

一応自分の環境(CakePHP2.7)でログインが正常に行えることを確認しています

投稿2016/05/27 04:02

KatsumiTanaka

総合スコア924

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tacchan

2016/05/27 14:57

非常にわかりやすく簡潔な説明ありがとうございます。 結構質問してから時間が経ってしまったので、あきらめかけてました。 自分の環境でも、ちゃんとログインすることができました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問