refs #512. Added new field 'user'
parent
b6ade24314
commit
d44882c59c
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DoctrineMigrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-generated Migration: Please modify to your needs!
|
||||||
|
*/
|
||||||
|
final class Version20240801130155 extends AbstractMigration
|
||||||
|
{
|
||||||
|
public function getDescription(): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this up() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->addSql('ALTER TABLE view ADD user_id INT DEFAULT NULL');
|
||||||
|
$this->addSql('ALTER TABLE view ADD CONSTRAINT FK_FEFDAB8EA76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
|
||||||
|
$this->addSql('CREATE INDEX IDX_FEFDAB8EA76ED395 ON view (user_id)');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this down() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->addSql('ALTER TABLE view DROP FOREIGN KEY FK_FEFDAB8EA76ED395');
|
||||||
|
$this->addSql('DROP INDEX IDX_FEFDAB8EA76ED395 ON view');
|
||||||
|
$this->addSql('ALTER TABLE view DROP user_id');
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,46 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Doctrine;
|
||||||
|
|
||||||
|
use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
|
||||||
|
use ApiPlatform\Doctrine\Orm\Extension\QueryItemExtensionInterface;
|
||||||
|
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
|
||||||
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use App\Entity\User;
|
||||||
|
use App\Entity\View;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
|
|
||||||
|
final readonly class UserViewExtension implements QueryCollectionExtensionInterface, QueryItemExtensionInterface
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private Security $security,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function applyToCollection(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void
|
||||||
|
{
|
||||||
|
$this->addWhere($queryBuilder, $resourceClass);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function applyToItem(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, array $identifiers, ?Operation $operation = null, array $context = []): void
|
||||||
|
{
|
||||||
|
$this->addWhere($queryBuilder, $resourceClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function addWhere(QueryBuilder $queryBuilder, string $resourceClass): void
|
||||||
|
{
|
||||||
|
if (View::class !== $resourceClass ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @var User $user */
|
||||||
|
$user = $this->security->getUser();
|
||||||
|
|
||||||
|
$rootAlias = $queryBuilder->getRootAliases()[0];
|
||||||
|
$queryBuilder->andWhere(sprintf('%s.user = :current_user', $rootAlias));
|
||||||
|
$queryBuilder->setParameter('current_user', $user->getId());
|
||||||
|
}
|
||||||
|
}
|
|
@ -17,6 +17,9 @@ class View extends \App\Entity\AbstractEntity
|
||||||
#[ORM\Column(type: Types::JSON, nullable: true)]
|
#[ORM\Column(type: Types::JSON, nullable: true)]
|
||||||
private ?array $filters = [];
|
private ?array $filters = [];
|
||||||
|
|
||||||
|
#[ORM\ManyToOne]
|
||||||
|
private ?User $user = null;
|
||||||
|
|
||||||
public function setName(string $name): static
|
public function setName(string $name): static
|
||||||
{
|
{
|
||||||
$this->name = $name;
|
$this->name = $name;
|
||||||
|
@ -47,4 +50,16 @@ class View extends \App\Entity\AbstractEntity
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getUser(): ?User
|
||||||
|
{
|
||||||
|
return $this->user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setUser(?User $user): static
|
||||||
|
{
|
||||||
|
$this->user = $user;
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,12 +12,14 @@ use ApiPlatform\Validator\ValidatorInterface;
|
||||||
use App\Dto\Input\ViewInput;
|
use App\Dto\Input\ViewInput;
|
||||||
use App\Dto\Output\ViewOutput;
|
use App\Dto\Output\ViewOutput;
|
||||||
use App\Repository\ViewRepository;
|
use App\Repository\ViewRepository;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
|
||||||
readonly class ViewProcessor implements ProcessorInterface
|
readonly class ViewProcessor implements ProcessorInterface
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ViewRepository $viewRepository,
|
private ViewRepository $viewRepository,
|
||||||
private ValidatorInterface $validator
|
private ValidatorInterface $validator,
|
||||||
|
private Security $security
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -52,6 +54,7 @@ readonly class ViewProcessor implements ProcessorInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
$view = $data->createOrUpdateEntity($entity);
|
$view = $data->createOrUpdateEntity($entity);
|
||||||
|
$view->setUser($this->security->getUser());
|
||||||
$this->validator->validate($view);
|
$this->validator->validate($view);
|
||||||
$this->viewRepository->save($view);
|
$this->viewRepository->save($view);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue