Completed
Push — master ( 8fa866...f2aec6 )
by André
44:30 queued 23:23
created

UserService::isUserGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
cc 1
nc 1
nop 1
rs 10
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\Content\Content;
13
use eZ\Publish\API\Repository\Values\User\PasswordValidationContext;
14
use eZ\Publish\API\Repository\Values\User\UserTokenUpdateStruct;
15
use eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct;
16
use eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct;
17
use eZ\Publish\API\Repository\Values\User\UserCreateStruct;
18
use eZ\Publish\API\Repository\Values\User\UserGroup;
19
use eZ\Publish\API\Repository\Values\User\User;
20
use eZ\Publish\API\Repository\Values\User\UserUpdateStruct;
21
use eZ\Publish\Core\SignalSlot\Signal\UserService\CreateUserGroupSignal;
22
use eZ\Publish\Core\SignalSlot\Signal\UserService\DeleteUserGroupSignal;
23
use eZ\Publish\Core\SignalSlot\Signal\UserService\MoveUserGroupSignal;
24
use eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserTokenSignal;
25
use eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserGroupSignal;
26
use eZ\Publish\Core\SignalSlot\Signal\UserService\CreateUserSignal;
27
use eZ\Publish\Core\SignalSlot\Signal\UserService\DeleteUserSignal;
28
use eZ\Publish\Core\SignalSlot\Signal\UserService\UpdateUserSignal;
29
use eZ\Publish\Core\SignalSlot\Signal\UserService\AssignUserToUserGroupSignal;
30
use eZ\Publish\Core\SignalSlot\Signal\UserService\UnAssignUserFromUserGroupSignal;
31
32
/**
33
 * UserService class.
34
 */
35
class UserService implements UserServiceInterface
36
{
37
    /**
38
     * Aggregated service.
39
     *
40
     * @var \eZ\Publish\API\Repository\UserService
41
     */
42
    protected $service;
43
44
    /**
45
     * SignalDispatcher.
46
     *
47
     * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher
48
     */
49
    protected $signalDispatcher;
50
51
    /**
52
     * Constructor.
53
     *
54
     * Construct service object from aggregated service and signal
55
     * dispatcher
56
     *
57
     * @param \eZ\Publish\API\Repository\UserService $service
58
     * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher
59
     */
60
    public function __construct(UserServiceInterface $service, SignalDispatcher $signalDispatcher)
61
    {
62
        $this->service = $service;
63
        $this->signalDispatcher = $signalDispatcher;
64
    }
65
66
    /**
67
     * Creates a new user group using the data provided in the ContentCreateStruct parameter.
68
     *
69
     * In 4.x in the content type parameter in the profile is ignored
70
     * - the content type is determined via configuration and can be set to null.
71
     * The returned version is published.
72
     *
73
     * @param \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct $userGroupCreateStruct a structure for setting all necessary data to create this user group
74
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $parentGroup
75
     *
76
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
77
     *
78
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group
79
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the input structure has invalid data
80
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userGroupCreateStruct is not valid
81
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing or set to an empty value
82
     */
83
    public function createUserGroup(UserGroupCreateStruct $userGroupCreateStruct, UserGroup $parentGroup)
84
    {
85
        $returnValue = $this->service->createUserGroup($userGroupCreateStruct, $parentGroup);
86
        $this->signalDispatcher->emit(
87
            new CreateUserGroupSignal(
88
                array(
89
                    'userGroupId' => $returnValue->id,
90
                )
91
            )
92
        );
93
94
        return $returnValue;
95
    }
96
97
    /**
98
     * Loads a user group for the given id.
99
     *
100
     * @param mixed $id
101
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
102
     *
103
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
104
     *
105
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group
106
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the user group with the given id was not found
107
     */
108
    public function loadUserGroup($id, array $prioritizedLanguages = [])
109
    {
110
        return $this->service->loadUserGroup($id, $prioritizedLanguages);
111
    }
112
113
    /**
114
     * Loads the sub groups of a user group.
115
     *
116
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
117
     * @param int $offset the start offset for paging
118
     * @param int $limit the number of user groups returned
119
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
120
     *
121
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup[]
122
     *
123
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read the user group
124
     */
125
    public function loadSubUserGroups(UserGroup $userGroup, $offset = 0, $limit = 25, array $prioritizedLanguages = [])
126
    {
127
        return $this->service->loadSubUserGroups($userGroup, $offset, $limit, $prioritizedLanguages);
128
    }
129
130
    /**
131
     * Removes a user group.
132
     *
133
     * the users which are not assigned to other groups will be deleted.
134
     *
135
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
136
     *
137
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a user group
138
     */
139 View Code Duplication
    public function deleteUserGroup(UserGroup $userGroup)
140
    {
141
        $returnValue = $this->service->deleteUserGroup($userGroup);
142
        $this->signalDispatcher->emit(
143
            new DeleteUserGroupSignal(
144
                array(
145
                    'userGroupId' => $userGroup->id,
146
                    'affectedLocationIds' => $returnValue,
147
                )
148
            )
149
        );
150
151
        return $returnValue;
152
    }
153
154
    /**
155
     * Moves the user group to another parent.
156
     *
157
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
158
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $newParent
159
     *
160
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group
161
     */
162 View Code Duplication
    public function moveUserGroup(UserGroup $userGroup, UserGroup $newParent)
163
    {
164
        $returnValue = $this->service->moveUserGroup($userGroup, $newParent);
165
        $this->signalDispatcher->emit(
166
            new MoveUserGroupSignal(
167
                array(
168
                    'userGroupId' => $userGroup->id,
169
                    'newParentId' => $newParent->id,
170
                )
171
            )
172
        );
173
174
        return $returnValue;
175
    }
176
177
    /**
178
     * Updates the group profile with fields and meta data.
179
     *
180
     * 4.x: If the versionUpdateStruct is set in $userGroupUpdateStruct, this method internally creates a content draft, updates ts with the provided data
181
     * and publishes the draft. If a draft is explicitly required, the user group can be updated via the content service methods.
182
     *
183
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
184
     * @param \eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct $userGroupUpdateStruct
185
     *
186
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup
187
     *
188
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group
189
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userGroupUpdateStruct is not valid
190
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set empty
191
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a field value is not accepted by the field type
192
     */
193
    public function updateUserGroup(UserGroup $userGroup, UserGroupUpdateStruct $userGroupUpdateStruct)
194
    {
195
        $returnValue = $this->service->updateUserGroup($userGroup, $userGroupUpdateStruct);
196
        $this->signalDispatcher->emit(
197
            new UpdateUserGroupSignal(
198
                array(
199
                    'userGroupId' => $userGroup->id,
200
                )
201
            )
202
        );
203
204
        return $returnValue;
205
    }
206
207
    /**
208
     * Create a new user. The created user is published by this method.
209
     *
210
     * @param \eZ\Publish\API\Repository\Values\User\UserCreateStruct $userCreateStruct the data used for creating the user
211
     * @param array $parentGroups the groups of type {@link \eZ\Publish\API\Repository\Values\User\UserGroup} which are assigned to the user after creation
212
     *
213
     * @return \eZ\Publish\API\Repository\Values\User\User
214
     *
215
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to move the user group
216
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userCreateStruct is not valid
217
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is missing or set  to an empty value
218
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a field value is not accepted by the field type
219
     *                                                                        if a user with provided login already exists
220
     */
221
    public function createUser(UserCreateStruct $userCreateStruct, array $parentGroups)
222
    {
223
        $returnValue = $this->service->createUser($userCreateStruct, $parentGroups);
224
        $this->signalDispatcher->emit(
225
            new CreateUserSignal(
226
                array(
227
                    'userId' => $returnValue->id,
228
                )
229
            )
230
        );
231
232
        return $returnValue;
233
    }
234
235
    /**
236
     * Loads a user.
237
     *
238
     * @param mixed $userId
239
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
240
     *
241
     * @return \eZ\Publish\API\Repository\Values\User\User
242
     *
243
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given id was not found
244
     */
245
    public function loadUser($userId, array $prioritizedLanguages = [])
246
    {
247
        return $this->service->loadUser($userId, $prioritizedLanguages);
248
    }
249
250
    /**
251
     * Loads anonymous user.
252
     *
253
     * @deprecated since 5.3, use loadUser( $anonymousUserId ) instead
254
     *
255
     * @uses ::loadUser()
256
     *
257
     * @return \eZ\Publish\API\Repository\Values\User\User
258
     */
259
    public function loadAnonymousUser()
260
    {
261
        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...
262
    }
263
264
    /**
265
     * Loads a user for the given login and password.
266
     *
267
     * {@inheritdoc}
268
     *
269
     * @param string $login
270
     * @param string $password the plain password
271
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
272
     *
273
     * @return \eZ\Publish\API\Repository\Values\User\User
274
     *
275
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if credentials are invalid
276
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given credentials was not found
277
     */
278
    public function loadUserByCredentials($login, $password, array $prioritizedLanguages = [])
279
    {
280
        return $this->service->loadUserByCredentials($login, $password, $prioritizedLanguages);
281
    }
282
283
    /**
284
     * Loads a user for the given login.
285
     *
286
     * {@inheritdoc}
287
     *
288
     * @param string $login
289
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
290
     *
291
     * @return \eZ\Publish\API\Repository\Values\User\User
292
     *
293
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given credentials was not found
294
     */
295
    public function loadUserByLogin($login, array $prioritizedLanguages = [])
296
    {
297
        return $this->service->loadUserByLogin($login, $prioritizedLanguages);
298
    }
299
300
    /**
301
     * Loads a user for the given email.
302
     *
303
     * {@inheritdoc}
304
     *
305
     * @param string $email
306
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
307
     *
308
     * @return \eZ\Publish\API\Repository\Values\User\User[]
309
     */
310
    public function loadUsersByEmail($email, array $prioritizedLanguages = [])
311
    {
312
        return $this->service->loadUsersByEmail($email, $prioritizedLanguages);
313
    }
314
315
    /**
316
     * Loads a user with user hash key.
317
     *
318
     * {@inheritdoc}
319
     *
320
     * @param string $hash
321
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
322
     *
323
     * @return \eZ\Publish\API\Repository\Values\User\User
324
     *
325
     * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given hash was not found
326
     */
327
    public function loadUserByToken($hash, array $prioritizedLanguages = [])
328
    {
329
        return $this->service->loadUserByToken($hash, $prioritizedLanguages);
330
    }
331
332
    /**
333
     * This method deletes a user.
334
     *
335
     * @param \eZ\Publish\API\Repository\Values\User\User $user
336
     *
337
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete the user
338
     */
339
    public function deleteUser(User $user)
340
    {
341
        $returnValue = $this->service->deleteUser($user);
342
        $this->signalDispatcher->emit(
343
            new DeleteUserSignal(
344
                array(
345
                    'userId' => $user->id,
346
                    'affectedLocationIds' => $returnValue,
347
                )
348
            )
349
        );
350
351
        return $returnValue;
352
    }
353
354
    /**
355
     * Updates a user.
356
     *
357
     * 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
358
     * and publishes the draft. If a draft is explicitly required, the user group can be updated via the content service methods.
359
     *
360
     * @param \eZ\Publish\API\Repository\Values\User\User $user
361
     * @param \eZ\Publish\API\Repository\Values\User\UserUpdateStruct $userUpdateStruct
362
     *
363
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update the user
364
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentFieldValidationException if a field in the $userUpdateStruct is not valid
365
     * @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if a required field is set empty
366
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if a field value is not accepted by the field type
367
     *
368
     * @return \eZ\Publish\API\Repository\Values\User\User
369
     */
370
    public function updateUser(User $user, UserUpdateStruct $userUpdateStruct)
371
    {
372
        $returnValue = $this->service->updateUser($user, $userUpdateStruct);
373
        $this->signalDispatcher->emit(
374
            new UpdateUserSignal(
375
                array(
376
                    'userId' => $user->id,
377
                )
378
            )
379
        );
380
381
        return $returnValue;
382
    }
383
384
    /**
385
     * Update the user account key information specified by the user account key struct.
386
     *
387
     * @param \eZ\Publish\API\Repository\Values\User\User $user
388
     * @param \eZ\Publish\API\Repository\Values\User\UserTokenUpdateStruct $userTokenUpdateStruct
389
     *
390
     * @return \eZ\Publish\API\Repository\Values\User\User
391
     */
392
    public function updateUserToken(User $user, UserTokenUpdateStruct $userTokenUpdateStruct)
393
    {
394
        $returnValue = $this->service->updateUserToken($user, $userTokenUpdateStruct);
395
        $this->signalDispatcher->emit(
396
            new UpdateUserTokenSignal(
397
                ['userId' => $user->id]
398
            )
399
        );
400
401
        return $returnValue;
402
    }
403
404
    /**
405
     * Expires user token with user hash.
406
     *
407
     * @param string $hash
408
     */
409
    public function expireUserToken($hash)
410
    {
411
        return $this->service->expireUserToken($hash);
412
    }
413
414
    /**
415
     * Assigns a new user group to the user.
416
     *
417
     * @param \eZ\Publish\API\Repository\Values\User\User $user
418
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
419
     *
420
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign the user group to the user
421
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the user is already in the given user group
422
     */
423 View Code Duplication
    public function assignUserToUserGroup(User $user, UserGroup $userGroup)
424
    {
425
        $returnValue = $this->service->assignUserToUserGroup($user, $userGroup);
426
        $this->signalDispatcher->emit(
427
            new AssignUserToUserGroupSignal(
428
                array(
429
                    'userId' => $user->id,
430
                    'userGroupId' => $userGroup->id,
431
                )
432
            )
433
        );
434
435
        return $returnValue;
436
    }
437
438
    /**
439
     * Removes a user group from the user.
440
     *
441
     * @param \eZ\Publish\API\Repository\Values\User\User $user
442
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
443
     *
444
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove the user group from the user
445
     * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the user is not in the given user group
446
     */
447 View Code Duplication
    public function unAssignUserFromUserGroup(User $user, UserGroup $userGroup)
448
    {
449
        $returnValue = $this->service->unAssignUserFromUserGroup($user, $userGroup);
450
        $this->signalDispatcher->emit(
451
            new UnAssignUserFromUserGroupSignal(
452
                array(
453
                    'userId' => $user->id,
454
                    'userGroupId' => $userGroup->id,
455
                )
456
            )
457
        );
458
459
        return $returnValue;
460
    }
461
462
    /**
463
     * Loads the user groups the user belongs to.
464
     *
465
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed read the user or user group
466
     *
467
     * @param \eZ\Publish\API\Repository\Values\User\User $user
468
     * @param int $offset the start offset for paging
469
     * @param int $limit the number of user groups returned
470
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
471
     *
472
     * @return \eZ\Publish\API\Repository\Values\User\UserGroup[]
473
     */
474
    public function loadUserGroupsOfUser(User $user, $offset = 0, $limit = 25, array $prioritizedLanguages = [])
475
    {
476
        return $this->service->loadUserGroupsOfUser($user, $offset, $limit, $prioritizedLanguages);
477
    }
478
479
    /**
480
     * Loads the users of a user group.
481
     *
482
     * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read the users or user group
483
     *
484
     * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup
485
     * @param int $offset the start offset for paging
486
     * @param int $limit the number of users returned
487
     * @param string[] $prioritizedLanguages Used as prioritized language code on translated properties of returned object.
488
     *
489
     * @return \eZ\Publish\API\Repository\Values\User\User[]
490
     */
491
    public function loadUsersOfUserGroup(
492
        UserGroup $userGroup,
493
        $offset = 0,
494
        $limit = 25,
495
        array $prioritizedLanguages = []
496
    ) {
497
        return $this->service->loadUsersOfUserGroup(
498
            $userGroup,
499
            $offset,
500
            $limit,
501
            $prioritizedLanguages
502
        );
503
    }
504
505
    /**
506
     * {@inheritdoc}
507
     */
508
    public function isUser(Content $content): bool
509
    {
510
        return $this->service->isUser($content);
511
    }
512
513
    /**
514
     * {@inheritdoc}
515
     */
516
    public function isUserGroup(Content $content): bool
517
    {
518
        return $this->service->isUserGroup($content);
519
    }
520
521
    /**
522
     * Instantiate a user create class.
523
     *
524
     * @param string $login the login of the new user
525
     * @param string $email the email of the new user
526
     * @param string $password the plain password of the new user
527
     * @param string $mainLanguageCode the main language for the underlying content object
528
     * @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
529
     *
530
     * @return \eZ\Publish\API\Repository\Values\User\UserCreateStruct
531
     */
532
    public function newUserCreateStruct($login, $email, $password, $mainLanguageCode, $contentType = null)
533
    {
534
        return $this->service->newUserCreateStruct($login, $email, $password, $mainLanguageCode, $contentType);
535
    }
536
537
    /**
538
     * Instantiate a user group create class.
539
     *
540
     * @param string $mainLanguageCode The main language for the underlying content object
541
     * @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
542
     *
543
     * @return \eZ\Publish\API\Repository\Values\User\UserGroupCreateStruct
544
     */
545
    public function newUserGroupCreateStruct($mainLanguageCode, $contentType = null)
546
    {
547
        return $this->service->newUserGroupCreateStruct($mainLanguageCode, $contentType);
548
    }
549
550
    /**
551
     * Instantiate a new user update struct.
552
     *
553
     * @return \eZ\Publish\API\Repository\Values\User\UserUpdateStruct
554
     */
555
    public function newUserUpdateStruct()
556
    {
557
        return $this->service->newUserUpdateStruct();
558
    }
559
560
    /**
561
     * Instantiate a new user group update struct.
562
     *
563
     * @return \eZ\Publish\API\Repository\Values\User\UserGroupUpdateStruct
564
     */
565
    public function newUserGroupUpdateStruct()
566
    {
567
        return $this->service->newUserGroupUpdateStruct();
568
    }
569
570
    /**
571
     * {@inheritdoc}
572
     */
573
    public function validatePassword(string $password, PasswordValidationContext $context = null): array
574
    {
575
        return $this->service->validatePassword($password, $context);
576
    }
577
}
578