pull/5206/head
parent
da9f6d1a8f
commit
69cbf94960
@ -1,60 +0,0 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CoreBundle\Controller\Api; |
||||
|
||||
use Chamilo\CoreBundle\Entity\User; |
||||
use Chamilo\CourseBundle\Entity\CCalendarEvent; |
||||
use Chamilo\CourseBundle\Repository\CCalendarEventRepository; |
||||
use DateTime; |
||||
use Exception; |
||||
use Symfony\Component\HttpFoundation\Request; |
||||
use Symfony\Component\Security\Core\Security; |
||||
|
||||
class CreateCCalendarEventAction extends BaseResourceFileAction |
||||
{ |
||||
public function __invoke(Request $request, CCalendarEventRepository $repo, Security $security): CCalendarEvent |
||||
{ |
||||
$event = new CCalendarEvent(); |
||||
$result = $this->handleCreateRequest($event, $repo, $request); |
||||
|
||||
/** @var User $currentUser */ |
||||
$currentUser = $security->getUser(); |
||||
|
||||
$event |
||||
->setContent($result['content'] ?? '') |
||||
->setComment($result['comment'] ?? '') |
||||
->setColor($result['color'] ?? '') |
||||
->setStartDate(new DateTime($result['startDate'] ?? '')) |
||||
->setEndDate(new DateTime($result['endDate'] ?? '')) |
||||
// ->setAllDay($result['allDay'] ?? false) |
||||
->setCollective($result['collective'] ?? false) |
||||
->setCreator($currentUser) |
||||
; |
||||
|
||||
// Detect event type based in the resource link array. |
||||
|
||||
$type = 'personal'; |
||||
if (!empty($event->getResourceLinkArray())) { |
||||
foreach ($event->getResourceLinkArray() as $link) { |
||||
if (isset($link['cid'])) { |
||||
$type = 'course'; |
||||
|
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
if ('personal' === $type) { |
||||
if ($currentUser->getResourceNode()->getId() !== $event->getParentResourceNode()) { |
||||
throw new Exception('Not allowed'); |
||||
} |
||||
} |
||||
// @todo check course access? Should be handle by CourseVoter? |
||||
|
||||
return $event; |
||||
} |
||||
} |
@ -0,0 +1,64 @@ |
||||
<?php |
||||
|
||||
namespace Chamilo\CoreBundle\State; |
||||
|
||||
use ApiPlatform\Metadata\Operation; |
||||
use ApiPlatform\State\ProcessorInterface; |
||||
use Chamilo\CoreBundle\Entity\User; |
||||
use Chamilo\CourseBundle\Entity\CCalendarEvent; |
||||
use Exception; |
||||
use Symfony\Component\Security\Core\Security; |
||||
|
||||
/** |
||||
* @implements ProcessorInterface<CCalendarEvent> |
||||
*/ |
||||
class CCalendarEventProcessor implements ProcessorInterface |
||||
{ |
||||
public function __construct( |
||||
private readonly ProcessorInterface $persistProcessor, |
||||
private readonly Security $security, |
||||
) { |
||||
} |
||||
|
||||
/** |
||||
* @throws Exception |
||||
*/ |
||||
public function process($data, Operation $operation, array $uriVariables = [], array $context = []): CCalendarEvent |
||||
{ |
||||
assert($data instanceof CCalendarEvent); |
||||
|
||||
/** @var User $currentUser */ |
||||
$currentUser = $this->security->getUser(); |
||||
|
||||
$data->setCreator($currentUser); |
||||
|
||||
|
||||
if ($this->isPersonalEvent($data)) { |
||||
if ($currentUser->getResourceNode()->getId() !== $data->getParentResourceNode()) { |
||||
throw new Exception('Not allowed'); |
||||
} |
||||
} |
||||
|
||||
/** @var CCalendarEvent $result */ |
||||
$result = $this->persistProcessor->process($data, $operation, $uriVariables, $context); |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
private function isPersonalEvent(CCalendarEvent $event): bool |
||||
{ |
||||
$type = 'personal'; |
||||
|
||||
if (!empty($event->getResourceLinkArray())) { |
||||
foreach ($event->getResourceLinkArray() as $link) { |
||||
if (isset($link['cid'])) { |
||||
$type = 'course'; |
||||
|
||||
break; |
||||
} |
||||
} |
||||
} |
||||
|
||||
return 'personal' === $type; |
||||
} |
||||
} |
Loading…
Reference in new issue