Completed
Push — sf_cache ( 0e8aea...6d5e03 )
by André
38:18 queued 11:23
created

UserHandler::loadRoleAssignmentsByGroupId()   B

Complexity

Conditions 6
Paths 14

Size

Total Lines 37
Code Lines 22

Duplication

Lines 5
Ratio 13.51 %

Importance

Changes 0
Metric Value
cc 6
eloc 22
nc 14
nop 2
dl 5
loc 37
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing a User Handler impl.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Persistence\Cache;
10
11
use eZ\Publish\SPI\Persistence\User\Handler as UserHandlerInterface;
12
use eZ\Publish\SPI\Persistence\User;
13
use eZ\Publish\SPI\Persistence\User\Role;
14
use eZ\Publish\SPI\Persistence\User\RoleCreateStruct;
15
use eZ\Publish\SPI\Persistence\User\RoleUpdateStruct;
16
use eZ\Publish\SPI\Persistence\User\Policy;
17
18
/**
19
 * Cache handler for user module.
20
 */
21
class UserHandler extends AbstractHandler implements UserHandlerInterface
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function create(User $user)
27
    {
28
        $this->logger->logCall(__METHOD__, array('struct' => $user));
29
        $return = $this->persistenceHandler->userHandler()->create($user);
30
31
        // Clear corresponding content cache as creation of the User changes it's external data
32
        $this->cache->invalidateTags(['content-fields-' . $user->id]);
33
34
        return $return;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 View Code Duplication
    public function load($userId)
41
    {
42
        $cacheItem = $this->cache->getItem("ez-user-${userId}");
43
        if ($cacheItem->isHit()) {
44
            return $cacheItem->get();
45
        }
46
47
        $this->logger->logCall(__METHOD__, array('user' => $userId));
48
        $user = $this->persistenceHandler->userHandler()->load($userId);
49
50
        $cacheItem->set($user);
51
        $cacheItem->tag(['content-' . $user->id, 'user-' . $user->id]);
52
        $this->cache->save($cacheItem);
53
54
        return $user;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 View Code Duplication
    public function loadByLogin($login)
61
    {
62
        $cacheItem = $this->cache->getItem("ez-user-${login}-by-login");
63
        if ($cacheItem->isHit()) {
64
            return $cacheItem->get();
65
        }
66
67
        $this->logger->logCall(__METHOD__, array('user' => $login));
68
        $user = $this->persistenceHandler->userHandler()->loadByLogin($login);
69
70
        $cacheItem->set($user);
71
        $cacheItem->tag(['content-' . $user->id, 'user-' . $user->id]);
72
        $this->cache->save($cacheItem);
73
74
        return $user;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 View Code Duplication
    public function loadByEmail($email)
81
    {
82
        $cacheItem = $this->cache->getItem('ez-user-' . str_replace('@', '§', $email) . '-by-email');
83
        if ($cacheItem->isHit()) {
84
            return $cacheItem->get();
85
        }
86
87
        $this->logger->logCall(__METHOD__, array('email' => $email));
88
        $users = $this->persistenceHandler->userHandler()->loadByEmail($email);
89
90
        $cacheItem->set($users);
91
        $cacheTags = [];
92
        foreach ($users as $user) {
93
            $cacheTags[] = 'content-' . $user->id;
94
            $cacheTags[] = 'user-' . $user->id;
95
        }
96
        $cacheItem->tag($cacheTags);
97
        $this->cache->save($cacheItem);
98
99
        return $users;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105 View Code Duplication
    public function update(User $user)
106
    {
107
        $this->logger->logCall(__METHOD__, array('struct' => $user));
108
        $return = $this->persistenceHandler->userHandler()->update($user);
109
110
        // Clear corresponding content cache as update of the User changes it's external data
111
        $this->cache->invalidateTags(['content-fields-' . $user->id, 'user-' . $user->id]);
112
113
        return $return;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function delete($userId)
120
    {
121
        $this->logger->logCall(__METHOD__, array('user' => $userId));
122
        $return = $this->persistenceHandler->userHandler()->delete($userId);
123
124
        // user id == content id == group id
125
        $this->cache->invalidateTags(['content-fields-' . $userId, 'user-' . $userId]);
126
127
        return $return;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function createRole(RoleCreateStruct $createStruct)
134
    {
135
        $this->logger->logCall(__METHOD__, array('struct' => $createStruct));
136
137
        return $this->persistenceHandler->userHandler()->createRole($createStruct);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function createRoleDraft($roleId)
144
    {
145
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
146
147
        return $this->persistenceHandler->userHandler()->createRoleDraft($roleId);
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function loadRole($roleId, $status = Role::STATUS_DEFINED)
154
    {
155
        if ($status !== Role::STATUS_DEFINED) {
156
            $this->logger->logCall(__METHOD__, array('role' => $roleId));
157
158
            return $this->persistenceHandler->userHandler()->loadRole($roleId, $status);
159
        }
160
161
        $cacheItem = $this->cache->getItem("ez-role-${roleId}");
162
        if ($cacheItem->isHit()) {
163
            return $cacheItem->get();
164
        }
165
166
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
167
        $role = $this->persistenceHandler->userHandler()->loadRole($roleId, $status);
168
169
        $cacheItem->set($role);
170
        $cacheItem->tag(['role-' . $role->id]);
171
        $this->cache->save($cacheItem);
172
173
        return $role;
174
    }
175
176
    /**
177
     * {@inheritdoc}
178
     */
179
    public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED)
180
    {
181
        if ($status !== Role::STATUS_DEFINED) {
182
            $this->logger->logCall(__METHOD__, array('role' => $identifier));
183
184
            return $this->persistenceHandler->userHandler()->loadRoleByIdentifier($identifier, $status);
185
        }
186
187
        $cacheItem = $this->cache->getItem("ez-role-${identifier}-by-identifier");
188
        if ($cacheItem->isHit()) {
189
            return $cacheItem->get();
190
        }
191
192
        $this->logger->logCall(__METHOD__, array('role' => $identifier));
193
        $role = $this->persistenceHandler->userHandler()->loadRoleByIdentifier($identifier, $status);
194
195
        $cacheItem->set($role);
196
        $cacheItem->tag(['role-' . $role->id]);
197
        $this->cache->save($cacheItem);
198
199
        return $role;
200
    }
201
202
    /**
203
     * {@inheritdoc}
204
     */
205
    public function loadRoleDraftByRoleId($roleId)
206
    {
207
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
208
209
        return $this->persistenceHandler->userHandler()->loadRoleDraftByRoleId($roleId);
210
    }
211
212
    /**
213
     * {@inheritdoc}
214
     */
215
    public function loadRoles()
216
    {
217
        $this->logger->logCall(__METHOD__);
218
219
        return $this->persistenceHandler->userHandler()->loadRoles();
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225 View Code Duplication
    public function loadRoleAssignment($roleAssignmentId)
226
    {
227
        $cacheItem = $this->cache->getItem("ez-role-assignment-${roleAssignmentId}");
228
        if ($cacheItem->isHit()) {
229
            return $cacheItem->get();
230
        }
231
232
        $this->logger->logCall(__METHOD__, array('assignment' => $roleAssignmentId));
233
        $roleAssignment = $this->persistenceHandler->userHandler()->loadRoleAssignment($roleAssignmentId);
234
235
        $cacheItem->set($roleAssignment);
236
        $cacheItem->tag(
237
            [
238
                'role-assignment-group-list-' . $roleAssignment->contentId,
239
                'role-assignment-role-list-' . $roleAssignment->roleId,
240
                'role-assignment-' . $roleAssignmentId,
241
            ]
242
        );
243
        $this->cache->save($cacheItem);
244
245
        return $roleAssignment;
246
    }
247
248
    /**
249
     * {@inheritdoc}
250
     */
251 View Code Duplication
    public function loadRoleAssignmentsByRoleId($roleId)
252
    {
253
        $cacheItem = $this->cache->getItem("ez-role-assignment-${roleId}-by-role");
254
        if ($cacheItem->isHit()) {
255
            return $cacheItem->get();
256
        }
257
258
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
259
        $roleAssignments = $this->persistenceHandler->userHandler()->loadRoleAssignmentsByRoleId($roleId);
260
261
        $cacheItem->set($roleAssignments);
262
        $cacheTags = ['role-assignment-role-list-' . $roleId];
263
        foreach ($roleAssignments as $roleAssignment) {
264
            $cacheTags[] = 'role-assignment-' . $roleAssignment->id;
265
            $cacheTags[] = 'role-assignment-group-list-' . $roleAssignment->contentId;
266
        }
267
        $cacheItem->tag($cacheTags);
268
        $this->cache->save($cacheItem);
269
270
        return $roleAssignments;
271
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276
    public function loadRoleAssignmentsByGroupId($groupId, $inherit = false)
277
    {
278
        if ($inherit) {
279
            $cacheItem = $this->cache->getItem("ez-role-assignment-${groupId}-by-group-inherited");
280
        } else {
281
            $cacheItem = $this->cache->getItem("ez-role-assignment-${groupId}-by-group");
282
        }
283
284
        if ($cacheItem->isHit()) {
285
            return $cacheItem->get();
286
        }
287
288
        $this->logger->logCall(__METHOD__, array('group' => $groupId, 'inherit' => $inherit));
289
        $roleAssignments = $this->persistenceHandler->userHandler()->loadRoleAssignmentsByGroupId($groupId, $inherit);
290
291
        $cacheItem->set($roleAssignments);
292
        // Tag below is for empty results,  non empty it might have duplicated tags but cache will reduce those.
293
        $cacheTags = ['role-assignment-group-list-' . $groupId];
294
        foreach ($roleAssignments as $roleAssignment) {
295
            $cacheTags[] = 'role-assignment-' . $roleAssignment->id;
296
            $cacheTags[] = 'role-assignment-group-list-' . $roleAssignment->contentId;
297
            $cacheTags[] = 'role-assignment-role-list-' . $roleAssignment->roleId;
298
        }
299
300
        // To make sure tree operations affecting this can clear the permission cache
301
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($groupId);
302 View Code Duplication
        foreach ($locations as $location) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
303
            foreach (explode('/', trim($location->pathString, '/')) as $pathId) {
304
                $cacheTags[] = 'location-path-' . $pathId;
305
            }
306
        }
307
308
        $cacheItem->tag($cacheTags);
309
        $this->cache->save($cacheItem);
310
311
        return $roleAssignments;
312
    }
313
314
    /**
315
     * {@inheritdoc}
316
     */
317 View Code Duplication
    public function updateRole(RoleUpdateStruct $struct)
318
    {
319
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
320
        $this->persistenceHandler->userHandler()->updateRole($struct);
321
322
        $this->cache->invalidateTags(['role-' . $struct->id]);
323
    }
324
325
    /**
326
     * {@inheritdoc}
327
     */
328
    public function deleteRole($roleId, $status = Role::STATUS_DEFINED)
329
    {
330
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
331
        $return = $this->persistenceHandler->userHandler()->deleteRole($roleId, $status);
332
333
        if ($status === Role::STATUS_DEFINED) {
334
            $this->cache->invalidateTags(['role-' . $roleId, 'role-assignment-role-list-' . $roleId]);
335
        }
336
337
        return $return;
338
    }
339
340
    /**
341
     * {@inheritdoc}
342
     */
343
    public function publishRoleDraft($roleDraftId)
344
    {
345
        $this->logger->logCall(__METHOD__, array('role' => $roleDraftId));
346
        $userHandler = $this->persistenceHandler->userHandler();
347
        $roleDraft = $userHandler->loadRole($roleDraftId, Role::STATUS_DRAFT);
348
        $return = $userHandler->publishRoleDraft($roleDraftId);
349
350
        // If there was a original role for the draft, then we clean cache for it
351
        if ($roleDraft->originalId > -1) {
352
            $this->cache->invalidateTags(['role-' . $roleDraft->originalId]);
353
        }
354
355
        return $return;
356
    }
357
358
    /**
359
     * {@inheritdoc}
360
     */
361
    public function addPolicyByRoleDraft($roleId, Policy $policy)
362
    {
363
        $this->logger->logCall(__METHOD__, array('role' => $roleId, 'struct' => $policy));
364
365
        return $this->persistenceHandler->userHandler()->addPolicyByRoleDraft($roleId, $policy);
366
    }
367
368
    /**
369
     * {@inheritdoc}
370
     */
371
    public function addPolicy($roleId, Policy $policy)
372
    {
373
        $this->logger->logCall(__METHOD__, array('role' => $roleId, 'struct' => $policy));
374
        $return = $this->persistenceHandler->userHandler()->addPolicy($roleId, $policy);
375
376
        $this->cache->invalidateTags(['role-' . $roleId]);
377
378
        return $return;
379
    }
380
381
    /**
382
     * {@inheritdoc}
383
     */
384 View Code Duplication
    public function updatePolicy(Policy $policy)
385
    {
386
        $this->logger->logCall(__METHOD__, array('struct' => $policy));
387
        $return = $this->persistenceHandler->userHandler()->updatePolicy($policy);
388
389
        $this->cache->invalidateTags(['policy-' . $policy->id, 'role-' . $policy->roleId]);
390
391
        return $return;
392
    }
393
394
    /**
395
     * {@inheritdoc}
396
     */
397 View Code Duplication
    public function deletePolicy($policyId, $roleId)
398
    {
399
        $this->logger->logCall(__METHOD__, array('policy' => $policyId));
400
        $this->persistenceHandler->userHandler()->deletePolicy($policyId, $roleId);
401
402
        $this->cache->invalidateTags(['policy-' . $policyId, 'role-' . $roleId]);
403
    }
404
405
    /**
406
     * {@inheritdoc}
407
     */
408
    public function loadPoliciesByUserId($userId)
409
    {
410
        $this->logger->logCall(__METHOD__, array('user' => $userId));
411
412
        return $this->persistenceHandler->userHandler()->loadPoliciesByUserId($userId);
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\SPI\Persisten...:loadPoliciesByUserId() has been deprecated with message: Since 6.8, not currently in use as permission system needs to know about role assignment limitations.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
413
    }
414
415
    /**
416
     * {@inheritdoc}
417
     */
418
    public function assignRole($contentId, $roleId, array $limitation = null)
419
    {
420
        $this->logger->logCall(__METHOD__, array('group' => $contentId, 'role' => $roleId, 'limitation' => $limitation));
421
        $return = $this->persistenceHandler->userHandler()->assignRole($contentId, $roleId, $limitation);
422
423
        $this->cache->invalidateTags(['role-assignment-group-list-' . $contentId, 'role-assignment-role-list-' . $roleId]);
424
425
        return $return;
426
    }
427
428
    /**
429
     * {@inheritdoc}
430
     */
431 View Code Duplication
    public function unassignRole($contentId, $roleId)
432
    {
433
        $this->logger->logCall(__METHOD__, array('group' => $contentId, 'role' => $roleId));
434
        $return = $this->persistenceHandler->userHandler()->unassignRole($contentId, $roleId);
435
436
        $this->cache->invalidateTags(['role-assignment-group-list-' . $contentId, 'role-assignment-role-list-' . $roleId]);
437
438
        return $return;
439
    }
440
441
    /**
442
     * {@inheritdoc}
443
     */
444 View Code Duplication
    public function removeRoleAssignment($roleAssignmentId)
445
    {
446
        $this->logger->logCall(__METHOD__, array('assignment' => $roleAssignmentId));
447
        $return = $this->persistenceHandler->userHandler()->removeRoleAssignment($roleAssignmentId);
448
449
        $this->cache->invalidateTags(['role-assignment-' . $roleAssignmentId]);
450
451
        return $return;
452
    }
453
}
454