1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Transfer. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE file located |
7
|
|
|
* in the root directory. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Transfer\EzPlatform\Repository\Manager; |
11
|
|
|
|
12
|
|
|
use eZ\Publish\API\Repository\Repository; |
13
|
|
|
use eZ\Publish\API\Repository\UserService; |
14
|
|
|
use eZ\Publish\API\Repository\Values\User\User; |
15
|
|
|
use Psr\Log\LoggerAwareInterface; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use Transfer\Data\ObjectInterface; |
18
|
|
|
use Transfer\EzPlatform\Data\UserGroupObject; |
19
|
|
|
use Transfer\EzPlatform\Data\UserObject; |
20
|
|
|
use Transfer\EzPlatform\Exception\UserNotFoundException; |
21
|
|
|
use Transfer\EzPlatform\Repository\Manager\Type\CreatorInterface; |
22
|
|
|
use Transfer\EzPlatform\Repository\Manager\Type\RemoverInterface; |
23
|
|
|
use Transfer\EzPlatform\Repository\Manager\Type\UpdaterInterface; |
24
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* User manager. |
28
|
|
|
*/ |
29
|
|
|
class UserManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var Repository |
33
|
|
|
*/ |
34
|
|
|
private $repository; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var LoggerInterface |
38
|
|
|
*/ |
39
|
|
|
private $logger; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var UserService |
43
|
|
|
*/ |
44
|
|
|
private $userService; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var UserGroupManager |
48
|
|
|
*/ |
49
|
|
|
private $userGroupManager; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Repository $repository |
53
|
|
|
* @param UserGroupManager $userGroupManager |
54
|
|
|
*/ |
55
|
2 |
|
public function __construct(Repository $repository, UserGroupManager $userGroupManager) |
56
|
|
|
{ |
57
|
2 |
|
$this->repository = $repository; |
58
|
2 |
|
$this->userService = $repository->getUserService(); |
59
|
2 |
|
$this->userGroupManager = $userGroupManager; |
60
|
2 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
3 |
|
public function setLogger(LoggerInterface $logger) |
66
|
|
|
{ |
67
|
3 |
|
$this->logger = $logger; |
68
|
3 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Finds user object by username. |
72
|
|
|
* |
73
|
|
|
* @param string $username |
74
|
|
|
* |
75
|
|
|
* @return User|false |
76
|
|
|
*/ |
77
|
9 |
|
public function findByUsername($username) |
78
|
|
|
{ |
79
|
9 |
|
if (!is_string($username)) { |
80
|
1 |
|
return false; |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
try { |
84
|
8 |
|
$user = $this->userService->loadUserByLogin($username); |
85
|
8 |
|
} catch (NotFoundException $e) { |
86
|
4 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
4 |
|
return $user; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
4 |
|
public function create(ObjectInterface $object) |
96
|
|
|
{ |
97
|
4 |
|
if (!$object instanceof UserObject) { |
98
|
1 |
|
return; |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
$userCreateStruct = $this->userService->newUserCreateStruct( |
102
|
3 |
|
$object->data['username'], |
103
|
3 |
|
$object->data['email'], |
104
|
3 |
|
$object->data['password'], |
105
|
3 |
|
$object->data['main_language_code'] |
106
|
3 |
|
); |
107
|
|
|
|
108
|
3 |
|
$object->getMapper()->getNewUserCreateStruct($userCreateStruct); |
109
|
|
|
|
110
|
3 |
|
$groups = []; |
111
|
3 |
|
foreach ($object->parents as $userGroup) { |
112
|
3 |
|
$userGroup = $this->userGroupManager->createOrUpdate($userGroup); |
113
|
3 |
|
if ($userGroup instanceof UserGroupObject) { |
114
|
3 |
|
$groups[] = $this->userGroupManager->find($userGroup->data['id']); |
115
|
3 |
|
} |
116
|
3 |
|
} |
117
|
|
|
|
118
|
3 |
|
$user = $this->userService->createUser($userCreateStruct, $groups); |
119
|
3 |
|
$object->data['id'] = $user->getUserId(); |
120
|
|
|
|
121
|
3 |
|
return $object; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
7 |
|
public function update(ObjectInterface $object) |
128
|
6 |
|
{ |
129
|
5 |
|
if (!$object instanceof UserObject) { |
130
|
1 |
|
return; |
131
|
|
|
} |
132
|
|
|
|
133
|
4 |
|
$user = $this->findByUsername($object->data['username']); |
134
|
|
|
|
135
|
4 |
|
if (!$user) { |
136
|
1 |
|
throw new UserNotFoundException(sprintf('User with username "%s" not found.', $object->data['username'])); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Populate struct |
140
|
3 |
|
$userUpdateStruct = $this->userService->newUserUpdateStruct(); |
141
|
3 |
|
$object->getMapper()->getNewUserUpdateStruct($userUpdateStruct); |
142
|
|
|
|
143
|
|
|
// Update user |
144
|
3 |
|
$ezuser = $this->userService->updateUser($user, $userUpdateStruct); |
145
|
|
|
|
146
|
|
|
// Assign user to usergroups |
147
|
3 |
|
$ezUserGroups = []; |
148
|
7 |
|
foreach ($object->parents as $userGroup) { |
149
|
3 |
|
$userGroup = $this->userGroupManager->createOrUpdate($userGroup); |
150
|
3 |
|
if ($userGroup instanceof UserGroupObject) { |
151
|
3 |
|
$ezUserGroup = $this->userGroupManager->find($userGroup->data['id']); |
152
|
3 |
|
if ($ezUserGroup) { |
153
|
3 |
|
$ezUserGroups[$ezUserGroup->id] = $ezUserGroup; |
154
|
3 |
|
$this->userService->assignUserToUserGroup($ezuser, $ezUserGroup); |
155
|
3 |
|
} |
156
|
3 |
|
} |
157
|
3 |
|
} |
158
|
|
|
|
159
|
|
|
// Unassign user from usergroups |
160
|
3 |
|
$existingUserGroups = $this->userService->loadUserGroupsOfUser($ezuser); |
161
|
3 |
|
foreach ($existingUserGroups as $existingUserGroup) { |
162
|
3 |
|
if (!array_key_exists($existingUserGroup->id, $ezUserGroups)) { |
163
|
3 |
|
$this->userService->unAssignUserFromUserGroup($ezuser, $existingUserGroup); |
164
|
3 |
|
} |
165
|
3 |
|
} |
166
|
|
|
|
167
|
3 |
|
return $object; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* {@inheritdoc} |
172
|
|
|
*/ |
173
|
5 |
|
public function createOrUpdate(ObjectInterface $object) |
174
|
|
|
{ |
175
|
5 |
|
if (!$object instanceof UserObject) { |
176
|
4 |
|
return; |
177
|
|
|
} |
178
|
|
|
|
179
|
4 |
|
if (!$this->findByUsername($object->data['username'])) { |
180
|
2 |
|
return $this->create($object); |
181
|
|
|
} else { |
182
|
2 |
|
return $this->update($object); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
3 |
|
public function remove(ObjectInterface $object) |
190
|
|
|
{ |
191
|
3 |
|
if (!$object instanceof UserObject) { |
192
|
1 |
|
return; |
193
|
|
|
} |
194
|
|
|
|
195
|
2 |
|
$user = $this->findByUsername($object->data['username']); |
196
|
|
|
|
197
|
2 |
|
if ($user) { |
198
|
1 |
|
$this->userService->deleteUser($user); |
199
|
1 |
|
} |
200
|
|
|
|
201
|
2 |
|
return true; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|