1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Rik van der Kemp <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://www.zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\PageBundle\Security\Voter; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
11
|
|
|
use Zicht\Bundle\PageBundle\Model\ScheduledContentInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Check content against the scheduled dates |
15
|
|
|
* |
16
|
|
|
* @package Zicht\Bundle\PageBundle\Security\Voter |
17
|
|
|
*/ |
18
|
|
|
class ScheduledContentVoter extends AbstractAdminAwareVoter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Decide based on the current date and time what the vote should be. Static so it's strategy can easily be accessed |
22
|
|
|
* by other components as well, without the actual need for the voter instance. |
23
|
|
|
* |
24
|
|
|
* @param ScheduledContentInterface $object |
25
|
|
|
* @param array $attributes |
26
|
|
|
* @return int |
27
|
|
|
*/ |
28
|
6 |
|
public static function decide(ScheduledContentInterface $object, array $attributes = []) |
29
|
|
|
{ |
30
|
6 |
|
$now = new \DateTimeImmutable(); |
31
|
6 |
|
$vote = VoterInterface::ACCESS_ABSTAIN; |
32
|
6 |
|
$from = $object->isScheduledFrom(); |
33
|
6 |
|
$till = $object->isScheduledTill(); |
34
|
|
|
|
35
|
6 |
|
if (!$object->isPublic() || false === self::notEmpty($from, $till)) { |
36
|
1 |
|
return $vote; |
37
|
|
|
} |
38
|
|
|
|
39
|
5 |
|
switch (true) { |
40
|
5 |
|
case is_null($from): |
41
|
1 |
|
$vote = $till >= $now ? VoterInterface::ACCESS_GRANTED : VoterInterface::ACCESS_DENIED; |
42
|
1 |
|
break; |
43
|
4 |
|
case is_null($till): |
44
|
1 |
|
switch (true) { |
45
|
1 |
|
case ($from <= $now): |
46
|
1 |
|
$vote = VoterInterface::ACCESS_GRANTED; |
47
|
1 |
|
break; |
48
|
|
|
case ($from > $now && self::hasCmsAttribute($attributes)): |
49
|
|
|
$vote = VoterInterface::ACCESS_GRANTED; |
50
|
|
|
break; |
51
|
|
|
default: |
52
|
|
|
$vote = VoterInterface::ACCESS_DENIED; |
53
|
|
|
} |
54
|
1 |
|
break; |
55
|
3 |
|
default: |
56
|
3 |
|
switch (true) { |
57
|
3 |
|
case ($from <= $now && $till >= $now): |
58
|
1 |
|
$vote = VoterInterface::ACCESS_GRANTED; |
59
|
1 |
|
break; |
60
|
2 |
|
case (($from > $now && $till >= $now) && self::hasCmsAttribute($attributes)): |
61
|
|
|
$vote = VoterInterface::ACCESS_GRANTED; |
62
|
|
|
break; |
63
|
2 |
|
default: |
64
|
2 |
|
$vote = VoterInterface::ACCESS_DENIED; |
65
|
2 |
|
} |
66
|
3 |
|
} |
67
|
|
|
|
68
|
5 |
|
return $vote; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks if the voter supports the given class. |
73
|
|
|
* |
74
|
|
|
* @param string $class A class name |
75
|
|
|
* |
76
|
|
|
* @return Boolean true if this Voter can process the class |
77
|
|
|
*/ |
78
|
7 |
|
public function supportsClass($class) |
79
|
|
|
{ |
80
|
7 |
|
return in_array(ScheduledContentInterface::class, class_implements($class)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @{inheritDoc} |
85
|
|
|
*/ |
86
|
7 |
|
public function vote(TokenInterface $token, $object, array $attributes) |
87
|
|
|
{ |
88
|
|
|
// Abstract class checks if user is admin, if not so it will return VoterInterface::ACCESS_ABSTAIN |
89
|
7 |
|
$vote = parent::vote($token, $object, $attributes); |
90
|
|
|
|
91
|
|
|
/** @var ScheduledContentInterface $object */ |
92
|
7 |
|
if ($vote === VoterInterface::ACCESS_ABSTAIN && $this->supportsClass(get_class($object))) { |
93
|
6 |
|
foreach ($attributes as $attribute) { |
94
|
6 |
|
if (!$this->supportsAttribute($attribute)) { |
95
|
|
|
continue; |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
$vote = self::decide($object, $attributes); |
99
|
6 |
|
} |
100
|
6 |
|
} |
101
|
|
|
|
102
|
7 |
|
return $vote; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|