Skip to content

[DX] Added CurrentUserProvider service #14407

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

Closed
wants to merge 1 commit into from

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Apr 19, 2015

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets none
License MIT
Doc PR symfony/symfony-docs#5192

I believe it would be nice to make it little easier to retrieve the current user. This provider will hide the complexity with the token and check if the $token->getUser() is an object or a string.

@Nyholm Nyholm force-pushed the CurrentUserProvider branch from ed12fe0 to 0532d49 Compare April 19, 2015 11:35
return;
}

if (!is_object($user = $token->getUser())) {
Copy link
Member

Choose a reason for hiding this comment

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

-1 on this logic, even if user is not a string, it should return it. anon. is a user too.

Choose a reason for hiding this comment

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

yep a user is not necessarily represented by an object, it depend of implementation of token. (for example UsernamePasswordToken accept string representation of user to handle InMemoryUserProvider).

Copy link
Member Author

Choose a reason for hiding this comment

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

I understand that some user representation might be a string. This PR is a refactor of the FrameworkBundle's Controller::getUser. I've just put it to a service.
The CurrentUserProvider::getUser function do have the same logic as in the FramworkBundle

Choose a reason for hiding this comment

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

It's a mistake from FrameworkBundle so, this is why when we retrieve the current user from different way we obtains 'anon.' or null.

@Nyholm
Copy link
Member Author

Nyholm commented Apr 19, 2015

I know that the anonymous user is also considered as a user. But IMHO it's very rare that you bother about the anonymous user. If you do $this->get('security.current_user_provider')->getUser() you want to take some action on your user model. You want to log something or maybe create something attached to that user.

The intention is to make it easier in the 90% of the cases when you just want to fetch your user model for the current user without any extra check for tokens and if user is the string ´anon.`.

@cvuorinen
Copy link

Maybe you could make that intention a little more explicit by renaming the getUser method to getAuthenticatedUser? Or rename CurrentUserProvider to AuthenticatedUserProvider? That way it would be clearer that it will not return 'anon', only an authenticated user.

@Nyholm
Copy link
Member Author

Nyholm commented Apr 21, 2015

You are absolutely correct @cvuorinen. If it is renamed to example AuthenticatedUserProvider::getUser or AuthenticatedUserProvider::getUserObject it will be more obvious what it will return and why.

@Nyholm
Copy link
Member Author

Nyholm commented May 5, 2015

Listening to your arguments and other friends in the community, I'm ready to drop this PR. It is bad design to use the user like this. One should inject the user in the method call as @iltar suggests.

Thank you all for taking time to review this PR.

@Nyholm Nyholm closed this May 5, 2015
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.

5 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy