I'm attending CakeFest 2010!

5.2.5 AuthComponent のメソッド

5.2.5.1 action

action (string $action = ':controller/:action')

もし ACL を利用していて、その構造の一部として ACO を用いているなら、 ある特定の controller/action ペアに結びついた ACO ノードのパスを取得できます。

    $acoNode = $this->Auth->action('users/delete');
  1. $acoNode = $this->Auth->action('users/delete');

何も値を渡さなければ、現在の controller/action ペアが使用されます。

5.2.5.2 allow

The original text for this section has changed since it was translated. Please help resolve this difference. You can:

More information about translations

コントローラ中で認証を行わないアクション(例えば登録を行うアクション)があるのなら、 allow メソッドを使って AuthComponent がそのアクションを無視するようにできます。 次の例では、「register」アクションで認証を無視するようにしています。

決して、 allow メソッドに「login」という名前のアクションを適用しないでください。認証機能が誤作動します。

    $this->Auth->allow('register');
  1. $this->Auth->allow('register');

複数のアクションで認証をスキップするようにするなら、それらのアクション名を allow() メソッドのパラメータに渡してください。

    $this->Auth->allow('foo', 'bar', 'baz');
  1. $this->Auth->allow('foo', 'bar', 'baz');

ショートカット:コントローラ中の全てのアクションに allow を実行する場合、「*」を指定してください。

    $this->Auth->allow('*');
  1. $this->Auth->allow('*');

レイアウトやエレメントで requestAction を使う場合、 ログインページがきちんと表示されるよう、それらのアクションを allow で許可するようにしてください。

認証コンポーネントは、アクション名が規約に沿ったものであり、アンダースコアによる記法であることを前提とします。

5.2.5.3 deny

allow で認証を行わないことを許可したアクション一覧から、一部のアクションを取り除きたいこと場合が出てくるかもしれません。 これを行うには次のようにします。

    function beforeFilter() {
        $this->Auth->authorize = 'controller';
        $this->Auth->allow('delete');
    }

    function isAuthorized() {
        if ($this->Auth->user('role') != 'admin') {
            $this->Auth->deny('delete');
        }

        ...
    }
  1. function beforeFilter() {
  2. $this->Auth->authorize = 'controller';
  3. $this->Auth->allow('delete');
  4. }
  5. function isAuthorized() {
  6. if ($this->Auth->user('role') != 'admin') {
  7. $this->Auth->deny('delete');
  8. }
  9. ...
  10. }

5.2.5.4 hashPasswords

The original text for this section has changed since it was translated. Please help resolve this difference. You can:

More information about translations

hashPasswords ($data)

このメソッドは、 $data にユーザ名とパスワードが含まれるかをチェックします。ユーザ名とパスワードは、 $userModel で定義されたモデル名ものにインデックスされ、$fields で定義されたものを利用します。もし $data 配列がユーザ名とパスワードの両方を含む場合、このメソッドはパスワードのフィールドをハッシュ化し、同じフォーマットで data 配列を返します。この機能は、パスワードのフィールドが発生するとき、ユーザのモデルに対する挿入や更新を行う前に実行しておくべきです。

    $data['User']['username'] = 'me@me.com';
    $data['User']['password'] = 'changeme';
    $hashedPasswords = $this->Auth->hashPasswords($data);
    print_r($hashedPasswords);
    /* returns:
    Array
    (
        [User] => Array
        (
            [email] => me@me.com
            [password] => 8ed3b7e8ced419a679a7df93eff22fae
        )
    )

    */
  1. $data['User']['username'] = 'me@me.com';
  2. $data['User']['password'] = 'changeme';
  3. $hashedPasswords = $this->Auth->hashPasswords($data);
  4. print_r($hashedPasswords);
  5. /* returns:
  6. Array
  7. (
  8. [User] => Array
  9. (
  10. [email] => me@me.com
  11. [password] => 8ed3b7e8ced419a679a7df93eff22fae
  12. )
  13. )
  14. */

この例において、 $hashedPasswords['User']['password'] フィールドはコンポーネントの password 関数を使ってハッシュ化されます。

もしコントローラが Auth コンポーネントを利用しており、データが前述したユーザ名やパスワードといったフィールドを保持していた場合、パスワードのフィールドはこの関数を使って自動的にハッシュ化されます。

5.2.5.5 mapActions

このセクションには保留されている変更があります. More information about translations

If you are using Acl in CRUD mode, you may want to assign certain non-default actions to each part of CRUD.

$this->Auth->mapActions(
	array(
		'create' => array('someAction'),
		'read' => array('someAction', 'someAction2'),
		'update' => array('someAction'),
		'delete' => array('someAction')
	)
);
  1. $this->Auth->mapActions(
  2. array(
  3. 'create' => array('someAction'),
  4. 'read' => array('someAction', 'someAction2'),
  5. 'update' => array('someAction'),
  6. 'delete' => array('someAction')
  7. )
  8. );

5.2.5.6 login

The original text for this section has changed since it was translated. Please help resolve this difference. You can:

More information about translations

login($data = null)

Ajax ベースのログイン等を利用したいなら、 このメソッドを使い、手動で利用者をシステムにログインさせることができます。 $data に値を渡さなければ、コントローラ中の POST されたデータを自動的に使用します。

例えば、ユーザーに自動的にパスワードを割り当てて、登録後にログインするアプリケーションにしたい。以上の簡単な例では:

View:

echo $form->create('User',array('action'=>'register'));
echo $form->input('username');
echo $form->end('Register');

Controller

function register() {
    if(!empty($this->data)) {
        $this->User->create();
        $assigned_password = "password";
        $this->data['User']['password'] = $assigned_password;
        if($this->User->save($this->data)) {
            // send signup email containing password to the user
            $this->Auth->login($this->data);
            $this->redirect("home");
    }
}

一点注意すべきこととしてloginRedirect呼び出されない場合は手動でログイン後にユーザーをリダイレクトする必要があります。

$this->Auth->login($data) は、成功時に1,失敗時に0を返します。

5.2.5.7 logout

利用者を認証していない状態(ログアウトした状態)にし、どこかにリダイレクトさせるための素早い方法を提供します。 このメソッドは、アプリケーション中のメンバーだけが表示できるページ内に「ログアウト」機能を提供したい時などに便利です。

Example:

$this->redirect($this->Auth->logout());
  1. $this->redirect($this->Auth->logout());

5.2.5.8 password

password (string $password)

文字列を渡すと、ハッシュ化されたものを取得できます。 この機能は、ログイン済みのユーザに対して、アプリケーションの重要な領域にアクセスさせる前にもう一度パスワードを入力してもらう時などに重要となります。

if ($this->data['User']['password'] ==
    $this->Auth->password($this->data['User']['password2'])) {

    // Passwords match, continue processing
    ...
} else {
    $this->flash('Typed passwords did not match', 'users/register');
}
  1. if ($this->data['User']['password'] ==
  2. $this->Auth->password($this->data['User']['password2'])) {
  3. // Passwords match, continue processing
  4. ...
  5. } else {
  6. $this->flash('Typed passwords did not match', 'users/register');
  7. }

認証コンポーネントは、送信されたデータの中に「username」フィールドがある場合、自動的に「password」フィールドをハッシュ化します。

5.2.5.9 user

The original text for this section has changed since it was translated. Please help resolve this difference. You can:

More information about translations

user(string $key = null)
  1. user(string $key = null)

このメソッドは、現在認証しているユーザの情報を提供します。この情報はセッションから取得されます。例は次のとおりです。

if ($this->Auth->user('role') == 'admin') {
    $this->flash('あなたは管理者権限でアクセスしています。');
}
  1. if ($this->Auth->user('role') == 'admin') {
  2. $this->flash('あなたは管理者権限でアクセスしています。');
  3. }

このメソッドは、ユーザのセッションデータを全て取得するためにも使えます。

$data['User'] = $this->Auth->user();
  1. $data['User'] = $this->Auth->user();

ユーザーがログインしていない場合、このメソッドは null を返します。