Skip to content

Instantly share code, notes, and snippets.

@aelfannir
Created March 22, 2022 15:14
Show Gist options
  • Save aelfannir/768f4441a10bb5e5ebb9adf2d87e59e3 to your computer and use it in GitHub Desktop.
Save aelfannir/768f4441a10bb5e5ebb9adf2d87e59e3 to your computer and use it in GitHub Desktop.
User, Role, Route
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Entity\Classes\Group;
use App\Entity\Traits\HasId;
use App\Entity\Traits\HasSortIndex;
use App\Service\Validator;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
use Gedmo\Mapping\Annotation as Gedmo;
#[UniqueEntity(fields: 'name', message: Validator::UNIQUE_ENTITY_MESSAGE)]
#[ORM\Entity]
#[ApiResource]
#[ApiFilter(SearchFilter::class, properties: ['name' => 'partial'])]
class Role
{
use HasId;
use HasSortIndex;
#[ORM\Column(unique: true)]
// #[Assert\Choice(choices: RoleEnum::class, message: Validator::CHOICE_MESSAGE)]
private string $roleKey = 'ROLE_CUSTOM';
#[Serializer\Groups([Group::AUTH_ME])]
#[Assert\NotBlank(message: Validator::NOT_BLANK_MESSAGE)]
#[ORM\Column(unique: true)]
#[Gedmo\Translatable]
private ?string $name;
#[Serializer\MaxDepth(2)]
#[ORM\ManyToMany(targetEntity: Route::class, inversedBy: 'roles', cascade: ['persist'])]
private Collection $routes;
#[Serializer\Ignore]
#[Serializer\MaxDepth(1)]
#[ORM\OneToMany(mappedBy: 'role', targetEntity: User::class, cascade: ['persist'])]
private Collection $users;
public function __construct()
{
$this->routes = new ArrayCollection;
$this->users = new ArrayCollection;
}
public function setRoleKey(string $roleKey): self
{
$this->roleKey = $roleKey;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getRoleKey(): ?string
{
return $this->roleKey;
}
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
$user->setRole($this);
}
return $this;
}
public function removeUser(User $user): self
{
if ($this->users->removeElement($user)) {
// set the owning side to null (unless already changed)
if ($user->getRole() === $this) {
$user->setRole(null);
}
}
return $this;
}
public function getRoutes(): Collection
{
return $this->routes;
}
public function addRoute(Route $route): self
{
if (!$this->routes->contains($route)) {
$this->routes[] = $route;
}
return $this;
}
public function addRoutes($routes): self
{
$this->getRoutes()->clear();
foreach ($routes as $route) {
$this->addRoute($route);
}
return $this;
}
public function removeRoute(Route $route): self
{
$this->routes->removeElement($route);
return $this;
}
}
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Entity\Traits\HasId;
use App\Entity\Traits\HasLocale;
use App\Entity\Traits\HasSortIndex;
use App\Entity\Traits\Tree\HasMaterializedPathTree;
use App\Service\Validator;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Entity\Translation;
use Gedmo\Tree\Entity\Repository\MaterializedPathRepository;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: MaterializedPathRepository::class)]
#[Gedmo\Tree(type: 'materializedPath')]
#[Gedmo\TranslationEntity(class: Translation::class)]
#[ApiFilter(SearchFilter::class, strategy: 'partial', properties: ['name' => 'partial', 'views'=>'partial', 'parent'=>'exact'])]
#[ApiFilter(ExistsFilter::class, properties: ['parent'])]
#[ApiResource]
class Route
{
use HasId;
use HasSortIndex;
use HasMaterializedPathTree;
use HasLocale;
#[Assert\NotBlank(message: Validator::NOT_BLANK_MESSAGE)]
#[ORM\Column(unique: true)]
private string $routeKey;
#[Assert\NotBlank(message: Validator::NOT_BLANK_MESSAGE)]
#[ORM\Column]
#[Gedmo\Translatable]
private string $title;
#[ORM\Column(nullable: true)]
private ?string $icon;
#[ORM\Column(type: 'array')]
private array $views = [];
#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'children')]
#[Gedmo\TreeParent]
#[Gedmo\SortableGroup]
private ?self $parent;
#[ORM\OneToMany(mappedBy: 'parent', targetEntity: self::class)]
#[ORM\OrderBy(['sortIndex' => 'ASC'])]
private Collection $children;
#[Serializer\Ignore]
#[Serializer\MaxDepth(1)]
#[ORM\ManyToMany(targetEntity: Role::class, mappedBy: 'routes')]
private Collection $roles;
public function __construct()
{
$this->roles = new ArrayCollection;
$this->children = new ArrayCollection;
}
#[Serializer\SerializedName('isModule')]
public function isModule(): bool
{
return $this->sortIndex === 0;
}
public function getRouteKey(): ?string
{
return $this->routeKey;
}
public function setRouteKey(string $routeKey): self
{
$this->routeKey = $routeKey;
return $this;
}
public function getRoles(): Collection
{
return $this->roles;
}
public function addRole(Role $role): self
{
if (!$this->roles->contains($role)) {
$this->roles[] = $role;
$role->addRoute($this);
}
return $this;
}
public function removeRole(Role $role): self
{
if ($this->roles->removeElement($role)) {
$role->removeRoute($this);
}
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getIcon(): ?string
{
return $this->icon;
}
public function setIcon(?string $icon): self
{
$this->icon = $icon;
return $this;
}
public function getViews(): ?array
{
return $this->views;
}
public function setViews(array $views): self
{
$this->views = $views;
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(Route $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(Route $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
}
<?php
declare(strict_types=1);
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use App\Entity\Classes\Group;
use App\Entity\Traits\HasId;
use App\Entity\Traits\Timestamp\HasTimestamps;
use App\Entity\Traits\UserEntity;
use App\Service\Validator;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Serializer\Annotation as Serializer;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[Vich\Uploadable]
#[ORM\Entity]
#[ORM\Table(name: '`user`')]
#[ORM\UniqueConstraint(name: 'user_unique', columns: ['username'])]
#[UniqueEntity(fields: 'username', message: Validator::UNIQUE_ENTITY_MESSAGE)]
#[ApiResource(
collectionOperations: [
'get',
'post' => [
'input_formats' => [
'multipart' => ['multipart/form-data']
]
]
]
)]
#[ApiFilter(SearchFilter::class, properties: ['firstName' => 'partial', 'lastName' => 'partial'])]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
use HasId;
// use FileEntity;
use UserEntity;
use HasTimestamps;
#[ApiProperty(iri: 'http://schema.org/contentUrl')]
public ?string $contentUrl = null;
#[Vich\UploadableField(mapping: 'default', fileNameProperty: 'fileName', size: 'fileSize')]
public ?File $file = null;
#[ORM\Column(nullable: true)]
public ?string $filePath = null;
#[Serializer\Groups([Group::AUTH_ME])]
#[ORM\ManyToOne(targetEntity: Role::class, cascade: ['persist'], inversedBy: 'users')]
#[ORM\JoinColumn(onDelete: 'SET NULL')]
#[Serializer\MaxDepth(2)]
private ?Role $role;
#[Serializer\Ignore]
#[Serializer\MaxDepth(1)]
#[ORM\ManyToMany(targetEntity: Team::class, mappedBy: 'users')]
private Collection $teams;
#[ORM\OneToMany(mappedBy: 'user', targetEntity: WorkOrderAssign::class)]
private Collection $workOrderAssignees;
public function __construct()
{
$this->teams = new ArrayCollection;
$this->workOrderAssignees = new ArrayCollection;
}
#[Serializer\Groups([Group::AUTH_ME])]
#[Serializer\SerializedName('roleKeys')]
public function getRoles(): array
{
$roleKey = $this->getRole()?->getRoleKey();
return $roleKey ? [$roleKey] : [];
}
public function getUserIdentifier(): string
{
return $this->getUsername();
}
public function getRole(): ?Role
{
return $this->role;
}
public function setRole(?Role $role): self
{
$this->role = $role;
return $this;
}
public function getTeams(): Collection
{
return $this->teams;
}
public function addTeam(Team $team): self
{
if (!$this->teams->contains($team)) {
$this->teams[] = $team;
$team->addUser($this);
}
return $this;
}
public function removeTeam(Team $team): self
{
if ($this->teams->removeElement($team)) {
$team->removeUser($this);
}
return $this;
}
public function getWorkOrderAssignees(): Collection
{
return $this->workOrderAssignees;
}
public function addWorkOrderAssignee(WorkOrderAssign $workOrderAssignee): self
{
if (!$this->workOrderAssignees->contains($workOrderAssignee)) {
$this->workOrderAssignees[] = $workOrderAssignee;
$workOrderAssignee->setUser($this);
}
return $this;
}
public function removeWorkOrderAssignee(WorkOrderAssign $workOrderAssignee): self
{
if ($this->workOrderAssignees->removeElement($workOrderAssignee)) {
// set the owning side to null (unless already changed)
if ($workOrderAssignee->getUser() === $this) {
$workOrderAssignee->setUser(null);
}
}
return $this;
}
public function getFilePath(): ?string
{
return $this->filePath;
}
public function setFilePath(?string $filePath): self
{
$this->filePath = $filePath;
return $this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment