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\ContentService; |
13
|
|
|
use eZ\Publish\API\Repository\ContentTypeService; |
14
|
|
|
use eZ\Publish\API\Repository\Repository; |
15
|
|
|
use eZ\Publish\API\Repository\UserService; |
16
|
|
|
use Psr\Log\LoggerAwareInterface; |
17
|
|
|
use Psr\Log\LoggerInterface; |
18
|
|
|
use Transfer\Data\ObjectInterface; |
19
|
|
|
use Transfer\EzPlatform\Data\UserGroupObject; |
20
|
|
|
use Transfer\EzPlatform\Exception\UserGroupNotFoundException; |
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\Values\User\UserGroup; |
25
|
|
|
use eZ\Publish\API\Repository\Exceptions\NotFoundException; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* User Group manager. |
29
|
|
|
* |
30
|
|
|
* @internal |
31
|
|
|
*/ |
32
|
|
|
class UserGroupManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Repository |
36
|
|
|
*/ |
37
|
|
|
private $repository; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var LoggerInterface |
41
|
|
|
*/ |
42
|
|
|
private $logger; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var UserService |
46
|
|
|
*/ |
47
|
|
|
private $userService; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ContentService |
51
|
|
|
*/ |
52
|
|
|
private $contentService; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ContentTypeService |
56
|
|
|
*/ |
57
|
|
|
private $contentTypeService; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Repository $repository |
61
|
|
|
*/ |
62
|
3 |
|
public function __construct(Repository $repository) |
63
|
|
|
{ |
64
|
3 |
|
$this->repository = $repository; |
65
|
3 |
|
$this->userService = $repository->getUserService(); |
66
|
3 |
|
$this->contentService = $repository->getContentService(); |
67
|
3 |
|
$this->contentTypeService = $repository->getContentTypeService(); |
68
|
3 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
4 |
|
public function setLogger(LoggerInterface $logger) |
74
|
|
|
{ |
75
|
4 |
|
$this->logger = $logger; |
76
|
4 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Shortcut to load a usergroup by id, without throwing an exception if it's not found. |
80
|
|
|
* |
81
|
|
|
* @param int $id |
82
|
|
|
* |
83
|
|
|
* @return UserGroup|false |
84
|
|
|
*/ |
85
|
15 |
|
public function find($id) |
86
|
|
|
{ |
87
|
|
|
try { |
88
|
15 |
|
return $this->userService->loadUserGroup($id); |
89
|
4 |
|
} catch (NotFoundException $e) { |
90
|
4 |
|
return false; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
14 |
|
public function create(ObjectInterface $object) |
98
|
|
|
{ |
99
|
14 |
|
if (!$object instanceof UserGroupObject) { |
100
|
1 |
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
13 |
|
$parentUserGroup = $this->find($object->data['parent_id']); |
104
|
|
|
|
105
|
13 |
|
if (!$parentUserGroup) { |
106
|
1 |
|
throw new UserGroupNotFoundException(sprintf('Usergroup with parent_id "%s" not found.', $object->data['parent_id'])); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Instantiate usergroup |
110
|
12 |
|
$contentType = $this->contentTypeService->loadContentTypeByIdentifier($object->data['content_type_identifier']); |
111
|
12 |
|
$userGroupCreateStruct = $this->userService->newUserGroupCreateStruct( |
112
|
12 |
|
$object->data['main_language_code'], |
113
|
|
|
$contentType |
114
|
12 |
|
); |
115
|
|
|
|
116
|
|
|
// Populate usergroup fields |
117
|
12 |
|
$object->getMapper()->populateUserGroupCreateStruct($userGroupCreateStruct); |
118
|
|
|
|
119
|
|
|
// Create usergroup |
120
|
12 |
|
$userGroup = $this->userService->createUserGroup($userGroupCreateStruct, $parentUserGroup); |
121
|
12 |
|
$object->data['id'] = $userGroup->id; |
122
|
|
|
|
123
|
12 |
|
return $object; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
*/ |
129
|
6 |
|
public function update(ObjectInterface $object) |
130
|
|
|
{ |
131
|
6 |
|
if (!$object instanceof UserGroupObject) { |
132
|
1 |
|
return; |
133
|
|
|
} |
134
|
|
|
|
135
|
5 |
|
if (!array_key_exists('id', $object->data)) { |
136
|
1 |
|
throw new UserGroupNotFoundException('Unable to update usergroup without an id.'); |
137
|
|
|
} |
138
|
|
|
|
139
|
4 |
|
$userGroup = $this->find($object->data['id']); |
140
|
|
|
|
141
|
4 |
|
if (!$userGroup) { |
142
|
1 |
|
throw new UserGroupNotFoundException(sprintf('Usergroup with id "%s" not found.', $object->data['id'])); |
143
|
|
|
} |
144
|
|
|
|
145
|
3 |
|
$userGroupUpdateStruct = $this->userService->newUserGroupUpdateStruct(); |
146
|
3 |
|
$userGroupUpdateStruct->contentUpdateStruct = $this->contentService->newContentUpdateStruct(); |
147
|
|
|
|
148
|
3 |
|
$object->getMapper()->populateUserGroupUpdateStruct($userGroupUpdateStruct); |
149
|
|
|
|
150
|
3 |
|
$this->userService->updateUserGroup($userGroup, $userGroupUpdateStruct); |
151
|
|
|
|
152
|
3 |
|
if ($userGroup->parentId !== $object->data['parent_id']) { |
153
|
1 |
|
$newParentGroup = $this->find($object->data['parent_id']); |
154
|
1 |
|
$this->userService->moveUserGroup($userGroup, $newParentGroup); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
3 |
|
return $object; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* {@inheritdoc} |
162
|
|
|
*/ |
163
|
10 |
|
public function createOrUpdate(ObjectInterface $object) |
164
|
|
|
{ |
165
|
10 |
|
if (!$object instanceof UserGroupObject) { |
166
|
1 |
|
return; |
167
|
|
|
} |
168
|
|
|
|
169
|
9 |
|
if (isset($object->data['id'])) { |
170
|
1 |
|
$userGroup = $this->find($object->data['id']); |
171
|
1 |
|
} |
172
|
9 |
|
if (!isset($userGroup) || false === $userGroup) { |
173
|
9 |
|
return $this->create($object); |
174
|
|
|
} else { |
175
|
1 |
|
return $this->update($object); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
4 |
|
public function remove(ObjectInterface $object) |
183
|
|
|
{ |
184
|
4 |
|
if (!$object instanceof UserGroupObject) { |
185
|
1 |
|
return; |
186
|
|
|
} |
187
|
|
|
|
188
|
3 |
|
if (array_key_exists('id', $object->data)) { |
189
|
2 |
|
$userGroup = $this->find($object->data['id']); |
190
|
2 |
|
} else { |
191
|
1 |
|
throw new UserGroupNotFoundException(sprintf('Usergroup with id "%s" not found.', '')); |
192
|
|
|
} |
193
|
|
|
|
194
|
2 |
|
if (!$userGroup) { |
195
|
1 |
|
throw new UserGroupNotFoundException(sprintf('Usergroup with id "%s" not found.', $object->data['id'])); |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
$this->userService->deleteUserGroup($userGroup); |
199
|
|
|
|
200
|
1 |
|
return true; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|