2017 © Pedro Peláez
 

symfony-bundle protection-proxy-bundle

Protection proxy generator for Symfony2

image

guilro/protection-proxy-bundle

Protection proxy generator for Symfony2

  • Friday, February 28, 2014
  • by Guilro
  • Repository
  • 1 Watchers
  • 6 Stars
  • 223 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 0 % Grown

The README.md

GuilroProtectionProxyBundle

Build Status, (*1)

Current version : 0.1.4, (*2)

Installation

Add this bundle to your composer.json file:, (*3)

{
    "require": {
        "guilro/protection-proxy-bundle": "0.1.*"
    }
}

Register the bundle in app/AppKernel.php:, (*4)

<?php

// app/AppKernel.php
public function registerBundles()
{
    return array(
        // ...
        new Guilro\ProtectionProxyBundle\GuilroProtectionProxyBundle(),
    );
}

Usage

You have to configure the protected classes and methods (for the moment in config.yml)., (*5)

# 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

Typicall usage in your controllers and views:, (*6)

$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)
);
  • If 'attribute' is set, when using the generated proxy, original methods getTitle() and setAuthor() of $comment will only be really executed if $securityContext->isGranted('attribute', $comment) returns true.
  • If 'expression' is set, when using the generated proxy, original methods will only be really executed if $securityContext->isGranted(new Expression($expression), $comment) returns true.
  • If both are set, both test are performed.
  • If $securityContext->isGranted() returns false, the original method will not be executed. It will return null, or deny_value if set.
  • If the original method returns an object of a pretected class, it will return the raw object or its protected proxy depending on return_proxy setting. Default for this setting is false.

If you use attributes other than roles, you should probably implements your own Voter in order to grant access or not to users., (*7)

The Versions