Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like RoleService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RoleService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class RoleService implements APIRoleService, Sessionable |
||
46 | { |
||
47 | /** |
||
48 | * @var \eZ\Publish\Core\REST\Client\UserService |
||
49 | */ |
||
50 | private $userService; |
||
51 | |||
52 | /** |
||
53 | * @var \eZ\Publish\Core\REST\Client\HttpClient |
||
54 | */ |
||
55 | private $client; |
||
56 | |||
57 | /** |
||
58 | * @var \eZ\Publish\Core\REST\Common\Input\Dispatcher |
||
59 | */ |
||
60 | private $inputDispatcher; |
||
61 | |||
62 | /** |
||
63 | * @var \eZ\Publish\Core\REST\Common\Output\Visitor |
||
64 | */ |
||
65 | private $outputVisitor; |
||
66 | |||
67 | /** |
||
68 | * @var \eZ\Publish\Core\REST\Common\RequestParser |
||
69 | */ |
||
70 | private $requestParser; |
||
71 | |||
72 | /** |
||
73 | * @param \eZ\Publish\Core\REST\Client\UserService $userService |
||
74 | * @param \eZ\Publish\Core\REST\Client\HttpClient $client |
||
75 | * @param \eZ\Publish\Core\REST\Common\Input\Dispatcher $inputDispatcher |
||
76 | * @param \eZ\Publish\Core\REST\Common\Output\Visitor $outputVisitor |
||
77 | * @param \eZ\Publish\Core\REST\Common\RequestParser $requestParser |
||
78 | */ |
||
79 | View Code Duplication | public function __construct(UserService $userService, HttpClient $client, Dispatcher $inputDispatcher, Visitor $outputVisitor, RequestParser $requestParser) |
|
80 | { |
||
81 | $this->userService = $userService; |
||
82 | $this->client = $client; |
||
83 | $this->inputDispatcher = $inputDispatcher; |
||
84 | $this->outputVisitor = $outputVisitor; |
||
85 | $this->requestParser = $requestParser; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Set session ID. |
||
90 | * |
||
91 | * Only for testing |
||
92 | * |
||
93 | * @param mixed $id |
||
94 | * |
||
95 | * @private |
||
96 | */ |
||
97 | public function setSession($id) |
||
98 | { |
||
99 | if ($this->outputVisitor instanceof Sessionable) { |
||
100 | $this->outputVisitor->setSession($id); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Creates a new RoleDraft for existing Role. |
||
106 | * |
||
107 | * @since 6.0 |
||
108 | * |
||
109 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a role |
||
110 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the Role already has a Role Draft that will need to be removed first |
||
111 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a policy limitation in the $roleCreateStruct is not valid |
||
112 | * |
||
113 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
114 | * |
||
115 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
116 | */ |
||
117 | public function createRoleDraft(APIRole $role) |
||
121 | |||
122 | /** |
||
123 | * Loads a role for the given id. |
||
124 | * |
||
125 | * @since 6.0 |
||
126 | * |
||
127 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
128 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a role with the given id was not found |
||
129 | * |
||
130 | * @param mixed $id |
||
131 | * |
||
132 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
133 | */ |
||
134 | public function loadRoleDraft($id) |
||
138 | |||
139 | /** |
||
140 | * Loads a RoleDraft by the ID of the role it was created from. |
||
141 | * |
||
142 | * @param mixed $roleId ID of the role the draft was created from. |
||
143 | * |
||
144 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
145 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a RoleDraft with the given id was not found |
||
146 | * |
||
147 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
148 | */ |
||
149 | public function loadRoleDraftByRoleId($roleId) |
||
153 | |||
154 | /** |
||
155 | * Updates the properties of a role draft. |
||
156 | * |
||
157 | * @since 6.0 |
||
158 | * |
||
159 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a role |
||
160 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the identifier of the role already exists |
||
161 | * |
||
162 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
163 | * @param \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct $roleUpdateStruct |
||
164 | * |
||
165 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
166 | */ |
||
167 | public function updateRoleDraft(APIRoleDraft $roleDraft, RoleUpdateStruct $roleUpdateStruct) |
||
171 | |||
172 | /** |
||
173 | * Adds a new policy to the role draft. |
||
174 | * |
||
175 | * @since 6.0 |
||
176 | * |
||
177 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to add a policy |
||
178 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy create |
||
179 | * struct or if limitation is not allowed on module/function |
||
180 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyCreateStruct is not valid |
||
181 | * |
||
182 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
183 | * @param \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStruct |
||
184 | * |
||
185 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
186 | */ |
||
187 | public function addPolicyByRoleDraft(APIRoleDraft $roleDraft, APIPolicyCreateStruct $policyCreateStruct) |
||
191 | |||
192 | /** |
||
193 | * Removes a policy from a role draft. |
||
194 | * |
||
195 | * @since 6.0 |
||
196 | * |
||
197 | * |
||
198 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
199 | * @param PolicyDraft $policyDraft the policy to remove from the role |
||
200 | * @return APIRoleDraft if the authenticated user is not allowed to remove a policy |
||
201 | */ |
||
202 | public function removePolicyByRoleDraft(APIRoleDraft $roleDraft, PolicyDraft $policyDraft) |
||
206 | |||
207 | /** |
||
208 | * Updates the limitations of a policy. The module and function cannot be changed and |
||
209 | * the limitations are replaced by the ones in $roleUpdateStruct. |
||
210 | * |
||
211 | * @since 6.0 |
||
212 | * |
||
213 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a policy |
||
214 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy update |
||
215 | * struct or if limitation is not allowed on module/function |
||
216 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyUpdateStruct is not valid |
||
217 | * |
||
218 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
219 | * @param \eZ\Publish\API\Repository\Values\User\PolicyDraft $policy |
||
220 | * @param \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct $policyUpdateStruct |
||
221 | * |
||
222 | * @return \eZ\Publish\API\Repository\Values\User\PolicyDraft |
||
223 | */ |
||
224 | public function updatePolicyByRoleDraft(APIRoleDraft $roleDraft, PolicyDraft $policy, APIPolicyUpdateStruct $policyUpdateStruct) |
||
228 | |||
229 | /** |
||
230 | * Deletes the given role. |
||
231 | * |
||
232 | * @since 6.0 |
||
233 | * |
||
234 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete this role |
||
235 | * |
||
236 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
237 | */ |
||
238 | public function deleteRoleDraft(APIRoleDraft $roleDraft) |
||
242 | |||
243 | /** |
||
244 | * Publishes a given Role draft. |
||
245 | * |
||
246 | * @since 6.0 |
||
247 | * |
||
248 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete this role |
||
249 | * |
||
250 | * @param \eZ\Publish\API\Repository\Values\User\RoleDraft $roleDraft |
||
251 | */ |
||
252 | public function publishRoleDraft(APIRoleDraft $roleDraft) |
||
256 | |||
257 | /** |
||
258 | * Creates a new Role draft. |
||
259 | * |
||
260 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to create a role |
||
261 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the name of the role already exists or if limitation of the |
||
262 | * same type is repeated in the policy create struct or if |
||
263 | * limitation is not allowed on module/function |
||
264 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a policy limitation in the $roleCreateStruct is not valid |
||
265 | * |
||
266 | * @param \eZ\Publish\API\Repository\Values\User\RoleCreateStruct $roleCreateStruct |
||
267 | * |
||
268 | * @return \eZ\Publish\API\Repository\Values\User\RoleDraft |
||
269 | */ |
||
270 | public function createRole(APIRoleCreateStruct $roleCreateStruct) |
||
271 | { |
||
272 | $inputMessage = $this->outputVisitor->visit($roleCreateStruct); |
||
273 | $inputMessage->headers['Accept'] = $this->outputVisitor->getMediaType('Role'); |
||
274 | |||
275 | $result = $this->client->request( |
||
276 | 'POST', |
||
277 | $this->requestParser->generate('roles'), |
||
278 | $inputMessage |
||
|
|||
279 | ); |
||
280 | |||
281 | $createdRole = $this->inputDispatcher->parse($result); |
||
282 | $createdRoleValues = $this->requestParser->parse('role', $createdRole->id); |
||
283 | |||
284 | $createdPolicies = array(); |
||
285 | foreach ($roleCreateStruct->getPolicies() as $policyCreateStruct) { |
||
286 | $inputMessage = $this->outputVisitor->visit($policyCreateStruct); |
||
287 | $inputMessage->headers['Accept'] = $this->outputVisitor->getMediaType('Policy'); |
||
288 | |||
289 | $result = $this->client->request( |
||
290 | 'POST', |
||
291 | $this->requestParser->generate('policies', array('role' => $createdRoleValues['role'])), |
||
292 | $inputMessage |
||
293 | ); |
||
294 | |||
295 | $createdPolicy = $this->inputDispatcher->parse($result); |
||
296 | |||
297 | // @todo Workaround for missing roleId in Policy XSD definition |
||
298 | $createdPolicyArray = array( |
||
299 | 'id' => $createdPolicy->id, |
||
300 | 'roleId' => $createdRole->id, |
||
301 | 'module' => $createdPolicy->module, |
||
302 | 'function' => $createdPolicy->function, |
||
303 | ); |
||
304 | |||
305 | $createdPolicy = new Policy($createdPolicyArray); |
||
306 | $createdPolicies[] = $createdPolicy; |
||
307 | } |
||
308 | |||
309 | return new RoleDraft( |
||
310 | array( |
||
311 | 'id' => $createdRole->id, |
||
312 | 'identifier' => $createdRole->identifier, |
||
313 | ), |
||
314 | $createdPolicies |
||
315 | ); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * Updates the name of the role. |
||
320 | * |
||
321 | * @deprecated since 6.0, use {@see updateRoleDraft} |
||
322 | * |
||
323 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a role |
||
324 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the name of the role already exists |
||
325 | * |
||
326 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
327 | * @param \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct $roleUpdateStruct |
||
328 | * |
||
329 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
330 | */ |
||
331 | public function updateRole(APIRole $role, RoleUpdateStruct $roleUpdateStruct) |
||
332 | { |
||
333 | $inputMessage = $this->outputVisitor->visit($roleUpdateStruct); |
||
334 | $inputMessage->headers['Accept'] = $this->outputVisitor->getMediaType('Role'); |
||
335 | $inputMessage->headers['X-HTTP-Method-Override'] = 'PATCH'; |
||
336 | |||
337 | $result = $this->client->request( |
||
338 | 'POST', |
||
339 | $role->id, |
||
340 | $inputMessage |
||
341 | ); |
||
342 | |||
343 | return $this->inputDispatcher->parse($result); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Adds a new policy to the role. |
||
348 | * |
||
349 | * @deprecated since 6.0, use {@see addPolicyByRoleDraft} |
||
350 | * |
||
351 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to add a policy |
||
352 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy create |
||
353 | * struct or if limitation is not allowed on module/function |
||
354 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyCreateStruct is not valid |
||
355 | * |
||
356 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
357 | * @param \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct $policyCreateStruct |
||
358 | * |
||
359 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
360 | */ |
||
361 | public function addPolicy(APIRole $role, APIPolicyCreateStruct $policyCreateStruct) |
||
362 | { |
||
363 | $values = $this->requestParser->parse('role', $role->id); |
||
364 | $inputMessage = $this->outputVisitor->visit($policyCreateStruct); |
||
365 | $inputMessage->headers['Accept'] = $this->outputVisitor->getMediaType('Policy'); |
||
366 | |||
367 | $result = $this->client->request( |
||
368 | 'POST', |
||
369 | $this->requestParser->generate('policies', array('role' => $values['role'])), |
||
370 | $inputMessage |
||
371 | ); |
||
372 | |||
373 | $createdPolicy = $this->inputDispatcher->parse($result); |
||
374 | |||
375 | // @todo Workaround for missing roleId in Policy XSD definition |
||
376 | $createdPolicyArray = array( |
||
377 | 'id' => $createdPolicy->id, |
||
378 | 'roleId' => $role->id, |
||
379 | 'module' => $createdPolicy->module, |
||
380 | 'function' => $createdPolicy->function, |
||
381 | ); |
||
382 | |||
383 | $createdPolicy = new Policy($createdPolicyArray); |
||
384 | |||
385 | $existingPolicies = $role->getPolicies(); |
||
386 | $existingPolicies[] = $createdPolicy; |
||
387 | |||
388 | return new Role( |
||
389 | array( |
||
390 | 'id' => $role->id, |
||
391 | 'identifier' => $role->identifier, |
||
392 | ), |
||
393 | $existingPolicies |
||
394 | ); |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Removes a policy from the role. |
||
399 | * |
||
400 | * @deprecated since 5.3, use {@link removePolicyByRoleDraft()} instead. |
||
401 | * |
||
402 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a policy |
||
403 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if policy does not belong to the given role |
||
404 | * |
||
405 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
406 | * @param \eZ\Publish\API\Repository\Values\User\Policy $policy the policy to remove from the role |
||
407 | * |
||
408 | * @return \eZ\Publish\API\Repository\Values\User\Role the updated role |
||
409 | */ |
||
410 | public function removePolicy(APIRole $role, APIPolicy $policy) |
||
411 | { |
||
412 | $values = $this->requestParser->parse('role', $role->id); |
||
413 | $response = $this->client->request( |
||
414 | 'DELETE', |
||
415 | $this->requestParser->generate( |
||
416 | 'policy', |
||
417 | array( |
||
418 | 'role' => $values['role'], |
||
419 | 'policy' => $policy->id, |
||
420 | ) |
||
421 | ), |
||
422 | new Message( |
||
423 | // @todo: What media-type should we set here? Actually, it should be |
||
424 | // all expected exceptions + none? Or is "Section" correct, |
||
425 | // since this is what is to be expected by the resource |
||
426 | // identified by the URL? |
||
427 | array('Accept' => $this->outputVisitor->getMediaType('Policy')) |
||
428 | ) |
||
429 | ); |
||
430 | |||
431 | if (!empty($response->body)) { |
||
432 | $this->inputDispatcher->parse($response); |
||
433 | } |
||
434 | |||
435 | return $this->loadRole($role->id); |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Deletes a policy. |
||
440 | * |
||
441 | * @deprecated since 6.0, use {@link removePolicyByRoleDraft()} instead. |
||
442 | * |
||
443 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a policy |
||
444 | * |
||
445 | * @param \eZ\Publish\API\Repository\Values\User\Policy $policy the policy to delete |
||
446 | */ |
||
447 | public function deletePolicy(APIPolicy $policy) |
||
451 | |||
452 | /** |
||
453 | * Updates the limitations of a policy. The module and function cannot be changed and |
||
454 | * the limitations are replaced by the ones in $roleUpdateStruct. |
||
455 | * |
||
456 | * @deprecated since 6.0, use {@link updatePolicyByRoleDraft()} instead. |
||
457 | * |
||
458 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to update a policy |
||
459 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if limitation of the same type is repeated in policy update |
||
460 | * struct or if limitation is not allowed on module/function |
||
461 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if a limitation in the $policyUpdateStruct is not valid |
||
462 | * |
||
463 | * @param \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct $policyUpdateStruct |
||
464 | * @param \eZ\Publish\API\Repository\Values\User\Policy $policy |
||
465 | * |
||
466 | * @return \eZ\Publish\API\Repository\Values\User\Policy |
||
467 | */ |
||
468 | public function updatePolicy(APIPolicy $policy, APIPolicyUpdateStruct $policyUpdateStruct) |
||
469 | { |
||
470 | $values = $this->requestParser->parse('role', $policy->roleId); |
||
471 | $inputMessage = $this->outputVisitor->visit($policyUpdateStruct); |
||
472 | $inputMessage->headers['Accept'] = $this->outputVisitor->getMediaType('Policy'); |
||
473 | $inputMessage->headers['X-HTTP-Method-Override'] = 'PATCH'; |
||
474 | |||
475 | $result = $this->client->request( |
||
476 | 'POST', |
||
477 | $this->requestParser->generate( |
||
478 | 'policy', |
||
479 | array( |
||
480 | 'role' => $values['role'], |
||
481 | 'policy' => $policy->id, |
||
482 | ) |
||
483 | ), |
||
484 | $inputMessage |
||
485 | ); |
||
486 | |||
487 | return $this->inputDispatcher->parse($result); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Loads a role for the given id. |
||
492 | * |
||
493 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
494 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a role with the given name was not found |
||
495 | * |
||
496 | * @param mixed $id |
||
497 | * |
||
498 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
499 | */ |
||
500 | public function loadRole($id) |
||
501 | { |
||
502 | $response = $this->client->request( |
||
503 | 'GET', |
||
504 | $id, |
||
505 | new Message( |
||
506 | array('Accept' => $this->outputVisitor->getMediaType('Role')) |
||
507 | ) |
||
508 | ); |
||
509 | |||
510 | $loadedRole = $this->inputDispatcher->parse($response); |
||
511 | $loadedRoleValues = $this->requestParser->parse('role', $loadedRole->id); |
||
512 | $response = $this->client->request( |
||
513 | 'GET', |
||
514 | $this->requestParser->generate('policies', array('role' => $loadedRoleValues['role'])), |
||
515 | new Message( |
||
516 | array('Accept' => $this->outputVisitor->getMediaType('PolicyList')) |
||
517 | ) |
||
518 | ); |
||
519 | |||
520 | $policies = $this->inputDispatcher->parse($response); |
||
521 | |||
522 | return new Role( |
||
523 | array( |
||
524 | 'id' => $loadedRole->id, |
||
525 | 'identifier' => $loadedRole->identifier, |
||
526 | ), |
||
527 | $policies |
||
528 | ); |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * Loads a role for the given name. |
||
533 | * |
||
534 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
535 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a role with the given name was not found |
||
536 | * |
||
537 | * @param string $name |
||
538 | * |
||
539 | * @return \eZ\Publish\API\Repository\Values\User\Role |
||
540 | */ |
||
541 | View Code Duplication | public function loadRoleByIdentifier($name) |
|
542 | { |
||
543 | $response = $this->client->request( |
||
544 | 'GET', |
||
545 | $this->requestParser->generate('roleByIdentifier', array('role' => $name)), |
||
546 | new Message( |
||
547 | array('Accept' => $this->outputVisitor->getMediaType('RoleList')) |
||
548 | ) |
||
549 | ); |
||
550 | |||
551 | $result = $this->inputDispatcher->parse($response); |
||
552 | |||
553 | return reset($result); |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * Loads all roles. |
||
558 | * |
||
559 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read the roles |
||
560 | * |
||
561 | * @return \eZ\Publish\API\Repository\Values\User\Role[] |
||
562 | */ |
||
563 | public function loadRoles() |
||
564 | { |
||
565 | $response = $this->client->request( |
||
566 | 'GET', |
||
567 | $this->requestParser->generate('roles'), |
||
568 | new Message( |
||
569 | array('Accept' => $this->outputVisitor->getMediaType('RoleList')) |
||
570 | ) |
||
571 | ); |
||
572 | |||
573 | return $this->inputDispatcher->parse($response); |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * Deletes the given role. |
||
578 | * |
||
579 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to delete this role |
||
580 | * |
||
581 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
582 | */ |
||
583 | View Code Duplication | public function deleteRole(APIRole $role) |
|
584 | { |
||
585 | $response = $this->client->request( |
||
586 | 'DELETE', |
||
587 | $role->id, |
||
588 | new Message( |
||
589 | // @todo: What media-type should we set here? Actually, it should be |
||
590 | // all expected exceptions + none? Or is "Section" correct, |
||
591 | // since this is what is to be expected by the resource |
||
592 | // identified by the URL? |
||
593 | array('Accept' => $this->outputVisitor->getMediaType('Role')) |
||
594 | ) |
||
595 | ); |
||
596 | |||
597 | if (!empty($response->body)) { |
||
598 | $this->inputDispatcher->parse($response); |
||
599 | } |
||
600 | } |
||
601 | |||
602 | /** |
||
603 | * Loads all policies from roles which are assigned to a user or to user groups to which the user belongs. |
||
604 | * |
||
605 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if a user with the given id was not found |
||
606 | * |
||
607 | * @param mixed $userId |
||
608 | * |
||
609 | * @return \eZ\Publish\API\Repository\Values\User\Policy[] |
||
610 | */ |
||
611 | View Code Duplication | public function loadPoliciesByUserId($userId) |
|
612 | { |
||
613 | $values = $this->requestParser->parse('user', $userId); |
||
614 | $response = $this->client->request( |
||
615 | 'GET', |
||
616 | $this->requestParser->generate('userPolicies', array('user' => $values['user'])), |
||
617 | new Message( |
||
618 | array('Accept' => $this->outputVisitor->getMediaType('PolicyList')) |
||
619 | ) |
||
620 | ); |
||
621 | |||
622 | return $this->inputDispatcher->parse($response); |
||
623 | } |
||
624 | |||
625 | /** |
||
626 | * Assigns a role to the given user group. |
||
627 | * |
||
628 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign a role |
||
629 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if $roleLimitation is not valid |
||
630 | * |
||
631 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
632 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
633 | * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation) |
||
634 | */ |
||
635 | View Code Duplication | public function assignRoleToUserGroup(APIRole $role, UserGroup $userGroup, RoleLimitation $roleLimitation = null) |
|
636 | { |
||
637 | $roleAssignment = new RoleAssignment( |
||
638 | array( |
||
639 | 'role' => $role, |
||
640 | 'limitation' => $roleLimitation, |
||
641 | ) |
||
642 | ); |
||
643 | |||
644 | $inputMessage = $this->outputVisitor->visit($roleAssignment); |
||
645 | $inputMessage->headers['Accept'] = $this->outputVisitor->getMediaType('RoleAssignmentList'); |
||
646 | |||
647 | $result = $this->client->request( |
||
648 | 'POST', |
||
649 | $this->requestParser->generate('groupRoleAssignments', array('group' => $userGroup->id)), |
||
650 | $inputMessage |
||
651 | ); |
||
652 | |||
653 | $this->inputDispatcher->parse($result); |
||
654 | } |
||
655 | |||
656 | /** |
||
657 | * removes a role from the given user group. |
||
658 | * |
||
659 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a role |
||
660 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the role is not assigned to the given user group |
||
661 | * |
||
662 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
663 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
664 | */ |
||
665 | View Code Duplication | public function unassignRoleFromUserGroup(APIRole $role, UserGroup $userGroup) |
|
666 | { |
||
667 | $values = $this->requestParser->parse('group', $userGroup->id); |
||
668 | $userGroupId = $values['group']; |
||
669 | |||
670 | $values = $this->requestParser->parse('role', $role->id); |
||
671 | $roleId = $values['role']; |
||
672 | |||
673 | $response = $this->client->request( |
||
674 | 'DELETE', |
||
675 | $this->requestParser->generate('groupRoleAssignment', array('group' => $userGroupId, 'role' => $roleId)), |
||
676 | new Message( |
||
677 | // @todo: What media-type should we set here? Actually, it should be |
||
678 | // all expected exceptions + none? Or is "Section" correct, |
||
679 | // since this is what is to be expected by the resource |
||
680 | // identified by the URL? |
||
681 | array('Accept' => $this->outputVisitor->getMediaType('RoleAssignmentList')) |
||
682 | ) |
||
683 | ); |
||
684 | |||
685 | if (!empty($response->body)) { |
||
686 | $this->inputDispatcher->parse($response); |
||
687 | } |
||
688 | } |
||
689 | |||
690 | /** |
||
691 | * Assigns a role to the given user. |
||
692 | * |
||
693 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to assign a role |
||
694 | * @throws \eZ\Publish\API\Repository\Exceptions\LimitationValidationException if $roleLimitation is not valid |
||
695 | * |
||
696 | * @todo add limitations |
||
697 | * |
||
698 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
699 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
700 | * @param \eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation $roleLimitation an optional role limitation (which is either a subtree limitation or section limitation) |
||
701 | */ |
||
702 | View Code Duplication | public function assignRoleToUser(APIRole $role, User $user, RoleLimitation $roleLimitation = null) |
|
703 | { |
||
704 | $roleAssignment = new RoleAssignment( |
||
705 | array( |
||
706 | 'role' => $role, |
||
707 | 'limitation' => $roleLimitation, |
||
708 | ) |
||
709 | ); |
||
710 | |||
711 | $inputMessage = $this->outputVisitor->visit($roleAssignment); |
||
712 | $inputMessage->headers['Accept'] = $this->outputVisitor->getMediaType('RoleAssignmentList'); |
||
713 | |||
714 | $result = $this->client->request( |
||
715 | 'POST', |
||
716 | $this->requestParser->generate('userRoleAssignments', array('user' => $user->id)), |
||
717 | $inputMessage |
||
718 | ); |
||
719 | |||
720 | $this->inputDispatcher->parse($result); |
||
721 | } |
||
722 | |||
723 | /** |
||
724 | * removes a role from the given user. |
||
725 | * |
||
726 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a role |
||
727 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the role is not assigned to the user |
||
728 | * |
||
729 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
730 | * @param \eZ\Publish\API\Repository\Values\User\User $user |
||
731 | */ |
||
732 | View Code Duplication | public function unassignRoleFromUser(APIRole $role, User $user) |
|
733 | { |
||
734 | $values = $this->requestParser->parse('user', $user->id); |
||
735 | $userId = $values['user']; |
||
736 | |||
737 | $values = $this->requestParser->parse('role', $role->id); |
||
738 | $roleId = $values['role']; |
||
739 | |||
740 | $response = $this->client->request( |
||
741 | 'DELETE', |
||
742 | $this->requestParser->generate('userRoleAssignment', array('user' => $userId, 'role' => $roleId)), |
||
743 | new Message( |
||
744 | // @todo: What media-type should we set here? Actually, it should be |
||
745 | // all expected exceptions + none? Or is "Section" correct, |
||
746 | // since this is what is to be expected by the resource |
||
747 | // identified by the URL? |
||
748 | array('Accept' => $this->outputVisitor->getMediaType('RoleAssignmentList')) |
||
749 | ) |
||
750 | ); |
||
751 | |||
752 | if (!empty($response->body)) { |
||
753 | $this->inputDispatcher->parse($response); |
||
754 | } |
||
755 | } |
||
756 | |||
757 | /** |
||
758 | * Removes the given role assignment. |
||
759 | * |
||
760 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to remove a role assignment |
||
761 | * |
||
762 | * @param \eZ\Publish\API\Repository\Values\User\RoleAssignment $roleAssignment |
||
763 | */ |
||
764 | public function removeRoleAssignment(APIRoleAssignment $roleAssignment) |
||
765 | { |
||
766 | throw new \Exception('@todo: Implement.'); |
||
767 | } |
||
768 | |||
769 | /** |
||
770 | * Loads a user assignment for the given id. |
||
771 | * |
||
772 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read this role |
||
773 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException If the role assignment was not found |
||
774 | * |
||
775 | * @param mixed $roleAssignmentId |
||
776 | * |
||
777 | * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment |
||
778 | */ |
||
779 | public function loadRoleAssignment($roleAssignmentId) |
||
780 | { |
||
781 | throw new \Exception('@todo: Implement.'); |
||
782 | } |
||
783 | |||
784 | /** |
||
785 | * Returns the assigned user and user groups to this role. |
||
786 | * |
||
787 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read a role |
||
788 | * |
||
789 | * @param \eZ\Publish\API\Repository\Values\User\Role $role |
||
790 | * |
||
791 | * @return \eZ\Publish\API\Repository\Values\User\RoleAssignment[] |
||
792 | */ |
||
793 | public function getRoleAssignments(APIRole $role) |
||
794 | { |
||
795 | throw new \Exception('@todo: Implement.'); |
||
796 | } |
||
797 | |||
798 | /** |
||
799 | * @see \eZ\Publish\API\Repository\RoleService::getRoleAssignmentsForUser() |
||
800 | */ |
||
801 | View Code Duplication | public function getRoleAssignmentsForUser(User $user, $inherited = false) |
|
802 | { |
||
803 | $response = $this->client->request( |
||
804 | 'GET', |
||
805 | $this->requestParser->generate('userRoleAssignments'), |
||
806 | new Message( |
||
807 | array('Accept' => $this->outputVisitor->getMediaType('RoleAssignmentList')) |
||
808 | ) |
||
809 | ); |
||
810 | |||
811 | $roleAssignments = $this->inputDispatcher->parse($response); |
||
812 | |||
813 | $userRoleAssignments = array(); |
||
814 | foreach ($roleAssignments as $roleAssignment) { |
||
815 | $userRoleAssignments[] = new UserRoleAssignment( |
||
816 | array( |
||
817 | 'limitation' => $roleAssignment->getRoleLimitation(), |
||
818 | 'role' => $roleAssignment->getRole(), |
||
819 | 'user' => $user, |
||
820 | ) |
||
821 | ); |
||
822 | } |
||
823 | |||
824 | return $userRoleAssignments; |
||
825 | } |
||
826 | |||
827 | /** |
||
828 | * Returns the roles assigned to the given user group. |
||
829 | * |
||
830 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the authenticated user is not allowed to read a user group |
||
831 | * |
||
832 | * @param \eZ\Publish\API\Repository\Values\User\UserGroup $userGroup |
||
833 | * |
||
834 | * @return \eZ\Publish\API\Repository\Values\User\UserGroupRoleAssignment[] |
||
835 | */ |
||
836 | View Code Duplication | public function getRoleAssignmentsForUserGroup(UserGroup $userGroup) |
|
837 | { |
||
838 | $response = $this->client->request( |
||
839 | 'GET', |
||
840 | $this->requestParser->generate('groupRoleAssignments'), |
||
841 | new Message( |
||
842 | array('Accept' => $this->outputVisitor->getMediaType('RoleAssignmentList')) |
||
843 | ) |
||
844 | ); |
||
845 | |||
846 | $roleAssignments = $this->inputDispatcher->parse($response); |
||
847 | |||
848 | $userGroupRoleAssignments = array(); |
||
849 | foreach ($roleAssignments as $roleAssignment) { |
||
850 | $userGroupRoleAssignments[] = new UserGroupRoleAssignment( |
||
851 | array( |
||
852 | 'limitation' => $roleAssignment->getRoleLimitation(), |
||
853 | 'role' => $roleAssignment->getRole(), |
||
854 | 'userGroup' => $userGroup, |
||
855 | ) |
||
856 | ); |
||
857 | } |
||
858 | |||
859 | return $userGroupRoleAssignments; |
||
860 | } |
||
861 | |||
862 | /** |
||
863 | * Instantiates a role create class. |
||
864 | * |
||
865 | * @param string $name |
||
866 | * |
||
867 | * @return \eZ\Publish\API\Repository\Values\User\RoleCreateStruct |
||
868 | */ |
||
869 | public function newRoleCreateStruct($name) |
||
873 | |||
874 | /** |
||
875 | * Instantiates a policy create class. |
||
876 | * |
||
877 | * @param string $module |
||
878 | * @param string $function |
||
879 | * |
||
880 | * @return \eZ\Publish\API\Repository\Values\User\PolicyCreateStruct |
||
881 | */ |
||
882 | public function newPolicyCreateStruct($module, $function) |
||
886 | |||
887 | /** |
||
888 | * Instantiates a policy update class. |
||
889 | * |
||
890 | * @return \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct |
||
891 | */ |
||
892 | public function newPolicyUpdateStruct() |
||
896 | |||
897 | /** |
||
898 | * Instantiates a policy update class. |
||
899 | * |
||
900 | * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct |
||
901 | */ |
||
902 | public function newRoleUpdateStruct() |
||
906 | |||
907 | /** |
||
908 | * Returns the LimitationType registered with the given identifier. |
||
909 | * |
||
910 | * @param string $identifier |
||
911 | * |
||
912 | * @return \eZ\Publish\SPI\Limitation\Type |
||
913 | * |
||
914 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if there is no LimitationType with $identifier |
||
915 | */ |
||
916 | public function getLimitationType($identifier) |
||
920 | |||
921 | /** |
||
922 | * Returns the LimitationType's assigned to a given module/function. |
||
923 | * |
||
924 | * Typically used for: |
||
925 | * - Internal validation limitation value use on Policies |
||
926 | * - Role admin gui for editing policy limitations incl list limitation options via valueSchema() |
||
927 | * |
||
928 | * @param string $module Legacy name of "controller", it's a unique identifier like "content" |
||
929 | * @param string $function Legacy name of a controller "action", it's a unique within the controller like "read" |
||
930 | * |
||
931 | * @return \eZ\Publish\SPI\Limitation\Type[] |
||
932 | * |
||
933 | * @throws \eZ\Publish\API\Repository\Exceptions\BadStateException If module/function to limitation type mapping |
||
934 | * refers to a non existing identifier. |
||
935 | */ |
||
936 | public function getLimitationTypesByModuleFunction($module, $function) |
||
940 | } |
||
941 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: