|
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\Metadata\Operation; |
|
10
|
|
|
use ApiPlatform\State\ProviderInterface; |
|
11
|
|
|
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
|
12
|
|
|
use Chamilo\CoreBundle\Repository\SessionRepository; |
|
13
|
|
|
use Chamilo\CoreBundle\ServiceHelper\AccessUrlHelper; |
|
14
|
|
|
use Chamilo\CoreBundle\ServiceHelper\UserHelper; |
|
15
|
|
|
use Exception; |
|
16
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
17
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
18
|
|
|
|
|
19
|
|
|
class UserSessionSubscriptionsStateProvider implements ProviderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
public function __construct( |
|
22
|
|
|
private readonly UserHelper $userHelper, |
|
23
|
|
|
private readonly AccessUrlHelper $accessUrlHelper, |
|
24
|
|
|
private readonly UserRepository $userRepository, |
|
25
|
|
|
private readonly SessionRepository $sessionRepository, |
|
26
|
|
|
) {} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @throws Exception |
|
30
|
|
|
*/ |
|
31
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []) |
|
32
|
|
|
{ |
|
33
|
|
|
$user = $this->userRepository->find($uriVariables['id']); |
|
34
|
|
|
|
|
35
|
|
|
if (!$user) { |
|
36
|
|
|
throw new NotFoundHttpException('User not found'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$currentUser = $this->userHelper->getCurrent(); |
|
40
|
|
|
$url = $this->accessUrlHelper->getCurrent(); |
|
41
|
|
|
|
|
42
|
|
|
$isAllowed = $user === $currentUser || $currentUser->isAdmin(); |
|
43
|
|
|
|
|
44
|
|
|
if (!$isAllowed) { |
|
45
|
|
|
throw new AccessDeniedException(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$qb = match ($operation->getName()) { |
|
49
|
|
|
'user_session_subscriptions_past' => $this->sessionRepository->getPastSessionsByUser($user, $url), |
|
50
|
|
|
'user_session_subscriptions_current' => $this->sessionRepository->getCurrentSessionsByUser($user, $url), |
|
51
|
|
|
'user_session_subscriptions_upcoming' => $this->sessionRepository->getUpcomingSessionsByUser($user, $url), |
|
52
|
|
|
}; |
|
53
|
|
|
|
|
54
|
|
|
return $qb->getQuery()->getResult(); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|