独自の Zend_Auth_Adapter を作成する必要があります。このアダプターは、3 つのリソースに対して認証を試み、プライベート メンバー変数でフラグを立てるため、どのログイン試行が正常に認証されたかを知ることができます。
認証アダプタを作成するには、Zend_Auth_Adapter_DbTable をベースにします。
したがって、__construct では、DbTable アダプターを 1 つだけ渡す代わりに、各リソースで使用される 3 つのアダプターを渡すことができます。たとえば LDAP や別のデータベースなど、それぞれが異なるリソースを使用する場合にのみ、そのようにします。そうでない場合は、1 つのアダプターのみを渡し、構成オプションで 3 つの異なるテーブル名を設定できます。
Zend_Auth_Adapter_DbTable の例を次に示します。
/**
* __construct() - Sets configuration options
*
* @param Zend_Db_Adapter_Abstract $zendDb
* @param string $tableName
* @param string $identityColumn
* @param string $credentialColumn
* @param string $credentialTreatment
* @return void
*/
public function __construct(Zend_Db_Adapter_Abstract $zendDb, $tableName = null, $identityColumn = null,
$credentialColumn = null, $credentialTreatment = null)
{
$this->_zendDb = $zendDb;
// Here you can set three table names instead of one
if (null !== $tableName) {
$this->setTableName($tableName);
}
if (null !== $identityColumn) {
$this->setIdentityColumn($identityColumn);
}
if (null !== $credentialColumn) {
$this->setCredentialColumn($credentialColumn);
}
if (null !== $credentialTreatment) {
$this->setCredentialTreatment($credentialTreatment);
}
}
Zend_Auth_Adapter_DbTable の次のメソッドは、1 つのテーブルに対して認証を試行します。3 つのテーブルで試行するように変更できます。それぞれのテーブルで成功した場合、これをプライベート メンバー変数のフラグとして設定します。$result['group1'] = 1; のようなもの。ログイン試行が成功するたびに 1 を設定します。
/**
* authenticate() - defined by Zend_Auth_Adapter_Interface. This method is called to
* attempt an authentication. Previous to this call, this adapter would have already
* been configured with all necessary information to successfully connect to a database
* table and attempt to find a record matching the provided identity.
*
* @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible
* @return Zend_Auth_Result
*/
public function authenticate()
{
$this->_authenticateSetup();
$dbSelect = $this->_authenticateCreateSelect();
$resultIdentities = $this->_authenticateQuerySelect($dbSelect);
if ( ($authResult = $this->_authenticateValidateResultset($resultIdentities)) instanceof Zend_Auth_Result) {
return $authResult;
}
$authResult = $this->_authenticateValidateResult(array_shift($resultIdentities));
return $authResult;
}
3 回のログイン試行のいずれかが正常に認証された場合にのみ、有効な $authresult が返されます。
ここで、コントローラーで、ログインを試みた後:
public function loginAction()
{
$form = new Admin_Form_Login();
if($this->getRequest()->isPost())
{
$formData = $this->_request->getPost();
if($form->isValid($formData))
{
$authAdapter = $this->getAuthAdapter();
$authAdapter->setIdentity($form->getValue('user'))
->setCredential($form->getValue('password'));
$result = $authAdapter->authenticate();
if($result->isValid())
{
$identity = $authAdapter->getResult();
Zend_Auth::getInstance()->getStorage()->write($identity);
// redirect here
}
}
}
$this->view->form = $form;
}
private function getAuthAdapter()
{
$authAdapter = new MyAuthAdapter(Zend_Db_Table::getDefaultAdapter());
// Here the three tables
$authAdapter->setTableName(array('users','users2','users3'))
->setIdentityColumn('user')
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)');
return $authAdapter;
}
ここで重要なのは、カスタム認証アダプターに実装される次の行です。
$identity = $authAdapter->getResult();
このフォーム Zend_Auth_Adapter_DbTable をベースとして使用できます。
/**
* getResultRowObject() - Returns the result row as a stdClass object
*
* @param string|array $returnColumns
* @param string|array $omitColumns
* @return stdClass|boolean
*/
public function getResultRowObject($returnColumns = null, $omitColumns = null)
{
// ...
}
認証に成功すると、ログイン試行で一致した行が返されます。したがって、この行と $this->result['groupX'] フラグを返すことができる getResult() メソッドを作成します。何かのようなもの:
public function authenticate()
{
// Perform the query for table 1 here and if ok:
$this->result = $row->toArrray(); // Here you can get the table result of just one table or even merge all in one array if necessary
$this->result['group1'] = 1;
// and so on...
$this->result['group2'] = 1;
// ...
$this->result['group3'] = 1;
// Else you will set all to 0 and return a fail result
}
public function getResult()
{
return $this->result;
}
結局、Zend_Acl を使用して、ビューやその他のアクションを制御できます。Zend Auth Storage にフラグがあるため、以下をロールとして使用できます。
$this->addRole(new Zend_Acl_Role($row['group1']));
ここにいくつかのリソースがあります:
http://framework.zend.com/manual/en/zend.auth.introduction.html
http://zendguru.wordpress.com/2008/11/06/zend-framework-auth-with-examples/
http://alex-tech-adventures.com/development/zend-framework/61-zendauth-and-zendform.html
http://alex-tech-adventures.com/development/zend-framework/62-allocation-resources-and-permissions-with-zendacl.html
http://alex-tech-adventures.com/development/zend-framework/68-zendregistry-and-authentication-improvement.html