26
26
use Xibo\Entity\Permission;
27
27
use Xibo\Entity\User;
28
28
use Xibo\Exception\NotFoundException;
29
use Xibo\Service\LogServiceInterface;
30
use Xibo\Service\SanitizerServiceInterface;
31
use Xibo\Storage\StorageServiceInterface;
30
use Xibo\Helper\Sanitize;
31
use Xibo\Storage\PDOConnect;
34
* Class PermissionFactory
35
* @package Xibo\Factory
37
33
class PermissionFactory extends BaseFactory
41
* @param StorageServiceInterface $store
42
* @param LogServiceInterface $log
43
* @param SanitizerServiceInterface $sanitizerService
45
public function __construct($store, $log, $sanitizerService)
47
$this->setCommonDependencies($store, $log, $sanitizerService);
54
public function createEmpty()
56
return new Permission(
63
36
* Create a new Permission
64
37
* @param int $groupId
65
38
* @param string $entity
69
42
* @param int $delete
70
43
* @return Permission
72
public function create($groupId, $entity, $objectId, $view, $edit, $delete)
45
public static function create($groupId, $entity, $objectId, $view, $edit, $delete)
74
47
// Lookup the entityId
75
$results = $this->getStore()->select('SELECT entityId FROM permissionentity WHERE entity = :entity', ['entity' => $entity]);
48
$results = PDOConnect::select('SELECT entityId FROM permissionentity WHERE entity = :entity', ['entity' => $entity]);
77
50
if (count($results) <= 0)
78
51
throw new \InvalidArgumentException('Entity not found: ' . $entity);
80
$permission = $this->createEmpty();
53
$permission = new Permission();
81
54
$permission->groupId = $groupId;
82
55
$permission->entityId = $results[0]['entityId'];
83
56
$permission->objectId = $objectId;
98
70
* @param int $delete
99
71
* @return Permission
101
public function createForEveryone($userGroupFactory, $entity, $objectId, $view, $edit, $delete)
73
public static function createForEveryone($entity, $objectId, $view, $edit, $delete)
103
75
// Lookup the entityId
104
$results = $this->getStore()->select('SELECT entityId FROM permissionentity WHERE entity = :entity', ['entity' => $entity]);
76
$results = PDOConnect::select('SELECT entityId FROM permissionentity WHERE entity = :entity', ['entity' => $entity]);
106
78
if (count($results) <= 0)
107
79
throw new \InvalidArgumentException('Entity not found: ' . $entity);
109
$permission = $this->createEmpty();
110
$permission->groupId = $userGroupFactory->getEveryone()->groupId;
81
$permission = new Permission();
82
$permission->groupId = UserGroupFactory::getEveryone()->groupId;
111
83
$permission->entityId = $results[0]['entityId'];
112
84
$permission->objectId = $objectId;
113
85
$permission->view =$view;
123
95
* @param string $entity
124
96
* @param int $objectId
125
97
* @param string $level
126
* @param UserGroupFactory $userGroupFactory
127
98
* @return array[Permission]
129
public function createForNewEntity($user, $entity, $objectId, $level, $userGroupFactory)
100
public static function createForNewEntity($user, $entity, $objectId, $level)
131
102
$permissions = [];
133
104
switch ($level) {
136
$permissions[] = $this->createForEveryone($userGroupFactory, $entity, $objectId, 1, 0, 0);
140
$permissions[] = $this->createForEveryone($userGroupFactory, $entity, $objectId, 1, 1, 0);
107
$permissions[] = PermissionFactory::createForEveryone($entity, $objectId, 1, 0, 0);
144
111
foreach ($user->groups as $group) {
145
$this->create($group->groupId, $entity, $objectId, 1, 0, 0)->save();
150
foreach ($user->groups as $group) {
151
$this->create($group->groupId, $entity, $objectId, 1, 1, 0)->save();
112
$permission = PermissionFactory::create($group->groupId, $entity, $objectId, 1, 0, 0);
168
130
* @param int $objectId
169
131
* @return array[Permission]
171
public function getByObjectId($entity, $objectId)
133
public static function getByObjectId($entity, $objectId)
173
135
$permissions = array();
176
SELECT `permissionId`, `groupId`, `view`, `edit`, `delete`, permissionentity.entityId
178
INNER JOIN `permissionentity`
179
ON `permissionentity`.entityId = permission.entityId
180
WHERE entity = :entity
181
AND objectId = :objectId
138
SELECT `permissionId`, `groupId`, `view`, `edit`, `delete`, permissionentity.entityId
140
INNER JOIN `permissionentity`
141
ON `permissionentity`.entityId = permission.entityId
142
WHERE entity = :entity
143
AND objectId = :objectId
184
145
$params = array('entity' => $entity, 'objectId' => $objectId);
146
Log::sql($sql, $params);
186
foreach ($this->getStore()->select($sql, $params) as $row) {
187
$permission = $this->createEmpty();
148
foreach (PDOConnect::select($sql, $params) as $row) {
149
$permission = new Permission();
188
150
$permission->permissionId = $row['permissionId'];
189
151
$permission->groupId = $row['groupId'];
190
152
$permission->view = $row['view'];
210
171
* @return array[Permission]
211
172
* @throws NotFoundException
213
public function getAllByObjectId($user, $entity, $objectId, $sortOrder = null, $filterBy = null)
174
public static function getAllByObjectId($entity, $objectId, $sortOrder = null, $filterBy = null)
215
176
// Look up the entityId for any add operation that might occur
216
$entityId = $this->getStore()->select('SELECT entityId FROM permissionentity WHERE entity = :entity', array('entity' => $entity));
177
$entityId = PDOConnect::select('SELECT entityId FROM permissionentity WHERE entity = :entity', array('entity' => $entity));
218
179
if (count($entityId) <= 0)
219
180
throw new NotFoundException(__('Entity not found'));
232
193
WHERE IsUserSpecific = 0 ';
234
195
// Permissions for the group section
235
if ($this->getSanitizer()->getCheckbox('disableUserCheck', 0, $filterBy) == 0) {
196
if (Sanitize::getCheckbox('disableUserCheck', 0, $filterBy) == 0) {
236
197
// Normal users can only see their group
237
if ($user->userTypeId != 1) {
198
if (self::getUser()->userTypeId != 1) {
239
200
AND `group`.groupId IN (
240
201
SELECT `group`.groupId
261
222
AND retired = 0 ';
263
224
// Permissions for the user section
264
if ($this->getSanitizer()->getCheckbox('disableUserCheck', 0, $filterBy) == 0) {
225
if (Sanitize::getCheckbox('disableUserCheck', 0, $filterBy) == 0) {
265
226
// Normal users can only see themselves
266
if ($user->userTypeId == 3) {
267
$body .= ' AND `user`.userId = :currentUserId ';
268
$params['currentUserId'] = $user->userId;
227
if (self::getUser()->userTypeId == 3) {
228
$filterBy['userId'] = self::getUser()->userId;
270
230
// Group admins can only see users from their groups.
271
else if ($user->userTypeId == 2) {
231
else if (self::getUser()->userTypeId == 2) {
273
233
AND user.userId IN (
274
234
SELECT `otherUserLinks`.userId
297
if ($this->getSanitizer()->getString('name', $filterBy) != null) {
257
if (Sanitize::getString('name', $filterBy) != null) {
298
258
$body .= ' AND joinedGroup.group LIKE :name ';
299
$params['name'] = '%' . $this->getSanitizer()->getString('name', $filterBy) . '%';
259
$params['name'] = '%' . Sanitize::getString('name', $filterBy) . '%';
310
if ($filterBy !== null && $this->getSanitizer()->getInt('start', $filterBy) !== null && $this->getSanitizer()->getInt('length', $filterBy) !== null) {
311
$limit = ' LIMIT ' . intval($this->getSanitizer()->getInt('start', $filterBy), 0) . ', ' . $this->getSanitizer()->getInt('length', 10, $filterBy);
270
if (Sanitize::getInt('start', $filterBy) !== null && Sanitize::getInt('length', $filterBy) !== null) {
271
$limit = ' LIMIT ' . intval(Sanitize::getInt('start'), 0) . ', ' . Sanitize::getInt('length', 10);
314
274
$sql = $select . $body . $order . $limit;
318
foreach ($this->getStore()->select($sql, $params) as $row) {
319
$permission = $this->createEmpty();
276
Log::sql($sql, $params);
278
foreach (PDOConnect::select($sql, $params) as $row) {
279
$permission = new Permission();
320
280
$permission->permissionId = $row['permissionId'];
321
281
$permission->groupId = $row['groupId'];
322
282
$permission->view = $row['view'];
326
286
$permission->entity = $entity;
327
287
$permission->entityId = $entityId;
328
288
$permission->isUser = $row['isuserspecific'];
329
$permission->group = $this->getSanitizer()->string($row['group']);
289
$permission->group = \Xibo\Helper\Sanitize::string($row['group']);
331
291
$permissions[] = $permission;
335
295
if ($limit != '' && count($permissions) > 0) {
336
$results = $this->getStore()->select('SELECT COUNT(*) AS total ' . $body, $params);
337
$this->_countLast = intval($results[0]['total']);
296
$results = PDOConnect::select('SELECT COUNT(*) AS total ' . $body, $params);
297
self::$_countLast = intval($results[0]['total']);
340
300
return $permissions;
363
323
$params = array('entity' => 'Xibo\Entity\\' . $entity, 'groupId' => $groupId);
367
foreach ($this->getStore()->select($sql, $params) as $row) {
368
$permission = $this->createEmpty();
325
Log::sql($sql, $params);
327
foreach (PDOConnect::select($sql, $params) as $row) {
328
$permission = new Permission();
369
329
$permission->permissionId = $row['permissionId'];
370
330
$permission->groupId = $row['groupId'];
371
331
$permission->view = $row['view'];
387
347
* @param int $userId
388
348
* @return array[Permission]
390
public function getByUserId($entity, $userId)
350
public static function getByUserId($entity, $userId)
392
352
$permissions = array();
395
SELECT `permission`.`permissionId`, `permission`.`groupId`, `permission`.`objectId`, `permission`.`view`, `permission`.`edit`, `permission`.`delete`, permissionentity.entityId
397
INNER JOIN `permissionentity`
398
ON `permissionentity`.entityId = permission.entityId
400
ON `group`.groupId = `permission`.groupId
401
LEFT OUTER JOIN `lkusergroup`
402
ON `lkusergroup`.groupId = `group`.groupId
403
LEFT OUTER JOIN `user`
404
ON lkusergroup.UserID = `user`.UserID
405
AND `user`.userId = :userId
406
WHERE entity = :entity
407
AND (`user`.userId IS NOT NULL OR `group`.IsEveryone = 1)
355
SELECT `permission`.`permissionId`, `permission`.`groupId`, `permission`.`objectId`, `permission`.`view`, `permission`.`edit`, `permission`.`delete`, permissionentity.entityId
357
INNER JOIN `permissionentity`
358
ON `permissionentity`.entityId = permission.entityId
360
ON `group`.groupId = `permission`.groupId
361
LEFT OUTER JOIN `lkusergroup`
362
ON `lkusergroup`.groupId = `group`.groupId
363
LEFT OUTER JOIN `user`
364
ON lkusergroup.UserID = `user`.UserID
365
AND `user`.userId = :userId
366
WHERE entity = :entity
367
AND (`user`.userId IS NOT NULL OR `group`.IsEveryone = 1)
409
369
$params = array('entity' => $entity, 'userId' => $userId);
411
foreach ($this->getStore()->select($sql, $params) as $row) {
412
$permission = $this->createEmpty();
371
\Xibo\Helper\Log::sql($sql, $params);
373
foreach (PDOConnect::select($sql, $params) as $row) {
374
$permission = new Permission();
413
375
$permission->permissionId = $row['permissionId'];
414
376
$permission->groupId = $row['groupId'];
415
377
$permission->view = $row['view'];