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