|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the FOSUserBundle package. |
|
5 * |
|
6 * (c) FriendsOfSymfony <http://friendsofsymfony.github.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 FOS\UserBundle\Security\Encoder; |
|
13 |
|
14 use FOS\UserBundle\Model\UserInterface; |
|
15 use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
|
16 use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface; |
|
17 |
|
18 /** |
|
19 * This factory assumes MessageDigestPasswordEncoder's constructor. |
|
20 * |
|
21 * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
22 * @author Jeremy Mikola <jmikola@gmail.com> |
|
23 */ |
|
24 class EncoderFactory implements EncoderFactoryInterface |
|
25 { |
|
26 protected $encoders; |
|
27 protected $encoderClass; |
|
28 protected $encodeHashAsBase64; |
|
29 protected $iterations; |
|
30 protected $genericFactory; |
|
31 |
|
32 /** |
|
33 * Constructor. |
|
34 * |
|
35 * @param string $encoderClass Encoder class |
|
36 * @param Boolean $encodeHashAsBase64 |
|
37 * @param integer $iterations |
|
38 * @param EncoderFactoryInterface $genericFactory |
|
39 */ |
|
40 public function __construct($encoderClass, $encodeHashAsBase64, $iterations, EncoderFactoryInterface $genericFactory) |
|
41 { |
|
42 $this->encoders = array(); |
|
43 $this->encoderClass = $encoderClass; |
|
44 $this->encodeHashAsBase64 = $encodeHashAsBase64; |
|
45 $this->iterations = $iterations; |
|
46 $this->genericFactory = $genericFactory; |
|
47 } |
|
48 |
|
49 /** |
|
50 * @see Symfony\Component\Security\Core\Encoder\EncoderFactory::getEncoder() |
|
51 */ |
|
52 public function getEncoder(SecurityUserInterface $user) |
|
53 { |
|
54 if (!$user instanceof UserInterface) { |
|
55 return $this->genericFactory->getEncoder($user); |
|
56 } |
|
57 |
|
58 if (isset($this->encoders[$algorithm = $user->getAlgorithm()])) { |
|
59 return $this->encoders[$algorithm]; |
|
60 } |
|
61 |
|
62 return $this->encoders[$algorithm] = $this->createEncoder($algorithm); |
|
63 } |
|
64 |
|
65 /** |
|
66 * Creates an encoder for the given algorithm. |
|
67 * |
|
68 * @param string $algorithm |
|
69 * @return PasswordEncoderInterface |
|
70 */ |
|
71 protected function createEncoder($algorithm) |
|
72 { |
|
73 $class = $this->encoderClass; |
|
74 |
|
75 return new $class( |
|
76 $algorithm, |
|
77 $this->encodeHashAsBase64, |
|
78 $this->iterations |
|
79 ); |
|
80 } |
|
81 } |