|
1 <?php |
|
2 |
|
3 namespace FOS\UserBundle\CouchDocument; |
|
4 |
|
5 use Doctrine\ODM\CouchDB\DocumentManager; |
|
6 use FOS\UserBundle\Model\UserInterface; |
|
7 use FOS\UserBundle\Model\UserManager as BaseUserManager; |
|
8 use FOS\UserBundle\Util\CanonicalizerInterface; |
|
9 use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
|
10 use Symfony\Component\Validator\Constraint; |
|
11 |
|
12 class UserManager extends BaseUserManager |
|
13 { |
|
14 /** |
|
15 * @var DocumentManager |
|
16 */ |
|
17 protected $dm; |
|
18 /** |
|
19 * @var DocumentRepository |
|
20 */ |
|
21 protected $repository; |
|
22 /** |
|
23 * @var string |
|
24 */ |
|
25 protected $class; |
|
26 |
|
27 /** |
|
28 * Constructor. |
|
29 * |
|
30 * @param EncoderFactoryInterface $encoderFactory |
|
31 * @param string $algorithm |
|
32 * @param CanonicalizerInterface $usernameCanonicalizer |
|
33 * @param CanonicalizerInterface $emailCanonicalizer |
|
34 * @param DocumentManager $dm |
|
35 * @param string $class |
|
36 */ |
|
37 public function __construct(EncoderFactoryInterface $encoderFactory, $algorithm, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, DocumentManager $dm, $class) |
|
38 { |
|
39 parent::__construct($encoderFactory, $algorithm, $usernameCanonicalizer, $emailCanonicalizer); |
|
40 |
|
41 $this->dm = $dm; |
|
42 $this->repository = $dm->getRepository($class); |
|
43 |
|
44 $metadata = $dm->getClassMetadata($class); |
|
45 $this->class = $metadata->name; |
|
46 } |
|
47 |
|
48 /** |
|
49 * {@inheritDoc} |
|
50 */ |
|
51 public function deleteUser(UserInterface $user) |
|
52 { |
|
53 $this->dm->remove($user); |
|
54 $this->dm->flush(); |
|
55 } |
|
56 |
|
57 /** |
|
58 * {@inheritDoc} |
|
59 */ |
|
60 public function getClass() |
|
61 { |
|
62 return $this->class; |
|
63 } |
|
64 |
|
65 /** |
|
66 * {@inheritDoc} |
|
67 */ |
|
68 public function findUserBy(array $criteria) |
|
69 { |
|
70 return $this->repository->findOneBy($criteria); |
|
71 } |
|
72 |
|
73 /** |
|
74 * {@inheritDoc} |
|
75 */ |
|
76 public function findUsers() |
|
77 { |
|
78 return $this->repository->findAll(); |
|
79 } |
|
80 |
|
81 /** |
|
82 * Updates a user. |
|
83 * |
|
84 * @param UserInterface $user |
|
85 * @param Boolean $andFlush Whether to flush the changes (default true) |
|
86 */ |
|
87 public function updateUser(UserInterface $user, $andFlush = true) |
|
88 { |
|
89 $this->updateCanonicalFields($user); |
|
90 $this->updatePassword($user); |
|
91 |
|
92 $this->dm->persist($user); |
|
93 if ($andFlush) { |
|
94 $this->dm->flush(); |
|
95 } |
|
96 } |
|
97 |
|
98 /** |
|
99 * {@inheritDoc} |
|
100 */ |
|
101 public function validateUnique(UserInterface $value, Constraint $constraint) |
|
102 { |
|
103 // for now unique checks are not implemented in Doctrine CouchDB yet |
|
104 return true; |
|
105 } |
|
106 |
|
107 public function reloadUser(UserInterface $user) |
|
108 { |
|
109 $this->dm->refresh($user); |
|
110 } |
|
111 } |
|
112 |