|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SkautisBundle\Security\Authentication\Connector; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManager; |
|
6
|
|
|
use SkautisBundle\Entity\DoctrineUserConnection; |
|
7
|
|
|
use SkautisBundle\Exception\UserConnectionNotFound; |
|
8
|
|
|
use SkautisBundle\Exception\UserNotFound; |
|
9
|
|
|
use SkautisBundle\Security\Authentication\Connector\UserConnectorInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Trida pro propojeni symfony uzivatele se Skautis uzivatelem, ukladajici data pomoci Doctrine ORM |
|
13
|
|
|
*/ |
|
14
|
|
|
class DoctrineUserConnector implements UserConnectorInterface |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var EntityManager |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $em; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(EntityManager $em) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->em = $em; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @inheritdoc |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getUsername($personId) |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @var DoctrineUserConnection |
|
34
|
|
|
*/ |
|
35
|
|
|
$connection = $this->em->find('SkautisBundle\Entity\DoctrineUserConnection', $personId); |
|
36
|
|
|
|
|
37
|
|
|
if (!$connection) { |
|
38
|
|
|
return ""; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $connection->getUsername(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @inheritdoc |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getPersonId($username) |
|
48
|
|
|
{ |
|
49
|
|
|
$connection = $this->findConnectionByUsername($username); |
|
50
|
|
|
|
|
51
|
|
|
if (!$connection) { |
|
52
|
|
|
return ""; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $connection->getUsername(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
|
|
public function connect($personId, $username) |
|
62
|
|
|
{ |
|
63
|
|
|
|
|
64
|
|
|
$queryBuilder = $this->em->createQueryBuilder() |
|
65
|
|
|
->select('C') |
|
66
|
|
|
->from('SkautisBundle\Entity\DoctrineUserConnection', 'C') |
|
67
|
|
|
->where('C.username = :username') |
|
68
|
|
|
->orWhere('C.personId = :person_id') |
|
69
|
|
|
->setParameter('username', $username) |
|
70
|
|
|
->setParameter('person_id', $personId); |
|
71
|
|
|
|
|
72
|
|
|
$result = $queryBuilder->getQuery()->getResult(); |
|
73
|
|
|
|
|
74
|
|
|
if (sizeof($result) > 0) { |
|
75
|
|
|
throw new UserNotFound("User cannot be connected because user '$username' does not exist!"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$connection = new DoctrineUserConnection(); |
|
80
|
|
|
$connection->setPersonId($personId); |
|
81
|
|
|
$connection->setUsername($username); |
|
82
|
|
|
$this->em->persist($connection); |
|
83
|
|
|
$this->em->flush(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function disconnect($username) |
|
90
|
|
|
{ |
|
91
|
|
|
$connection = $this->findConnectionByUsername($username); |
|
92
|
|
|
|
|
93
|
|
|
if (!$connection) { |
|
94
|
|
|
throw new UserConnectionNotFound("User '$username'' cannot be disconnected because he is not connected!"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
$this->em->remove($connection); |
|
98
|
|
|
$this->em->flush(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $username |
|
103
|
|
|
* @return DoctrineUserConnection|null |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function findConnectionByUsername($username) |
|
106
|
|
|
{ |
|
107
|
|
|
$queryBuilder = $this->em->createQueryBuilder() |
|
108
|
|
|
->select('C') |
|
109
|
|
|
->from('SkautisBundle\Entity\DoctrineUserConnection', 'C') |
|
110
|
|
|
->where('C.username = :username') |
|
111
|
|
|
->setParameter('username', $username); |
|
112
|
|
|
|
|
113
|
|
|
$result = $queryBuilder->getQuery()->getResult(); |
|
114
|
|
|
|
|
115
|
|
|
if (sizeof($result) < 1) { |
|
116
|
|
|
return null; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $result[0]; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|