1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Manager; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
7
|
|
|
use Doctrine\Common\Util\ClassUtils; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* User manager for doctrine entities. |
11
|
|
|
* |
12
|
|
|
* @author Yann Eugoné <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class DoctrineUserManager implements UserManagerInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var ManagerRegistry |
18
|
|
|
*/ |
19
|
|
|
private $doctrine; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param ManagerRegistry $doctrine The doctrine registry |
23
|
|
|
*/ |
24
|
5 |
|
public function __construct(ManagerRegistry $doctrine) |
25
|
|
|
{ |
26
|
5 |
|
$this->doctrine = $doctrine; |
27
|
5 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
32
|
2 |
|
public function supportsClass($class) |
33
|
|
|
{ |
34
|
|
|
try { |
35
|
2 |
|
$manager = $this->getManagerFor($class); |
36
|
1 |
|
} catch (\Exception $exception) { |
37
|
1 |
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
return $manager instanceof ObjectManager; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritDoc |
45
|
|
|
*/ |
46
|
2 |
|
public function supportsUser($user) |
47
|
|
|
{ |
48
|
2 |
|
return $this->supportsClass( |
49
|
2 |
|
$this->getClass($user) |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
1 |
|
public function get($class, $id) |
57
|
|
|
{ |
58
|
1 |
|
return $this->getManagerFor($class)->find($class, $id); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritDoc |
63
|
|
|
*/ |
64
|
4 |
|
public function getClass($user) |
65
|
|
|
{ |
66
|
4 |
|
return ClassUtils::getClass($user); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
1 |
|
public function getId($user) |
73
|
|
|
{ |
74
|
1 |
|
$class = $this->getClass($user); |
75
|
1 |
|
$identifiers = $this->getManagerFor($class)->getClassMetadata($class)->getIdentifierValues($user); |
76
|
|
|
|
77
|
1 |
|
if (count($identifiers) > 1) { |
78
|
|
|
throw new \RuntimeException('Entities with composite ids are not supported'); |
79
|
|
|
} |
80
|
|
|
|
81
|
1 |
|
return (string) reset($identifiers); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get doctrine object manager for a class. |
86
|
|
|
* |
87
|
|
|
* @param string $class The user class |
88
|
|
|
* |
89
|
|
|
* @return ObjectManager |
90
|
|
|
*/ |
91
|
4 |
|
private function getManagerFor($class) |
92
|
|
|
{ |
93
|
4 |
|
$manager = $this->doctrine->getManagerForClass($class); |
94
|
|
|
|
95
|
4 |
|
if ($manager === null) { |
96
|
1 |
|
throw new \RuntimeException( |
97
|
1 |
|
sprintf( |
98
|
1 |
|
'Class "%s" seems not to be a managed Doctrine entity. Did you forget to map it?', |
99
|
1 |
|
$class |
100
|
|
|
) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
3 |
|
return $manager; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|