Completed
Push — ezp-24830_REST_for_role_drafts ( 7038df...e2f179 )
by
unknown
26:43
created

RoleService::addPolicyByRoleDraft()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * File containing the RoleService 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
 * @version //autogentag//
10
 */
11
namespace eZ\Publish\Core\REST\Client;
12
13
use eZ\Publish\API\Repository\RoleService as APIRoleService;
14
use eZ\Publish\API\Repository\Values\User\Limitation\RoleLimitation;
15
use eZ\Publish\API\Repository\Values\User\Policy as APIPolicy;
16
use eZ\Publish\API\Repository\Values\User\PolicyCreateStruct as APIPolicyCreateStruct;
17
use eZ\Publish\API\Repository\Values\User\PolicyDraft;
18
use eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct as APIPolicyUpdateStruct;
19
use eZ\Publish\API\Repository\Values\User\Role as APIRole;
20
use eZ\Publish\API\Repository\Values\User\RoleAssignment as APIRoleAssignment;
21
use eZ\Publish\API\Repository\Values\User\RoleDraft as APIRoleDraft;
22
use eZ\Publish\API\Repository\Values\User\RoleCreateStruct as APIRoleCreateStruct;
23
use eZ\Publish\API\Repository\Values\User\RoleUpdateStruct;
24
use eZ\Publish\API\Repository\Values\User\User;
25
use eZ\Publish\API\Repository\Values\User\UserGroup;
26
use eZ\Publish\Core\Repository\Values\User\RoleDraft;
27
use eZ\Publish\Core\Repository\Values\User\UserRoleAssignment;
28
use eZ\Publish\Core\Repository\Values\User\UserGroupRoleAssignment;
29
use eZ\Publish\Core\REST\Client\Values\User\PolicyCreateStruct;
30
use eZ\Publish\Core\REST\Client\Values\User\PolicyUpdateStruct;
31
use eZ\Publish\Core\REST\Client\Values\User\Role;
32
use eZ\Publish\Core\REST\Client\Values\User\Policy;
33
use eZ\Publish\Core\REST\Client\Values\User\RoleAssignment;
34
use eZ\Publish\Core\REST\Common\RequestParser;
35
use eZ\Publish\Core\REST\Common\Input\Dispatcher;
36
use eZ\Publish\Core\REST\Common\Output\Visitor;
37
use eZ\Publish\Core\REST\Common\Message;
38
39
/**
40
 * Implementation of the {@link \eZ\Publish\API\Repository\RoleService}
41
 * interface.
42
 *
43
 * @see \eZ\Publish\API\Repository\RoleService
44
 */
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)
118
    {
119
        //TODO
120
    }
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)
135
    {
136
        //TODO
137
    }
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)
150
    {
151
        // TODO
152
    }
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)
168
    {
169
        //TODO
170
    }
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)
188
    {
189
        //TODO
190
    }
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)
203
    {
204
        //TODO
205
    }
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)
225
    {
226
        //TODO
227
    }
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)
239
    {
240
        //TODO
241
    }
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)
253
    {
254
        //TODO
255
    }
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
0 ignored issues
show
Documentation introduced by
$inputMessage is of type object<Symfony\Component\HttpFoundation\Response>, but the function expects a null|object<eZ\Publish\Core\REST\Common\Message>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
279
        );
280
281
        $createdRole = $this->inputDispatcher->parse($result);
282
        $createdRoleValues = $this->requestParser->parse('role', $createdRole->id);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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
0 ignored issues
show
Documentation introduced by
$inputMessage is of type object<Symfony\Component\HttpFoundation\Response>, but the function expects a null|object<eZ\Publish\Core\REST\Common\Message>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
300
                'roleId' => $createdRole->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
301
                'module' => $createdPolicy->module,
0 ignored issues
show
Documentation introduced by
The property module does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
302
                'function' => $createdPolicy->function,
0 ignored issues
show
Documentation introduced by
The property function does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
303
            );
304
305
            $createdPolicy = new Policy($createdPolicyArray);
306
            $createdPolicies[] = $createdPolicy;
307
        }
308
309
        return new RoleDraft(
310
            array(
311
                'id' => $createdRole->id,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
312
                'identifier' => $createdRole->identifier,
0 ignored issues
show
Documentation introduced by
The property identifier does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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
0 ignored issues
show
Documentation introduced by
$inputMessage is of type object<Symfony\Component\HttpFoundation\Response>, but the function expects a null|object<eZ\Publish\Core\REST\Common\Message>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
0 ignored issues
show
Documentation introduced by
$inputMessage is of type object<Symfony\Component\HttpFoundation\Response>, but the function expects a null|object<eZ\Publish\Core\REST\Common\Message>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
378
            'roleId' => $role->id,
379
            'module' => $createdPolicy->module,
0 ignored issues
show
Documentation introduced by
The property module does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
380
            'function' => $createdPolicy->function,
0 ignored issues
show
Documentation introduced by
The property function does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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)
448
    {
449
        throw new \Exception('@todo: Implement.');
450
    }
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
0 ignored issues
show
Documentation introduced by
$inputMessage is of type object<Symfony\Component\HttpFoundation\Response>, but the function expects a null|object<eZ\Publish\Core\REST\Common\Message>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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,
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
525
                'identifier' => $loadedRole->identifier,
0 ignored issues
show
Documentation introduced by
The property identifier does not exist on object<eZ\Publish\API\Re...ory\Values\ValueObject>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
526
            ),
527
            $policies
0 ignored issues
show
Documentation introduced by
$policies is of type object<eZ\Publish\API\Re...ory\Values\ValueObject>, but the function expects a array<integer,object<eZ\...ry\Values\User\Policy>>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->inputDispatcher->parse($response); (eZ\Publish\API\Repository\Values\ValueObject) is incompatible with the return type declared by the interface eZ\Publish\API\Repository\RoleService::loadRoles of type eZ\Publish\API\Repository\Values\User\Role[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->inputDispatcher->parse($response); (eZ\Publish\API\Repository\Values\ValueObject) is incompatible with the return type declared by the interface eZ\Publish\API\Repositor...e::loadPoliciesByUserId of type eZ\Publish\API\Repository\Values\User\Policy[].

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
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
0 ignored issues
show
Documentation introduced by
$inputMessage is of type object<Symfony\Component\HttpFoundation\Response>, but the function expects a null|object<eZ\Publish\Core\REST\Common\Message>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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
0 ignored issues
show
Documentation introduced by
$inputMessage is of type object<Symfony\Component\HttpFoundation\Response>, but the function expects a null|object<eZ\Publish\Core\REST\Common\Message>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The expression $roleAssignments of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not traversable.
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The expression $roleAssignments of type object<eZ\Publish\API\Re...ory\Values\ValueObject> is not traversable.
Loading history...
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)
870
    {
871
        return new Values\User\RoleCreateStruct($name);
872
    }
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)
883
    {
884
        return new PolicyCreateStruct($module, $function);
885
    }
886
887
    /**
888
     * Instantiates a policy update class.
889
     *
890
     * @return \eZ\Publish\API\Repository\Values\User\PolicyUpdateStruct
891
     */
892
    public function newPolicyUpdateStruct()
893
    {
894
        return new PolicyUpdateStruct();
895
    }
896
897
    /**
898
     * Instantiates a policy update class.
899
     *
900
     * @return \eZ\Publish\API\Repository\Values\User\RoleUpdateStruct
901
     */
902
    public function newRoleUpdateStruct()
903
    {
904
        return new RoleUpdateStruct();
905
    }
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)
917
    {
918
        throw new \eZ\Publish\API\Repository\Exceptions\NotImplementedException(__METHOD__);
919
    }
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)
937
    {
938
        throw new \eZ\Publish\API\Repository\Exceptions\NotImplementedException(__METHOD__);
939
    }
940
}
941