Skip to content

Commit 4141af3

Browse files
SimperfitAmrouche Hamza
authored andcommitted
feature: add doc for #230
1 parent 305d78e commit 4141af3

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

core/security.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,52 @@ section.
66
Using API Platform, you can leverage all security features provided by the [Symfony Security component](http://symfony.com/doc/current/book/security.html).
77
For instance, if you wish to restrict the access of some endpoints, you can use [access controls directives](http://symfony.com/doc/current/book/security.html#securing-url-patterns-access-control).
88

9+
You can also add security directly in the entity using the [Symfony's access control expressions](https://symfony.com/doc/current/expressions.html#security-complex-access-controls-with-expressions).
10+
Here is a little exemple for this :
11+
12+
```php
13+
14+
use ApiPlatform\Core\Annotation\ApiResource;
15+
use Doctrine\ORM\Mapping as ORM;
16+
use Symfony\Component\Validator\Constraints as Assert;
17+
18+
/**
19+
* Secured resource.
20+
*
21+
* @author Kévin Dunglas <dunglas@gmail.com>
22+
*
23+
* @ApiResource(
24+
* attributes={"is_granted"="has_role('ROLE_USER')"},
25+
* collectionOperations={
26+
* "get"={"method"="GET"},
27+
* "post"={"method"="POST", "is_granted"="has_role('ROLE_ADMIN')"}
28+
* },
29+
* itemOperations={
30+
* "get"={"method"="GET", "is_granted"="has_role('ROLE_USER') and object.getOwner() == user"}
31+
* }
32+
* )
33+
* @ORM\Entity
34+
*/
35+
class Book
36+
{
37+
/**
38+
* @var int
39+
*
40+
* @ORM\Column(type="integer")
41+
* @ORM\Id
42+
* @ORM\GeneratedValue(strategy="AUTO")
43+
*/
44+
public $id;
45+
/**
46+
* @var string The title
47+
*
48+
* @ORM\Column
49+
* @Assert\NotBlank
50+
*/
51+
public $title;
52+
}
53+
```
54+
955
It is also possible to use the [event system](events.md) for more advanced logic or even [custom actions](operations.md#creating-custom-operations-and-controllers)
1056
if you really need to.
1157

0 commit comments

Comments
 (0)