10000 [Security] Added type-hints to user providers by derrabus · Pull Request #32353 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Security] Added type-hints to user providers #32353

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function __construct(ManagerRegistry $registry, string $classOrAlias, str
/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
public function loadUserByUsername(string $username)
Copy link
Member Author

Choose a reason for hiding this comment

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

💡 These changes to doctrine-bridge 5 break compatibility with security-core 4.4, thus the bump in composer.json. Can be reverted and re-applied later, if you don't want to bump yet.

{
$repository = $this->getRepository();
if (null !== $this->property) {
Expand Down Expand Up @@ -102,7 +102,7 @@ public function refreshUser(UserInterface $user)
/**
* {@inheritdoc}
*/
public function supportsClass($class)
public function supportsClass(string $class)
{
return $class === $this->getClass() || is_subclass_of($class, $this->getClass());
}
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"symfony/property-access": "^4.4|^5.0",
"symfony/property-info": "^4.4|^5.0",
"symfony/proxy-manager-bridge": "^4.4|^5.0",
"symfony/security-core": "^4.4|^5.0",
"symfony/security-core": "^5.0",
"symfony/expression-language": "^4.4|^5.0",
"symfony/validator": "^4.4|^5.0",
"symfony/translation": "^4.4|^5.0",
Expand All @@ -49,7 +49,8 @@
"phpunit/phpunit": "<5.4.3",
"symfony/dependency-injection": "<4.4",
"symfony/form": "<4.4",
"symfony/messenger": "<4.4"
"symfony/messenger": "<4.4",
"symfony/security-core": "<5"
},
"suggest": {
"symfony/form": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function getProviders()
/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
public function loadUserByUsername(string $username)
{
foreach ($this->providers as $provider) {
try {
Expand Down Expand Up @@ -94,7 +94,7 @@ public function refreshUser(UserInterface $user)
/**
* {@inheritdoc}
*/
public function supportsClass($class)
public function supportsClass(string $class)
{
foreach ($this->providers as $provider) {
if ($provider->supportsClass($class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function createUser(UserInterface $user)
/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
public function loadUserByUsername(string $username)
{
$user = $this->getUser($username);

Expand All @@ -85,7 +85,7 @@ public function refreshUser(UserInterface $user)
/**
* {@inheritdoc}
*/
public function supportsClass($class)
public function supportsClass(string $class)
{
return 'Symfony\Component\Security\Core\User\User' === $class;
}
Expand All @@ -99,7 +99,7 @@ public function supportsClass($class)
*
* @throws UsernameNotFoundException if user whose given username does not exist
*/
private function getUser($username)
private function getUser(string $username)
{
if (!isset($this->users[strtolower($username)])) {
$ex = new UsernameNotFoundException(sprintf('Username "%s" does not exist.', $username));
Expand Down
14 changes: 4 additions & 10 deletions src/Symfony/Component/Security/Core/User/LdapUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct(LdapInterface $ldap, string $baseDn, string $searchD
/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
public function loadUserByUsername(string $username)
{
try {
$this->ldap->bind($this->searchDn, $this->searchPassword);
Expand Down Expand Up @@ -109,20 +109,17 @@ public function refreshUser(UserInterface $user)
/**
* {@inheritdoc}
*/
public function supportsClass($class)
public function supportsClass(string $class)
{
return 'Symfony\Component\Security\Core\User\User' === $class;
}

/**
* Loads a user from an LDAP entry.
*
* @param string $username
* @param Entry $entry
*
* @return User
*/
protected function loadUser($username, Entry $entry)
protected function loadUser(string $username, Entry $entry)
{
$password = null;
$extraFields = [];
Expand All @@ -140,11 +137,8 @@ protected function loadUser($username, Entry $entry)

/**
* Fetches a required unique attribute value from an LDAP entry.
*
* @param Entry|null $entry
Copy link
Member Author

Choose a reason for hiding this comment

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

This @param annotation was obviously lying to us. 🤔

* @param string $attribute
*/
private function getAttributeValue(Entry $entry, $attribute)
private function getAttributeValue(Entry $entry, string $attribute)
{
if (!$entry->hasAttribute($attribute)) {
throw new InvalidArgumentException(sprintf('Missing attribute "%s" for user "%s".', $attribute, $entry->getDn()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(string $firewall)
/**
* {@inheritdoc}
*/
public function loadUserByUsername($username)
public function loadUserByUsername(string $username)
{
throw new \BadMethodCallException();
}
Expand All @@ -48,7 +48,7 @@ public function refreshUser(UserInterface $user)
/**
* {@inheritdoc}
*/
public function supportsClass($class)
public function supportsClass(string $class)
{
throw new \BadMethodCallException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ interface UserProviderInterface
* This method must throw UsernameNotFoundException if the user is not
* found.
*
* @param string $username The username
*
* @return UserInterface
*
* @throws UsernameNotFoundException if the user is not found
*/
public function loadUserByUsername($username);
public function loadUserByUsername(string $username);

/**
* Refreshes the user.
Expand All @@ -65,9 +63,7 @@ public function refreshUser(UserInterface $user);
/**
* Whether this provider supports the given user class.
*
* @param string $class
*
* @return bool
*/
public function supportsClass($class);
public function supportsClass(string $class);
}
0