HOME>WEBプログラム覚書>[CakePHP2.0.x]Authコンポーネントの変更点

[CakePHP2.0.x]Authコンポーネントの変更点

CakePHP2系が出たので、勉強しなおしてるんですが、Authコンポーネントがいろいろ変わってましたのでメモしときます。1系もあまり勉強できてないので、CakePHPの書き方として正しいのかは不明ですが、とりあえずこれで動いたという感じです。

以前の書き方

adminルーティングを利用して、認証のモデルはManegerの場合。

app_controller.php

  1. <?php
  2. class AppController extends Controller {
  3.  
  4.     var $components = array('Auth');
  5.  
  6.     function beforeFilter()
  7.     {
  8.         // admin ルーティングの場合、認証が必要(もっとちゃんとした書き方があると思う)
  9.         if (isset($this->params['prefix']) && $this->params['prefix'] === 'admin') {
  10.             // UsersではなくManeger
  11.             $this->Auth->userModel = 'Maneger';
  12.  
  13.             // 認証に利用するフィールドの変更
  14.             $this->Auth->fields = array(
  15.                 'username' => 'name',
  16.                 'password' => 'passwd'
  17.             );
  18.             $this->Auth->loginError = 'パスワードが違います。';
  19.             $this->Auth->authError = '管理者用のページです。';
  20.         } else {
  21.             // Adminルーティング以外は全て閲覧可能
  22.             $this->Auth->allow('*');
  23.         }
  24. }
  25. ?>

manegers_controller.php

  1. <?php
  2. class ManegersController extends AppController {
  3.  
  4.     // ログイン処理は空
  5.     function admin_login()
  6.     {
  7.     }
  8.  
  9.     // ログアウト
  10.     function admin_logout()
  11.     {
  12.         $this->redirect($this->Auth->logout());
  13.     }
  14. }
  15. ?>

AppControllerの$componentsで宣言して、beforeFilter()でAdminルーティング以外は閲覧可能というような分岐をしています。 login()メソッドは空です。

2.0.Xでの書き方

login()メソッドで$this->Auth->login()を明示的にコールする必要がある。

CakePHP1系では$this->Auth->login()が自動で呼ばれるので、マニュアルなどlogin()メソッドが空で書かれていますが 2.0.xでは$this->Auth->login()を手動でコールしなければなりません。

In the past AuthComponent auto-magically logged users in. This was confusing for many people, and made using AuthComponent a bit difficult at times. For 2.0, you'll need to manually call $this->Auth->login() to log a user in.

Authentication -- Cookbook v2.x documentation

なので2.0.xでの最も簡潔なlogin()は下記のようになります。

ManegersController.php

  1. <?php
  2. class ManegersController extends AppController {
  3.  
  4.     public function admin_login() {
  5.         if ($this->request->is('post')) {
  6.             if ($this->Auth->login()) {
  7.                 return $this->redirect($this->Auth->redirect());
  8.             } else {
  9.                 $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
  10.             }
  11.         }
  12.     }
  13.  
  14.     // ログアウト
  15.     function admin_logout()
  16.     {
  17.         $this->redirect($this->Auth->logout());
  18.     }
  19. }
  20. ?>

ググっても出てこなかったのでたぶんlogin()を空にしてる人はいないってことでしょうねw

$this->Auth->fieldsの廃止

$this->Auth->fieldsがなくなってて、authenticateというプロパティに設定するようになりました。

CakePHP2.0.xでは認証の方法がForm、Basic、Digestというの3つを利用できるようです。 ここではフォームを使うのでFormの中でフィールドを設定します。

AppController.php

  1. <?php
  2. class AppController extends Controller {
  3.  
  4.     var $components = array('Auth');
  5.  
  6.     public function beforeFilter()
  7.     {
  8.         // adminルーティングの判定は
  9.         // $this->request->admin でできる。
  10.         if ($this->request->admin) {
  11.             $this->Auth->authenticate = array(
  12.                 // フォーム認証を利用
  13.                 'Form' => array(
  14.                     // 認証に利用するモデルのフィードを変更
  15.                     'fields' => array('username' => 'name',
  16.                                       'password' => 'passwd',
  17.                                       ),
  18.                      ),
  19.             );
  20.         }
  21.     }
  22. }
  23. ?>

$this->Auth->userModelの廃止

こちらもauthenticateで設定するようになりました。

AppController.php

  1. <?php
  2. class AppController extends Controller {
  3.  
  4.     var $components = array('Auth');
  5.  
  6.     public function beforeFilter()
  7.     {
  8.         // adminルーティングの判定は
  9.         // $this->request->admin でできる。
  10.         if ($this->request->admin) {
  11.             $this->Auth->authenticate = array(
  12.                 // フォーム認証を利用
  13.                 'Form' => array(
  14.                      // 認証に利用するモデルのフィードを変更
  15.                     'fields' => array('username' => 'name',
  16.                                       'password' => 'passwd',
  17.                                       ),
  18.                      ),
  19.                     // 認証に利用するモデルの変更
  20.                     'userModel' => 'Maneger'
  21.             );
  22.         }
  23.     }
  24. }
  25. ?>

$this->Auth->loginErrorの廃止

あんまり覚えてないんですが、ログイン失敗時に出てた気がするのでたぶん、Users::login()で設定するようになったんだと思います。

ManegersController.php

  1. <?php
  2. public function admin_login() {
  3.     if ($this->request->is('post')) {
  4.         if ($this->Auth->login()) {
  5.             return $this->redirect($this->Auth->redirect());
  6.         } else {
  7.             // loginErrorに当たる部分
  8.             $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
  9.         }
  10.     }
  11. }
  12. ?>

というのが、気がついた範囲での変更点となります。 また上記の書き方より、もっとかっこいい?書き方もあって$componentsでAuth利用宣言と一緒に設定することができます。

AppController.php

  1. <?php
  2. public $components = array(
  3.     'Auth' => array(
  4.         'loginAction' => array(
  5.             'controller' => 'users',
  6.             'action'     => 'login',
  7.             'plugin'     => 'users'
  8.         ),
  9.         'authError' => 'Did you really think you are allowed to see that?',
  10.         'authenticate' => array(
  11.             'Form' => array(
  12.                 'fields' => array('username' => 'email')
  13.             )
  14.         )
  15.     )
  16. );
  17. ?>

こんな感じに。

僕の場合、標準的な使い方しかしてないのですが、結構変更されてるなと思いました。 色々やるてるかたは、一度下記ページに目を通しておくと動かなかったときにそういえば!と気が付けるかもしれませんので軽く目を通しておくのをおすすめします。

Adminルーティング時のAuthComponent

2.0.xに限った話ではありませんが、いまさらながら気がついたので。

今まで、AppControllerの$componentsで宣言して、beforeFilter()でAdminルーティング以外は閲覧可能というような分岐をしており、 全てのページでAuthComponentがインスタンス化されるので無駄だなと思ってたんですが、 constructClasses()でやればAdminルーティングの時だけAuthComponentをインスタンス化するようにできるみたいです。

AppController.php

  1. <?php
  2. class AppController extends Controller {
  3.  
  4.     public function beforeFilter()
  5.     {
  6.         if ($this->request->admin) {
  7.             $this->theme = 'Admin';
  8.             $this->set('userdata', $this->Auth->user());
  9.         }
  10.     }
  11.  
  12.     public function constructClasses()
  13.     {
  14.         if ($this->request->admin) {
  15.             $this->components['Auth'] = array(
  16.                 'authenticate' => array(
  17.                     'Form' => array(
  18.                         'fields' => array('username' => 'name'),
  19.                     ),
  20.                 ),
  21.                 'authError' => 'Did you really think you are allowed to see that?'
  22.             );
  23.         }
  24.         parent::constructClasses();
  25.     }
  26. }
  27. ?>
投稿日 2012年3月21日 05:47
カテゴリ PHP
タグ CakePHP | ライブラリ
トラックバック URL http://www.kantenna.com/cgi-bin/mt504/mt-tb.cgi/1289

コメント

ありがとうございました。
ここのサイトでlogin()を思いっきり空にしていてハマってましたw
http://yasigani-ni.com/cakephp/cakephp%E3%80%80auth%E3%82%B3%E3%83%B3%E3%83%9D%E3%83%BC%E3%83%8D%E3%83%B3%E3%83%88%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6%E3%81%BF%E3%81%9F/

kantennaさんはバージョンも書いてくださってますので助かります。

コメントする
Name
Email Address
URL