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