1
|
|
|
<?php |
2
|
|
|
namespace Mado\QueryBundle\Services; |
3
|
|
|
|
4
|
|
|
use Doctrine\ORM\EntityManager; |
5
|
|
|
use Doctrine\ORM\QueryBuilder; |
6
|
|
|
use Mado\QueryBundle\Component\ConfigProvider as ConfigProviderInterface; |
7
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
8
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
9
|
|
|
|
10
|
|
|
class ConfigProvider implements ConfigProviderInterface |
11
|
|
|
{ |
12
|
|
|
private $user; |
13
|
|
|
|
14
|
|
|
private $request; |
15
|
|
|
|
16
|
|
|
private $manager; |
17
|
|
|
|
18
|
|
|
private $domainConfiguration; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
RequestStack $requestStack, |
22
|
|
|
TokenStorage $tokenStorage, |
23
|
|
|
EntityManager $manager |
24
|
|
|
) { |
25
|
|
|
$this->token = $tokenStorage->getToken(); |
|
|
|
|
26
|
|
|
$this->user = $this->token->getUser(); |
|
|
|
|
27
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
28
|
|
|
$this->manager = $manager; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function setDomainConfiguration(array $domainConfiguration = []) |
32
|
|
|
{ |
33
|
|
|
$this->domainConfiguration = $domainConfiguration; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getConf() |
37
|
|
|
{ |
38
|
|
|
return $this->domainConfiguration; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getUser() |
42
|
|
|
{ |
43
|
|
|
return $this->user; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getRequest() |
47
|
|
|
{ |
48
|
|
|
return $this->request; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getUserRoles() |
52
|
|
|
{ |
53
|
|
|
return $this->token->getRoles(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function filterRelation(QueryBuilder $qb) |
57
|
|
|
{ |
58
|
|
|
$conf = $this->getConf(); |
59
|
|
|
|
60
|
|
|
foreach ($conf['additional-filters'] as $entityClass => $entityIds) { |
61
|
|
|
if (isset($conf['entity-map'][$conf['root-entity']])) { |
62
|
|
|
$relationName = $conf['entity-map'][$conf['root-entity']]; |
63
|
|
|
|
64
|
|
|
$qb->andWhere( |
65
|
|
|
$qb->expr()->in( |
66
|
|
|
$qb->getRootAlias() . '.' . $relationName[$entityClass], |
|
|
|
|
67
|
|
|
$entityIds |
68
|
|
|
) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
var_dump($qb->getQuery()->getDql()); |
|
|
|
|
72
|
|
|
die; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $qb; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|