1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* For licensing terms, see /license.txt */ |
4
|
|
|
|
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace Chamilo\CoreBundle\State; |
8
|
|
|
|
9
|
|
|
use ApiPlatform\Metadata\Operation; |
10
|
|
|
use ApiPlatform\State\ProcessorInterface; |
11
|
|
|
use Chamilo\CoreBundle\Entity\User; |
12
|
|
|
use Chamilo\CourseBundle\Entity\CCalendarEvent; |
13
|
|
|
use Exception; |
14
|
|
|
use Symfony\Component\Security\Core\Security; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @implements ProcessorInterface<CCalendarEvent> |
18
|
|
|
*/ |
19
|
|
|
class CCalendarEventProcessor implements ProcessorInterface |
20
|
|
|
{ |
21
|
|
|
public function __construct( |
22
|
|
|
private readonly ProcessorInterface $persistProcessor, |
23
|
|
|
private readonly Security $security, |
24
|
|
|
) {} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param mixed $data |
28
|
|
|
* |
29
|
|
|
* @throws Exception |
30
|
|
|
*/ |
31
|
|
|
public function process($data, Operation $operation, array $uriVariables = [], array $context = []): CCalendarEvent |
32
|
|
|
{ |
33
|
|
|
\assert($data instanceof CCalendarEvent); |
34
|
|
|
|
35
|
|
|
/** @var User $currentUser */ |
36
|
|
|
$currentUser = $this->security->getUser(); |
37
|
|
|
|
38
|
|
|
$data->setCreator($currentUser); |
39
|
|
|
|
40
|
|
|
if ($this->isPersonalEvent($data)) { |
41
|
|
|
if ($currentUser->getResourceNode()->getId() !== $data->getParentResourceNode()) { |
42
|
|
|
throw new Exception('Not allowed'); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @var CCalendarEvent $result */ |
47
|
|
|
return $this->persistProcessor->process($data, $operation, $uriVariables, $context); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function isPersonalEvent(CCalendarEvent $event): bool |
51
|
|
|
{ |
52
|
|
|
$type = 'personal'; |
53
|
|
|
|
54
|
|
|
if (!empty($event->getResourceLinkArray())) { |
55
|
|
|
foreach ($event->getResourceLinkArray() as $link) { |
56
|
|
|
if (isset($link['cid'])) { |
57
|
|
|
$type = 'course'; |
58
|
|
|
|
59
|
|
|
break; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return 'personal' === $type; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|