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

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

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

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

CakePHP

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

Q&A

解決済

1回答

858閲覧

cakephp ログインがうまくいかない

tunnel

総合スコア30

PHP

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

CakePHP

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

0グッド

0クリップ

投稿2019/07/29 08:17

編集2019/08/08 09:09

ログインがうまくいかないので色々見ていたらハッシュ化ができていないことに気づきました。
しかし命名規則も守れているようだし原因がわかりません。お力添えをお願いします。

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>

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

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

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

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

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

yukikp

2019/07/29 08:28

各処理段階で、var_dump()とdie()でログイン関連の変数の中身を吐き出させて確認するとよいですよ? とりあえず、Authクラスのidentifyメソッドの中で、ログインに使ったパスワードと、データベースから撮ってきたパスワードを比較してるはずなので、その直前で両者を吐き出してdie()で処理を止めて、その中身を見て見ましょう!
tunnel

2019/07/29 09:21

直後で吐き出したらやはりbool(false)となりましたが直前での吐き出し方がわからないです、、どのようにすればよいでしょうか?よろしくお願いします。
yukikp

2019/07/29 09:30

【Authクラスが書かれているPHPファイルを探して、その中にあるidentify()関数の中で】ログインに使ったパスワードと、データベースから撮ってきたパスワードを比較してるはずなので、その直前で両者を吐き出してdie()で処理を止めて、その中身を見て見ましょう! という意味ですよ~。 なのでAuthクラスが書かれているファイルを探しましょ!
tunnel

2019/07/29 09:34

わかりました!ありがとうございます
tunnel

2019/08/02 06:59

Authクラスのidentify()はどこにありますか?Authフォルダは見つかったのですがクラスとメソッドが検索かけても出てこないです。
tunnel

2019/08/02 07:02

あ、見つかりました。
yukikp

2019/08/02 09:23

おお!着々と進んでますね。頑張って!
tunnel

2019/08/02 09:46

identify()の中のメソッドの挙動がわからず苦戦しております、、 メソッドの宣言をしている場所を効率よく探し出す方法ありましたらご教授ください<(_ _)>
yukikp

2019/08/02 09:59

今見たところ、CakePHP 3.8.1では、 src\Controller\Component\AuthComponent.php の816行目にありました。
yukikp

2019/08/02 10:30

どこにつながるか(ちょっと責任感じて)チラチラ見てたら、 最後は、 src\Auth\BaseAuthenticate.phpの112行目の protected function _findUser($username, $password = null) の中に、 $hasher->check($password, $hashedPassword) と言うのがあって、 ここで、 $password(送信されてきた平文のパスワード)$hashedPassword(送信されてきたusernameを使ってデータベースからとってきた暗号化されたパスワード)を比較してます。 で、この$hasher->checkメソッドは、長々と辿ると、 src\Auth\DefaultPasswordHasher.phpの、63行目にあり、中身は、 password_verify($password, $hashedPassword);でした。 つまりは、単にpassowrd_verify関数でチェックしてるだけでしたね。 ということは、すみません。 src\Auth\BaseAuthenticate.phpの112行目の protected function _findUser($username, $password = null)関数の中で、$hasher->check($password, $hashedPassword)の前に、 //ここから echo '平文パスワード:' . $password; echo '暗号化したあなたのパスワード:' . password_hash($password, PASSWORD_DEFAULT, [] ); echo 'データベース内のパスワード:' . $hashedPassword; die(); //ここまで追加 $hasher->check($password, $hashedPassword); とかけば、あなたの入れたパスワードとデータベースに保存されているパスワードが比較できるという感じです。
tunnel

2019/08/02 14:10

詳細にありがとうございます。地道に探すしか無いんですね、、平文パスとデータベースパスは一致していました。なぜログインできないんでしょう.....。
guest

回答1

0

自己解決

何故か解決しました。

投稿2019/08/08 12:14

tunnel

総合スコア30

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問