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

UserService::updateUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * UserService class.
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\SignalSlot;
10
11
use eZ\Publish\API\Repository\UserService as UserServiceInterface;
12
use eZ\Publish\API\Repository\Values\User\UserTokenUpdateStruct;
13
use eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct;
14
use eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct;
15
use eZ\Publish\API\Repository\Values\User\UserCreateStruct;
16
use eZ\Publish\API\Repository\Values\User\UserGroup;
17
use eZ\Publish\API\Repository\Values\User\User;
18
use eZ\Publish\API\Repository\Values\User\UserUpdateStruct;
19
use eZ\Publish\Core\SignalSlot\Signal\UserService\CreateUserGroupSignal;
20
use eZ\Publish\Core\SignalSlot\Signal\UserService\DeleteUserGroupSignal;
21
use eZ\Publish\Core\SignalSlot\Signal\UserService\MoveUserGroupSignal;
22
use eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserTokenSignal;
23
use eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserGroupSignal;
24
use eZ\Publish\Core\SignalSlot\Signal\UserService\CreateUserSignal;
25
use eZ\Publish\Core\SignalSlot\Signal\UserService\DeleteUserSignal;
26
use eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserSignal;
27
use eZ\Publish\Core\SignalSlot\Signal\UserService\AssignUserToUserGroupSignal;
28
use eZ\Publish\Core\SignalSlot\Signal\UserService\UnAssignUserFromUserGroupSignal;
29
30
/**
31
 * UserService class.
32
 */
33
class UserService implements UserServiceInterface
34
{
35
    /**
36
     * Aggregated service.
37
     *
38
     * @var \eZ\Publish\API\Repository\UserService
39
     */
40
    protected $service;
41
42
    /**
43
     * SignalDispatcher.
44
     *
45
     * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher
46
     */
47
    protected $signalDispatcher;
48
49
    /**
50
     * Constructor.
51
     *
52
     * Construct service object from aggregated service and signal
53
     * dispatcher
54
     *
55
     * @param \eZ\Publish\API\Repository\UserService $service
56
     * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher
57
     */
58
    public function __construct(UserServiceInterface $service, SignalDispatcher $signalDispatcher)
59
    {
60
        $this->service = $service;
61
        $this->signalDispatcher = $signalDispatcher;
62
    }
63
64
    /**
65
     * Creates a new user group using the data provided in the ContentCreateStruct parameter.
66
     *
67
     * In 4.x in the content type parameter in the profile is ignored
68
     * - the content type is determined via configuration and can be set to null.
69
     * The returned version is published.
70
     *
71
     * @param \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct $userGroupCreateStruct a structure for setting all necessary data to create this user group
72
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $parentGroup
73
     *
74
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
75
     *
76
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group
77
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the input structure has invalid data
78
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userGroupCreateStruct is not valid
79
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing or set to an empty value
80
     */
81
    public function createUserGroup(UserGroupCreateStruct $userGroupCreateStruct, UserGroup $parentGroup)
82
    {
83
        $returnValue = $this->service->createUserGroup($userGroupCreateStruct, $parentGroup);
84
        $this->signalDispatcher->emit(
85
            new CreateUserGroupSignal(
86
                array(
87
                    'userGroupId' => $returnValue->id,
88
                )
89
            )
90
        );
91
92
        return $returnValue;
93
    }
94
95
    /**
96
     * Loads a user group for the given id.
97
     *
98
     * @param mixed $id
99
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
100
     *
101
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
102
     *
103
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group
104
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the user group with the given id was not found
105
     */
106
    public function loadUserGroup($id, array $prioritizedLanguages = [])
107
    {
108
        return $this->service->loadUserGroup($id, $prioritizedLanguages);
109
    }
110
111
    /**
112
     * Loads the sub groups of a user group.
113
     *
114
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
115
     * @param int $offset the start offset for paging
116
     * @param int $limit the number of user groups returned
117
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
118
     *
119
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup[]
120
     *
121
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read the user group
122
     */
123
    public function loadSubUserGroups(UserGroup $userGroup, $offset = 0, $limit = 25, array $prioritizedLanguages = [])
124
    {
125
        return $this->service->loadSubUserGroups($userGroup, $offset, $limit, $prioritizedLanguages);
126
    }
127
128
    /**
129
     * Removes a user group.
130
     *
131
     * the users which are not assigned to other groups will be deleted.
132
     *
133
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
134
     *
135
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group
136
     */
137 View Code Duplication
    public function deleteUserGroup(UserGroup $userGroup)
138
    {
139
        $returnValue = $this->service->deleteUserGroup($userGroup);
140
        $this->signalDispatcher->emit(
141
            new DeleteUserGroupSignal(
142
                array(
143
                    'userGroupId' => $userGroup->id,
144
                    'affectedLocationIds' => $returnValue,
145
                )
146
            )
147
        );
148
149
        return $returnValue;
150
    }
151
152
    /**
153
     * Moves the user group to another parent.
154
     *
155
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
156
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $newParent
157
     *
158
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group
159
     */
160 View Code Duplication
    public function moveUserGroup(UserGroup $userGroup, UserGroup $newParent)
161
    {
162
        $returnValue = $this->service->moveUserGroup($userGroup, $newParent);
163
        $this->signalDispatcher->emit(
164
            new MoveUserGroupSignal(
165
                array(
166
                    'userGroupId' => $userGroup->id,
167
                    'newParentId' => $newParent->id,
168
                )
169
            )
170
        );
171
172
        return $returnValue;
173
    }
174
175
    /**
176
     * Updates the group profile with fields and meta data.
177
     *
178
     * 4.x: If the versionUpdateStruct is set in $userGroupUpdateStruct, this method internally creates a content draft, updates ts with the provided data
179
     * and publishes the draft. If a draft is explicitly required, the user group can be updated via the content service methods.
180
     *
181
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
182
     * @param \eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct $userGroupUpdateStruct
183
     *
184
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
185
     *
186
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group
187
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userGroupUpdateStruct is not valid
188
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set empty
189
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a field value is not accepted by the field type
190
     */
191
    public function updateUserGroup(UserGroup $userGroup, UserGroupUpdateStruct $userGroupUpdateStruct)
192
    {
193
        $returnValue = $this->service->updateUserGroup($userGroup, $userGroupUpdateStruct);
194
        $this->signalDispatcher->emit(
195
            new UpdateUserGroupSignal(
196
                array(
197
                    'userGroupId' => $userGroup->id,
198
                )
199
            )
200
        );
201
202
        return $returnValue;
203
    }
204
205
    /**
206
     * Create a new user. The created user is published by this method.
207
     *
208
     * @param \eZ\Publish\API\Repository\Values\User\UserCreateStruct $userCreateStruct the data used for creating the user
209
     * @param array $parentGroups the groups of type {@link \eZ\Publish\API\Repository\Values\User\UserGroup} which are assigned to the user after creation
210
     *
211
     * @return \eZ\Publish\API\Repository\Values\User\User
212
     *
213
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group
214
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userCreateStruct is not valid
215
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing or set  to an empty value
216
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a field value is not accepted by the field type
217
     *                                                                        if a user with provided login already exists
218
     */
219
    public function createUser(UserCreateStruct $userCreateStruct, array $parentGroups)
220
    {
221
        $returnValue = $this->service->createUser($userCreateStruct, $parentGroups);
222
        $this->signalDispatcher->emit(
223
            new CreateUserSignal(
224
                array(
225
                    'userId' => $returnValue->id,
226
                )
227
            )
228
        );
229
230
        return $returnValue;
231
    }
232
233
    /**
234
     * Loads a user.
235
     *
236
     * @param mixed $userId
237
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
238
     *
239
     * @return \eZ\Publish\API\Repository\Values\User\User
240
     *
241
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given id was not found
242
     */
243
    public function loadUser($userId, array $prioritizedLanguages = [])
244
    {
245
        return $this->service->loadUser($userId, $prioritizedLanguages);
246
    }
247
248
    /**
249
     * Loads anonymous user.
250
     *
251
     * @deprecated since 5.3, use loadUser( $anonymousUserId ) instead
252
     *
253
     * @uses ::loadUser()
254
     *
255
     * @return \eZ\Publish\API\Repository\Values\User\User
256
     */
257
    public function loadAnonymousUser()
258
    {
259
        return $this->service->loadAnonymousUser();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...ce::loadAnonymousUser() has been deprecated with message: since 5.3, use loadUser( $anonymousUserId ) instead

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...
260
    }
261
262
    /**
263
     * Loads a user for the given login and password.
264
     *
265
     * {@inheritdoc}
266
     *
267
     * @param string $login
268
     * @param string $password the plain password
269
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
270
     *
271
     * @return \eZ\Publish\API\Repository\Values\User\User
272
     *
273
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if credentials are invalid
274
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given credentials was not found
275
     */
276
    public function loadUserByCredentials($login, $password, array $prioritizedLanguages = [])
277
    {
278
        return $this->service->loadUserByCredentials($login, $password, $prioritizedLanguages);
279
    }
280
281
    /**
282
     * Loads a user for the given login.
283
     *
284
     * {@inheritdoc}
285
     *
286
     * @param string $login
287
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
288
     *
289
     * @return \eZ\Publish\API\Repository\Values\User\User
290
     *
291
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given credentials was not found
292
     */
293
    public function loadUserByLogin($login, array $prioritizedLanguages = [])
294
    {
295
        return $this->service->loadUserByLogin($login, $prioritizedLanguages);
296
    }
297
298
    /**
299
     * Loads a user for the given email.
300
     *
301
     * {@inheritdoc}
302
     *
303
     * @param string $email
304
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
305
     *
306
     * @return \eZ\Publish\API\Repository\Values\User\User[]
307
     */
308
    public function loadUsersByEmail($email, array $prioritizedLanguages = [])
309
    {
310
        return $this->service->loadUsersByEmail($email, $prioritizedLanguages);
311
    }
312
313
    /**
314
     * Loads a user with user hash key.
315
     *
316
     * {@inheritdoc}
317
     *
318
     * @param string $hash
319
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
320
     *
321
     * @return \eZ\Publish\API\Repository\Values\User\User
322
     *
323
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given hash was not found
324
     */
325
    public function loadUserByToken($hash, array $prioritizedLanguages = [])
326
    {
327
        return $this->service->loadUserByToken($hash, $prioritizedLanguages);
328
    }
329
330
    /**
331
     * This method deletes a user.
332
     *
333
     * @param \eZ\Publish\API\Repository\Values\User\User $user
334
     *
335
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete the user
336
     */
337
    public function deleteUser(User $user)
338
    {
339
        $returnValue = $this->service->deleteUser($user);
340
        $this->signalDispatcher->emit(
341
            new DeleteUserSignal(
342
                array(
343
                    'userId' => $user->id,
344
                    'affectedLocationIds' => $returnValue,
345
                )
346
            )
347
        );
348
349
        return $returnValue;
350
    }
351
352
    /**
353
     * Updates a user.
354
     *
355
     * 4.x: If the versionUpdateStruct is set in the user update structure, this method internally creates a content draft, updates ts with the provided data
356
     * and publishes the draft. If a draft is explicitly required, the user group can be updated via the content service methods.
357
     *
358
     * @param \eZ\Publish\API\Repository\Values\User\User $user
359
     * @param \eZ\Publish\API\Repository\Values\User\UserUpdateStruct $userUpdateStruct
360
     *
361
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update the user
362
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userUpdateStruct is not valid
363
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set empty
364
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a field value is not accepted by the field type
365
     *
366
     * @return \eZ\Publish\API\Repository\Values\User\User
367
     */
368
    public function updateUser(User $user, UserUpdateStruct $userUpdateStruct)
369
    {
370
        $returnValue = $this->service->updateUser($user, $userUpdateStruct);
371
        $this->signalDispatcher->emit(
372
            new UpdateUserSignal(
373
                array(
374
                    'userId' => $user->id,
375
                )
376
            )
377
        );
378
379
        return $returnValue;
380
    }
381
382
    /**
383
     * Update the user account key information specified by the user account key struct.
384
     *
385
     * @param \eZ\Publish\API\Repository\Values\User\User $user
386
     * @param \eZ\Publish\API\Repository\Values\User\UserTokenUpdateStruct $userTokenUpdateStruct
387
     *
388
     * @return \eZ\Publish\API\Repository\Values\User\User
389
     */
390
    public function updateUserToken(User $user, UserTokenUpdateStruct $userTokenUpdateStruct)
391
    {
392
        $returnValue = $this->service->updateUserToken($user, $userTokenUpdateStruct);
393
        $this->signalDispatcher->emit(
394
            new UpdateUserTokenSignal(
395
                ['userId' => $user->id]
396
            )
397
        );
398
399
        return $returnValue;
400
    }
401
402
    /**
403
     * Expires user token with user hash.
404
     *
405
     * @param string $hash
406
     */
407
    public function expireUserToken($hash)
408
    {
409
        return $this->service->expireUserToken($hash);
410
    }
411
412
    /**
413
     * Assigns a new user group to the user.
414
     *
415
     * @param \eZ\Publish\API\Repository\Values\User\User $user
416
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
417
     *
418
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign the user group to the user
419
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the user is already in the given user group
420
     */
421 View Code Duplication
    public function assignUserToUserGroup(User $user, UserGroup $userGroup)
422
    {
423
        $returnValue = $this->service->assignUserToUserGroup($user, $userGroup);
424
        $this->signalDispatcher->emit(
425
            new AssignUserToUserGroupSignal(
426
                array(
427
                    'userId' => $user->id,
428
                    'userGroupId' => $userGroup->id,
429
                )
430
            )
431
        );
432
433
        return $returnValue;
434
    }
435
436
    /**
437
     * Removes a user group from the user.
438
     *
439
     * @param \eZ\Publish\API\Repository\Values\User\User $user
440
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
441
     *
442
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove the user group from the user
443
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the user is not in the given user group
444
     */
445 View Code Duplication
    public function unAssignUserFromUserGroup(User $user, UserGroup $userGroup)
446
    {
447
        $returnValue = $this->service->unAssignUserFromUserGroup($user, $userGroup);
448
        $this->signalDispatcher->emit(
449
            new UnAssignUserFromUserGroupSignal(
450
                array(
451
                    'userId' => $user->id,
452
                    'userGroupId' => $userGroup->id,
453
                )
454
            )
455
        );
456
457
        return $returnValue;
458
    }
459
460
    /**
461
     * Loads the user groups the user belongs to.
462
     *
463
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed read the user or user group
464
     *
465
     * @param \eZ\Publish\API\Repository\Values\User\User $user
466
     * @param int $offset the start offset for paging
467
     * @param int $limit the number of user groups returned
468
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
469
     *
470
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup[]
471
     */
472
    public function loadUserGroupsOfUser(User $user, $offset = 0, $limit = 25, array $prioritizedLanguages = [])
473
    {
474
        return $this->service->loadUserGroupsOfUser($user, $offset, $limit, $prioritizedLanguages);
475
    }
476
477
    /**
478
     * Loads the users of a user group.
479
     *
480
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read the users or user group
481
     *
482
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
483
     * @param int $offset the start offset for paging
484
     * @param int $limit the number of users returned
485
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
486
     *
487
     * @return \eZ\Publish\API\Repository\Values\User\User[]
488
     */
489
    public function loadUsersOfUserGroup(
490
        UserGroup $userGroup,
491
        $offset = 0,
492
        $limit = 25,
493
        array $prioritizedLanguages = []
494
    ) {
495
        return $this->service->loadUsersOfUserGroup(
496
            $userGroup,
497
            $offset,
498
            $limit,
499
            $prioritizedLanguages
500
        );
501
    }
502
503
    /**
504
     * Instantiate a user create class.
505
     *
506
     * @param string $login the login of the new user
507
     * @param string $email the email of the new user
508
     * @param string $password the plain password of the new user
509
     * @param string $mainLanguageCode the main language for the underlying content object
510
     * @param \eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType 5.x the content type for the underlying content object. In 4.x it is ignored and taken from the configuration
511
     *
512
     * @return \eZ\Publish\API\Repository\Values\User\UserCreateStruct
513
     */
514
    public function newUserCreateStruct($login, $email, $password, $mainLanguageCode, $contentType = null)
515
    {
516
        return $this->service->newUserCreateStruct($login, $email, $password, $mainLanguageCode, $contentType);
517
    }
518
519
    /**
520
     * Instantiate a user group create class.
521
     *
522
     * @param string $mainLanguageCode The main language for the underlying content object
523
     * @param null|\eZ\Publish\API\Repository\Values\ContentType\ContentType $contentType 5.x the content type for the underlying content object. In 4.x it is ignored and taken from the configuration
524
     *
525
     * @return \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct
526
     */
527
    public function newUserGroupCreateStruct($mainLanguageCode, $contentType = null)
528
    {
529
        return $this->service->newUserGroupCreateStruct($mainLanguageCode, $contentType);
530
    }
531
532
    /**
533
     * Instantiate a new user update struct.
534
     *
535
     * @return \eZ\Publish\API\Repository\Values\User\UserUpdateStruct
536
     */
537
    public function newUserUpdateStruct()
538
    {
539
        return $this->service->newUserUpdateStruct();
540
    }
541
542
    /**
543
     * Instantiate a new user group update struct.
544
     *
545
     * @return \eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct
546
     */
547
    public function newUserGroupUpdateStruct()
548
    {
549
        return $this->service->newUserGroupUpdateStruct();
550
    }
551
}
552