~xibo-maintainers/xibo/tempel

« back to all changes in this revision

Viewing changes to lib/Factory/UserGroupFactory.php

  • Committer: Dan Garner
  • Date: 2016-02-12 10:41:25 UTC
  • mto: (454.4.130)
  • mto: This revision was merged to the branch mainline in revision 484.
  • Revision ID: git-v1:ca673f8ea522eac5f311ed779b0fbfeb35a0e4dd
Handle duration changes in XLF generation. Fixed region option factory.
xibosignage/xibo#721

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
namespace Xibo\Factory;
10
10
 
11
11
 
12
 
use Xibo\Entity\User;
13
12
use Xibo\Entity\UserGroup;
14
13
use Xibo\Exception\NotFoundException;
15
 
use Xibo\Service\LogServiceInterface;
16
 
use Xibo\Service\SanitizerServiceInterface;
17
 
use Xibo\Storage\StorageServiceInterface;
 
14
use Xibo\Helper\Log;
 
15
use Xibo\Helper\Sanitize;
 
16
use Xibo\Storage\PDOConnect;
18
17
 
19
 
/**
20
 
 * Class UserGroupFactory
21
 
 * @package Xibo\Factory
22
 
 */
23
18
class UserGroupFactory extends BaseFactory
24
19
{
25
20
    /**
26
 
     * Construct a factory
27
 
     * @param StorageServiceInterface $store
28
 
     * @param LogServiceInterface $log
29
 
     * @param SanitizerServiceInterface $sanitizerService
30
 
     * @param User $user
31
 
     * @param UserFactory $userFactory
32
 
     */
33
 
    public function __construct($store, $log, $sanitizerService, $user, $userFactory)
34
 
    {
35
 
        $this->setCommonDependencies($store, $log, $sanitizerService);
36
 
 
37
 
        $this->setAclDependencies($user, $userFactory);
38
 
    }
39
 
 
40
 
    /**
41
 
     * Create Empty User Group Object
42
 
     * @return UserGroup
43
 
     */
44
 
    public function createEmpty()
45
 
    {
46
 
        return new UserGroup($this->getStore(), $this->getLog(), $this, $this->getUserFactory());
47
 
    }
48
 
 
49
 
    /**
50
 
     * Create User Group
51
 
     * @param $userGroup
52
 
     * @param $libraryQuota
53
 
     * @return UserGroup
54
 
     */
55
 
    public function create($userGroup, $libraryQuota)
56
 
    {
57
 
        $group = $this->createEmpty();
58
 
        $group->group = $userGroup;
59
 
        $group->libraryQuota = $libraryQuota;
60
 
 
61
 
        return $group;
62
 
    }
63
 
 
64
 
    /**
65
21
     * Get by Group Id
66
22
     * @param int $groupId
67
23
     * @return UserGroup
68
24
     * @throws NotFoundException
69
25
     */
70
 
    public function getById($groupId)
 
26
    public static function getById($groupId)
71
27
    {
72
 
        $groups = $this->query(null, ['disableUserCheck' => 1, 'groupId' => $groupId, 'isUserSpecific' => -1]);
 
28
        $groups = UserGroupFactory::query(null, ['disableUserCheck' => 1, 'groupId' => $groupId, 'isUserSpecific' => -1]);
73
29
 
74
30
        if (count($groups) <= 0)
75
31
            throw new NotFoundException(__('Group not found'));
80
36
    /**
81
37
     * Get by Group Name
82
38
     * @param string $group
83
 
     * @param int $isUserSpecific
84
39
     * @return UserGroup
85
40
     * @throws NotFoundException
86
41
     */
87
 
    public function getByName($group, $isUserSpecific = 0)
 
42
    public static function getByName($group)
88
43
    {
89
 
        $groups = $this->query(null, ['disableUserCheck' => 1, 'exactGroup' => $group, 'isUserSpecific' => $isUserSpecific]);
 
44
        $groups = UserGroupFactory::query(null, ['disableUserCheck' => 1, 'group' => $group, 'isUserSpecific' => 0]);
90
45
 
91
46
        if (count($groups) <= 0)
92
47
            throw new NotFoundException(__('Group not found'));
99
54
     * @return UserGroup
100
55
     * @throws NotFoundException
101
56
     */
102
 
    public function getEveryone()
 
57
    public static function getEveryone()
103
58
    {
104
 
        $groups = $this->query(null, ['disableUserCheck' => 1, 'isEveryone' => 1]);
 
59
        $groups = UserGroupFactory::query(null, ['disableUserCheck' => 1, 'isEveryone' => 1]);
105
60
 
106
61
        if (count($groups) <= 0)
107
62
            throw new NotFoundException(__('Group not found'));
110
65
    }
111
66
 
112
67
    /**
113
 
     * Get isSystemNotification Group
114
 
     * @return UserGroup[]
115
 
     */
116
 
    public function getSystemNotificationGroups()
117
 
    {
118
 
        return $this->query(null, ['disableUserCheck' => 1, 'isSystemNotification' => 1, 'isUserSpecific' => -1]);
119
 
    }
120
 
 
121
 
    /**
122
 
     * Get isDisplayNotification Group
123
 
     * @param int|null $displayGroupId Optionally provide a displayGroupId to restrict to view permissions.
124
 
     * @return UserGroup[]
125
 
     */
126
 
    public function getDisplayNotificationGroups($displayGroupId = null)
127
 
    {
128
 
        return $this->query(null, [
129
 
            'disableUserCheck' => 1,
130
 
            'isDisplayNotification' => 1,
131
 
            'isUserSpecific' => -1,
132
 
            'displayGroupId' => $displayGroupId
133
 
        ]);
134
 
    }
135
 
 
136
 
    /**
137
68
     * Get by User Id
138
69
     * @param int $userId
139
70
     * @return array[UserGroup]
140
71
     * @throws NotFoundException
141
72
     */
142
 
    public function getByUserId($userId)
143
 
    {
144
 
        return $this->query(null, ['disableUserCheck' => 1, 'userId' => $userId, 'isUserSpecific' => 0]);
145
 
    }
146
 
 
147
 
    /**
148
 
     * Get User Groups assigned to Notifications
149
 
     * @param int $notificationId
150
 
     * @return array[UserGroup]
151
 
     */
152
 
    public function getByNotificationId($notificationId)
153
 
    {
154
 
        return $this->query(null, ['disableUserCheck' => 1, 'notificationId' => $notificationId, 'isUserSpecific' => -1]);
155
 
    }
156
 
 
157
 
    /**
158
 
     * Get by Display Group
159
 
     * @param int $displayGroupId
160
 
     * @return UserGroup[]
161
 
     */
162
 
    public function getByDisplayGroupId($displayGroupId)
163
 
    {
164
 
        return $this->query(null, ['disableUserCheck' => 1, 'displayGroupId' => $displayGroupId]);
 
73
    public static function getByUserId($userId)
 
74
    {
 
75
        return UserGroupFactory::query(null, ['disableUserCheck' => 1, 'userId' => $userId, 'isUserSpecific' => 0]);
165
76
    }
166
77
 
167
78
    /**
170
81
     * @return array[UserGroup]
171
82
     * @throws \Exception
172
83
     */
173
 
    public function query($sortOrder = null, $filterBy = [])
 
84
    public static function query($sortOrder = null, $filterBy = null)
174
85
    {
175
86
        $entries = array();
176
87
        $params = array();
177
88
 
178
 
        if ($sortOrder === null)
179
 
            $sortOrder = ['`group`'];
180
 
 
181
89
        try {
182
90
            $select = '
183
91
            SELECT      `group`.group,
192
100
                                ';
193
101
            }
194
102
 
195
 
            if (DBVERSION >= 124) {
196
 
                                $select .= '
197
 
                                    ,
198
 
                                    `group`.isSystemNotification
199
 
                                ';
200
 
            }
201
 
 
202
 
            if (DBVERSION >= 134) {
203
 
                                $select .= '
204
 
                                    ,
205
 
                                    `group`.isDisplayNotification
206
 
                                ';
207
 
            }
208
 
 
209
103
            $body = '
210
104
              FROM `group`
211
105
             WHERE 1 = 1
212
106
            ';
213
107
 
214
108
            // Permissions
215
 
            if ($this->getSanitizer()->getCheckbox('disableUserCheck', 0, $filterBy) == 0) {
 
109
            if (Sanitize::getCheckbox('disableUserCheck', 0, $filterBy) == 0) {
216
110
                // Normal users can only see their group
217
 
                if ($this->getUser()->userTypeId != 1) {
 
111
                if (self::getUser()->userTypeId != 1) {
218
112
                    $body .= '
219
113
                    AND `group`.groupId IN (
220
114
                        SELECT `group`.groupId
225
119
                         WHERE `lkusergroup`.userId = :currentUserId
226
120
                    )
227
121
                    ';
228
 
                    $params['currentUserId'] = $this->getUser()->userId;
 
122
                    $params['currentUserId'] = self::getUser()->userId;
229
123
                }
230
124
            }
231
125
 
232
126
            // Filter by Group Id
233
 
            if ($this->getSanitizer()->getInt('groupId', $filterBy) !== null) {
 
127
            if (Sanitize::getInt('groupId', $filterBy) !== null) {
234
128
                $body .= ' AND `group`.groupId = :groupId ';
235
 
                $params['groupId'] = $this->getSanitizer()->getInt('groupId', $filterBy);
 
129
                $params['groupId'] = Sanitize::getInt('groupId', $filterBy);
236
130
            }
237
131
 
238
132
            // Filter by Group Name
239
 
            if ($this->getSanitizer()->getString('group', $filterBy) != null) {
240
 
                // Convert into commas
241
 
                foreach (explode(',', $this->getSanitizer()->getString('group', $filterBy)) as $term) {
242
 
 
243
 
                    if (empty(trim($term)))
244
 
                        continue;
245
 
 
246
 
                    // convert into a space delimited array
247
 
                    $names = explode(' ', $term);
248
 
 
249
 
                    $i = 0;
250
 
                    foreach ($names as $searchName) {
251
 
                        $i++;
252
 
                        // Not like, or like?
253
 
                        if (substr($searchName, 0, 1) == '-') {
254
 
                            $body .= " AND `group`.group NOT RLIKE (:group$i) ";
255
 
                            $params['group' . $i] = ltrim(($searchName), '-');
256
 
                        } else {
257
 
                            $body .= " AND `group`.group RLIKE (:group$i) ";
258
 
                            $params['group' . $i] = $searchName;
259
 
                        }
260
 
                    }
261
 
                }
262
 
            }
263
 
 
264
 
            if ($this->getSanitizer()->getString('exactGroup', $filterBy) != null) {
265
 
                $body .= ' AND `group`.group = :exactGroup ';
266
 
                $params['exactGroup'] = $this->getSanitizer()->getString('exactGroup', $filterBy);
 
133
            if (Sanitize::getString('group', $filterBy) != null) {
 
134
                $body .= ' AND `group`.group = :group ';
 
135
                $params['group'] = Sanitize::getString('group', $filterBy);
267
136
            }
268
137
 
269
138
            // Filter by User Id
270
 
            if ($this->getSanitizer()->getInt('userId', $filterBy) !== null) {
 
139
            if (Sanitize::getInt('userId', $filterBy) !== null) {
271
140
                $body .= ' AND `group`.groupId IN (SELECT groupId FROM `lkusergroup` WHERE userId = :userId) ';
272
 
                $params['userId'] = $this->getSanitizer()->getInt('userId', $filterBy);
 
141
                $params['userId'] = Sanitize::getInt('userId', $filterBy);
273
142
            }
274
143
 
275
 
            if ($this->getSanitizer()->getInt('isUserSpecific', $filterBy) != -1) {
 
144
            if (Sanitize::getInt('isUserSpecific', $filterBy) != -1) {
276
145
                $body .= ' AND isUserSpecific = :isUserSpecific ';
277
 
                $params['isUserSpecific'] = $this->getSanitizer()->getInt('isUserSpecific', 0, $filterBy);
 
146
                $params['isUserSpecific'] = Sanitize::getInt('isUserSpecific', 0, $filterBy);
278
147
            }
279
148
 
280
 
            if ($this->getSanitizer()->getInt('isEveryone', $filterBy) != -1) {
 
149
            if (Sanitize::getInt('isEveryone', $filterBy) != -1) {
281
150
                $body .= ' AND isEveryone = :isEveryone ';
282
 
                $params['isEveryone'] = $this->getSanitizer()->getInt('isEveryone', 0, $filterBy);
283
 
            }
284
 
 
285
 
            if ($this->getSanitizer()->getInt('isSystemNotification', $filterBy) !== null) {
286
 
                $body .= ' AND isSystemNotification = :isSystemNotification ';
287
 
                $params['isSystemNotification'] = $this->getSanitizer()->getInt('isSystemNotification', $filterBy);
288
 
            }
289
 
 
290
 
            if (DBVERSION >= 134 && $this->getSanitizer()->getInt('isDisplayNotification', $filterBy) !== null) {
291
 
                $body .= ' AND isDisplayNotification = :isDisplayNotification ';
292
 
                $params['isDisplayNotification'] = $this->getSanitizer()->getInt('isDisplayNotification', $filterBy);
293
 
            }
294
 
 
295
 
            if ($this->getSanitizer()->getInt('notificationId', $filterBy) !== null) {
296
 
                $body .= ' AND `group`.groupId IN (SELECT groupId FROM `lknotificationgroup` WHERE notificationId = :notificationId) ';
297
 
                $params['notificationId'] = $this->getSanitizer()->getInt('notificationId', $filterBy);
298
 
            }
299
 
 
300
 
            if ($this->getSanitizer()->getInt('displayGroupId', $filterBy) !== null) {
301
 
                $body .= ' 
302
 
                    AND `group`.groupId IN (
303
 
                        SELECT DISTINCT `permission`.groupId
304
 
                          FROM `permission`
305
 
                            INNER JOIN `permissionentity`
306
 
                            ON `permissionentity`.entityId = permission.entityId
307
 
                                AND `permissionentity`.entity = \'Xibo\\Entity\\DisplayGroup\'
308
 
                         WHERE `permission`.objectId = :displayGroupId
309
 
                            AND `permission`.view = 1
310
 
                    )
311
 
                ';
312
 
                $params['displayGroupId'] = $this->getSanitizer()->getInt('displayGroupId', $filterBy);
 
151
                $params['isEveryone'] = Sanitize::getInt('isEveryone', 0, $filterBy);
313
152
            }
314
153
 
315
154
            // Sorting?
319
158
 
320
159
            $limit = '';
321
160
            // Paging
322
 
            if ($filterBy !== null && $this->getSanitizer()->getInt('start', $filterBy) !== null && $this->getSanitizer()->getInt('length', $filterBy) !== null) {
323
 
                $limit = ' LIMIT ' . intval($this->getSanitizer()->getInt('start', $filterBy), 0) . ', ' . $this->getSanitizer()->getInt('length', 10, $filterBy);
 
161
            if (Sanitize::getInt('start', $filterBy) !== null && Sanitize::getInt('length', $filterBy) !== null) {
 
162
                $limit = ' LIMIT ' . intval(Sanitize::getInt('start'), 0) . ', ' . Sanitize::getInt('length', 10);
324
163
            }
325
164
 
326
165
            $sql = $select . $body . $order . $limit;
327
166
 
328
 
            foreach ($this->getStore()->select($sql, $params) as $row) {
329
 
                $entries[] = $this->createEmpty()->hydrate($row);
 
167
            Log::sql($sql, $params);
 
168
 
 
169
            foreach (PDOConnect::select($sql, $params) as $row) {
 
170
                $entries[] = (new UserGroup())->hydrate($row);
330
171
            }
331
172
 
332
173
            // Paging
333
174
            if ($limit != '' && count($entries) > 0) {
334
 
                $results = $this->getStore()->select('SELECT COUNT(*) AS total ' . $body, $params);
335
 
                $this->_countLast = intval($results[0]['total']);
 
175
                $results = PDOConnect::select('SELECT COUNT(*) AS total ' . $body, $params);
 
176
                self::$_countLast = intval($results[0]['total']);
336
177
            }
337
178
 
338
179
            return $entries;
339
180
 
340
181
        } catch (\Exception $e) {
341
182
 
342
 
            $this->getLog()->error($e);
 
183
            Log::error($e);
343
184
 
344
185
            throw $e;
345
186
        }