-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feature: add doc for #230 #231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4141af3
feature: add doc for #230
Simperfit 65741ea
fix from review
Simperfit 18678c3
add explanation
9e9dd4d
ffr
2c89fe8
ffr
06caa20
meyer
7ef207d
owner
badf98c
owner
ed06ea7
string
7175c9b
yup
d2d329b
yup
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,58 @@ section. | |
Using API Platform, you can leverage all security features provided by the [Symfony Security component](http://symfony.com/doc/current/book/security.html). | ||
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). | ||
|
||
Since 2.1, you can add security through [Symfony's access control expressions](https://symfony.com/doc/current/expressions.html#security-complex-access-controls-with-expressions) in your entities. | ||
|
||
Here is an example: | ||
|
||
```php | ||
use ApiPlatform\Core\Annotation\ApiResource; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
/** | ||
* Secured resource. | ||
* | ||
* @ApiResource( | ||
* attributes={"access_control"="is_granted('ROLE_USER')}, | ||
* collectionOperations={ | ||
* "get"={"method"="GET"}, | ||
* "post"={"method"="POST", "access_control"="is_granted('ROLE_USER')} | ||
* }, | ||
* itemOperations={ | ||
* "get"={"method"="GET", "access_control"="is_granted('ROLE_USER') and object.owner == user"} | ||
* } | ||
* ) | ||
* @ORM\Entity | ||
*/ | ||
class Book | ||
{ | ||
/** | ||
* @var int | ||
* | ||
* @ORM\Column(type="integer") | ||
* @ORM\Id | ||
* @ORM\GeneratedValue(strategy="AUTO") | ||
*/ | ||
public $id; | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty line missing |
||
* @var string The title | ||
* | ||
* @ORM\Column | ||
* @Assert\NotBlank | ||
*/ | ||
public $title; | ||
|
||
/** | ||
* @ORM\Column(type="text") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It should be string or a relation. |
||
*/ | ||
public $owner; | ||
} | ||
``` | ||
|
||
This exemple is going to allow only fetching the book related to the current user. if he tries to fetch a book that is linked to his account, that will not return the resource. In addition, only admins are able to create books which means that a user could not create a book. | ||
|
||
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) | ||
if you really need to. | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blank line?