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\Entity\SessionRelUser; |
13
|
|
|
use Chamilo\CoreBundle\Entity\User; |
14
|
|
|
use Doctrine\ORM\QueryBuilder; |
15
|
|
|
use Symfony\Bundle\SecurityBundle\Security; |
16
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
17
|
|
|
|
18
|
|
|
// use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface; |
19
|
|
|
|
20
|
|
|
final class SessionRelUserExtension implements QueryCollectionExtensionInterface // , QueryItemExtensionInterface |
21
|
|
|
{ |
22
|
|
|
public function __construct( |
23
|
|
|
private readonly Security $security, |
24
|
|
|
) {} |
25
|
|
|
|
26
|
|
|
public function applyToCollection( |
27
|
|
|
QueryBuilder $queryBuilder, |
28
|
|
|
QueryNameGeneratorInterface $queryNameGenerator, |
29
|
|
|
string $resourceClass, |
30
|
|
|
?Operation $operation = null, |
31
|
|
|
array $context = [] |
32
|
|
|
): void { |
33
|
|
|
$this->addWhere($queryBuilder, $resourceClass); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/*public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, string $operationName = null, array $context = []): void |
37
|
|
|
{ |
38
|
|
|
error_log('applyToItem'); |
39
|
|
|
$this->addWhere($queryBuilder, $resourceClass); |
40
|
|
|
}*/ |
41
|
|
|
|
42
|
|
|
private function addWhere(QueryBuilder $qb, string $resourceClass): void |
43
|
|
|
{ |
44
|
|
|
if (SessionRelUser::class !== $resourceClass) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$alias = $qb->getRootAliases()[0]; |
49
|
|
|
|
50
|
|
|
$qb->innerJoin("$alias.session", 's'); |
51
|
|
|
|
52
|
|
|
if ($this->security->isGranted('ROLE_ADMIN')) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @var User|null $user */ |
57
|
|
|
if (null === $user = $this->security->getUser()) { |
58
|
|
|
throw new AccessDeniedException('Access Denied SessionRelUser'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$qb->andWhere(sprintf('%s.user = :current_user', $alias)); |
62
|
|
|
$qb->setParameter('current_user', $user->getId()); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|