Skip to content

Commit

Permalink
feature #1037 Deprecate user_identity_field config option (chalasr)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.x branch.

Discussion
----------

Deprecate user_identity_field config option

Fixes #881

Commits
-------

61c5a24 Deprecate user_identity_field config option
  • Loading branch information
chalasr committed Jun 12, 2022
2 parents bd59cdc + 61c5a24 commit 7e57628
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* LexikJWTAuthenticationBundle Configuration.
Expand Down Expand Up @@ -78,6 +79,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->scalarNode('user_identity_field')
->setDeprecated(...$this->getDeprecationParameters('The "%path%.%node%" configuration key is deprecated since version 2.15, implement "'.UserInterface::class.'::getUserIdentifier()" instead.', '2.5'))
->defaultValue('username')
->cannotBeEmpty()
->end()
Expand Down
1 change: 1 addition & 0 deletions Resources/config/jwt_manager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<argument>%lexik_jwt_authentication.user_id_claim%</argument>
<call method="setUserIdentityField">
<argument>%lexik_jwt_authentication.user_identity_field%</argument>
<argument>false</argument>
</call>
</service>

Expand Down
12 changes: 11 additions & 1 deletion Services/JWTManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class JWTManager implements JWTManagerInterface, JWTTokenManagerInterface

/**
* @var string
*
* @deprecated since v2.15
*/
protected $userIdentityField;

Expand Down Expand Up @@ -150,14 +152,18 @@ protected function addUserIdentityToPayload(UserInterface $user, array &$payload
return;
}

$payload[$this->userIdClaim ?: $this->userIdentityField] = $accessor->getValue($user, $this->userIdentityField);
$payload[$this->userIdClaim ?: $this->userIdentityField] = $accessor->getValue($user, $accessor->isReadable($user, $this->userIdentityField) ? $this->userIdentityField : 'user_identifier');
}

/**
* {@inheritdoc}
*/
public function getUserIdentityField(): string
{
if (0 === func_num_args() || func_get_arg(0)) {
trigger_deprecation('lexik/jwt-authentication-bundle', '2.15', 'The "%s()" method is deprecated.', __METHOD__);
}

return $this->userIdentityField;
}

Expand All @@ -166,6 +172,10 @@ public function getUserIdentityField(): string
*/
public function setUserIdentityField($field)
{
if (1 >= func_num_args() || func_get_arg(1)) {
trigger_deprecation('lexik/jwt-authentication-bundle', '2.15', 'The "%s()" method is deprecated.', __METHOD__);
}

$this->userIdentityField = $field;
}

Expand Down
4 changes: 4 additions & 0 deletions Services/JWTTokenManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ public function decode(TokenInterface $token);
* Sets the field used as identifier to load an user from a JWT payload.
*
* @param string $field
*
* @deprecated since 2.15, use {@see UserInterface::getUserIdentifier()} instead
*/
public function setUserIdentityField($field);

/**
* Returns the field used as identifier to load an user from a JWT payload.
*
* @return string
*
* @deprecated since 2.15, use {@see UserInterface::getUserIdentifier()} instead
*/
public function getUserIdentityField();

Expand Down
2 changes: 1 addition & 1 deletion Tests/Services/JWTManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function testParse()
}

/**
* test identity field.
* @group legacy
*/
public function testIdentityField()
{
Expand Down

0 comments on commit 7e57628

Please sign in to comment.