Completed
Push — migrate-files-no-interaction ( 025687...608925 )
by
unknown
46:43 queued 18:48
created

UserHandler::updateRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 7
loc 7
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\API\Repository\Exceptions\NotFoundException;
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\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->clear('content', $user->id);
34
35
        return $return;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function load($userId)
42
    {
43
        $this->logger->logCall(__METHOD__, array('user' => $userId));
44
45
        return $this->persistenceHandler->userHandler()->load($userId);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function loadByLogin($login)
52
    {
53
        $this->logger->logCall(__METHOD__, array('user' => $login));
54
55
        return $this->persistenceHandler->userHandler()->loadByLogin($login);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function loadByEmail($email)
62
    {
63
        $this->logger->logCall(__METHOD__, array('email' => $email));
64
65
        return $this->persistenceHandler->userHandler()->loadByEmail($email);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function update(User $user)
72
    {
73
        $this->logger->logCall(__METHOD__, array('struct' => $user));
74
        $return = $this->persistenceHandler->userHandler()->update($user);
75
76
        // Clear corresponding content cache as update of the User changes it's external data
77
        $this->cache->clear('content', $user->id);
78
79
        return $return;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function delete($userId)
86
    {
87
        $this->logger->logCall(__METHOD__, array('user' => $userId));
88
        $return = $this->persistenceHandler->userHandler()->delete($userId);
89
90
        // user id == content id == group id
91
        $this->cache->clear('content', $userId);
92
        $this->cache->clear('user', 'role', 'assignments', 'byGroup', $userId);
93
        $this->cache->clear('user', 'role', 'assignments', 'byGroup', 'inherited', $userId);
94
95
        return $return;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function createRole(RoleCreateStruct $createStruct)
102
    {
103
        $this->logger->logCall(__METHOD__, array('struct' => $createStruct));
104
105
        return $this->persistenceHandler->userHandler()->createRole($createStruct);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function createRoleDraft($roleId)
112
    {
113
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
114
115
        return $this->persistenceHandler->userHandler()->createRoleDraft($roleId);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function loadRole($roleId, $status = Role::STATUS_DEFINED)
122
    {
123
        if ($status === Role::STATUS_DEFINED) {
124
            $cache = $this->cache->getItem('user', 'role', $roleId);
125
            $role = $cache->get();
126
            if ($cache->isMiss()) {
127
                $this->logger->logCall(__METHOD__, array('role' => $roleId));
128
                $role = $this->persistenceHandler->userHandler()->loadRole($roleId, $status);
129
                $cache->set($role)->save();
130
            }
131
        } else {
132
            $role = $this->persistenceHandler->userHandler()->loadRole($roleId, $status);
133
        }
134
135
        return $role;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED)
142
    {
143
        $this->logger->logCall(__METHOD__, array('role' => $identifier));
144
145
        return $this->persistenceHandler->userHandler()->loadRoleByIdentifier($identifier, $status);
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function loadRoleDraftByRoleId($roleId)
152
    {
153
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
154
155
        return $this->persistenceHandler->userHandler()->loadRoleDraftByRoleId($roleId);
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161
    public function loadRoles()
162
    {
163
        $this->logger->logCall(__METHOD__);
164
165
        return $this->persistenceHandler->userHandler()->loadRoles();
166
    }
167
168
    /**
169
     * {@inheritdoc}
170
     */
171
    public function loadRoleAssignment($roleAssignmentId)
172
    {
173
        $this->logger->logCall(__METHOD__, array('assignment' => $roleAssignmentId));
174
175
        return $this->persistenceHandler->userHandler()->loadRoleAssignment($roleAssignmentId);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function loadRoleAssignmentsByRoleId($roleId)
182
    {
183
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
184
185
        return $this->persistenceHandler->userHandler()->loadRoleAssignmentsByRoleId($roleId);
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function loadRoleAssignmentsByGroupId($groupId, $inherit = false)
192
    {
193
        if ($inherit) {
194
            $cache = $this->cache->getItem('user', 'role', 'assignments', 'byGroup', 'inherited', $groupId);
195
        } else {
196
            $cache = $this->cache->getItem('user', 'role', 'assignments', 'byGroup', $groupId);
197
        }
198
        $assignments = $cache->get();
199 View Code Duplication
        if ($cache->isMiss()) {
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...
200
            $this->logger->logCall(__METHOD__, array('group' => $groupId, 'inherit' => $inherit));
201
            $assignments = $this->persistenceHandler->userHandler()->loadRoleAssignmentsByGroupId(
202
                $groupId,
203
                $inherit
204
            );
205
            $cache->set($assignments)->save();
206
        }
207
208
        return $assignments;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214 View Code Duplication
    public function updateRole(RoleUpdateStruct $struct)
215
    {
216
        $this->logger->logCall(__METHOD__, array('struct' => $struct));
217
        $this->persistenceHandler->userHandler()->updateRole($struct);
218
219
        $this->cache->clear('user', 'role', $struct->id);
220
    }
221
222
    /**
223
     * {@inheritdoc}
224
     */
225
    public function deleteRole($roleId, $status = Role::STATUS_DEFINED)
226
    {
227
        $this->logger->logCall(__METHOD__, array('role' => $roleId));
228
        $return = $this->persistenceHandler->userHandler()->deleteRole($roleId, $status);
229
230
        if ($status === Role::STATUS_DEFINED) {
231
            $this->cache->clear('user', 'role', $roleId);
232
            $this->cache->clear('user', 'role', 'assignments');
233
        }
234
235
        return $return;
236
    }
237
238
    /**
239
     * {@inheritdoc}
240
     */
241
    public function publishRoleDraft($roleDraftId)
242
    {
243
        $this->logger->logCall(__METHOD__, array('role' => $roleDraftId));
244
        $userHandler = $this->persistenceHandler->userHandler();
245
        $roleDraft = $userHandler->loadRole($roleDraftId, Role::STATUS_DRAFT);
246
        $return = $userHandler->publishRoleDraft($roleDraftId);
247
248
        $this->cache->clear('user', 'role', 'assignments');
249
        // Get right published role to cache it.
250
        try {
251
            // Role draft created from existing role.
252
            $publishedRole = $userHandler->loadRole($roleDraft->originalId);
253
        } catch (NotFoundException $e) {
254
            // Completely new role.
255
            $publishedRole = $userHandler->loadRole($roleDraftId);
256
        }
257
        $this->cache
258
            ->getItem('user', 'role', $publishedRole->id)
259
            ->set($publishedRole)
260
            ->save();
261
262
        return $return;
263
    }
264
265
    /**
266
     * {@inheritdoc}
267
     */
268
    public function addPolicyByRoleDraft($roleId, Policy $policy)
269
    {
270
        $this->logger->logCall(__METHOD__, array('role' => $roleId, 'struct' => $policy));
271
272
        return $this->persistenceHandler->userHandler()->addPolicyByRoleDraft($roleId, $policy);
273
    }
274
275
    /**
276
     * {@inheritdoc}
277
     */
278
    public function addPolicy($roleId, Policy $policy)
279
    {
280
        $this->logger->logCall(__METHOD__, array('role' => $roleId, 'struct' => $policy));
281
        $return = $this->persistenceHandler->userHandler()->addPolicy($roleId, $policy);
282
283
        $this->cache->clear('user', 'role', $roleId);
284
285
        return $return;
286
    }
287
288
    /**
289
     * {@inheritdoc}
290
     */
291
    public function updatePolicy(Policy $policy)
292
    {
293
        $this->logger->logCall(__METHOD__, array('struct' => $policy));
294
        $return = $this->persistenceHandler->userHandler()->updatePolicy($policy);
295
296
        $this->cache->clear('user', 'role', $policy->roleId);
297
298
        return $return;
299
    }
300
301
    /**
302
     * {@inheritdoc}
303
     */
304 View Code Duplication
    public function deletePolicy($policyId)
305
    {
306
        $this->logger->logCall(__METHOD__, array('policy' => $policyId));
307
        $this->persistenceHandler->userHandler()->deletePolicy($policyId);
308
309
        $this->cache->clear('user', 'role');
310
    }
311
312
    /**
313
     * {@inheritdoc}
314
     */
315
    public function loadPoliciesByUserId($userId)
316
    {
317
        $this->logger->logCall(__METHOD__, array('user' => $userId));
318
319
        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...
320
    }
321
322
    /**
323
     * {@inheritdoc}
324
     */
325 View Code Duplication
    public function assignRole($contentId, $roleId, array $limitation = null)
326
    {
327
        $this->logger->logCall(__METHOD__, array('group' => $contentId, 'role' => $roleId, 'limitation' => $limitation));
328
        $return = $this->persistenceHandler->userHandler()->assignRole($contentId, $roleId, $limitation);
329
330
        $this->cache->clear('user', 'role', $roleId);
331
        $this->cache->clear('user', 'role', 'assignments', 'byGroup', $contentId);
332
        $this->cache->clear('user', 'role', 'assignments', 'byGroup', 'inherited');
333
334
        return $return;
335
    }
336
337
    /**
338
     * {@inheritdoc}
339
     */
340 View Code Duplication
    public function unassignRole($contentId, $roleId)
341
    {
342
        $this->logger->logCall(__METHOD__, array('group' => $contentId, 'role' => $roleId));
343
        $return = $this->persistenceHandler->userHandler()->unassignRole($contentId, $roleId);
344
345
        $this->cache->clear('user', 'role', $roleId);
346
        $this->cache->clear('user', 'role', 'assignments', 'byGroup', $contentId);
347
        $this->cache->clear('user', 'role', 'assignments', 'byGroup', 'inherited');
348
349
        return $return;
350
    }
351
352
    /**
353
     * {@inheritdoc}
354
     */
355 View Code Duplication
    public function removeRoleAssignment($roleAssignmentId)
356
    {
357
        $this->logger->logCall(__METHOD__, array('assignment' => $roleAssignmentId));
358
        $return = $this->persistenceHandler->userHandler()->removeRoleAssignment($roleAssignmentId);
359
360
        // We don't know the contentId, so clear all assignment cache.
361
        $this->cache->clear('user', 'role', 'assignments'); //TIMBER!
362
363
        return $return;
364
    }
365
}
366