Skip to content

Commit

Permalink
Eliminada la ACL y reemplazada por un Security Voter propio
Browse files Browse the repository at this point in the history
  • Loading branch information
javiereguiluz committed Aug 25, 2013
1 parent 6297b1a commit 0d25fdb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
3 changes: 0 additions & 3 deletions app/config/security.yml
@@ -1,8 +1,5 @@
security:

acl:
connection: default

firewalls:
# Firewall de la parte de administración o backend
backend:
Expand Down
5 changes: 5 additions & 0 deletions app/config/services.yml
Expand Up @@ -21,3 +21,8 @@ services:
class: Cupon\OfertaBundle\Listener\RequestListener
tags:
- { name: kernel.event_listener, event: kernel.request }

cupon.tiendabundle.security.ownervoter:
class: Cupon\TiendaBundle\Security\OwnerVoter
tags:
- { name: security.voter }
31 changes: 0 additions & 31 deletions src/Cupon/OfertaBundle/DataFixtures/ORM/Ofertas.php
Expand Up @@ -15,13 +15,7 @@
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
use Cupon\CiudadBundle\Entity\Ciudad;
use Cupon\OfertaBundle\Entity\Oferta;
use Cupon\TiendaBundle\Entity\Tienda;

/**
* Fixtures de la entidad Oferta.
Expand Down Expand Up @@ -98,31 +92,6 @@ public function load(ObjectManager $manager)

$manager->persist($oferta);
$manager->flush();

// Otorgar el permiso adecuado a cada oferta utilizando la ACL

// Obtener la identidad del objeto oferta y del usuario
$idObjeto = ObjectIdentity::fromDomainObject($oferta);
$idUsuario = UserSecurityIdentity::fromAccount($tienda);

// Buscar si la oferta ya dispone de una ACL previa
$proveedor = $this->container->get('security.acl.provider');

try {
$acl = $proveedor->findAcl($idObjeto, array($idUsuario));
} catch (AclNotFoundException $e) {
// El objeto no disponía de ninguna ACL, crearla
$acl = $proveedor->createAcl($idObjeto);
}

// Borrar los ACEs previos que (a lo mejor) dispone este objeto
$aces = $acl->getObjectAces();
foreach ($aces as $index => $ace) {
$acl->deleteObjectAce($index);
}

$acl->insertObjectAce($idUsuario, MaskBuilder::MASK_OPERATOR);
$proveedor->updateAcl($acl);
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions src/Cupon/TiendaBundle/Security/OwnerVoter.php
@@ -0,0 +1,48 @@
<?php

/*
* (c) Javier Eguiluz <javier.eguiluz@gmail.com>
*
* Este archivo pertenece a la aplicación de prueba Cupon.
* El código fuente de la aplicación incluye un archivo llamado LICENSE
* con toda la información sobre el copyright y la licencia.
*/

namespace Cupon\TiendaBundle\Security;

use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

class OwnerVoter implements VoterInterface
{
public function supportsAttribute($attribute)
{
return 'ROLE_EDITAR_OFERTA' == $attribute;
}

public function supportsClass($class)
{
return true;
}

public function vote(TokenInterface $token, $object, array $attributes)
{
$vote = VoterInterface::ACCESS_ABSTAIN;

foreach ($attributes as $attribute) {
if (false === $this->supportsAttribute($attribute)) {
continue;
}

$user = $token->getUser();
$vote = VoterInterface::ACCESS_DENIED;

// comprobar que la oferta que se edita fue publicada por esta misma tienda
if ($object->getTienda()->getId() === $user->getId()) {
$vote = VoterInterface::ACCESS_GRANTED;
}
}

return $vote;
}
}

0 comments on commit 0d25fdb

Please sign in to comment.