Skip to content

#21571 Comparing roles to detected that users has changed #31177

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 1 commit into from
Sep 6, 2019

Conversation

oleg-andreyev
Copy link
Contributor

@oleg-andreyev oleg-andreyev commented Apr 18, 2019

Q A
Branch? 4.4
Bug fix? yes
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
License MIT
Fixed tickets #21571 (comment)
Docs symfony/symfony-docs#11457

Case 1:
User A has roles foo, bar and admin, User A is signed-in into application and token is persisted, later another User B with role admin, decided to restrict role admin for User A, so User A won't lose it's privileges until session is expired or logout, because token is persisted with roles and authenticated=true and roles are not compared.

Ref. to the previous attempt: #27121

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Apr 19, 2019
@fabpot
Copy link
Member

fabpot commented May 15, 2019

This looks like a new feature to me, I would target master.

@oleg-andreyev oleg-andreyev changed the base branch from 3.4 to master June 11, 2019 12:00
@oleg-andreyev
Copy link
Contributor Author

This looks like a new feature to me, I would target master.

Changed to master

@OskarStark
Copy link
Contributor

Changed to master

Thank you, but now it should target 4.4, as master is for Symfony 5 features now ;-)

@oleg-andreyev
Copy link
Contributor Author

Thank you, but now it should target 4.4, as master is for Symfony 5 features now ;-)

It seems that documentation needs to be improved https://symfony.com/doc/current/contributing/code/pull_requests.html#choose-the-right-branch

@oleg-andreyev oleg-andreyev changed the base branch from master to 4.4 June 11, 2019 14:43
@oleg-andreyev
Copy link
Contributor Author

oleg-andreyev commented Jun 13, 2019

Thank you, but now it should target 4.4, as master is for Symfony 5 features now ;-)

@OskarStark can you please explain how it should be (I'd like to prepare PR):

  • bug fixes go to corresponded branch
  • new feature go to master branch (5.x)

but why then this PR, should be pointed to 4.4?
also it seems that it contradicts with @fabpot comment

@OskarStark
Copy link
Contributor

OskarStark commented Jun 13, 2019

https://symfony.com/doc/current/contributing/code/pull_requests.html#choose-the-right-branch

Currently we are in state where 4.3 is stable, 4.4 will be the next version with new features and master will be the new 5.0 major version. So 4.4 should be the correct branch.

This looks like a new feature to me, I would target master.

The comment by @fabpot was made before the new 4.4 branch was born, correct @fabpot ?

cc @javiereguiluz

@fabpot
Copy link
Member

fabpot commented Jun 13, 2019

New features should indeed target the 4.4 branch. It's not the master branch as we have 2 new versions at the same time: 4.4 and 5.0 (which is special as it happens only once every two years).

@oleg-andreyev
Copy link
Contributor Author

Alright I've created symfony/symfony-docs#11741, so this PR can be finally reviewed.

javiereguiluz added a commit to javiereguiluz/symfony-docs that referenced this pull request Jun 14, 2019
…ment branch instead of master for new features. (oleg-andreyev)

This PR was merged into the 3.4 branch.

Discussion
----------

Adding "special case" when need to select development branch instead of master for new features.

As per discussion in symfony#11457 and symfony/symfony#31177 (comment) updated section on how to select branch

Commits
-------

1c2bae2 added "special case"
@fabpot
Copy link
Member

fabpot commented Jun 19, 2019

Tests for low are broken, which means that some constraints need to be changed (SecurityBundle depends on the Security component).

@oleg-andreyev
Copy link
Contributor Author

@fabpot the only way is to skip SecurityTest::testUserWillBeMarkedAsChangedIfRolesHasChanged for low, because it's a new test which was added in this PR.

Or there is another solution for such case?

@fabpot
Copy link
Member

fabpot commented Jun 19, 2019

You can bump the minimum version for the dependency on SecurityBundle.

@oleg-andreyev
Copy link
Contributor Author

and it will pull changes from the current branch, right?

@xabbuh
Copy link
Member

xabbuh commented Jun 19, 2019

@oleg-andreyev yes :)

@oleg-andreyev
Copy link
Contributor Author

@xabbuh thanks for explaining, didn't know that

@oleg-andreyev
Copy link
Contributor Author

Just some minor comments, but what I am more concerned about is that this will also log out users if they have been granted new roles. I suspect that this is something that is not expected.

I can understand this concern but theoretically we can add new role which actually limits previous roles and because we can't detect it the easiest way to ensure that ROLE will be taken into account is to logout user and force them to login again.

@oleg-andreyev
Copy link
Contributor Author

Ready for review.

Travis failed with:

1) Symfony\Component\Mailer\Tests\TransportTest::testFromDsnMailgun
stream_get_meta_data() expects parameter 1 to be resource, string given

@oleg-andreyev
Copy link
Contributor Author

@nicolas-grekas @fabpot @xabbuh @OskarStark please take a look.
Fail build is not related to changes in this PR.

@fabpot
Copy link
Member

fabpot commented Sep 6, 2019

@oleg-andreyev Can you squash your commits please?

- Updated isEqualTo method to match roles as default User implements EquatableInterface
- added test case
- bumped symfony/security-core to 4.4
@oleg-andreyev
Copy link
Contributor Author

oleg-andreyev commented Sep 6, 2019

@oleg-andreyev Can you squash your commits please?

Done.

@fabpot
Copy link
Member

fabpot commented Sep 6, 2019

Thank you @oleg-andreyev.

fabpot added a commit that referenced this pull request Sep 6, 2019
…ged (oleg-andreyev)

This PR was merged into the 4.4 branch.

Discussion
----------

#21571 Comparing roles to detected that users has changed

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT
| Fixed tickets | #21571 (comment)
| Docs | symfony/symfony-docs#11457

**Case 1:**
User A has roles `foo, bar and admin`, User A is signed-in into application and token is persisted, later another User B with role `admin`, decided to restrict role `admin` for User A, so User A won't lose it's privileges until session is expired or logout, because token is persisted with `roles` and `authenticated=true` and roles are not compared.

Ref. to the previous attempt: #27121

Commits
-------

4f4c30d - updated AbstractToken to compare Roles - Updated isEqualTo method to match roles as default User implements EquatableInterface - added test case - bumped symfony/security-core to 4.4
@fabpot fabpot merged commit 4f4c30d into symfony:4.4 Sep 6, 2019
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
This was referenced Nov 12, 2019
@XWB
Copy link
Contributor

XWB commented Nov 17, 2019

@oleg-andreyev We are doing something similar in our User entity:

namespace App\Entity;

use Symfony\Component\Security\Core\User\EquatableInterface;

class User implements EquatableInterface
{
    public function isEqualTo(UserInterface $user)
    {
        $isEqual = count($this->getRoles()) === count($user->getRoles());
        if ($isEqual) {
            foreach ($this->getRoles() as $role) {
                $isEqual = $isEqual && in_array($role, $user->getRoles());
            }
        }

        return $isEqual;
    }
}

Does that mean we can remove User::isEqualTo once we upgrade to Symfony 4.4? Will this feature have the same result?

@oleg-andreyev
Copy link
Contributor Author

@XWB yes, results will be the same.

@XWB
Copy link
Contributor

XWB commented Nov 19, 2019

@oleg-andreyev Thank you.

wouterj added a commit to wouterj/symfony that referenced this pull request May 29, 2020
nicolas-grekas added a commit that referenced this pull request May 30, 2020
This PR was squashed before being merged into the 4.4 branch.

Discussion
----------

[Security] Fixed AbstractToken::hasUserChanged()

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36989
| License       | MIT
| Doc PR        | -

This PR completely reverts #35944.

That PR tried to fix a BC break (ref #35941, #35509) introduced by #31177. However, this broke many authentications (ref #36989), as the User is serialized in the session (as hinted by @stof). Many applications don't include the `roles` property in the serialization (at least, the MakerBundle doesn't include it).

In 5.2, we should probably deprecate having different roles in token and user, which fixes the BC breaks all together.

Commits
-------

f297beb [Security] Fixed AbstractToken::hasUserChanged()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants