|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yokai\SecurityTokenBundle\Repository; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use Doctrine\ORM\EntityRepository; |
|
7
|
|
|
use Yokai\SecurityTokenBundle\Entity\Token; |
|
8
|
|
|
use Yokai\SecurityTokenBundle\Exception\TokenExpiredException; |
|
9
|
|
|
use Yokai\SecurityTokenBundle\Exception\TokenNotFoundException; |
|
10
|
|
|
use Yokai\SecurityTokenBundle\Exception\TokenUsedException; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Yann Eugoné <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class DoctrineORMTokenRepository implements TokenRepositoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var EntityManager |
|
19
|
|
|
*/ |
|
20
|
|
|
private $manager; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var EntityRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
private $repository; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param EntityManager $manager |
|
29
|
|
|
* @param EntityRepository $repository |
|
30
|
|
|
*/ |
|
31
|
7 |
|
public function __construct(EntityManager $manager, EntityRepository $repository) |
|
32
|
|
|
{ |
|
33
|
7 |
|
$this->manager = $manager; |
|
34
|
7 |
|
$this->repository = $repository; |
|
35
|
7 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @inheritdoc |
|
39
|
|
|
*/ |
|
40
|
5 |
|
public function get($value, $purpose) |
|
41
|
|
|
{ |
|
42
|
5 |
|
$token = $this->repository->findOneBy( |
|
43
|
|
|
[ |
|
44
|
5 |
|
'value' => $value, |
|
45
|
5 |
|
'purpose' => $purpose, |
|
46
|
|
|
] |
|
47
|
|
|
); |
|
48
|
|
|
|
|
49
|
5 |
|
if (!$token instanceof Token) { |
|
50
|
1 |
|
throw TokenNotFoundException::create($value, $purpose); |
|
51
|
|
|
} |
|
52
|
4 |
|
if ($token->isExpired()) { |
|
53
|
1 |
|
throw TokenExpiredException::create($value, $purpose, $token->getExpiresAt()); |
|
54
|
|
|
} |
|
55
|
3 |
|
if ($token->isUsed()) { |
|
56
|
2 |
|
throw TokenUsedException::create($value, $purpose, $token->getCountUsages()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return $token; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritDoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function findExisting($userClass, $userId, $purpose) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->repository->findOneBy( |
|
68
|
|
|
[ |
|
69
|
|
|
'userClass' => $userClass, |
|
70
|
|
|
'userId' => $userId, |
|
71
|
|
|
'purpose' => $purpose, |
|
72
|
|
|
] |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @inheritdoc |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function create(Token $token) |
|
80
|
|
|
{ |
|
81
|
1 |
|
$this->manager->persist($token); |
|
82
|
1 |
|
$this->manager->flush($token); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function update(Token $token) |
|
89
|
|
|
{ |
|
90
|
1 |
|
$this->manager->persist($token); |
|
91
|
1 |
|
$this->manager->flush($token); |
|
92
|
1 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
|
|
public function exists($value, $purpose) |
|
98
|
|
|
{ |
|
99
|
|
|
$builder = $this->repository->createQueryBuilder('token'); |
|
100
|
|
|
$builder |
|
101
|
|
|
->select('COUNT(token.id)') |
|
102
|
|
|
->where('token.value = :value') |
|
103
|
|
|
->andWhere('token.purpose = :purpose') |
|
104
|
|
|
->setParameters( |
|
105
|
|
|
[ |
|
106
|
|
|
'value' => $value, |
|
107
|
|
|
'purpose' => $purpose, |
|
108
|
|
|
] |
|
109
|
|
|
) |
|
110
|
|
|
; |
|
111
|
|
|
|
|
112
|
|
|
return intval($builder->getQuery()->getSingleScalarResult()) > 0; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|