Zend 导航和 Zend ACL
我正在使用 Zend Acl 和 Zend Navigation。我正在引导程序中设置导航。如果用户无权访问该资源,我试图不显示链接。我已经阅读了几个教程,多次浏览了 zend 参考手册,但是导航中的所有链接仍然显示给来宾用户,即使有些应该只显示给管理员用户
protected function _initNavigationMenu()
{
$this->bootstrap("layout");
$layout = $this->getResource('layout');
$view = $layout->getView();
$navigation_model = new Core_Model_Navigation();
$result = $navigation_model->getTopLevelNavigationLinksForDisplay();
$sanitized = $navigation_model->sanatizeNavigationForDisplay($result);
$config = new Zend_Config($sanitized);
$nav = new Zend_Navigation($config);
$view->navigation($nav)
->setAcl($this->_acl->acl())
->setRole((string)BW::user() -> role);
}
所有 ACL 角色和资源以及导航都出现了来自数据库,以防
此处重要的是由 $sanitized 创建的数组
Array
(
[0] => Array
(
[parent_id] => 0
[label] => File Manager
[order] => 1
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[module] => file
[reset_params] => 1
[id] => fileManagerLink
)
[1] => Array
(
[parent_id] => 0
[label] => Upload
[title] => Upload a file
[order] => 2
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[controller] => upload
[module] => file
[reset_params] => 1
[id] => fileManagerUploadLink
)
[2] => Array
(
[parent_id] => 0
[label] => Files
[title] => Manage your files
[order] => 3
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[controller] => manage
[module] => file
[reset_params] => 1
[id] => FileManagerFilesLink
)
[3] => Array
(
[parent_id] => 0
[label] => Contacts
[order] => 4
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[controller] => contact
[module] => file
[reset_params] => 1
[id] => Contacts
)
[4] => Array
(
[parent_id] => 0
[label] => My Account
[title] => Your Account
[order] => 5
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => index
[controller] => user
[reset_params] => 1
[id] => myAccountNavigationLink
)
[5] => Array
(
[parent_id] => 0
[label] => Admin
[title] => The administration panel
[order] => 6
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[module] => admin
[reset_params] => 1
[id] => Administration
)
[6] => Array
(
[parent_id] => 0
[label] => Test for ACL
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[7] => Array
(
[parent_id] => 0
[label] => Test for ACL
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[8] => Array
(
[parent_id] => 0
[label] => Test for ACL
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[9] => Array
(
[parent_id] => 0
[label] => Test for ACL
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[10] => Array
(
[parent_id] => 0
[label] => ACL Test
[order] => 0
[resource] => 8
[privilage] => index
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[11] => Array
(
[parent_id] => 0
[label] => Joey
[order] => 0
[resource] => adminIndexIndex
[privilage] => index
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[12] => Array
(
[parent_id] => 0
[label] => another test
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[13] => Array
(
[parent_id] => 0
[label] => another test
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[14] => Array
(
[parent_id] => 0
[label] => another test
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[15] => Array
(
[parent_id] => 0
[label] => another stupid test
[order] => 0
[resource] => Admin Homepage
[privilage] => index
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[16] => Array
(
[parent_id] => 0
[label] => another stupid test
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
)
I am working with Zend Acl and Zend Navigation. I am setting up the navigation in the bootstrap. I am trying to have links not show if the user doesn't have access to the resource. I have read several tutorials, gone through the zend reference manual several times, but all of the links in the navigation are still appearing for the guest user even though some should only be shown to an admin user
protected function _initNavigationMenu()
{
$this->bootstrap("layout");
$layout = $this->getResource('layout');
$view = $layout->getView();
$navigation_model = new Core_Model_Navigation();
$result = $navigation_model->getTopLevelNavigationLinksForDisplay();
$sanitized = $navigation_model->sanatizeNavigationForDisplay($result);
$config = new Zend_Config($sanitized);
$nav = new Zend_Navigation($config);
$view->navigation($nav)
->setAcl($this->_acl->acl())
->setRole((string)BW::user() -> role);
}
All ACL roles and resources and the navigation come from a DB in case that matters
here is teh array created by $sanitized
Array
(
[0] => Array
(
[parent_id] => 0
[label] => File Manager
[order] => 1
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[module] => file
[reset_params] => 1
[id] => fileManagerLink
)
[1] => Array
(
[parent_id] => 0
[label] => Upload
[title] => Upload a file
[order] => 2
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[controller] => upload
[module] => file
[reset_params] => 1
[id] => fileManagerUploadLink
)
[2] => Array
(
[parent_id] => 0
[label] => Files
[title] => Manage your files
[order] => 3
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[controller] => manage
[module] => file
[reset_params] => 1
[id] => FileManagerFilesLink
)
[3] => Array
(
[parent_id] => 0
[label] => Contacts
[order] => 4
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[controller] => contact
[module] => file
[reset_params] => 1
[id] => Contacts
)
[4] => Array
(
[parent_id] => 0
[label] => My Account
[title] => Your Account
[order] => 5
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => index
[controller] => user
[reset_params] => 1
[id] => myAccountNavigationLink
)
[5] => Array
(
[parent_id] => 0
[label] => Admin
[title] => The administration panel
[order] => 6
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[module] => admin
[reset_params] => 1
[id] => Administration
)
[6] => Array
(
[parent_id] => 0
[label] => Test for ACL
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[7] => Array
(
[parent_id] => 0
[label] => Test for ACL
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[8] => Array
(
[parent_id] => 0
[label] => Test for ACL
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[9] => Array
(
[parent_id] => 0
[label] => Test for ACL
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[10] => Array
(
[parent_id] => 0
[label] => ACL Test
[order] => 0
[resource] => 8
[privilage] => index
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[11] => Array
(
[parent_id] => 0
[label] => Joey
[order] => 0
[resource] => adminIndexIndex
[privilage] => index
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[12] => Array
(
[parent_id] => 0
[label] => another test
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[13] => Array
(
[parent_id] => 0
[label] => another test
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[14] => Array
(
[parent_id] => 0
[label] => another test
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[15] => Array
(
[parent_id] => 0
[label] => another stupid test
[order] => 0
[resource] => Admin Homepage
[privilage] => index
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
[16] => Array
(
[parent_id] => 0
[label] => another stupid test
[order] => 0
[resource] => 9
[active] => 1
[visible] => 1
[internal_page] => 1
[action] => add-navigation
[controller] => manage
[module] => admin
[reset_params] => 1
)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
乔伊,
关于这方面的良好文档可能很难找到,但它存在。在应用程序的引导程序中,您需要做两件事:
在我的引导程序中,我使用类似于以下内容的函数来执行此操作。以下是关键方面的示例:
生成 ACL:
此处,ACL 是根据您的应用程序的需要进行设置的。资源方法返回它们以供其他地方需要时使用,并且它们也存储在注册表中。
将导航链接到生成的 ACL(还指定默认角色):
资源方法从注册表中获取先前创建的 ACL,并使用 setDefaultAcl 方法将它们与应用程序导航对象一起分配给应用程序导航对象。默认角色。
构建尊重 ACL 的导航
在这里,我们创建了一个名为管理的部分,要求用户对 admin:copyright 具有查看权限资源,这要归功于预先构建的 acl 列表。
现在,当您调用 $this->navigation()->menu()->render() 等时,菜单选项将基于用户的访问权限。
嗯,我想我应该在我的网站上添加一个帖子。一切顺利。
马特
Joey,
good documentation on this can be hard to find, but it exists. What you need to do, in your application's bootstrap is two things:
In my bootstraps, I use the functions similar to the following to do this. Here's examples of the key aspects:
Generating the ACL's:
Here, the ACL's are setup as needed for your application. The resource method returns them for use if needed elsewhere and they're also stored in the registry.
Linking the navigation to the generated ACL's (also specifies a default role):
The resource method picks up the previously created acl's from the registry and uses the setDefaultAcl method to assign them to the application navigation object along with the default role.
Build navigation that respects ACLs
Here, we've created a section called administration that requires the user to have the view privilege on the admin:copyright resource, which guest does thanks to the pre-built acl list.
Now, when you call $this->navigation()->menu()->render() etc, the menu options will be based on the access of the user.
Hmmm, I think I should add a post to this on my site. All the best with it.
Matt
我建议您在导航 xml 文件中添加另一个与 ACL 资源相关的标签,并将其映射到存储 ACL 映射的 ini 文件中。该逻辑可以最好地在引导程序上实现,以便可以在事情发生之前读取它。如果您有 ajax 调用,也不要忘记将其实现。
I suggest that you add another tag in your navigation xml file something that pertains to ACL resource and map it in your ini file where you store your ACL mapping. The logic can be best implemented on your bootstrap so that it can be read before things happen. Do not also forget to implement it to your ajax call in case you have one.
我认为控制器插件最好使用 Zend_Nav 和 Zend_Acl 进行管理,例如:
I think a controller plugin is better to manage with Zend_Nav and Zend_Acl like:
我希望它对你有帮助
I hope it help you