Skip to content
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

[make:user] Keep implementing deprecated username methods #890

Merged
merged 3 commits into from
Jun 18, 2021
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
10 changes: 10 additions & 0 deletions src/Resources/skeleton/security/UserProvider.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public function <?= $uses_user_identifier ? 'loadUserByIdentifier($identifier)'
throw new \Exception('TODO: fill in <?= $uses_user_identifier ? 'loadUserByIdentifier()' : 'loadUserByUsername()' ?> inside '.__FILE__);
}

<?php if ($uses_user_identifier) : ?>
/**
* @deprecated since Symfony 5.3, loadUserByIdentifier() is used instead
*/
public function loadUserByUsername($username): UserInterface
{
return $this->loadUserByIdentifier($username);
}

<?php endif ?>
/**
* Refreshes the user after being reloaded from the session.
*
Expand Down
19 changes: 14 additions & 5 deletions src/Security/UserClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,9 @@ private function addGetUsername(ClassSourceManipulator $manipulator, UserClassCo
);
}

$getterIdentifierName = 'getUsername';

// Check if we're using Symfony 5.3+ - UserInterface::getUsername() was replaced with UserInterface::getUserIdentifier()
if (class_exists(InMemoryUser::class)) {
$getterIdentifierName = 'getUserIdentifier';
}
$symfony53GTE = class_exists(InMemoryUser::class);
$getterIdentifierName = $symfony53GTE ? 'getUserIdentifier' : 'getUsername';

// define getUsername (if it was defined above, this will override)
$manipulator->addAccessorMethod(
Expand All @@ -118,6 +115,18 @@ private function addGetUsername(ClassSourceManipulator $manipulator, UserClassCo
],
true
);

if ($symfony53GTE) {
// also add the deprecated getUsername method
$manipulator->addAccessorMethod(
$userClassConfig->getIdentityPropertyName(),
'getUsername',
'string',
false,
['@deprecated since Symfony 5.3, use getUserIdentifier instead'],
true
);
}
}

private function addGetRoles(ClassSourceManipulator $manipulator, UserClassConfiguration $userClassConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ public function getUserIdentifier(): string
return (string) $this->email;
}

/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->email;
}

/**
* @see UserInterface
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Security/fixtures/expected/UserEntityWithPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public function setUserIdentifier(string $userIdentifier): self
return $this;
}

/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->userIdentifier;
}

/**
* @see UserInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public function setUserIdentifier(string $user_identifier): self
return $this;
}

/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->user_identifier;
}

/**
* @see UserInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public function setUserIdentifier(string $userIdentifier): self
return $this;
}

/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->userIdentifier;
}

/**
* @see UserInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public function getUserIdentifier(): string
return (string) $this->email;
}

/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->email;
}

/**
* @see UserInterface
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Security/fixtures/expected/UserModelWithPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public function setUserIdentifier(string $userIdentifier): self
return $this;
}

/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->userIdentifier;
}

/**
* @see UserInterface
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Security/fixtures/expected/UserModelWithoutPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public function setUserIdentifier(string $userIdentifier): self
return $this;
}

/**
* @deprecated since Symfony 5.3, use getUserIdentifier instead
*/
public function getUsername(): string
{
return (string) $this->userIdentifier;
}

/**
* @see UserInterface
*/
Expand Down