Skip to content

[Security] Adding __toString() method to Role class #10179

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 2 commits into from

Conversation

jameshalsall
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? possibly ?
Deprecations? no
Tests pass? yes
Fixed tickets N/A
License MIT

This PR proposes adding the toString() method to the \Symfony\Component\Security\Core\Role\Role class. This should ease the comparison of role strings and instances of Role itself. For example, in the FOSUserBundle the User model contains roles which are represented as strings. This makes comparisons between arrays of Role objects and role strings difficult without creating your own Role class with a __toString(), and this isn't always desirable.

I found some conversation around adding the \Seriazable interface to the Role class, and I realise that this might also be considered a BC break if people are relying on the fact that it will not cast to a string.

*/
public function __toString()
{
return $this->role;
Copy link
Member

Choose a reason for hiding this comment

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

This will produce a fatal error if $this->role is null. It is allowed by the RoleInterface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good spot, will fix and add a test

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests to ensure that the __toString() behaves as expected, although it was never possible for it to be null because the role was immutable and cast to a string in the constructor

@jameshalsall
Copy link
Contributor Author

@fabpot is this okay to be merged?

@Tobion
Copy link
Contributor

Tobion commented Apr 13, 2014

It cannot be merged into 2.3. Also the toString doesn't help much when it's not part of the interface. So it would need to be added there as well which makes it a BC break. Could be worth, not sure.

@jameshalsall
Copy link
Contributor Author

@Tobion I'm not sure the interface would actually require the __toString() because it's not usually explicitly called? It's there so that PHP knows how to cast it as a string, but isn't really a requirement of all roles implementing the RoleInterface.

It certainly eases things when you're trying to compare an array of Role objects and an array of role string names.

@Tobion
Copy link
Contributor

Tobion commented Oct 9, 2014

@fabpot @stof should be close this considering the Role and RoleInterface will probably be deprecated according to #11742 ?

@fabpot
Copy link
Member

fabpot commented Feb 5, 2015

Closing in favor of what I explained in #8313 (comment)

@fabpot fabpot closed this Feb 5, 2015
@fabpot fabpot reopened this Feb 5, 2015
@fabpot fabpot closed this Feb 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants