1: <?php
2:
3: /*
4: * This file is part of the Symfony package.
5: *
6: * (c) Fabien Potencier <fabien@symfony.com>
7: *
8: * For the full copyright and license information, please view the LICENSE
9: * file that was distributed with this source code.
10: */
11:
12: namespace Symfony\Component\Security\Core\User;
13:
14: use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
15: use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
16:
17: /**
18: * Chain User Provider.
19: *
20: * This provider calls several leaf providers in a chain until one is able to
21: * handle the request.
22: *
23: * @author Johannes M. Schmitt <schmittjoh@gmail.com>
24: */
25: class ChainUserProvider implements UserProviderInterface
26: {
27: private $providers;
28:
29: public function __construct(array $providers)
30: {
31: $this->providers = $providers;
32: }
33:
34: /**
35: * @return array
36: */
37: public function getProviders()
38: {
39: return $this->providers;
40: }
41:
42: /**
43: * {@inheritdoc}
44: */
45: public function loadUserByUsername($username)
46: {
47: foreach ($this->providers as $provider) {
48: try {
49: return $provider->loadUserByUsername($username);
50: } catch (UsernameNotFoundException $notFound) {
51: // try next one
52: }
53: }
54:
55: $ex = new UsernameNotFoundException(sprintf('There is no user with name "%s".', $username));
56: $ex->setUsername($username);
57: throw $ex;
58: }
59:
60: /**
61: * {@inheritdoc}
62: */
63: public function refreshUser(UserInterface $user)
64: {
65: $supportedUserFound = false;
66:
67: foreach ($this->providers as $provider) {
68: try {
69: return $provider->refreshUser($user);
70: } catch (UnsupportedUserException $unsupported) {
71: // try next one
72: } catch (UsernameNotFoundException $notFound) {
73: $supportedUserFound = true;
74: // try next one
75: }
76: }
77:
78: if ($supportedUserFound) {
79: $ex = new UsernameNotFoundException(sprintf('There is no user with name "%s".', $user->getUsername()));
80: $ex->setUsername($user->getUsername());
81: throw $ex;
82: } else {
83: throw new UnsupportedUserException(sprintf('The account "%s" is not supported.', get_class($user)));
84: }
85: }
86:
87: /**
88: * {@inheritdoc}
89: */
90: public function supportsClass($class)
91: {
92: foreach ($this->providers as $provider) {
93: if ($provider->supportsClass($class)) {
94: return true;
95: }
96: }
97:
98: return false;
99: }
100: }
101: