1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** Acl.php */ |
11
|
|
|
namespace Acl\Controller\Plugin; |
12
|
|
|
|
13
|
|
|
use Zend\Mvc\Controller\Plugin\AbstractPlugin; |
14
|
|
|
use Zend\Permissions\Acl\AclInterface; |
15
|
|
|
use Auth\Entity\UserInterface; |
16
|
|
|
use Auth\Exception\UnauthorizedAccessException; |
17
|
|
|
use Core\Entity\FileInterface; |
18
|
|
|
use Auth\Exception\UnauthorizedImageAccessException; |
19
|
|
|
use Zend\Permissions\Acl\Role\RoleInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Acl |
23
|
|
|
* @package Acl\Controller\Plugin |
24
|
|
|
*/ |
25
|
|
|
class Acl extends AbstractPlugin |
26
|
|
|
{ |
27
|
|
|
protected $acl; |
28
|
|
|
protected $user; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param AclInterface $acl |
32
|
|
|
* @param UserInterface $user |
33
|
|
|
*/ |
34
|
|
|
public function __construct(AclInterface $acl, UserInterface $user = null) |
35
|
|
|
{ |
36
|
|
|
$this->setAcl($acl); |
37
|
|
|
if (null !== $user) { |
38
|
|
|
$this->setUser($user); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return AclInterface |
44
|
|
|
*/ |
45
|
|
|
public function getAcl() |
46
|
|
|
{ |
47
|
|
|
return $this->acl; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param AclInterface $acl |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function setAcl(AclInterface $acl) |
55
|
|
|
{ |
56
|
|
|
$this->acl = $acl; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return \Auth\Entity\User |
62
|
|
|
*/ |
63
|
|
|
public function getUser() |
64
|
|
|
{ |
65
|
|
|
if (!$this->user) { |
66
|
|
|
$this->user = new \Auth\Entity\User(); |
67
|
|
|
$this->user->setRole('guest'); |
68
|
|
|
} |
69
|
|
|
return $this->user; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param UserInterface $user |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setUser(UserInterface $user) |
77
|
|
|
{ |
78
|
|
|
$this->user = $user; |
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Returns true, if the logged in user is of a specific role. |
84
|
|
|
* |
85
|
|
|
* If $inherit is TRUE, inheritance is also considered. |
86
|
|
|
* In that case, the third parameter is used to determine, wether only the |
87
|
|
|
* direct parent role should be checked or not. |
88
|
|
|
* |
89
|
|
|
* @param string|\Zend\Permissions\Acl\Role\RoleInterface $role Matching role. |
90
|
|
|
* @param bool $inherit |
91
|
|
|
* @param bool $onlyParents |
92
|
|
|
* @return bool |
93
|
|
|
* @uses \Zend\Permission\Acl\Acl::inheritsRole() |
94
|
|
|
*/ |
95
|
|
|
public function isRole($role, $inherit = false, $onlyParents = false) |
96
|
|
|
{ |
97
|
|
|
if ($role instanceof RoleInterface) { |
98
|
|
|
$role = $role->getRoleId(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$userRole = $this->getUser()->getRole(); |
102
|
|
|
$isRole = $userRole == $role; |
103
|
|
|
|
104
|
|
|
/* |
105
|
|
|
* @todo remove this, if the admin module is implemented |
106
|
|
|
*/ |
107
|
|
|
if ('recruiter' == $role) { |
108
|
|
|
$inherit = true; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($isRole || !$inherit) { |
112
|
|
|
return $isRole; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$acl = $this->getAcl(); /* @var $acl \Zend\Permissions\Acl\Acl */ |
116
|
|
|
|
117
|
|
|
return method_exists($acl, 'inheritsRole') && $acl->inheritsRole($userRole, $role, $onlyParents); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $resource |
122
|
|
|
* @param null $privilege |
|
|
|
|
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function test($resource, $privilege = null) |
126
|
|
|
{ |
127
|
|
|
return $this->getAcl()->isAllowed($this->getUser(), $resource, $privilege); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $resource |
132
|
|
|
* @param null $privilege |
|
|
|
|
133
|
|
|
* @throws \Auth\Exception\UnauthorizedImageAccessException |
134
|
|
|
* @throws \Auth\Exception\UnauthorizedAccessException |
135
|
|
|
*/ |
136
|
|
|
public function check($resource, $privilege = null) |
137
|
|
|
{ |
138
|
|
|
if (!$this->test($resource, $privilege)) { |
139
|
|
|
$msg = null === $privilege |
|
|
|
|
140
|
|
|
? sprintf( |
141
|
|
|
'You are not allowed to access resource "%s"', |
142
|
|
|
is_object($resource) ? $resource->getResourceId() : $resource |
143
|
|
|
) |
144
|
|
|
: sprintf( |
145
|
|
|
'You are not allowed to execute operation "%s" on resource "%s"', |
146
|
|
|
$privilege, |
147
|
|
|
is_object($resource) ? $resource->getResourceId() : $resource |
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
if ($resource instanceof FileInterface && 0 == strpos($resource->getType(), 'image/')) { |
151
|
|
|
throw new UnauthorizedImageAccessException(str_replace('resource', 'image', $msg)); |
152
|
|
|
} |
153
|
|
|
throw new UnauthorizedAccessException($msg); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param null $resource |
|
|
|
|
159
|
|
|
* @param null $privilege |
|
|
|
|
160
|
|
|
* @param string $mode |
161
|
|
|
* @return $this|bool |
162
|
|
|
*/ |
163
|
|
|
public function __invoke($resource = null, $privilege = null, $mode = 'check') |
164
|
|
|
{ |
165
|
|
|
if (null === $resource) { |
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
if ('test' == $mode) { |
170
|
|
|
return $this->test($resource, $privilege); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$this->check($resource, $privilege); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|