forked from kaliop-uk/ezmigrationbundle
/
UserManager.php
257 lines (210 loc) · 8.69 KB
/
UserManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
namespace Kaliop\eZMigrationBundle\Core\Executor;
use Kaliop\eZMigrationBundle\API\Collection\UserCollection;
use Kaliop\eZMigrationBundle\Core\Matcher\UserGroupMatcher;
use Kaliop\eZMigrationBundle\Core\Matcher\UserMatcher;
/**
* Handles user migrations.
*/
class UserManager extends RepositoryExecutor
{
protected $supportedStepTypes = array('user');
protected $userMatcher;
protected $userGroupMatcher;
public function __construct(UserMatcher $userMatcher, UserGroupMatcher $userGroupMatcher)
{
$this->userMatcher = $userMatcher;
$this->userGroupMatcher = $userGroupMatcher;
}
/**
* Creates a user based on the DSL instructions.
*
* @todo allow setting extra profile attributes!
*/
protected function create($step)
{
if (!isset($step->dsl['groups'])) {
throw new \Exception('No user groups set to create user in.');
}
if (!is_array($step->dsl['groups'])) {
$step->dsl['groups'] = array($step->dsl['groups']);
}
$userService = $this->repository->getUserService();
$contentTypeService = $this->repository->getContentTypeService();
$userGroups = array();
foreach ($step->dsl['groups'] as $groupId) {
$groupId = $this->referenceResolver->resolveReference($groupId);
$userGroup = $this->userGroupMatcher->matchOneByKey($groupId);
// q: in which case can we have no group? And should we throw an exception?
//if ($userGroup) {
$userGroups[] = $userGroup;
//}
}
// FIXME: Hard coding content type to user for now
$userContentType = $contentTypeService->loadContentTypeByIdentifier($this->getUserContentType($step));
$userCreateStruct = $userService->newUserCreateStruct(
$step->dsl['username'],
$step->dsl['email'],
$step->dsl['password'],
$this->getLanguageCode($step),
$userContentType
);
$userCreateStruct->setField('first_name', $step->dsl['first_name']);
$userCreateStruct->setField('last_name', $step->dsl['last_name']);
// Create the user
$user = $userService->createUser($userCreateStruct, $userGroups);
$this->setReferences($user, $step);
return $user;
}
/**
* Method to handle the update operation of the migration instructions
*
* @todo allow setting extra profile attributes!
*/
protected function update($step)
{
$userCollection = $this->matchUsers('user', $step);
if (count($userCollection) > 1 && isset($step->dsl['references'])) {
throw new \Exception("Can not execute User update because multiple user match, and a references section is specified in the dsl. References can be set when only 1 user matches");
}
if (count($userCollection) > 1 && isset($step->dsl['email'])) {
throw new \Exception("Can not execute User update because multiple user match, and an email section is specified in the dsl.");
}
$userService = $this->repository->getUserService();
foreach ($userCollection as $key => $user) {
$userUpdateStruct = $userService->newUserUpdateStruct();
if (isset($step->dsl['email'])) {
$userUpdateStruct->email = $step->dsl['email'];
}
if (isset($step->dsl['password'])) {
$userUpdateStruct->password = (string)$step->dsl['password'];
}
if (isset($step->dsl['enabled'])) {
$userUpdateStruct->enabled = $step->dsl['enabled'];
}
$user = $userService->updateUser($user, $userUpdateStruct);
if (isset($step->dsl['groups'])) {
$groups = $step->dsl['groups'];
if (!is_array($groups)) {
$groups = array($groups);
}
$assignedGroups = $userService->loadUserGroupsOfUser($user);
$targetGroupIds = [];
// Assigning new groups to the user
foreach ($groups as $groupToAssignId) {
$groupId = $this->referenceResolver->resolveReference($groupToAssignId);
$groupToAssign = $this->userGroupMatcher->matchOneByKey($groupId);
$targetGroupIds[] = $groupToAssign->id;
$present = false;
foreach ($assignedGroups as $assignedGroup) {
// Make sure we assign the user only to groups he isn't already assigned to
if ($assignedGroup->id == $groupToAssign->id) {
$present = true;
break;
}
}
if (!$present) {
$userService->assignUserToUserGroup($user, $groupToAssign);
}
}
// Unassigning groups that are not in the list in the migration
foreach ($assignedGroups as $assignedGroup) {
if (!in_array($assignedGroup->id, $targetGroupIds)) {
$userService->unAssignUserFromUserGroup($user, $assignedGroup);
}
}
}
$userCollection[$key] = $user;
}
$this->setReferences($userCollection, $step);
return $userCollection;
}
/**
* Method to handle the delete operation of the migration instructions
*/
protected function delete($step)
{
$userCollection = $this->matchUsers('delete', $step);
$this->setReferences($userCollection, $step);
$userService = $this->repository->getUserService();
foreach ($userCollection as $user) {
$userService->deleteUser($user);
}
return $userCollection;
}
/**
* @param string $action
* @return UserCollection
* @throws \Exception
*/
protected function matchUsers($action, $step)
{
if (!isset($step->dsl['id']) && !isset($step->dsl['user_id']) && !isset($step->dsl['email']) && !isset($step->dsl['username']) && !isset($step->dsl['match'])) {
throw new \Exception("The id, email or username of a user or a match condition is required to $action it");
}
// Backwards compat
if (isset($step->dsl['match'])) {
$match = $step->dsl['match'];
} else {
$conds = array();
if (isset($step->dsl['id'])) {
$conds['id'] = $step->dsl['id'];
}
if (isset($step->dsl['user_id'])) {
$conds['id'] = $step->dsl['user_id'];
}
if (isset($step->dsl['email'])) {
$conds['email'] = $step->dsl['email'];
}
if (isset($step->dsl['username'])) {
$conds['login'] = $step->dsl['username'];
}
$match = $conds;
}
// convert the references passed in the match
$match = $this->resolveReferencesRecursively($match);
return $this->userMatcher->match($match);
}
/**
* Sets references to object attributes.
*
* The User manager currently only supports setting references to user_id.
*
* @param \eZ\Publish\API\Repository\Values\User\User|UserCollection $user
* @throws \InvalidArgumentException when trying to set references to unsupported attributes
* @return boolean
*/
protected function setReferences($user, $step)
{
if (!array_key_exists('references', $step->dsl)) {
return false;
}
$this->setReferencesCommon($user, $step);
$user = $this->insureSingleEntity($user, $step);
foreach ($step->dsl['references'] as $reference) {
switch ($reference['attribute']) {
case 'user_id':
case 'id':
$value = $user->id;
break;
case 'email':
$value = $user->email;
break;
case 'enabled':
$value = $user->enabled;
break;
case 'login':
$value = $user->login;
break;
default:
throw new \InvalidArgumentException('User Manager does not support setting references for attribute ' . $reference['attribute']);
}
$overwrite = false;
if (isset($reference['overwrite'])) {
$overwrite = $reference['overwrite'];
}
$this->referenceResolver->addReference($reference['identifier'], $value, $overwrite);
}
return true;
}
}