HOME > WEBプログラム覚書 > [CakePHP2.0.x]Authコンポーネントの変更点
[CakePHP2.0.x]Authコンポーネントの変更点
CakePHP2系が出たので、勉強しなおしてるんですが、Authコンポーネントがいろいろ変わってましたのでメモしときます。1系もあまり勉強できてないので、CakePHPの書き方として正しいのかは不明ですが、とりあえずこれで動いたという感じです。
以前の書き方
adminルーティングを利用して、認証のモデルはManegerの場合。
app_controller.php
<?php
class AppController extends Controller {
var $components = array ( 'Auth' ) ;
function beforeFilter( )
{
// admin ルーティングの場合、認証が必要(もっとちゃんとした書き方があると思う)
if ( isset ( $this -> params [ 'prefix' ] ) && $this -> params [ 'prefix' ] === 'admin' ) {
// UsersではなくManeger
$this -> Auth -> userModel = 'Maneger' ;
// 認証に利用するフィールドの変更
$this -> Auth -> fields = array (
'username' => 'name' ,
'password' => 'passwd'
) ;
$this -> Auth -> loginError = 'パスワードが違います。' ;
$this -> Auth -> authError = '管理者用のページです。' ;
} else {
// Adminルーティング以外は全て閲覧可能
$this -> Auth -> allow ( '*' ) ;
}
}
?>
manegers_controller.php
<?php
class ManegersController extends AppController {
// ログイン処理は空
function admin_login( )
{
}
// ログアウト
function admin_logout( )
{
$this -> redirect ( $this -> Auth -> logout ( ) ) ;
}
}
?>
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
<?php
class ManegersController extends AppController {
public function admin_login( ) {
if ( $this -> request -> is ( 'post' ) ) {
if ( $this -> Auth -> login ( ) ) {
return $this -> redirect ( $this -> Auth -> redirect ( ) ) ;
} else {
$this -> Session -> setFlash ( __( 'Username or password is incorrect' ) , 'default' , array ( ) , 'auth' ) ;
}
}
}
// ログアウト
function admin_logout( )
{
$this -> redirect ( $this -> Auth -> logout ( ) ) ;
}
}
?>
ググっても出てこなかったのでたぶんlogin()を空にしてる人はいないってことでしょうねw
$this->Auth->fieldsの廃止
$this->Auth->fieldsがなくなってて、authenticateというプロパティに設定するようになりました。
CakePHP2.0.xでは認証の方法がForm、Basic、Digestというの3つを利用できるようです。
ここではフォームを使うのでFormの中でフィールドを設定します。
AppController.php
<?php
class AppController extends Controller {
var $components = array ( 'Auth' ) ;
public function beforeFilter( )
{
// adminルーティングの判定は
// $this->request->admin でできる。
if ( $this -> request -> admin ) {
$this -> Auth -> authenticate = array (
// フォーム認証を利用
'Form' => array (
// 認証に利用するモデルのフィードを変更
'fields' => array ( 'username' => 'name' ,
'password' => 'passwd' ,
) ,
) ,
) ;
}
}
}
?>
$this->Auth->userModelの廃止
こちらもauthenticateで設定するようになりました。
AppController.php
<?php
class AppController extends Controller {
var $components = array ( 'Auth' ) ;
public function beforeFilter( )
{
// adminルーティングの判定は
// $this->request->admin でできる。
if ( $this -> request -> admin ) {
$this -> Auth -> authenticate = array (
// フォーム認証を利用
'Form' => array (
// 認証に利用するモデルのフィードを変更
'fields' => array ( 'username' => 'name' ,
'password' => 'passwd' ,
) ,
) ,
// 認証に利用するモデルの変更
'userModel' => 'Maneger'
) ;
}
}
}
?>
$this->Auth->loginErrorの廃止
あんまり覚えてないんですが、ログイン失敗時に出てた気がするのでたぶん、Users::login()で設定するようになったんだと思います。
ManegersController.php
<?php
public function admin_login( ) {
if ( $this -> request -> is ( 'post' ) ) {
if ( $this -> Auth -> login ( ) ) {
return $this -> redirect ( $this -> Auth -> redirect ( ) ) ;
} else {
// loginErrorに当たる部分
$this -> Session -> setFlash ( __( 'Username or password is incorrect' ) , 'default' , array ( ) , 'auth' ) ;
}
}
}
?>
というのが、気がついた範囲での変更点となります。
また上記の書き方より、もっとかっこいい?書き方もあって$componentsでAuth利用宣言と一緒に設定することができます。
AppController.php
<?php
public $components = array (
'Auth' => array (
'loginAction' => array (
'controller' => 'users' ,
'action' => 'login' ,
'plugin' => 'users'
) ,
'authError' => 'Did you really think you are allowed to see that?' ,
'authenticate' => array (
'Form' => array (
'fields' => array ( 'username' => 'email' )
)
)
)
) ;
?>
こんな感じに。
僕の場合、標準的な使い方しかしてないのですが、結構変更されてるなと思いました。
色々やるてるかたは、一度下記ページに目を通しておくと動かなかったときにそういえば!と気が付けるかもしれませんので軽く目を通しておくのをおすすめします。
Adminルーティング時のAuthComponent
2.0.xに限った話ではありませんが、いまさらながら気がついたので。
今まで、AppControllerの$componentsで宣言して、beforeFilter()でAdminルーティング以外は閲覧可能というような分岐をしており、
全てのページでAuthComponentがインスタンス化されるので無駄だなと思ってたんですが、
constructClasses()でやればAdminルーティングの時だけAuthComponentをインスタンス化するようにできるみたいです。
AppController.php
<?php
class AppController extends Controller {
public function beforeFilter( )
{
if ( $this -> request -> admin ) {
$this -> theme = 'Admin' ;
$this -> set ( 'userdata' , $this -> Auth -> user ( ) ) ;
}
}
public function constructClasses( )
{
if ( $this -> request -> admin ) {
$this -> components [ 'Auth' ] = array (
'authenticate' => array (
'Form' => array (
'fields' => array ( 'username' => 'name' ) ,
) ,
) ,
'authError' => 'Did you really think you are allowed to see that?'
) ;
}
parent:: constructClasses ( ) ;
}
}
?>
投稿日
2012年3月21日 05:47
カテゴリ
PHP
タグ
CakePHP | ライブラリ
トラックバック URL
http://www.kantenna.com/cgi-bin/mt504/mt-tb.cgi/1289
匿名
2013年1月25日 04:18 | 返信
ありがとうございました。
ここのサイトで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さんはバージョンも書いてくださってますので助かります。