Skip to content

feat(subresource): Link security #1692

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 3 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions core/subresources.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,28 @@ class Company {
// ...
}
```

## Security

In order to use Symfony's built-in security system on subresources the security option of the `Link` attribute can be used.

To restrict the access to a subresource based on the parent object simply use the Symfony expression language as you would do normally, with the exception that the name defined in `toProperty` or `fromProperty` is used to access the object.

Alternatively you can also use the `securityObjectName` to set a custom name

```php
<?php
#[ApiResource(
uriTemplate: '/employees/{employeeId}/company',
uriVariables: [
'employeeId' => new Link(fromClass: Employee::class, toProperty: 'company', security: "is_granted(some_voter, company)"),
],
operations: [
new Get()
]
)]

class Company {
// ...
}
```