今年7月に Piece_Unity と付随するコンポーネント群が Stable 版として一斉にリリースされていた中、唯一このコンポーネントのみが Beta 版のまま残り続けていた状態でした。実はこの数ヶ月間の間に Stable 版として実用に耐えうるよう議論、実装、試用を続けてきたのでした。
リリースノートにも記載しているとおり、この Stable 版は過去バージョンの内容と一切の互換性はありません。したがって過去のバージョンからの移行は設定ポイントの修正が必要となりますので注意してください。
そしてもう1つ注意点として、次に紹介する Stable 版の機能を確認してしまうと、居ても立っても居られずに Stable 版の仕様に修正したくなってしまうかもしれません。:)
Piece_Unity_Component_Authentication は、以下の2つのポイントがあります。
- インターセプタープラグイン
- クライアント向けインターフェース
(1) インターセプタープラグイン
ベータ版当時より存在していたインターセプタープラグインですが、設定ポイントの内容が変更になっています。
具体的には、設定ポイントの階層が1つ上にあがっています。
以前は「services」設定ポイント以下に認証対象を指定する「resources」や認証フローへのアドレス「url」などを連想配列で指定していましたが、servicesを排除し「resources」「url」といった設定ポイントへ繰り上がることになりました。
また後述するクライアント向けインターフェースの登場により、認証確認のためのクラス・メソッドが必要でなくなりました。
認証機構が含まれるサイトを構築する際における、普段の認証向け設定ポイントのエントリは以下のようにすっきりしたものになります。
この例では、「/members/」というパスが含まれるURLにアクセスした場合に認証是非の判定が行われ、認証済み状態ならばそのまま実行、非認証状態ならば「https://<任意のドメイン>/login.php」へ自動転送される動作となります。
- name: Interceptor_Authentication
point:
- name: resourcesMatch
type: configuration
value:
- "/members/.*"
- name: url
type: configuration
value: https://example.com/login.php(2) クライアント向けインターフェース
Piece_Unity 利用時における、認証に関する情報の保持・取得を行うためインターフェースとして、「Piece_Unity_Service_Authentication」という専用のクラスが新たに準備されました。
このクラスは、任意の Piece_Unity のフロー (例えば Authentication フロー) 内、つまりフローアクション内で利用します。
たとえばユーザに対して認証済みの状態にする場合、以下のように Service_Authentication の login() メソッドを実行します。
$authentication = &new Piece_Unity_Service_Authentication(); $authentication->login();
ログアウト時、すなわち非認証状態にする場合は、logout() メソッドを実行します。
$authentication = &new Piece_Unity_Service_Authentication(); $authentication->logout();
たったこれだけのコードで Interceptor_Authentication における認証是非の判定が得られます。
過去のバージョンを利用した認証機構サンプルコードでは、非認証状態時に元々アクセスしようとしていたURLへ認証後自動転送されるという処理コードを紹介していましたが、この機能も removeCallback() メソッド1つですべて実現できるようになりました。
恐らく認証後に転送されるという利用法になるでしょうから、以下のコードのように login() → removeCallback() と連続で実行するようにします。
$authentication = &new Piece_Unity_Service_Authentication(); $authentication->login(); $authentication->removeCallback();
(3) 認証フローサンプル
最後に、簡易的な認証フローを実現するためのサンプルを軽く紹介しておきます。これだけで認証状態のON/OFF切り替えと、元URLへの自動転送が実現できます。以前のものと比べて「かなり」スマートな内容になっているかと思います。(以前のものはもう書かない。)
* Authentication.yaml (フロー定義)
firstState: DisplayForm
viewState:
- name: DisplayForm
view: Form
transition:
- event: authenticate
nextState: ProcessAuthentication
- name: DisplayMenu
view: Menu
transition:
- event: logout
nextState: DisplayForm
action:
method: logout
actionState:
- name: ProcessAuthentication
activity:
method: authenticate
transition:
- event: goDisplayForm
nextState: DisplayForm
- event: goDisplayMenu
nextState: DisplayMenu* AuthenticationAction.php (フローアクション)
<?php
require_once 'Piece/Unity/Service/FlowAction.php';
require_once 'Piece/Unity/Service/Authentication.php';
class AuthenticationAction extends Piece_Unity_Service_FlowAction
{
function authenticate()
{
$this->_user = &new StdClass();
$validation = &$this->_context->getValidation();
if (!$validation->validate('Authentication', $this->_user)) {
return 'goDisplayForm';
}
$authentication = &new Piece_Unity_Service_Authentication();
$authentication->login();
$authentication->redirectToCallbackURL();
return 'goDisplayMenu';
}
function logout()
{
$authentication = &new Piece_Unity_Service_Authentication();
$authentication->logout();
}
function checkPassword($password, $context, $results)
{
$username = $results->getFieldValue('username');
if ( <$username, $password を使って認証是非を確認> ) {
return true;
} else {
return false;
}
}
}Authentication.yaml (バリデーションファイル)
- name: username
required:
message: ユーザ名を入力ください
filter: &AlphaNumeric
- trim
- JapaneseAlphaNumeric
- name: password
required:
message: パスワードを入力ください
filter: *AlphaNumeric
validator:
- name: WithMethod
rule:
class: AuthenticationAction
method: checkPassword
message: 認証に失敗しました