ユーザーがロールに基づいてコントローラー/ビューにアクセスする権限を持っているかどうかを決定するコントローラープラグインを作成できます。何かのようなもの:
class My_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$auth = Zend_Registry::getInstance()->get('auth');
$acl = new Zend_Acl();
// for default module
if ($request->getModuleName() == 'default') {
// access resources (controllers)
// usually there will be more access resources
$acl->add(new Zend_Acl_Resource('index'));
$acl->add(new Zend_Acl_Resource('error'));
// access roles
$acl->addRole(new Zend_Acl_Role('guest'));
$acl->addRole(new Zend_Acl_Role('user'));
$acl->addRole(new Zend_Acl_Role('administrator'));
// access rules
$acl->allow('guest'); // allow guests everywhere
$acl->allow('user'); // allow users everywhere
$acl->allow('administrator'); // allow administrators everywhere
$role = ($auth->getIdentity() && $auth->getIdentity()->status = 'approved')
? $auth->getIdentity()->role : 'guest';
$controller = $request->getControllerName();
$action = $request->getActionName();
if (!$acl->isAllowed($role, $controller, $action)) {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoUrlAndExit('error/denied');
}
}
// for member module
else if ($request->getModuleName() == 'member') {
// access resources (controllers)
// usually there will be more access resources
$acl->add(new Zend_Acl_Resource('index'));
$acl->add(new Zend_Acl_Resource('error'));
// access roles
$acl->addRole(new Zend_Acl_Role('guest'));
$acl->addRole(new Zend_Acl_Role('user'));
$acl->addRole(new Zend_Acl_Role('administrator'));
// access rules
$acl->allow('user'); // allow users everywhere
$acl->allow('administrator'); // allow administrators everywhere
$role = ($auth->getIdentity() && $auth->getIdentity()->status = 'approved')
? $auth->getIdentity()->role : 'guest';
$controller = $request->getControllerName();
$action = $request->getActionName();
if (!$acl->isAllowed($role, $controller, $action)) {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoUrlAndExit('error/denied');
}
}
// for admin module
else if ($request->getModuleName() == 'admin') {
// access resources (controllers)
// usually there will be more access resources
$acl->add(new Zend_Acl_Resource('index'));
$acl->add(new Zend_Acl_Resource('error'));
// access roles
$acl->addRole(new Zend_Acl_Role('guest'));
$acl->addRole(new Zend_Acl_Role('user'));
$acl->addRole(new Zend_Acl_Role('administrator'));
// access rules
$acl->allow('administrator'); // allow administrators everywhere
$role = ($auth->getIdentity() && $auth->getIdentity()->status = 'approved')
? $auth->getIdentity()->role : 'guest';
$controller = $request->getControllerName();
$action = $request->getActionName();
if (!$acl->isAllowed($role, $controller, $action)) {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoUrlAndExit('error/denied');
}
}
}
}
ここでステップバイステップのチュートリアルを見ることができます: http://blog.richardknop.com/2009/06/user-login-and-authentication-with-zend_auth-and-zend_acl/