/
AbstractPermissionHelper.php
192 lines (168 loc) · 5.36 KB
/
AbstractPermissionHelper.php
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* Routes.
*
* @copyright Zikula contributors (Zikula)
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
* @author Zikula contributors <info@ziku.la>.
* @see https://ziku.la
* @version Generated by ModuleStudio 1.4.0 (https://modulestudio.de).
*/
declare(strict_types=1);
namespace Zikula\RoutesModule\Helper\Base;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\RequestStack;
use Zikula\Bundle\CoreBundle\Doctrine\EntityAccess;
use Zikula\GroupsModule\Entity\GroupEntity;
use Zikula\PermissionsModule\Api\ApiInterface\PermissionApiInterface;
use Zikula\UsersModule\Api\ApiInterface\CurrentUserApiInterface;
use Zikula\UsersModule\Entity\RepositoryInterface\UserRepositoryInterface;
use Zikula\UsersModule\Entity\UserEntity;
/**
* Permission helper base class.
*/
abstract class AbstractPermissionHelper
{
/**
* @var RequestStack
*/
protected $requestStack;
/**
* @var PermissionApiInterface
*/
protected $permissionApi;
/**
* @var CurrentUserApiInterface
*/
protected $currentUserApi;
/**
* @var UserRepositoryInterface
*/
protected $userRepository;
public function __construct(
RequestStack $requestStack,
PermissionApiInterface $permissionApi,
CurrentUserApiInterface $currentUserApi,
UserRepositoryInterface $userRepository
) {
$this->requestStack = $requestStack;
$this->permissionApi = $permissionApi;
$this->currentUserApi = $currentUserApi;
$this->userRepository = $userRepository;
}
/**
* Checks if the given entity instance may be read.
*/
public function mayRead(EntityAccess $entity, int $userId = null): bool
{
return $this->hasEntityPermission($entity, ACCESS_READ, $userId);
}
/**
* Checks if the given entity instance may be edited.
*/
public function mayEdit(EntityAccess $entity, int $userId = null): bool
{
return $this->hasEntityPermission($entity, ACCESS_EDIT, $userId);
}
/**
* Checks if the given entity instance may be deleted.
*/
public function mayDelete(EntityAccess $entity, int $userId = null): bool
{
return $this->hasEntityPermission($entity, ACCESS_DELETE, $userId);
}
/**
* Checks if a certain permission level is granted for the given entity instance.
*/
public function hasEntityPermission(EntityAccess $entity, int $permissionLevel, int $userId = null): bool
{
$objectType = $entity->get_objectType();
$instance = $entity->getKey() . '::';
return $this->permissionApi->hasPermission(
'ZikulaRoutesModule:' . ucfirst($objectType) . ':',
$instance,
$permissionLevel,
$userId
);
}
/**
* Filters a given collection of entities based on different permission checks.
*
* @param array|ArrayCollection $entities The given list of entities
*/
public function filterCollection($objectType, $entities, int $permissionLevel, int $userId = null): array
{
$filteredEntities = [];
foreach ($entities as $routes) {
if (!$this->hasEntityPermission($routes, $permissionLevel, $userId)) {
continue;
}
$filteredEntities[] = $routes;
}
return $filteredEntities;
}
/**
* Checks if a certain permission level is granted for the given object type.
*/
public function hasComponentPermission(string $objectType, int $permissionLevel, int $userId = null): bool
{
return $this->permissionApi->hasPermission(
'ZikulaRoutesModule:' . ucfirst($objectType) . ':',
'::',
$permissionLevel,
$userId
);
}
/**
* Checks if the quick navigation form for the given object type may be used or not.
*/
public function mayUseQuickNav(string $objectType, int $userId = null): bool
{
return $this->hasComponentPermission($objectType, ACCESS_READ, $userId);
}
/**
* Checks if a certain permission level is granted for the application in general.
*/
public function hasPermission(int $permissionLevel, int $userId = null): bool
{
return $this->permissionApi->hasPermission(
'ZikulaRoutesModule::',
'::',
$permissionLevel,
$userId
);
}
/**
* Returns the list of user group ids of the current user.
*
* @return int[] List of group ids
*/
public function getUserGroupIds(): array
{
$isLoggedIn = $this->currentUserApi->isLoggedIn();
if (!$isLoggedIn) {
return [];
}
$groupIds = [];
$groups = $this->currentUserApi->get('groups');
/** @var GroupEntity $group */
foreach ($groups as $group) {
$groupIds[] = $group->getGid();
}
return $groupIds;
}
/**
* Returns the the current user's id.
*/
public function getUserId(): int
{
return (int)$this->currentUserApi->get('uid');
}
/**
* Returns the the current user's entity.
*/
public function getUser(): UserEntity
{
return $this->userRepository->find($this->getUserId());
}
}