Completed
Push — master ( 59965c...4339a8 )
by
unknown
114:08 queued 95:41
created

UserHandler::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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