Completed
Push — master ( 97e40f...89ec5c )
by André
40:26 queued 12:35
created

UserHandler::loadRoleAssignment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 16
rs 9.4285
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\RoleAssignment;
15
use eZ\Publish\SPI\Persistence\User\RoleCreateStruct;
16
use eZ\Publish\SPI\Persistence\User\RoleUpdateStruct;
17
use eZ\Publish\SPI\Persistence\User\Policy;
18
19
/**
20
 * Cache handler for user module.
21
 */
22
class UserHandler extends AbstractHandler implements UserHandlerInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function create(User $user)
28
    {
29
        $this->logger->logCall(__METHOD__, array('struct' => $user));
30
        $return = $this->persistenceHandler->userHandler()->create($user);
31
32
        // Clear corresponding content cache as creation of the User changes it's external data
33
        $this->cache->invalidateTags(['content-fields-' . $user->id]);
34
35
        return $return;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function load($userId)
42
    {
43
        $cacheItem = $this->cache->getItem("ez-user-${userId}");
44
        if ($cacheItem->isHit()) {
45
            return $cacheItem->get();
46
        }
47
48
        $this->logger->logCall(__METHOD__, array('user' => $userId));
49
        $user = $this->persistenceHandler->userHandler()->load($userId);
50
51
        $cacheItem->set($user);
52
        $cacheItem->tag(['content-' . $user->id, 'user-' . $user->id]);
53
        $this->cache->save($cacheItem);
54
55
        return $user;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function loadByLogin($login)
62
    {
63
        $cacheItem = $this->cache->getItem("ez-user-${login}-by-login");
64
        if ($cacheItem->isHit()) {
65
            return $cacheItem->get();
66
        }
67
68
        $this->logger->logCall(__METHOD__, array('user' => $login));
69
        $user = $this->persistenceHandler->userHandler()->loadByLogin($login);
70
71
        $cacheItem->set($user);
72
        $cacheItem->tag(['content-' . $user->id, 'user-' . $user->id]);
73
        $this->cache->save($cacheItem);
74
75
        return $user;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function loadByEmail($email)
82
    {
83
        $cacheItem = $this->cache->getItem('ez-user-' . str_replace('@', '§', $email) . '-by-email');
84
        if ($cacheItem->isHit()) {
85
            return $cacheItem->get();
86
        }
87
88
        $this->logger->logCall(__METHOD__, array('email' => $email));
89
        $users = $this->persistenceHandler->userHandler()->loadByEmail($email);
90
91
        $cacheItem->set($users);
92
        $cacheTags = [];
93
        foreach ($users as $user) {
94
            $cacheTags[] = 'content-' . $user->id;
95
            $cacheTags[] = 'user-' . $user->id;
96
        }
97
        $cacheItem->tag($cacheTags);
98
        $this->cache->save($cacheItem);
99
100
        return $users;
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 View Code Duplication
    public function update(User $user)
107
    {
108
        $this->logger->logCall(__METHOD__, array('struct' => $user));
109
        $return = $this->persistenceHandler->userHandler()->update($user);
110
111
        // Clear corresponding content cache as update of the User changes it's external data
112
        $this->cache->invalidateTags(['content-fields-' . $user->id, 'user-' . $user->id]);
113
114
        return $return;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function delete($userId)
121
    {
122
        $this->logger->logCall(__METHOD__, array('user' => $userId));
123
        $return = $this->persistenceHandler->userHandler()->delete($userId);
124
125
        // user id == content id == group id
126
        $this->cache->invalidateTags(['content-fields-' . $userId, 'user-' . $userId]);
127
128
        return $return;
129
    }
130
131
    /**
132
     * {@inheritdoc}
133
     */
134
    public function createRole(RoleCreateStruct $createStruct)
135
    {
136
        $this->logger->logCall(__METHOD__, array('struct' => $createStruct));
137
138
        return $this->persistenceHandler->userHandler()->createRole($createStruct);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144
    public function createRoleDraft($roleId)
145
    {
146
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
147
148
        return $this->persistenceHandler->userHandler()->createRoleDraft($roleId);
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154 View Code Duplication
    public function loadRole($roleId, $status = Role::STATUS_DEFINED)
155
    {
156
        if ($status !== Role::STATUS_DEFINED) {
157
            $this->logger->logCall(__METHOD__, array('role' => $roleId));
158
159
            return $this->persistenceHandler->userHandler()->loadRole($roleId, $status);
160
        }
161
162
        $cacheItem = $this->cache->getItem("ez-role-${roleId}");
163
        if ($cacheItem->isHit()) {
164
            return $cacheItem->get();
165
        }
166
167
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
168
        $role = $this->persistenceHandler->userHandler()->loadRole($roleId, $status);
169
170
        $cacheItem->set($role);
171
        $cacheItem->tag(['role-' . $role->id]);
172
        $this->cache->save($cacheItem);
173
174
        return $role;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 View Code Duplication
    public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED)
181
    {
182
        if ($status !== Role::STATUS_DEFINED) {
183
            $this->logger->logCall(__METHOD__, array('role' => $identifier));
184
185
            return $this->persistenceHandler->userHandler()->loadRoleByIdentifier($identifier, $status);
186
        }
187
188
        $cacheItem = $this->cache->getItem("ez-role-${identifier}-by-identifier");
189
        if ($cacheItem->isHit()) {
190
            return $cacheItem->get();
191
        }
192
193
        $this->logger->logCall(__METHOD__, array('role' => $identifier));
194
        $role = $this->persistenceHandler->userHandler()->loadRoleByIdentifier($identifier, $status);
195
196
        $cacheItem->set($role);
197
        $cacheItem->tag(['role-' . $role->id]);
198
        $this->cache->save($cacheItem);
199
200
        return $role;
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public function loadRoleDraftByRoleId($roleId)
207
    {
208
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
209
210
        return $this->persistenceHandler->userHandler()->loadRoleDraftByRoleId($roleId);
211
    }
212
213
    /**
214
     * {@inheritdoc}
215
     */
216
    public function loadRoles()
217
    {
218
        $this->logger->logCall(__METHOD__);
219
220
        return $this->persistenceHandler->userHandler()->loadRoles();
221
    }
222
223
    /**
224
     * {@inheritdoc}
225
     */
226
    public function loadRoleAssignment($roleAssignmentId)
227
    {
228
        $cacheItem = $this->cache->getItem("ez-role-assignment-${roleAssignmentId}");
229
        if ($cacheItem->isHit()) {
230
            return $cacheItem->get();
231
        }
232
233
        $this->logger->logCall(__METHOD__, array('assignment' => $roleAssignmentId));
234
        $roleAssignment = $this->persistenceHandler->userHandler()->loadRoleAssignment($roleAssignmentId);
235
236
        $cacheItem->set($roleAssignment);
237
        $cacheItem->tag($this->getCacheTagsForRoleAssignment($roleAssignment));
238
        $this->cache->save($cacheItem);
239
240
        return $roleAssignment;
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246 View Code Duplication
    public function loadRoleAssignmentsByRoleId($roleId)
247
    {
248
        $cacheItem = $this->cache->getItem("ez-role-assignment-${roleId}-by-role");
249
        if ($cacheItem->isHit()) {
250
            return $cacheItem->get();
251
        }
252
253
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
254
        $roleAssignments = $this->persistenceHandler->userHandler()->loadRoleAssignmentsByRoleId($roleId);
255
256
        $cacheItem->set($roleAssignments);
257
        $cacheTags = ['role-assignment-role-list-' . $roleId];
258
        foreach ($roleAssignments as $roleAssignment) {
259
            $cacheTags = $this->getCacheTagsForRoleAssignment($roleAssignment, $cacheTags);
260
        }
261
        $cacheItem->tag($cacheTags);
262
        $this->cache->save($cacheItem);
263
264
        return $roleAssignments;
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270
    public function loadRoleAssignmentsByGroupId($groupId, $inherit = false)
271
    {
272
        if ($inherit) {
273
            $cacheItem = $this->cache->getItem("ez-role-assignment-${groupId}-by-group-inherited");
274
        } else {
275
            $cacheItem = $this->cache->getItem("ez-role-assignment-${groupId}-by-group");
276
        }
277
278
        if ($cacheItem->isHit()) {
279
            return $cacheItem->get();
280
        }
281
282
        $this->logger->logCall(__METHOD__, array('group' => $groupId, 'inherit' => $inherit));
283
        $roleAssignments = $this->persistenceHandler->userHandler()->loadRoleAssignmentsByGroupId($groupId, $inherit);
284
285
        $cacheItem->set($roleAssignments);
286
        // Tag below is for empty results,  non empty it might have duplicated tags but cache will reduce those.
287
        $cacheTags = ['role-assignment-group-list-' . $groupId];
288
        foreach ($roleAssignments as $roleAssignment) {
289
            $cacheTags = $this->getCacheTagsForRoleAssignment($roleAssignment, $cacheTags);
290
        }
291
292
        // To make sure tree operations affecting this can clear the permission cache
293
        $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($groupId);
294
        foreach ($locations as $location) {
295
            foreach (explode('/', trim($location->pathString, '/')) as $pathId) {
296
                $cacheTags[] = 'location-path-' . $pathId;
297
            }
298
        }
299
300
        $cacheItem->tag($cacheTags);
301
        $this->cache->save($cacheItem);
302
303
        return $roleAssignments;
304
    }
305
306
    /**
307
     * {@inheritdoc}
308
     */
309
    public function updateRole(RoleUpdateStruct $struct)
310
    {
311
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
312
        $this->persistenceHandler->userHandler()->updateRole($struct);
313
314
        $this->cache->invalidateTags(['role-' . $struct->id]);
315
    }
316
317
    /**
318
     * {@inheritdoc}
319
     */
320
    public function deleteRole($roleId, $status = Role::STATUS_DEFINED)
321
    {
322
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
323
        $return = $this->persistenceHandler->userHandler()->deleteRole($roleId, $status);
324
325
        if ($status === Role::STATUS_DEFINED) {
326
            $this->cache->invalidateTags(['role-' . $roleId, 'role-assignment-role-list-' . $roleId]);
327
        }
328
329
        return $return;
330
    }
331
332
    /**
333
     * {@inheritdoc}
334
     */
335
    public function publishRoleDraft($roleDraftId)
336
    {
337
        $this->logger->logCall(__METHOD__, array('role' => $roleDraftId));
338
        $userHandler = $this->persistenceHandler->userHandler();
339
        $roleDraft = $userHandler->loadRole($roleDraftId, Role::STATUS_DRAFT);
340
        $return = $userHandler->publishRoleDraft($roleDraftId);
341
342
        // If there was a original role for the draft, then we clean cache for it
343
        if ($roleDraft->originalId > -1) {
344
            $this->cache->invalidateTags(['role-' . $roleDraft->originalId]);
345
        }
346
347
        return $return;
348
    }
349
350
    /**
351
     * {@inheritdoc}
352
     */
353
    public function addPolicyByRoleDraft($roleId, Policy $policy)
354
    {
355
        $this->logger->logCall(__METHOD__, array('role' => $roleId, 'struct' => $policy));
356
357
        return $this->persistenceHandler->userHandler()->addPolicyByRoleDraft($roleId, $policy);
358
    }
359
360
    /**
361
     * {@inheritdoc}
362
     */
363
    public function addPolicy($roleId, Policy $policy)
364
    {
365
        $this->logger->logCall(__METHOD__, array('role' => $roleId, 'struct' => $policy));
366
        $return = $this->persistenceHandler->userHandler()->addPolicy($roleId, $policy);
367
368
        $this->cache->invalidateTags(['role-' . $roleId]);
369
370
        return $return;
371
    }
372
373
    /**
374
     * {@inheritdoc}
375
     */
376 View Code Duplication
    public function updatePolicy(Policy $policy)
377
    {
378
        $this->logger->logCall(__METHOD__, array('struct' => $policy));
379
        $return = $this->persistenceHandler->userHandler()->updatePolicy($policy);
380
381
        $this->cache->invalidateTags(['policy-' . $policy->id, 'role-' . $policy->roleId]);
382
383
        return $return;
384
    }
385
386
    /**
387
     * {@inheritdoc}
388
     */
389
    public function deletePolicy($policyId, $roleId)
390
    {
391
        $this->logger->logCall(__METHOD__, array('policy' => $policyId));
392
        $this->persistenceHandler->userHandler()->deletePolicy($policyId, $roleId);
393
394
        $this->cache->invalidateTags(['policy-' . $policyId, 'role-' . $roleId]);
395
    }
396
397
    /**
398
     * {@inheritdoc}
399
     */
400
    public function loadPoliciesByUserId($userId)
401
    {
402
        $this->logger->logCall(__METHOD__, array('user' => $userId));
403
404
        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...
405
    }
406
407
    /**
408
     * {@inheritdoc}
409
     */
410 View Code Duplication
    public function assignRole($contentId, $roleId, array $limitation = null)
411
    {
412
        $this->logger->logCall(__METHOD__, array('group' => $contentId, 'role' => $roleId, 'limitation' => $limitation));
413
        $return = $this->persistenceHandler->userHandler()->assignRole($contentId, $roleId, $limitation);
414
415
        $this->cache->invalidateTags(['role-assignment-group-list-' . $contentId, 'role-assignment-role-list-' . $roleId]);
416
417
        return $return;
418
    }
419
420
    /**
421
     * {@inheritdoc}
422
     */
423 View Code Duplication
    public function unassignRole($contentId, $roleId)
424
    {
425
        $this->logger->logCall(__METHOD__, array('group' => $contentId, 'role' => $roleId));
426
        $return = $this->persistenceHandler->userHandler()->unassignRole($contentId, $roleId);
427
428
        $this->cache->invalidateTags(['role-assignment-group-list-' . $contentId, 'role-assignment-role-list-' . $roleId]);
429
430
        return $return;
431
    }
432
433
    /**
434
     * {@inheritdoc}
435
     */
436
    public function removeRoleAssignment($roleAssignmentId)
437
    {
438
        $this->logger->logCall(__METHOD__, array('assignment' => $roleAssignmentId));
439
        $return = $this->persistenceHandler->userHandler()->removeRoleAssignment($roleAssignmentId);
440
441
        $this->cache->invalidateTags(['role-assignment-' . $roleAssignmentId]);
442
443
        return $return;
444
    }
445
446
    /**
447
     * Reusable function to return relevant role assignment tags so cache can be purged reliably.
448
     *
449
     * @param \eZ\Publish\SPI\Persistence\User\RoleAssignment $roleAssignment
450
     * @param array $tags Optional, can be used to specify other tags.
451
     *
452
     * @return array
453
     */
454
    private function getCacheTagsForRoleAssignment(RoleAssignment $roleAssignment, array $tags = [])
455
    {
456
        $tags[] = 'role-assignment-' . $roleAssignment->id;
457
        $tags[] = 'role-assignment-group-list-' . $roleAssignment->contentId;
458
        $tags[] = 'role-assignment-role-list-' . $roleAssignment->roleId;
459
460
        return $tags;
461
    }
462
}
463