脳みそスワップアウト

揮発性なもので。おもにPHPのこととか。

Cake2 で一般ユーザと管理ユーザのテーブルが違う場合

CakePHP2 で、一般ユーザと管理ユーザのDBテーブルが違う場合

<?php
class AppController extends Controller {
  public $components = [
    'Auth' => [
      'authenticate' => [
        'Form' => ['passwordHasher' => ['className'=>'AppName']]
      ]
    ]
  ];

  public function beforeFilter() {
    parent::beforeFilter();

    if($this->params['prefix'] === null && $this->params['plugin'] === null) {
      // 一般ユーザは User モデルを使い、かつステータスが登録済みのもの
      $this->Auth->authenticate['Form']['userModel'] = 'User';
      $this->Auth->authenticate['Form']['scope'] = ['User.status'=>Def\User::STATUS_REGISTERD];

      $this->Auth->loginAction = '/';
      $this->Auth->loginRedirect = null;
    } else if($this->params['prefix'] === 'admin' && $this->params['plugin'] === null) {
      // 管理ユーザは AdminUser モデルを使う
      $this->Auth->authenticate['Form']['userModel'] = 'AdminUser';

      $this->Auth->loginAction = '/admin/login';
      $this->Auth->loginRedirect = '/admin';
      $this->Auth->authError = 'Admin only';
      AuthComponent::$sessionKey = 'Auth.AdminUser';

      $this->layout = 'admin';
    } else {
      // plugin(DebugKit)
      $this->Components->unload('Auth');
    }
  }
}