~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Factory/PermissionFactory.php

  • Committer: Dan Garner
  • Date: 2016-02-16 14:21:08 UTC
  • mto: This revision was merged to the branch mainline in revision 484.
  • Revision ID: git-v1:63232095626c7ce5aee618d037440309aa4f8e42
UI/Model/Structure for dynamic display groups.
xibosignage/xibo#724

Show diffs side-by-side

added added

removed removed

Lines of Context:
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;
 
29
use Xibo\Helper\Log;
 
30
use Xibo\Helper\Sanitize;
 
31
use Xibo\Storage\PDOConnect;
32
32
 
33
 
/**
34
 
 * Class PermissionFactory
35
 
 * @package Xibo\Factory
36
 
 */
37
33
class PermissionFactory extends BaseFactory
38
34
{
39
35
    /**
40
 
     * Construct a factory
41
 
     * @param StorageServiceInterface $store
42
 
     * @param LogServiceInterface $log
43
 
     * @param SanitizerServiceInterface $sanitizerService
44
 
     */
45
 
    public function __construct($store, $log, $sanitizerService)
46
 
    {
47
 
        $this->setCommonDependencies($store, $log, $sanitizerService);
48
 
    }
49
 
 
50
 
    /**
51
 
     * Create Empty
52
 
     * @return Permission
53
 
     */
54
 
    public function createEmpty()
55
 
    {
56
 
        return new Permission(
57
 
            $this->getStore(),
58
 
            $this->getLog()
59
 
        );
60
 
    }
61
 
 
62
 
    /**
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
71
44
     */
72
 
    public function create($groupId, $entity, $objectId, $view, $edit, $delete)
 
45
    public static function create($groupId, $entity, $objectId, $view, $edit, $delete)
73
46
    {
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]);
76
49
 
77
50
        if (count($results) <= 0)
78
51
            throw new \InvalidArgumentException('Entity not found: ' . $entity);
79
52
 
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;
90
63
 
91
64
    /**
92
65
     * Create a new Permission
93
 
     * @param UserGroupFactory $userGroupFactory
94
66
     * @param string $entity
95
67
     * @param int $objectId
96
68
     * @param int $view
98
70
     * @param int $delete
99
71
     * @return Permission
100
72
     */
101
 
    public function createForEveryone($userGroupFactory, $entity, $objectId, $view, $edit, $delete)
 
73
    public static function createForEveryone($entity, $objectId, $view, $edit, $delete)
102
74
    {
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]);
105
77
 
106
78
        if (count($results) <= 0)
107
79
            throw new \InvalidArgumentException('Entity not found: ' . $entity);
108
80
 
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]
128
99
     */
129
 
    public function createForNewEntity($user, $entity, $objectId, $level, $userGroupFactory)
 
100
    public static function createForNewEntity($user, $entity, $objectId, $level)
130
101
    {
131
102
        $permissions = [];
132
103
 
133
104
        switch ($level) {
134
105
 
135
106
            case 'public':
136
 
                $permissions[] = $this->createForEveryone($userGroupFactory, $entity, $objectId, 1, 0, 0);
137
 
                break;
138
 
 
139
 
            case 'public write':
140
 
                $permissions[] = $this->createForEveryone($userGroupFactory, $entity, $objectId, 1, 1, 0);
 
107
                $permissions[] = PermissionFactory::createForEveryone($entity, $objectId, 1, 0, 0);
141
108
                break;
142
109
 
143
110
            case 'group':
144
111
                foreach ($user->groups as $group) {
145
 
                    $this->create($group->groupId, $entity, $objectId, 1, 0, 0)->save();
146
 
                }
147
 
                break;
148
 
 
149
 
            case 'group write':
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);
 
113
                    $permission->save();
152
114
                }
153
115
                break;
154
116
 
168
130
     * @param int $objectId
169
131
     * @return array[Permission]
170
132
     */
171
 
    public function getByObjectId($entity, $objectId)
 
133
    public static function getByObjectId($entity, $objectId)
172
134
    {
173
135
        $permissions = array();
174
136
 
175
137
        $sql = '
176
 
            SELECT `permissionId`, `groupId`, `view`, `edit`, `delete`, permissionentity.entityId
177
 
              FROM `permission`
178
 
                INNER JOIN `permissionentity`
179
 
                ON `permissionentity`.entityId = permission.entityId
180
 
             WHERE entity = :entity
181
 
                AND objectId = :objectId
182
 
        ';
183
 
 
 
138
SELECT `permissionId`, `groupId`, `view`, `edit`, `delete`, permissionentity.entityId
 
139
  FROM `permission`
 
140
    INNER JOIN `permissionentity`
 
141
    ON `permissionentity`.entityId = permission.entityId
 
142
 WHERE entity = :entity
 
143
    AND objectId = :objectId
 
144
';
184
145
        $params = array('entity' => $entity, 'objectId' => $objectId);
 
146
        Log::sql($sql, $params);
185
147
 
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'];
202
164
 
203
165
    /**
204
166
     * Get All Permissions by Entity ObjectId
205
 
     * @param User $user
206
167
     * @param string $entity
207
168
     * @param int $objectId
208
169
     * @param array[string] $sortOrder
210
171
     * @return array[Permission]
211
172
     * @throws NotFoundException
212
173
     */
213
 
    public function getAllByObjectId($user, $entity, $objectId, $sortOrder = null, $filterBy = null)
 
174
    public static function getAllByObjectId($entity, $objectId, $sortOrder = null, $filterBy = null)
214
175
    {
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));
217
178
 
218
179
        if (count($entityId) <= 0)
219
180
            throw new NotFoundException(__('Entity not found'));
232
193
                 WHERE IsUserSpecific = 0 ';
233
194
 
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) {
238
199
                $body .= '
239
200
                    AND `group`.groupId IN (
240
201
                        SELECT `group`.groupId
245
206
                         WHERE `lkusergroup`.userId = :currentUserId
246
207
                    )
247
208
                    ';
248
 
                $params['currentUserId'] = $user->userId;
 
209
                $params['currentUserId'] = self::getUser()->userId;
249
210
            }
250
211
        }
251
212
 
261
222
                        AND retired = 0 ';
262
223
 
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;
269
229
            }
270
230
            // Group admins can only see users from their groups.
271
 
            else if ($user->userTypeId == 2) {
 
231
            else if (self::getUser()->userTypeId == 2) {
272
232
                $body .= '
273
233
                    AND user.userId IN (
274
234
                        SELECT `otherUserLinks`.userId
281
241
                         WHERE `lkusergroup`.userId = :currentUserId
282
242
                    )
283
243
                ';
284
 
                $params['currentUserId'] = $user->userId;
 
244
                $params['currentUserId'] = self::getUser()->userId;
285
245
            }
286
246
        }
287
247
 
294
254
         WHERE 1 = 1
295
255
        ';
296
256
 
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) . '%';
300
260
        }
301
261
 
302
262
        $order = '';
307
267
 
308
268
        $limit = '';
309
269
        // Paging
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);
312
272
        }
313
273
 
314
274
        $sql = $select . $body . $order . $limit;
315
275
 
316
 
 
317
 
 
318
 
        foreach ($this->getStore()->select($sql, $params) as $row) {
319
 
            $permission = $this->createEmpty();
 
276
        Log::sql($sql, $params);
 
277
 
 
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']);
330
290
 
331
291
            $permissions[] = $permission;
332
292
        }
333
293
 
334
294
        // Paging
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']);
338
298
        }
339
299
 
340
300
        return $permissions;
346
306
     * @param int $groupId
347
307
     * @return array[Permission]
348
308
     */
349
 
    public function getByGroupId($entity, $groupId)
 
309
    public static function getByGroupId($entity, $groupId)
350
310
    {
351
311
        $permissions = array();
352
312
 
362
322
        ';
363
323
        $params = array('entity' => 'Xibo\Entity\\' . $entity, 'groupId' => $groupId);
364
324
 
365
 
 
366
 
 
367
 
        foreach ($this->getStore()->select($sql, $params) as $row) {
368
 
            $permission = $this->createEmpty();
 
325
        Log::sql($sql, $params);
 
326
 
 
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]
389
349
     */
390
 
    public function getByUserId($entity, $userId)
 
350
    public static function getByUserId($entity, $userId)
391
351
    {
392
352
        $permissions = array();
393
353
 
394
354
        $sql = '
395
 
            SELECT `permission`.`permissionId`, `permission`.`groupId`, `permission`.`objectId`, `permission`.`view`, `permission`.`edit`, `permission`.`delete`, permissionentity.entityId
396
 
              FROM `permission`
397
 
                INNER JOIN `permissionentity`
398
 
                ON `permissionentity`.entityId = permission.entityId
399
 
                INNER JOIN `group`
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)
408
 
        ';
 
355
SELECT `permission`.`permissionId`, `permission`.`groupId`, `permission`.`objectId`, `permission`.`view`, `permission`.`edit`, `permission`.`delete`, permissionentity.entityId
 
356
  FROM `permission`
 
357
    INNER JOIN `permissionentity`
 
358
    ON `permissionentity`.entityId = permission.entityId
 
359
    INNER JOIN `group`
 
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)
 
368
';
409
369
        $params = array('entity' => $entity, 'userId' => $userId);
410
370
 
411
 
        foreach ($this->getStore()->select($sql, $params) as $row) {
412
 
            $permission = $this->createEmpty();
 
371
        \Xibo\Helper\Log::sql($sql, $params);
 
372
 
 
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'];
429
391
     * Get Full Permissions
430
392
     * @return Permission
431
393
     */
432
 
    public function getFullPermissions()
 
394
    public static function getFullPermissions()
433
395
    {
434
 
        $permission = $this->createEmpty();
 
396
        $permission = new Permission();
435
397
        $permission->view = 1;
436
398
        $permission->edit = 1;
437
399
        $permission->delete = 1;