Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
Deprecated the AdvancedUserInterface
  • Loading branch information
Iltar van der Berg authored and linaori committed Feb 4, 2018
commit 8456f3b32ce6ec394fb27b9fc9a2989ed54862b1
4 changes: 4 additions & 0 deletions UPGRADE-4.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ Security
--------

* The `ContextListener::setLogoutOnUserChange()` method is deprecated and will be removed in 5.0.
* Using the `AdvancedUserInterface` is now deprecated. To use the existing
functionality, create a custom user-checker based on the
`Symfony\Component\Security\Core\User\UserChecker`. This functionality will
be removed in Symfony 5.0.

SecurityBundle
--------------
Expand Down
1 change: 1 addition & 0 deletions UPGRADE-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Security
--------

* The `ContextListener::setLogoutOnUserChange()` method has been removed.
* The `Symfony\Component\Security\Core\User\AdvancedUserInterface` has been removed.

SecurityBundle
--------------
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Security/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ CHANGELOG

* The `ContextListener::setLogoutOnUserChange()` method is deprecated and will be removed in 5.0.
* added `UserValueResolver`.
* Using the AdvancedUserInterface is now deprecated. To use the existing
functionality, create a custom user-checker based on the
`Symfony\Component\Security\Core\User\UserChecker`. This functionality will
be removed in Symfony 5.0.

4.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ private function hasUserChanged(UserInterface $user)
}

if ($this->user instanceof AdvancedUserInterface && $user instanceof AdvancedUserInterface) {
@trigger_error(sprintf('Checking for the AdvancedUserInterface in %s has been deprecated in 4.1 and will be removed in 5.0. Implement the %s to check if the user has been changed,', __METHOD__, EquatableInterface::class), E_USER_DEPRECATED);
if ($this->user->isAccountNonExpired() !== $user->isAccountNonExpired()) {
return true;
}
Expand All @@ -277,6 +278,8 @@ private function hasUserChanged(UserInterface $user)
return true;
}
} elseif ($this->user instanceof AdvancedUserInterface xor $user instanceof AdvancedUserInterface) {
@trigger_error(sprintf('Checking for the AdvancedUserInterface in %s has been deprecated in 4.1 and will be removed in 5.0. Implement the %s to check if the user has been changed,', __METHOD__, EquatableInterface::class), E_USER_DEPRECATED);

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public function getCredentials()
}
}

/** @noinspection PhpUndefinedClassInspection */
class AbstractTokenTest extends TestCase
{
public function testGetUsername()
Expand Down Expand Up @@ -185,10 +184,8 @@ public function testSetUser($user)
public function getUsers()
{
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
$advancedUser = $this->getMockBuilder('Symfony\Component\Security\Core\User\AdvancedUserInterface')->getMock();

return array(
array($advancedUser),
array($user),
array(new TestUser('foo')),
array('foo'),
Expand All @@ -212,53 +209,59 @@ public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $
}

public function getUserChanges()
{
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm just wondering since I'm not too familiar with the Symfony testing guidelines: Wouldn't it be easier and more refactoring-friendly to use the ::class constants here instead of hand-writing the FQCN?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consistency in this case. I personally would update everything to use ::class, but that might cause merge conflicts for no reason when merging changes from lower branches upwards.

Copy link
Contributor

@keichinger keichinger Mar 1, 2018

Choose a reason for hiding this comment

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

Fair enough :) Thanks!

Opened #26349


return array(
array('foo', 'bar'),
array('foo', new TestUser('bar')),
array('foo', $user),
array($user, 'foo'),
array($user, new TestUser('foo')),
array(new TestUser('foo'), new TestUser('bar')),
array(new TestUser('foo'), 'bar'),
array(new TestUser('foo'), $user),
);
}

/**
* @group legacy
*
* @dataProvider getUserChangesAdvancedUser
*/
public function testSetUserSetsAuthenticatedToFalseWhenUserChangesdvancedUser($firstUser, $secondUser)
Copy link
Contributor

Choose a reason for hiding this comment

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

There's an 'A' missing here to complete the word 'Advanced' :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Heh, you're completely right! If you wish, you could make a PR to fix this (you can just click the edit file). Gives you a nice contributor tag

Copy link
Contributor

Choose a reason for hiding this comment

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

Will do =) Thank you.

{
$token = $this->getToken();
$token->setAuthenticated(true);
$this->assertTrue($token->isAuthenticated());

$token->setUser($firstUser);
$this->assertTrue($token->isAuthenticated());

$token->setUser($secondUser);
$this->assertFalse($token->isAuthenticated());
}

public function getUserChangesAdvancedUser()
{
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
$advancedUser = $this->getMockBuilder('Symfony\Component\Security\Core\User\AdvancedUserInterface')->getMock();

return array(
array(
'foo', 'bar',
),
array(
'foo', new TestUser('bar'),
),
array(
'foo', $user,
),
array(
'foo', $advancedUser,
),
array(
$user, 'foo',
),
array(
$advancedUser, 'foo',
),
array(
$user, new TestUser('foo'),
),
array(
$advancedUser, new TestUser('foo'),
),
array(
new TestUser('foo'), new TestUser('bar'),
),
array(
new TestUser('foo'), 'bar',
),
array(
new TestUser('foo'), $user,
),
array(
new TestUser('foo'), $advancedUser,
),
array(
$user, $advancedUser,
),
array(
$advancedUser, $user,
),
array('foo', 'bar'),
array('foo', new TestUser('bar')),
array('foo', $user),
array('foo', $advancedUser),
array($user, 'foo'),
array($advancedUser, 'foo'),
array($user, new TestUser('foo')),
array($advancedUser, new TestUser('foo')),
array(new TestUser('foo'), new TestUser('bar')),
array(new TestUser('foo'), 'bar'),
array(new TestUser('foo'), $user),
array(new TestUser('foo'), $advancedUser),
array($user, $advancedUser),
array($advancedUser, $user),
);
}

Expand Down
68 changes: 60 additions & 8 deletions src/Symfony/Component/Security/Core/Tests/User/UserCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Security\Core\Tests\User;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\UserChecker;

class UserCheckerTest extends TestCase
Expand All @@ -24,6 +25,16 @@ public function testCheckPostAuthNotAdvancedUserInterface()
}

public function testCheckPostAuthPass()
{
$checker = new UserChecker();
$this->assertNull($checker->checkPostAuth(new User('John', 'password')));
}

/**
* @group legacy
Copy link
Contributor

Choose a reason for hiding this comment

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

missing @expectedDeprecation?

* @expectedDeprecation Calling Symfony\Component\Security\Core\User\UserChecker::checkPostAuth with an AdvancedUserInterface is deprecated as of 4.1 and will be removed in 5.0. Create a custom user checker if you wish to keep this functionality.
*/
public function testCheckPostAuthPassAdvancedUser()
{
$checker = new UserChecker();

Expand All @@ -39,21 +50,29 @@ public function testCheckPostAuthPass()
public function testCheckPostAuthCredentialsExpired()
{
$checker = new UserChecker();

$account = $this->getMockBuilder('Symfony\Component\Security\Core\User\AdvancedUserInterface')->getMock();
$account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(false));

$checker->checkPostAuth($account);
$checker->checkPostAuth(new User('John', 'password', array(), true, true, false, true));
}

public function testCheckPreAuthNotAdvancedUserInterface()
/**
* @group legacy
* @expectedDeprecation Calling Symfony\Component\Security\Core\User\UserChecker::checkPostAuth with an AdvancedUserInterface is deprecated as of 4.1 and will be removed in 5.0. Create a custom user checker if you wish to keep this functionality.
* @expectedException \Symfony\Component\Security\Core\Exception\CredentialsExpiredException
*/
public function testCheckPostAuthCredentialsExpiredAdvancedUser()
{
$checker = new UserChecker();

$this->assertNull($checker->checkPreAuth($this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock()));
$account = $this->getMockBuilder('Symfony\Component\Security\Core\User\AdvancedUserInterface')->getMock();
$account->expects($this->once())->method('isCredentialsNonExpired')->will($this->returnValue(false));

$checker->checkPostAuth($account);
}

public function testCheckPreAuthPass()
/**
* @group legacy
* @expectedDeprecation Calling Symfony\Component\Security\Core\User\UserChecker::checkPreAuth with an AdvancedUserInterface is deprecated as of 4.1 and will be removed in 5.0. Create a custom user checker if you wish to keep this functionality.
*/
public function testCheckPreAuthPassAdvancedUser()
{
$checker = new UserChecker();

Expand All @@ -69,6 +88,17 @@ public function testCheckPreAuthPass()
* @expectedException \Symfony\Component\Security\Core\Exception\LockedException
*/
public function testCheckPreAuthAccountLocked()
{
$checker = new UserChecker();
$checker->checkPreAuth(new User('John', 'password', array(), true, true, false, false));
}

/**
* @group legacy
* @expectedDeprecation Calling Symfony\Component\Security\Core\User\UserChecker::checkPreAuth with an AdvancedUserInterface is deprecated as of 4.1 and will be removed in 5.0. Create a custom user checker if you wish to keep this functionality.
* @expectedException \Symfony\Component\Security\Core\Exception\LockedException
*/
public function testCheckPreAuthAccountLockedAdvancedUser()
{
$checker = new UserChecker();

Expand All @@ -82,6 +112,17 @@ public function testCheckPreAuthAccountLocked()
* @expectedException \Symfony\Component\Security\Core\Exception\DisabledException
*/
public function testCheckPreAuthDisabled()
{
$checker = new UserChecker();
$checker->checkPreAuth(new User('John', 'password', array(), false, true, false, true));
}

/**
* @group legacy
* @expectedDeprecation Calling Symfony\Component\Security\Core\User\UserChecker::checkPreAuth with an AdvancedUserInterface is deprecated as of 4.1 and will be removed in 5.0. Create a custom user checker if you wish to keep this functionality.
* @expectedException \Symfony\Component\Security\Core\Exception\DisabledException
*/
public function testCheckPreAuthDisabledAdvancedUser()
{
$checker = new UserChecker();

Expand All @@ -96,6 +137,17 @@ public function testCheckPreAuthDisabled()
* @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException
*/
public function testCheckPreAuthAccountExpired()
{
$checker = new UserChecker();
$checker->checkPreAuth(new User('John', 'password', array(), true, false, true, true));
}

/**
* @group legacy
* @expectedDeprecation Calling Symfony\Component\Security\Core\User\UserChecker::checkPreAuth with an AdvancedUserInterface is deprecated as of 4.1 and will be removed in 5.0. Create a custom user checker if you wish to keep this functionality.
* @expectedException \Symfony\Component\Security\Core\Exception\AccountExpiredException
*/
public function testCheckPreAuthAccountExpiredAdvancedUser()
{
$checker = new UserChecker();

Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/Security/Core/Tests/User/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
namespace Symfony\Component\Security\Core\Tests\User;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\UserInterface;

class UserTest extends TestCase
{
Expand Down Expand Up @@ -99,4 +101,37 @@ public function testToString()
$user = new User('fabien', 'superpass');
$this->assertEquals('fabien', (string) $user);
}

/**
* @dataProvider isEqualToData
*
* @param bool $expectation
* @param EquatableInterface|UserInterface $a
* @param EquatableInterface|UserInterface $b
*/
public function testIsEqualTo($expectation, $a, $b)
{
$this->assertSame($expectation, $a->isEqualTo($b));
$this->assertSame($expectation, $b->isEqualTo($a));
}

public static function isEqualToData()
{
return array(
array(true, new User('username', 'password'), new User('username', 'password')),
array(true, new User('username', 'password', array('ROLE')), new User('username', 'password')),
array(true, new User('username', 'password', array('ROLE')), new User('username', 'password', array('NO ROLE'))),
array(false, new User('diff', 'diff'), new User('username', 'password')),
array(false, new User('diff', 'diff', array(), false), new User('username', 'password')),
array(false, new User('diff', 'diff', array(), false, false), new User('username', 'password')),
array(false, new User('diff', 'diff', array(), false, false, false), new User('username', 'password')),
array(false, new User('diff', 'diff', array(), false, false, false, false), new User('username', 'password')),
);
}

public function testIsEqualToWithDifferentUser()
{
$user = new User('username', 'password');
$this->assertFalse($user->isEqualTo($this->getMockBuilder(UserInterface::class)->getMock()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
*
* @see UserInterface
* @see AccountStatusException
* @deprecated since version 4.1, will be removed in 5.0.
*
* @author Fabien Potencier <[email protected]>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ interface EquatableInterface
* However, you do not need to compare every attribute, but only those that
* are relevant for assessing whether re-authentication is required.
*
* Also implementation should consider that $user instance may implement
* the extended user interface `AdvancedUserInterface`.
*
* @return bool
*/
public function isEqualTo(UserInterface $user);
Expand Down
Loading