春咲さんのメモ。

自分メモ的な。主にPHPについて。github:mindlessdoll(冬眠してるけど)

Cakephpの認証時に独自テーブルを使ってはまりました。

えぇ、はまりましたよ。

Auth認証。

本家ではUserテーブルを使ってますよね。

認証 — CakePHP Cookbook 2.x ドキュメント

今回はUserテーブル使いません。usernameなんてフィールドもありません。

認証に使うテーブル:ManagementAccount

各フィールド名

 username→login_id

 password→そのまま

 

さて。

まずModel

<?php

App::uses('AppModel', 'Model');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
App::uses('AuthComponent', 'Controller/Component');

class ManagementAccount extends AppModel {
    public $name = 'ManagementAccount';
    public $useTable = 'm_management_account';

    public $validate = array(
        'login_id' => array(
            'required' => array(
                'rule' => 'notBlank',
                'message' => 'A username is required'
            )
        ),
        'password' => array(
            'required' => array(
                'rule' => 'notBlank',
                'message' => 'A password is required'
            )
        ),
    );
    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;
    }
}
    

ここでポイントになるのが「AuthComponent」こいつを使う宣言をすること

次にController

App::uses('AppController', 'Controller');

class AdminLoginController extends AppController {

    public $components = array('Auth');
    // レイアウト指定
    public $layout = 'adminLoginLayout';

    public function beforeFilter(){
        $this->autoLayout = false;
        parent::beforeFilter();
        $this->Auth->autoRedirect = false;
        $this->Auth->allow('index', 'logout');

        $this->Auth->loginAction    = '/admin/login/';
        $this->Auth->loginRedirect  = '/admin/dashboard';
        $this->Auth->logoutRedirect = '/admin/';

        $this->Auth->authorize = 'Controller';
        // フォームの認証設定
        $this->Auth->authenticate = array(

            // フォーム認証を利用
            'Form' => array(
                // 認証に利用するモデルの変更
                'userModel' => 'ManagementAccount', //HogeUserモデルを指定
                // 認証に利用するモデルのフィードを変更
                'fields' => array('username' => 'login_id', 'password' => 'password')
            )
        );
    }


    public function index() {
        $this->set('title_for_layout', '管理画面');
    }

    public function login() {
        if ($this->request->is('post')) {
            if ($this->Auth->login()) {
                $this->redirect('/admin/dashboard');
            } else {
                //$this->Session->setFlash(__('Invalid username or password, try again'));
                $this->redirect('/admin/');
            }
        } else {
            $this->redirect('/admin/');
        }
    }

    public function dashboard() {

    }

    public function logout() {
        $this->redirect($this->Auth->logout());
    }
}

ここでのポイントは「$this->Auth->authenticate 」の設定

次にView

<?php echo $this->Form->create('ManagementAccount', array('url' => '/admin/login')); ?>
<table>
<tbody>
<tr>
<th>
<img src="/img/admin/icon_user.gif" width="16" height="14" alt="ユーザーID">
</th>
<td>
<input name="data[ManagementAccount][login_id]" type="text" id="ManagementAccountUsername"/>
</td>
</tr>
<tr>
<th>
<img src="/img/admin/icon_pass.gif" width="16" height="17" alt="パスワード">
</th>
<td>
<?php echo $this->Form->input('password', array('label' => false)); ?>
</td>
</tr>
</tbody>
</table>
<p class="login">
</p>
<?php echo $this->Form->end('ログイン'); ?>

login_idだけ、Formヘルパーで生成するとセレクトボックスになってしまうので手書きです

これだけなんだけどね!

あとあと、重要なんだけど、パスワードはvar_dumpとかでAuthComponent::password使ってハッシュ化したものをテーブルに登録しておかないと通らないよ!