guilro / protection-proxy-bundle
此包已废弃且不再维护。未建议替代包。
Symfony2 的保护代理生成器
0.1.4
2014-02-28 14:34 UTC
Requires
- ocramius/proxy-manager: 0.5.*
- symfony/config: ~2.4
- symfony/dependency-injection: ~2.4
- symfony/expression-language: ~2.4
- symfony/http-kernel: ~2.4
- symfony/security-bundle: ~2.4
This package is not auto-updated.
Last update: 2020-01-19 16:28:16 UTC
README
当前版本:0.1.4
安装
将此包添加到您的 composer.json
文件中
{ "require": { "guilro/protection-proxy-bundle": "0.1.*" } }
在 app/AppKernel.php 中注册此包
<?php // app/AppKernel.php public function registerBundles() { return array( // ... new Guilro\ProtectionProxyBundle\GuilroProtectionProxyBundle(), ); }
使用方法
您需要配置受保护的类和方法(目前位于 config.yml 中)。
# app/config/config.yml guilro_protection_proxy: caching: true #optional, default to false protected_classes: Acme\BlogBundle\Entity\Comment: methods: getTitle: attribute: ROLE_USER #can be a role, or any attribute that a voter can handle deny_value: Title hidden ! #optional setting, default will return null on deny getAuthor: expression: '"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())' return_proxy: true
在控制器和视图中典型使用
$em->getRepository('AcmeBlogBundle:Comment')->find(342); $proxyManager = $this->get('guilro.protection_proxy'); $commentProxy = $proxyManager->getProxy($comment); $this->render( 'AcmeBlogBundle:Comment:show.twig.html', array('comment' => $commentProxy) );
- 如果设置了 'attribute',当使用生成的代理时,
$comment
的原始方法getTitle()
和setAuthor()
只在实际执行$securityContext->isGranted('attribute', $comment)
返回true
时才执行。 - 如果设置了 'expression',当使用生成的代理时,原始方法只在实际执行
$securityContext->isGranted(new Expression($expression), $comment)
返回true
时才执行。 - 如果两者都设置,则都会执行测试。
- 如果
$securityContext->isGranted()
返回 false,则原始方法不会执行。它将返回null
,或者如果设置了,则返回deny_value
。 - 如果原始方法返回受保护类的对象,它将返回原始对象或其受保护的代理,具体取决于
return_proxy
设置。此设置的默认值为false
。
如果您使用除角色之外的其他属性,您可能需要实现自己的 Voter,以便授予或拒绝用户访问权限。