auth認証で登録は出来るがログインできない
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 3,077
タイトル通りの質問です。cakephpのauthコンポーネントを使用してログイン機能を入れようとしています。
登録は出来るのですが、ログインすることができません。
cakephpのチュートリアルとあまり変わったことはせず、以前に練習でチュートリアル通りに試してみた時にはきちんと機能していました。
なので、今回も前回のものを元にコードを書いてみたのですが、上手く機能しません。
teratailで似た内容の質問なども確認して試してみたのですが、解決には至りませんでした。
もしかすると、saltやcipherSeedあたりが原因かもしれません。赤いnoticeが出たので変更はしました。
登録時とログイン時でsaltやcipherSeedの値が変わってしまったからとも思い、変更後に新たに登録したものでログインも試みましたが、結果は変わりませんでした。
ログイン周りのコードを以下に記載します。具体的にどこが駄目なのか説明してくださると助かります。よろしくお願いします。
//controller/UsersController.php
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public $components = array('Paginator', 'Flash', 'Session', 'Auth');
public function beforeFilter() {
parent::beforeFilter();
// ユーザー自身による登録とログアウトを許可する
$this->Auth->allow('add', 'logout');
}
public function isAuthorized($user) {
if (in_array($this->action, array('view', 'edit', 'delete'))) {
$postId = (int) $this->request->params['pass'][0];
if ($this->Auth->user('id') == $postId) {
return true;
}
}
return parent::isAuthorized($user);
}
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->Paginator->paginate());
}
public function view($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(array('controller' => 'sites', 'action' => 'index'));
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
}
}
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->User->save($this->request->data)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
public function delete($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->request->allowMethod('post', 'delete');
if ($this->User->delete()) {
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Flash->error(__('Invalid username or password, try again'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
}
//Controller/AppController.php
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Flash',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'sites',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'sites'/*'pages'*/,
'action' => 'index'/*'display',
'home'*/
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
),
'authorize' => array('Controller')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view', 'edit', 'search');
$this->set('auth',$this->Auth);//これがないとログインログアウトの表示切替ができない
}
public function isAuthorized($user) {
return false;
}
}
//model/User.php
<?php
App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
/**
* User Model
*
* @property Plan $Plan
*/
class User extends AppModel {
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'user_name' => array(
'notBlank' => array(
'rule' => array('notBlank'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'password' => array(
'notBlank' => array(
'rule' => array('notBlank'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'e-mail' => array(
'email' => array(
'rule' => array('email'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash(
$this->data[$this->alias]['password']
);
}
return true;
}
// The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Plan' => array(
'className' => 'Plan',
'joinTable' => 'users_plans',
'foreignKey' => 'user_id',
'associationForeignKey' => 'plan_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
}
//view/login.ctp
<div class="users form">
<?php echo $this->Flash->render('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend>
<?php echo __('Please enter your username and password'); ?>
</legend>
<?php
echo $this->Form->input('username');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
AppController内の $componentsの定義で、PasswordHasherにBlowfishを使用するように定義しているのですが、これが上手く働かないようです
そのため、DB上はBlowfishでハッシュ化されたパスワードが登録されるのですが、ログイン時のパスワードチェックでは初期値(Simple)のハッシュアルゴリズムが使用されてしまうようです
ですので、以下のようにbeforeFilter()内で再度定義してやることで、ログイン処理時のPasswordHasherをBlowfishに設定することができます
// AppController.php
public function beforeFilter() {
$this->Auth->allow('index', 'view', 'edit', 'search');
$this->Auth->authenticate = array(
'Form' => array('userModel' => 'User','passwordHasher' => 'Blowfish')
);
$this->set('auth',$this->Auth);//これがないとログインログアウトの表示切替ができない
}
一応自分の環境(CakePHP2.7)でログインが正常に行えることを確認しています
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.33%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2016/05/27 23:57
結構質問してから時間が経ってしまったので、あきらめかけてました。
自分の環境でも、ちゃんとログインすることができました。
ありがとうございました。