Skip to content

Commit

Permalink
Api formations file (#10873)
Browse files Browse the repository at this point in the history
* Allow users to download formations

* [Api] Formation content endpoints

* Fix review
  • Loading branch information
Remg authored Oct 15, 2024
1 parent dc496fe commit 45912d2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 11 deletions.
32 changes: 32 additions & 0 deletions src/Controller/Api/AbstractFormationContentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Controller\Api;

use App\Entity\Adherent;
use App\Entity\AdherentFormation\Formation;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

abstract class AbstractFormationContentController extends AbstractController
{
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}

protected function printFormation(Formation $formation): void
{
/** @var Adherent $adherent */
$adherent = $this->getUser();

if (
!$adherent instanceof Adherent
|| $this->isGranted('ROLE_OAUTH_SCOPE_JEMENGAGE_ADMIN')
) {
return;
}

if ($formation->addPrintByAdherent($adherent)) {
$this->entityManager->flush();
}
}
}
20 changes: 11 additions & 9 deletions src/Controller/Api/FormationDownloadFileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@
use App\Utils\HttpUtils;
use Cocur\Slugify\Slugify;
use League\Flysystem\FilesystemOperator;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class FormationDownloadFileController extends AbstractController
class FormationDownloadFileController extends AbstractFormationContentController
{
public function __construct(private readonly FilesystemOperator $defaultStorage)
{
}
public function __invoke(
FilesystemOperator $defaultStorage,
Formation $formation,
): Response {
if (!$formation->isFileContent()) {
throw $this->createNotFoundException('Formation has no file.');
}

$this->printFormation($formation);

public function __invoke(Request $request, Formation $formation): Response
{
$filePath = $formation->getFilePath();

return HttpUtils::createResponse(
$this->defaultStorage,
$defaultStorage,
$filePath,
(new Slugify())->slugify($formation->getTitle()).'.'.pathinfo($filePath, \PATHINFO_EXTENSION)
);
Expand Down
28 changes: 28 additions & 0 deletions src/Controller/Api/FormationLinkController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Controller\Api;

use App\Entity\AdherentFormation\Formation;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;

class FormationLinkController extends AbstractFormationContentController
{
public function __invoke(
SerializerInterface $serializer,
Formation $formation,
): Response {
if (!$formation->isLinkContent()) {
throw $this->createNotFoundException('Formation has no link');
}

$this->printFormation($formation);

return $this->json(
$formation,
Response::HTTP_OK,
[],
['groups' => ['formation_read_link']]
);
}
}
11 changes: 9 additions & 2 deletions src/Entity/AdherentFormation/Formation.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ApiPlatform\Metadata\Put;
use App\Api\Filter\ScopeVisibilityFilter;
use App\Controller\Api\FormationDownloadFileController;
use App\Controller\Api\FormationLinkController;
use App\Controller\Api\FormationUploadFileController;
use App\Entity\Adherent;
use App\Entity\EntityAdherentBlameableInterface;
Expand Down Expand Up @@ -61,7 +62,13 @@
uriTemplate: '/formations/{uuid}/file',
requirements: ['uuid' => '%pattern_uuid%'],
controller: FormationDownloadFileController::class,
security: 'is_granted(\'ROLE_OAUTH_SCOPE_JEMENGAGE_ADMIN\') and is_granted(\'IS_FEATURE_GRANTED\', \'adherent_formations\') and is_granted(\'SCOPE_CAN_MANAGE\', object)'
security: 'is_granted(\'ROLE_USER\')'
),
new Get(
uriTemplate: '/formations/{uuid}/link',
requirements: ['uuid' => '%pattern_uuid%'],
controller: FormationLinkController::class,
security: 'is_granted(\'ROLE_USER\')'
),
new Delete(
uriTemplate: '/formations/{uuid}',
Expand Down Expand Up @@ -126,7 +133,7 @@ class Formation implements EntityScopeVisibilityWithZoneInterface, EntityAdheren
private ?string $filePath = null;

#[Assert\Url]
#[Groups(['formation_read', 'formation_list_read', 'formation_write'])]
#[Groups(['formation_read', 'formation_list_read', 'formation_write', 'formation_read_link'])]
#[ORM\Column(nullable: true)]
private ?string $link = null;

Expand Down

0 comments on commit 45912d2

Please sign in to comment.