-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathArticleVoter.php
More file actions
137 lines (119 loc) · 3.71 KB
/
ArticleVoter.php
File metadata and controls
137 lines (119 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
/**
* Created by Eton Digital.
* User: Vladimir Mladenovic (vladimir.mladenovic@etondigital.com)
* Date: 2.7.15.
* Time: 09.00
*/
namespace ED\BlogBundle\Security\Authorization\Voter;
use ED\BlogBundle\Interfaces\Model\ArticleInterface;
use ED\BlogBundle\Model\Entity\Article;
use ED\BlogBundle\Security\ACL\ArticlePermissionMap;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
class ArticleVoter implements VoterInterface
{
private $permissionMap;
function __construct(ArticlePermissionMap $permissionMap)
{
$this->permissionMap = $permissionMap;
}
public function supportsAttribute($attribute)
{
return $this->permissionMap->supports($attribute);
}
public function supportsClass($class)
{
try
{
$testObject = new $class();
if ($testObject instanceof ArticleInterface)
{
return true;
}
else
{
return false;
}
}
catch(\Exception $e)
{
return false;
}
catch(\Error $e)
{
return false;
}
}
public function vote(TokenInterface $token, $object, array $attributes)
{
$class = get_class($object);
if (!$this->supportsClass($class))
{
return self::ACCESS_ABSTAIN;
}
$user = $token->getUser();
if($user === 'anon.')
{
return self::ACCESS_ABSTAIN;
}
else
{
if(in_array('EDIT_ARTICLE', $attributes))
{
if($user->hasRole('ROLE_BLOG_ADMIN') || $user->hasRole('ROLE_BLOG_EDITOR'))
{
return self::ACCESS_GRANTED;
}
else
{
if($user->hasRole('ROLE_BLOG_AUTHOR') || $user->hasRole('ROLE_BLOG_CONTRIBUTOR'))
{
if($object instanceof ArticleInterface && $object->getAuthor() == $user)
{
return self::ACCESS_GRANTED;
}
else
{
return self::ACCESS_DENIED;
}
}
}
}
elseif(in_array('PUBLISH_ARTICLE', $attributes))
{
if($user->hasRole('ROLE_BLOG_ADMIN') || $user->hasRole('ROLE_BLOG_EDITOR'))
{
return self::ACCESS_GRANTED;
}
else
{
if($user->hasRole('ROLE_BLOG_AUTHOR'))
{
if($object instanceof ArticleInterface && $object->getAuthor() == $user)
{
return self::ACCESS_GRANTED;
}
}
return self::ACCESS_DENIED;
}
}
elseif(in_array('EDIT_PUBLISH_STATUS', $attributes) && $object instanceof ArticleInterface)
{
$testObject = $object->getParent() ? $object->getParent() : $object;
if($user->hasRole('ROLE_BLOG_CONTRIBUTOR'))
{
return self::ACCESS_DENIED;
}
elseif( $testObject && $testObject->getStatus() != Article::STATUS_PUBLISHED )
{
return self::ACCESS_DENIED;
}
else
{
return self::ACCESS_GRANTED;
}
}
}
}
}