summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Core/User/ChainUserProvider.php70
1 files changed, 70 insertions, 0 deletions
diff --git a/Core/User/ChainUserProvider.php b/Core/User/ChainUserProvider.php
new file mode 100644
index 0000000..296d099
--- /dev/null
+++ b/Core/User/ChainUserProvider.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace Symfony\Component\Security\Core\User;
+
+use Symfony\Component\Security\Core\Exception\UnsupportedAccountException;
+use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
+
+/**
+ * Chain User Provider.
+ *
+ * This provider calls several leaf providers in a chain until one is able to
+ * handle the request.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class ChainUserProvider implements UserProviderInterface
+{
+ protected $providers;
+
+ public function __construct(array $providers)
+ {
+ $this->providers = $providers;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function loadUserByUsername($username)
+ {
+ foreach ($this->providers as $provider) {
+ try {
+ return $provider->loadUserByUsername($username);
+ } catch (UsernameNotFoundException $notFound) {
+ // try next one
+ }
+ }
+
+ throw new UsernameNotFoundException(sprintf('There is no user with name "%s".', $username));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function loadUserByAccount(AccountInterface $account)
+ {
+ foreach ($this->providers as $provider) {
+ try {
+ return $provider->loadUserByAccount($account);
+ } catch (UnsupportedAccountException $unsupported) {
+ // try next one
+ }
+ }
+
+ throw new UnsupportedAccountException(sprintf('The account "%s" is not supported.', get_class($account)));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public function supportsClass($class)
+ {
+ foreach ($this->providers as $provider) {
+ if ($provider->supportsClass($class)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+} \ No newline at end of file