1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* For licensing terms, see /license.txt */ |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\State; |
8
|
|
|
|
9
|
|
|
use ApiPlatform\Doctrine\Orm\Extension\PaginationExtension; |
10
|
|
|
use ApiPlatform\Doctrine\Orm\Util\QueryNameGenerator; |
11
|
|
|
use ApiPlatform\Metadata\Operation; |
12
|
|
|
use ApiPlatform\State\ProviderInterface; |
13
|
|
|
use Chamilo\CoreBundle\Entity\Session; |
14
|
|
|
use Chamilo\CoreBundle\Helpers\AccessUrlHelper; |
15
|
|
|
use Chamilo\CoreBundle\Helpers\UserHelper; |
16
|
|
|
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
17
|
|
|
use Chamilo\CoreBundle\Repository\SessionRepository; |
18
|
|
|
use Exception; |
19
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
20
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @template-implements ProviderInterface<Session> |
24
|
|
|
*/ |
25
|
|
|
class UserSessionSubscriptionsStateProvider implements ProviderInterface |
26
|
|
|
{ |
27
|
|
|
public function __construct( |
28
|
|
|
private readonly UserHelper $userHelper, |
29
|
|
|
private readonly AccessUrlHelper $accessUrlHelper, |
30
|
|
|
private readonly UserRepository $userRepository, |
31
|
|
|
private readonly SessionRepository $sessionRepository, |
32
|
|
|
private readonly PaginationExtension $paginationExtension, |
33
|
|
|
) {} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @throws Exception |
37
|
|
|
*/ |
38
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): iterable |
39
|
|
|
{ |
40
|
|
|
$user = $this->userRepository->find($uriVariables['id']); |
41
|
|
|
|
42
|
|
|
if (!$user) { |
43
|
|
|
throw new NotFoundHttpException('User not found'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$currentUser = $this->userHelper->getCurrent(); |
47
|
|
|
$url = $this->accessUrlHelper->getCurrent(); |
48
|
|
|
|
49
|
|
|
$isAllowed = $user === $currentUser || ($currentUser && $currentUser->isAdmin()); |
50
|
|
|
|
51
|
|
|
if (!$isAllowed) { |
52
|
|
|
throw new AccessDeniedException(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ('user_session_subscriptions_past' === $operation->getName()) { |
56
|
|
|
return $this->sessionRepository->getPastSessionsOfUserInUrl($user, $url); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$qb = match ($operation->getName()) { |
60
|
|
|
'user_session_subscriptions_current' => $this->sessionRepository->getCurrentSessionsOfUserInUrl( |
61
|
|
|
$user, |
62
|
|
|
$url |
63
|
|
|
), |
64
|
|
|
'user_session_subscriptions_upcoming' => $this->sessionRepository->getUpcomingSessionsOfUserInUrl( |
65
|
|
|
$user, |
66
|
|
|
$url |
67
|
|
|
), |
68
|
|
|
}; |
69
|
|
|
|
70
|
|
|
$this->paginationExtension->applyToCollection( |
71
|
|
|
$qb, |
72
|
|
|
new QueryNameGenerator(), |
73
|
|
|
Session::class, |
74
|
|
|
$operation, |
75
|
|
|
$context |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return $this->paginationExtension->getResult($qb, Session::class, $operation, $context); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|