私の知る限り、これを行う唯一の方法は、ゲスト ユーザーを作成することです。これは、isAuthorized()
あなたが説明したようになる前に、Auth コンポーネントがユーザーの存在をチェックするためです。
これは、セッションに直接書き込むことで実行できます。これは、誰かがログインしていることを Auth コンポーネントに伝えるため、isAuthorized()
メソッドが呼び出されます。
AppController
public function beforeFilter() {
// if no one is logged in, log in a guest
if (!$this->Auth->user()) {
$this->Session->write(AuthComponent::$sessionKey, array(
'User' => array(
'id' => 0
)
));
}
}
public function isAuthorized($user) {
$authorized = false;
if ($this->Auth->user('id') == 0) {
// public guest user access
}
// other logic
return $authorized;
}
これを行うためのおそらくより良い方法は、カスタム認証オブジェクトを使用することです。これは基本的に、Cake にそのクラスを使用して認証を支援するように指示します。これにより、ロジックが別のクラスに分割され、テストが容易になり、無効にすることさえできます。
アプリ/コントローラー/コンポーネント/認証/GuestAuthenticate.php
App::uses('BaseAuthenticate', 'Controller/Component/Auth');
class GuestAuthenticate extends BaseAuthenticate {
public function authenticate(CakeRequest $request, CakeResponse $response) {
// no real authentication logic, just return a guest user
return array('User' => array('id' => 0));
}
}
AppController
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form',
'Guest' // tell Cake to try Form authentication then Guest authentication
)
)
);
public function beforeFilter() {
if (!$this->Auth->user()) {
// no user? log in a guest (this will fail form authentication
// then try guest authentication)
$this->Auth->login();
}
}
public function isAuthorized($user) {
$authorized = false;
if ($this->Auth->user('id') == 0) {
// public guest user access
}
// other logic
return $authorized;
}
カスタム認証オブジェクトの詳細については、http: //book.cakephp.org/2.0/en/core-libraries/components/authentication.htmlを参照してください。