guilro/protection-proxy-bundle

此包已废弃且不再维护。未建议替代包。

Symfony2 的保护代理生成器

安装: 231

依赖者: 0

建议者: 0

安全: 0

星标: 6

关注者: 1

分支: 0

类型:symfony-bundle

0.1.4 2014-02-28 14:34 UTC

This package is not auto-updated.

Last update: 2020-01-19 16:28:16 UTC


README

Build Status

当前版本: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,以便授予或拒绝用户访问权限。