1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\DataProvider\Extension; |
8
|
|
|
|
9
|
|
|
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; |
10
|
|
|
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
11
|
|
|
use ApiPlatform\Metadata\Operation; |
12
|
|
|
use Chamilo\CoreBundle\Component\Utils\CreateDefaultPages; |
13
|
|
|
use Chamilo\CoreBundle\Entity\Page; |
14
|
|
|
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper; |
15
|
|
|
use Doctrine\ORM\QueryBuilder; |
16
|
|
|
use Symfony\Bundle\SecurityBundle\Security; |
17
|
|
|
|
18
|
|
|
final class PageExtension implements QueryCollectionExtensionInterface // , QueryItemExtensionInterface |
19
|
|
|
{ |
20
|
|
|
public function __construct( |
21
|
|
|
private readonly Security $security, |
22
|
|
|
private readonly AccessUrlHelper $accessUrlHelper, |
23
|
|
|
) {} |
24
|
|
|
|
25
|
|
|
public function applyToCollection( |
26
|
|
|
QueryBuilder $queryBuilder, |
27
|
|
|
QueryNameGeneratorInterface $queryNameGenerator, |
28
|
|
|
string $resourceClass, |
29
|
|
|
?Operation $operation = null, |
30
|
|
|
array $context = [] |
31
|
|
|
): void { |
32
|
|
|
if (Page::class !== $resourceClass) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$alias = $queryBuilder->getRootAliases()[0]; |
37
|
|
|
|
38
|
|
|
$url = $this->accessUrlHelper->getCurrent(); |
39
|
|
|
|
40
|
|
|
// Url filter by default. |
41
|
|
|
$queryBuilder |
42
|
|
|
->andWhere("$alias.url = :url") |
43
|
|
|
->setParameter('url', $url->getId()) |
44
|
|
|
->innerJoin( |
45
|
|
|
"$alias.category", |
46
|
|
|
'category', |
47
|
|
|
) |
48
|
|
|
; |
49
|
|
|
|
50
|
|
|
if (!$this->security->isGranted('ROLE_ADMIN')) { |
51
|
|
|
$queryBuilder->andWhere("$alias.enabled = 1") |
52
|
|
|
->andWhere( |
53
|
|
|
$queryBuilder->expr()->notIn( |
54
|
|
|
'category.title', |
55
|
|
|
CreateDefaultPages::getCategoriesForAdminBlocks() |
56
|
|
|
) |
57
|
|
|
) |
58
|
|
|
; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (!$this->security->isGranted('IS_AUTHENTICATED')) { |
62
|
|
|
$queryBuilder |
63
|
|
|
->andWhere( |
64
|
|
|
$queryBuilder->expr()->in('category.title', ':anon_categories') |
65
|
|
|
) |
66
|
|
|
->setParameter( |
67
|
|
|
'anon_categories', |
68
|
|
|
[ |
69
|
|
|
'faq', |
70
|
|
|
'demo', |
71
|
|
|
'home', |
72
|
|
|
'public', |
73
|
|
|
'footer_public', |
74
|
|
|
] |
75
|
|
|
) |
76
|
|
|
; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|