Using Api Platform, automatically assign user to Object (OneToMany)
This should be possible using an EventListener
: https://api-platform.com/docs/core/events
With these you can hook into the internal process of ApiPlatform process without a new controller. Perfect fit for your usecase.
An implementation could look like this:
<?php
// api/src/EventSubscriber/AddOwnerToArticleSubscriber.php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Article;
use App\Entity\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
final class AddOwnerToArticleSubscriber implements EventSubscriberInterface
{
/**
* @var TokenStorageInterface
*/
private $tokenStorage;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['attachOwner', EventPriorities::PRE_WRITE],
];
}
public function attachOwner(GetResponseForControllerResultEvent $event)
{
$article = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$article instanceof Article || Request::METHOD_POST !== $method) {
// Only handle Article entities (Event is called on any Api entity)
return;
}
// maybe these extra null checks are not even needed
$token = $this->tokenStorage->getToken();
if (!$token) {
return;
}
$owner = $token->getUser();
if (!$owner instanceof User) {
return;
}
// Attach the user to the not yet persisted Article
$article->setUser($owner);
}
}
You can create an entity named Base and, you can have some property like "createdBy", "modifiedBy", "createdAt", "modifiedAt", "status" in this class.
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\MappedSuperclass()
*/
class Base implements PublishedInfoEntityInterface
{
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $modifiedAt;
/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(nullable=true)
*/
private $createdBy;
/**
* @ORM\ManyToOne(targetEntity="User")
* @ORM\JoinColumn(nullable=true)
*/
private $modifiedBy;
/**
* @ORM\Column(type="integer", nullable=true, length=2)
*/
private $status;
public function getCreatedAt()
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): PublishedInfoEntityInterface
{
$this->createdAt = $createdAt;
return $this;
}
public function getModifiedAt()
{
return $this->modifiedAt;
}
public function setModifiedAt(\DateTimeInterface $modifiedAt): PublishedInfoEntityInterface
{
$this->modifiedAt = $modifiedAt;
return $this;
}
/**
* @return User
*/
public function getCreatedBy()
{
return $this->createdBy;
}
/**
* @param User $createdBy
* @return Base
*/
public function setCreatedBy($createdBy): self
{
$this->createdBy = $createdBy;
return $this;
}
/**
* @return User
*/
public function getModifiedBy()
{
return $this->modifiedBy;
}
/**
* @param User $modifiedBy
* @return Base
*/
public function setModifiedBy($modifiedBy): self
{
$this->modifiedBy = $modifiedBy;
return $this;
}
/**
* @return int
*/
public function getStatus()
{
return $this->status;
}
/**
* @param integer $status
*/
public function setStatus($status): void
{
$this->status = $status;
return $this;
}
}
Create a subscriber class for set automatic createdBy and modifiedBy like this code
<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Base;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
class AuthoredEntitySubscriber implements EventSubscriberInterface
{
private $entityManager;
/**
* @var Security
*/
private $security;
public function __construct(EntityManagerInterface $entityManager,Security $security)
{
$this->entityManager = $entityManager;
$this->security = $security;
}
public static function getSubscribedEvents()
{
return [KernelEvents::VIEW => ['setAuthor', EventPriorities::PRE_WRITE]];
}
public function setAuthor(ViewEvent $event)
{
$entity = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
$role = $this->security->getToken()->getRoleNames();
if (!$entity instanceof Base || !in_array($method, [Request::METHOD_POST, Request::METHOD_PUT]) || !$role) {
return;
}
$entity->setModifiedBy($this->security->getUser());
if (Request::METHOD_POST === $method) {
$entity->setCreatedBy($this->security->getUser());
}
}
}
If you want to add automatic createdAt and modifiedAt you must create a interface class named PublishedInfoEntityInterface for createdAt and modifiedAt and this class write this code:
<?php
namespace App\Entity;
interface PublishedInfoEntityInterface
{
public function setCreatedAt(\DateTimeInterface $dateTime): PublishedInfoEntityInterface;
public function setModifiedAt(\DateTimeInterface $dateTime): PublishedInfoEntityInterface;
}
and create an subscriber for automatic fill createdAt and modifiedAt like this
<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\PublishedInfoEntityInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class PublishedInfoEntitySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [KernelEvents::VIEW => ['setDataTime', EventPriorities::PRE_WRITE]];
}
public function setDataTime(ViewEvent $event)
{
$entity = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$entity instanceof PublishedInfoEntityInterface || !in_array($method, [Request::METHOD_POST, Request::METHOD_PUT])) {
return;
}
$entity->setCreatedAt(new \DateTime());
if (Request::METHOD_POST === $method){
$entity->setModifiedAt(new \DateTime());
}
}
}
Finally each class you want to have those property you just extend this class like this
class User extends Base implements UserInterface
And create a migration,