0

次のように動作するように Zend_Acl をセットアップしました。

$acl->addRole(new Zend_Acl_Role('admin'));
$acl->addRole(new Zend_Acl_Role('user'));
$acl->add(new Zend_Acl_Resource('frontoffice'));
$acl->add(new Zend_Acl_Resource('backoffice'));
$acl->deny('user');
$acl->allow('user', null, 'frontoffice');
$acl->allow('admin');

したがって、ロール「管理者」はすべてにアクセスでき、「ユーザー」はフロントオフィスにしかアクセスできません。フロントオフィスはモジュールの名前で、バックオフィスはモジュールの名前です。ACL はカスタム プラグインでチェックされます。

<?php
class Custom_Controller_Plugin_Auth extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
    $loginController = 'auth';
    $loginAction     = 'index';

    $auth = Zend_Auth::getInstance();

    // If user is not logged in and is not requesting login page
    // - redirect to login page.
    if (!$auth->hasIdentity()
            && $request->getControllerName() != $loginController
            && $request->getActionName()     != $loginAction) {

        $redirector =     Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
        $redirector->gotoSimpleAndExit($loginAction, $loginController);
    }

    // User is logged in or on login page.

    if ($auth->hasIdentity()) {
        // Is logged in
        // Let's check the credential;
    $registry = Zend_Registry::getInstance();
        $acl = $registry->get('acl');

        $identity = $auth->getIdentity();
        // role is a column in the user table (database)
        $isAllowed = $acl->isAllowed($identity->role, null,
                                     $request->getModuleName());
        if (!$isAllowed) {
            $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
            $redirector->gotoUrlAndExit('/');
        }
    }
}
}
?>

今、私のリソースの名前は現在のモジュールの名前です。acl を Zend_Navigation にプラグインし、メニュー項目のリソースを frontoffice に設定すると、メニュー項目はユーザーと管理者の両方で消えますが、どちらも表示できるはずです。これは、ブートストラップのナビゲーション コードです。

protected function _initNavigation()
{
    $this->bootstrap('layout');
    $layout = $this->getResource('layout');
    $view = $layout->getView();
    $navigation = new Zend_Navigation($this->getOption('navigation'));
    $auth = Zend_Auth::getInstance();
    $role = $auth->getIdentity()->role;
    $view->navigation($navigation)->setAcl(Zend_Registry::get('acl'))
                      ->setRole($role);
}

これを解決する方法について誰か提案がありますか? 前もって感謝します!

4

1 に答える 1

0

私が知っているように、ACL でモジュールを直接定義することはできません。次の構文を使用して、各モジュールの各コントローラーを定義する必要があります。

$this->add(new Zend_Acl_Resource("module_name:controller_name");
于 2012-07-29T15:07:57.917 に答える