Skip to content

Commit b214121

Browse files
committed
twig extension => voter
1 parent a87456d commit b214121

File tree

3 files changed

+111
-47
lines changed

3 files changed

+111
-47
lines changed

Resources/config/services.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ services:
1010
gatekeeper.repository.dummy:
1111
class: GateKeeper\Repository\DummyRepository
1212

13-
gatekeeper.twig:
14-
class: GateKeeperBundle\Twig\TwigExtension
15-
arguments: [@gatekeeper]
13+
gatekeeper.gates_provider.dummy:
14+
class: GateKeeper\Provider\DummyGatesProvider
15+
16+
gatekeeper.voter:
17+
class: GateKeeperBundle\Voter\GateKeeper
18+
arguments: [@gatekeeper, @gatekeeper.gates_provider.dummy]
1619
tags:
17-
- { name: twig.extension }
20+
- { name: security.voter }
1821

1922
gatekeeper.access.allow:
2023
public: false

Twig/TwigExtension.php

Lines changed: 0 additions & 43 deletions
This file was deleted.

Voter/GateKeeper.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Author: Łukasz Barulski
4+
* Date: 29.04.14 14:55
5+
*/
6+
7+
namespace GateKeeperBundle\Voter;
8+
9+
use GateKeeper\GateKeeper as Keeper;
10+
use GateKeeper\Object\ObjectInterface;
11+
use GateKeeper\Provider\GatesProviderInterface;
12+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
13+
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
14+
15+
class GateKeeper implements VoterInterface
16+
{
17+
/**
18+
* @var array|null
19+
*/
20+
private $gates;
21+
22+
/**
23+
* @var GatesProviderInterface
24+
*/
25+
private $gatesProvider;
26+
27+
/**
28+
* @var \GateKeeper\GateKeeper
29+
*/
30+
private $gateKeeper;
31+
32+
/**
33+
* @param Keeper $gateKeeper
34+
* @param GatesProviderInterface $gatesProvider
35+
*/
36+
public function __construct(Keeper $gateKeeper, GatesProviderInterface $gatesProvider)
37+
{
38+
$this->gatesProvider = $gatesProvider;
39+
$this->gateKeeper = $gateKeeper;
40+
}
41+
42+
/**
43+
* Checks if the voter supports the given attribute.
44+
*
45+
* @param string $attribute An attribute
46+
*
47+
* @return Boolean true if this Voter supports the attribute, false otherwise
48+
*/
49+
public function supportsAttribute($attribute)
50+
{
51+
if (null === $this->gates)
52+
{
53+
$this->gates = $this->gatesProvider->getGates();
54+
}
55+
56+
return in_array($attribute, $this->gates);
57+
}
58+
59+
/**
60+
* Checks if the voter supports the given class.
61+
*
62+
* @param string $class A class name
63+
*
64+
* @return Boolean true if this Voter can process the class
65+
*/
66+
public function supportsClass($class)
67+
{
68+
return false;
69+
}
70+
71+
/**
72+
* Returns the vote for the given parameters.
73+
* This method must return one of the following constants:
74+
* ACCESS_GRANTED, ACCESS_DENIED, or ACCESS_ABSTAIN.
75+
*
76+
* @param TokenInterface $token A TokenInterface instance
77+
* @param object $object The object to secure
78+
* @param array $attributes An array of attributes associated with the method being invoked
79+
*
80+
* @return integer either ACCESS_GRANTED, ACCESS_ABSTAIN, or ACCESS_DENIED
81+
*/
82+
public function vote(TokenInterface $token, $object, array $attributes)
83+
{
84+
if (1 !== count($attributes))
85+
{
86+
throw new \InvalidArgumentException('Only one attribute is allowed');
87+
}
88+
89+
if (false === $this->supportsAttribute($attributes[0]))
90+
{
91+
return self::ACCESS_ABSTAIN;
92+
}
93+
94+
$user = $token->getUser() instanceof ObjectInterface ? $token->getUser() : null;
95+
$attributes = is_array($object) ? $object : [];
96+
97+
if ($this->gateKeeper->hasAccess($attributes[0], $user, $object))
98+
{
99+
return self::ACCESS_GRANTED;
100+
}
101+
102+
return self::ACCESS_DENIED;
103+
}
104+
}

0 commit comments

Comments
 (0)