0

我有模塊,建立我的ACL樹工作正常。ZF2重定向,如果ACL不允許訪問頁面

我還在config/autoload目錄中有一個導航配置文件,它詳細說明了我的應用程序結構以及與條目相關的資源。我的應用程序模塊配置中也有一個導航工廠。

所有這一切都工作正常,我正在渲染我的菜單基於登錄用戶的角色和導航配置頁面上的資源的權限。

我無法解決的是如何防止訪問用戶無法訪問的頁面(隱藏在呈現的導航菜單中的頁面)。我希望在模塊中進行管理。我假設在我的Module.php文件中的onBootstrap函數,我需要運行isAllowed對ACL和重定向(如在這個問題 - Forward to another controller/action from module.php)。 isAllowed似乎需要資源來查詢,但是。這需要從導航配置中獲得。

如果我硬編碼isAllowed函數所需的資源,我可以得到這個工作。實際上,我只需要從導航配置中獲取當前頁面請求的資源。

我確定這必須是標準功能,但我找不到任何具體的例子。

任何幫助表示讚賞。

克里斯

回答

0

這是你正在尋找的東西,或者是你在尋找如何從onBootstrap方法中訪問你的配置?

public function onBootstrap($event) { 
    $matched_route = $event->getRouteMatch()->getMatchedRouteName(); 
    $someOtherClass = new MyClassThatAuthenticatesRoutes(); 
    if(!($someOtherClass->isAllowed($matched_route)){ 
     $response = $event->getResponse(); 
     $response->setStatusCode(401); 
     $response->setReasonPhrase('Not allowed!'); 
     return $response; 
    } 

如果你正在尋找的只是配置,你可以去:

$sm = $e->getApplication()->getServiceManager(); 
$config = $sm->get('config'); 
+0

嗨。感謝您的回覆。對不起,如果我的措辭不是最好的,但那是我有效的地方。 因此,在$ someOtherClass-> isAllowed需要獲取該匹配路徑(或父母)中當前頁面的資源,以評估用戶是否被允許訪問該頁面。 功能已經可以在麪包屑視圖助手的組合中找到,它可以從導航cfg中找到我的當前頁面,菜單視圖助手可以根據用戶的角色和該頁面的資源隱藏菜單中的某些頁面。 我假設會有一種方法來實現這一點。 –

0

如果你需要爲ACL路由匹配考慮做這樣的事情:

/** 
* Retrieve the route match 
* 
* @return string 
*/ 
protected function getMatchRoute() 
{ 
    $router = $this->getServiceLocator()->get('router'); 
    $request = $this->getServiceLocator()->get('request');  

    $this->routeMatch = $router->match($request)->getMatchedRouteName(); 

    return $this->routeMatch; 
} 

然後在你的控制器中:

// note, $acl is just a class I wrote to extend class Zend\Permissions\Acl\Acl 
// because I needed additional functionality  
$acl = new \PATH_TO\Acl\Acl(); 

// checkAcl(), just however you plan on handling permissions 
// $role is obviously just that, the role of the user, where ever 
// you are setting that. 
// the second param is from the method in the above code block which is the 
// resource (page) you are wanting to check against 
$access = $acl->checkAcl($role, $this->getMatchRoute()); 

// they don't have access so redirect them 
if (!$access) 
{ 
    return $this->redirect()->toRoute('your_route', array()); 
} 

如果你需要看到更多的代碼,請讓我知道,但希望這可以讓你開始。

相關問題