1

ACLinのモジュールを書いていますがZF2、ほぼ完成です。

私が立ち往生しているポイントは、ユーザーが要求されたページへのアクセスを許可されていないときに、ユーザーをメッセージを表示するページに転送したいということです。403

ユーザーをにリダイレクトしようとしまし403たが、更新さURLれるため、ユーザーを転送しようとしています。

私がやりたいのはからModule.phpです。以下のコードを試しました-

Module.php

if (!$isAllowed) {
    $e->getApplication()->getServiceManager()->get('ControllerPluginManager')->get('forward')->dispatch('acl');
}

これを使用すると、次のエラーが発生しました-

キャッチされない例外「Zend\Mvc\Exception\DomainException」とメッセージ「Forward plugin requires a controller that implements InjectApplicationEventInterface」

Aclでコントローラーを実装しようとしましInjectApplicationEventInterfaceたが、問題は同じままです。

からForward別の人に方法を説明してもらえますか? 詳細が必要な場合はお知らせください。ActionModule.php

4

4 に答える 4

0

次のように、フォワードすることはできませんが、403 ページにリダイレクトすることはできません。

if (!$acl->isAllowed()) {
    $response = $e->getResponse();
    $response->getHeaders()->addHeaderLine('Location', $e->getRequest()->getBaseUrl() . '/403page');
    $response->setStatusCode(403);
}
于 2013-10-29T09:01:40.963 に答える
0

URLを更新せずに、ユーザーが制限区域にいるという情報をユーザーに送信したい場合は、次のことを行う必要があると思います。

  1. 制限区域メッセージを表示するレイアウトを変更します。
  2. 元のレイアウトのままにして、アクションのテンプレートを切り替えるだけで、立ち入り禁止エリアのメッセージが表示されます。

このアクションのいずれかの後、Response を 403 に変更するだけです。それが必要な場合は、簡単に実行できます。403 ステータスを送信したいコントローラーのアクションには、次を使用します。

 $viewModel->setTemplate('partial/noRights');
 $this->getResponse()->setStatusCode('403');
 return $viewModel;

または、カスタム レイアウトのレイアウトを変更する場合は、次のようにします。

 $this->layout('layout/custom');
 $this->getResponse()->setStatusCode('403');
 return $viewModel;

もちろん、event_dispatch にリスナーを追加するモジュールのブートストラップ セクションでそれを行うことができ、その後 $acl->isAllowed() が上で書いた変更を行うかどうかを確認します。例:

public function onBootstrap(MvcEvent $e)
{
    $app = $e->getApplication();
    $acl = $app->getServiceManager()->get('ACL'); // get your ACL here

    if (!$acl->isAllowed()) {
        $eventManager = $app->getEventManager();
        $sharedEventManager = $eventManager->getSharedManager();
        $sharedEventManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, function($e) {
            $controller = $e->getTarget(); //controller`s action which triggered event_dispatch
            $controller->getResponse()->setStatusCode('403');
            $controller->layout('layout/custom');
        }, 1000);
    }
}
于 2013-10-29T11:26:07.493 に答える
0

ACL を実装したとき、承認と認証用に独自の AUTH モジュールを作成しました。
そのモジュールで、ACLプラグインを作成しました..以下のようなもの

//Module.php

 /**
     * This method is called once the MVC bootstrapping is complete
     * 
     * @param \Zend\EventManager\EventInterface $e
     */
    public function onBootstrap(Event $e) 
    {
        $services = $e->getApplication()->getServiceManager();

        $eventManager = $e->getApplication()->getEventManager();
        $eventManager->attach('dispatch', array($this, 'loadConfiguration'), 101);
    }

    /**
     * 
     * @param \Zend\Mvc\MvcEvent $e
     */
    public function loadConfiguration(MvcEvent $e) 
    {        
        $e->getApplication()->getServiceManager()
                ->get('ControllerPluginManager')->get('AclPlugin')
                ->checkAcl($e); //Auth/src/Auth/Controller/AclPlugin      
    }

//Auth/src/Auth/Controller/AclPlugin

namespace Auth\Controller\Plugin;

/**
 * load libraries here
 */
class AclPlugin extends AbstractPlugin implements ServiceManagerAwareInterface 
{
    /*
     * @var Doctrine\ORM\EntityManager
     */

    protected $em;
    protected $sm;

    /**
     * @param Doctrine\ORM\EntityManager $em
     * @return string
     */
public function checkAcl($e) 
    {

        $matches = $e->getRouteMatch();
        $controller = $matches->getParam('controller');
        $action = $matches->getParam('action', 'index');

       if ($acl->isAllowed($role, $resource, $permission)) {


            return;

        } else {
            $matches->setParam('controller', 'Auth\Controller\User'); // redirect
            $matches->setParam('action', 'accessdenied');

            return;
        }

}

/// rest of the code here

}
于 2013-10-29T15:57:09.170 に答える