From 674295da0a0f03aaf7fae102703547f43951f55b Mon Sep 17 00:00:00 2001 From: Michal Trojanowski Date: Sat, 29 Nov 2014 13:56:09 +0100 Subject: [PATCH 1/2] [Security] [Doctrine] Added BaseUserProviderInterface You can now implement BaseUserProviderInterface without the need of implementing `supportsClass` function. --- .../Security/User/EntityUserProvider.php | 2 +- .../Core/User/BaseUserProviderInterface.php | 67 +++++++++++++++++++ .../Core/User/UserProviderInterface.php | 34 +--------- 3 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 src/Symfony/Component/Security/Core/User/BaseUserProviderInterface.php diff --git a/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php b/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php index 0385a1dd7faed..894973e9397f2 100644 --- a/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php +++ b/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php @@ -54,7 +54,7 @@ public function loadUserByUsername($username) if (null !== $this->property) { $user = $this->repository->findOneBy(array($this->property => $username)); } else { - if (!$this->repository instanceof UserProviderInterface) { + if (!$this->repository instanceof BaseUserProviderInterface) { throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($this->repository))); } diff --git a/src/Symfony/Component/Security/Core/User/BaseUserProviderInterface.php b/src/Symfony/Component/Security/Core/User/BaseUserProviderInterface.php new file mode 100644 index 0000000000000..6d9e0c6c28f20 --- /dev/null +++ b/src/Symfony/Component/Security/Core/User/BaseUserProviderInterface.php @@ -0,0 +1,67 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Security\Core\User; + +use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; +use Symfony\Component\Security\Core\Exception\UnsupportedUserException; + +/** + * Represents a class that loads UserInterface objects from some source for the authentication system. + * + * In a typical authentication configuration, a username (i.e. some unique + * user identifier) credential enters the system (via form login, or any + * method). The user provider that is configured with that authentication + * method is asked to load the UserInterface object for the given username + * (via loadUserByUsername) so that the rest of the process can continue. + * + * Internally, a user provider can load users from any source (databases, + * configuration, web service). This is totally independent of how the authentication + * information is submitted or what the UserInterface object looks like. + * + * @see UserInterface + * + * @author Fabien Potencier + */ +interface BaseUserProviderInterface +{ + /** + * Loads the user for the given username. + * + * This method must throw UsernameNotFoundException if the user is not + * found. + * + * @param string $username The username + * + * @return UserInterface + * + * @see UsernameNotFoundException + * + * @throws UsernameNotFoundException if the user is not found + * + */ + public function loadUserByUsername($username); + + /** + * Refreshes the user for the account interface. + * + * It is up to the implementation to decide if the user data should be + * totally reloaded (e.g. from the database), or if the UserInterface + * object can just be merged into some internal array of users / identity + * map. + * @param UserInterface $user + * + * @return UserInterface + * + * @throws UnsupportedUserException if the account is not supported + */ + public function refreshUser(UserInterface $user); +} diff --git a/src/Symfony/Component/Security/Core/User/UserProviderInterface.php b/src/Symfony/Component/Security/Core/User/UserProviderInterface.php index 6b7895cd204e6..cc8c33b710ae6 100644 --- a/src/Symfony/Component/Security/Core/User/UserProviderInterface.php +++ b/src/Symfony/Component/Security/Core/User/UserProviderInterface.php @@ -31,40 +31,8 @@ * * @author Fabien Potencier */ -interface UserProviderInterface +interface UserProviderInterface extends BaseUserProviderInterface { - /** - * Loads the user for the given username. - * - * This method must throw UsernameNotFoundException if the user is not - * found. - * - * @param string $username The username - * - * @return UserInterface - * - * @see UsernameNotFoundException - * - * @throws UsernameNotFoundException if the user is not found - * - */ - public function loadUserByUsername($username); - - /** - * Refreshes the user for the account interface. - * - * It is up to the implementation to decide if the user data should be - * totally reloaded (e.g. from the database), or if the UserInterface - * object can just be merged into some internal array of users / identity - * map. - * @param UserInterface $user - * - * @return UserInterface - * - * @throws UnsupportedUserException if the account is not supported - */ - public function refreshUser(UserInterface $user); - /** * Whether this provider supports the given user class * From 82a3469e610e0fb18fa6d6e1ab06936873f9854c Mon Sep 17 00:00:00 2001 From: Michal Trojanowski Date: Sat, 29 Nov 2014 14:51:52 +0100 Subject: [PATCH 2/2] [Security] [Doctrine] Fixed compliance with symfony coding standards * Removed unneeded `use` statements. * Fixed error message to reference the new interface * Changed `refreshUser` to compare to the new interface --- .../Bridge/Doctrine/Security/User/EntityUserProvider.php | 4 ++-- .../Component/Security/Core/User/UserProviderInterface.php | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php b/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php index 894973e9397f2..97664dcde82a4 100644 --- a/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php +++ b/src/Symfony/Bridge/Doctrine/Security/User/EntityUserProvider.php @@ -55,7 +55,7 @@ public function loadUserByUsername($username) $user = $this->repository->findOneBy(array($this->property => $username)); } else { if (!$this->repository instanceof BaseUserProviderInterface) { - throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement UserProviderInterface.', get_class($this->repository))); + throw new \InvalidArgumentException(sprintf('The Doctrine repository "%s" must implement BaseUserProviderInterface.', get_class($this->repository))); } $user = $this->repository->loadUserByUsername($username); @@ -77,7 +77,7 @@ public function refreshUser(UserInterface $user) throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); } - if ($this->repository instanceof UserProviderInterface) { + if ($this->repository instanceof BaseUserProviderInterface) { $refreshedUser = $this->repository->refreshUser($user); } else { // The user must be reloaded via the primary key as all other data diff --git a/src/Symfony/Component/Security/Core/User/UserProviderInterface.php b/src/Symfony/Component/Security/Core/User/UserProviderInterface.php index cc8c33b710ae6..86b46627fd35e 100644 --- a/src/Symfony/Component/Security/Core/User/UserProviderInterface.php +++ b/src/Symfony/Component/Security/Core/User/UserProviderInterface.php @@ -11,9 +11,6 @@ namespace Symfony\Component\Security\Core\User; -use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; -use Symfony\Component\Security\Core\Exception\UnsupportedUserException; - /** * Represents a class that loads UserInterface objects from some source for the authentication system. *