Skip to content

Example Doctrine filter usage, flagging the issue on the priority for… #228

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 5 commits into from
Dec 26, 2017
Merged

Example Doctrine filter usage, flagging the issue on the priority for… #228

merged 5 commits into from
Dec 26, 2017

Conversation

djoos
Copy link
Contributor

@djoos djoos commented Jun 21, 2017

…the filter to work as expected

More information on Doctrine filters can be found at http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/filters.html
Using a great tutorial on implementing a "UserAware" Doctrine filter in Symfony (http://blog.michaelperrin.fr/2014/12/05/doctrine-filters/), this information not only allows an API Platform dev to implement a Doctrine filter, but also sail around the issue where having a too low priority makes the Pagination go off, as discussed in api-platform/core#1185.

Please let me know if any changes are needed to get this merged. Thanks in advance!

Kind regards,
David

djoos and others added 4 commits June 21, 2017 12:55
… the filter to work as expected

More information on Doctrine filters can be found at http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/filters.html
Using a great tutorial on implementing a "UserAware" Doctrine filter in Symfony (http://blog.michaelperrin.fr/2014/12/05/doctrine-filters/), this information not only allows an API Platform dev to implement a Doctrine filter, but also sail around the issue where having a too low priority makes the Pagination go off, as discussed in api-platform/core#1185.

public function onKernelRequest(): void
{
if (!$user = $this->getUser()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one looks tricky. If a user is anonymous, shouldn't an exception be thrown instead? It looks like the current implementation can cause security concerns with some firewall setups.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I see your point - I actually kept the logic here mainly to filtering while security would be handled on the firewall level. It's up to the implementation to decide what needs to happen exactly when dealing with an anonymous user; skipping the filter is indeed most likely not desirable...

Perhaps filtering on id "-1" is an option, instead of returning? Feels quite like a hack though.

What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my projects I add something like WHERE 1=2 but it's definitely a trick. Throwing an exception telling that the security must be enabled looks good enough for a doc entry to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree!

@dunglas
Copy link
Member

dunglas commented Sep 11, 2017

Thank you very much for this doc entry @djoos. It's really appreciated. I made some cosmetic / CS changes in new commits.
What do you think of my remaining comment?

@dunglas dunglas merged commit e8fb5e3 into api-platform:master Dec 26, 2017
@dunglas
Copy link
Member

dunglas commented Dec 26, 2017

Thanks @djoos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants