Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Entity;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\Common\Collections\Collection;
- use Doctrine\ORM\Mapping as ORM;
- use Symfony\Component\Serializer\Annotation\Groups;
- /**
- * @ORM\Entity(repositoryClass="App\Repository\PermissionRepository")
- */
- class Permission
- {
- /**
- * @ORM\Id()
- * @ORM\GeneratedValue()
- * @ORM\Column(type="integer")
- */
- private $id;
- /**
- * @ORM\Column(type="string", length=40)
- */
- private $name;
- /**
- * @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="permissionId")
- * @Groups({"permission"})
- */
- private $roleId;
- public function __construct()
- {
- $this->roleId = new ArrayCollection();
- }
- public function getId(): ?int
- {
- return $this->id;
- }
- public function getName(): ?string
- {
- return $this->name;
- }
- public function setName(string $name): self
- {
- $this->name = $name;
- return $this;
- }
- /**
- * @return Collection|User[]
- */
- public function getRoleId(): Collection
- {
- return $this->roleId;
- }
- public function addRoleId(User $roleId): self
- {
- if (!$this->roleId->contains($roleId)) {
- $this->roleId[] = $roleId;
- }
- return $this;
- }
- public function removeRoleId(User $roleId): self
- {
- if ($this->roleId->contains($roleId)) {
- $this->roleId->removeElement($roleId);
- }
- return $this;
- }
- }
- <?php
- declare(strict_types=1);
- namespace App\Entity;
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\Common\Collections\Collection;
- use Doctrine\ORM\Mapping as ORM;
- use JMS\Serializer\Annotation as Serializer;
- use Symfony\Component\Security\Core\User\UserInterface;
- /**
- * @ORM\Table(name="users")
- * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
- *
- * @ORM\HasLifecycleCallbacks()
- */
- class User implements UserInterface, \Serializable
- {
- /**
- * @ORM\Id()
- * @ORM\GeneratedValue()
- * @ORM\Column(type="integer")
- * @Serializer\Groups({"permission"})
- */
- private $id;
- /**
- * @ORM\Column(type="string", length=50, unique=true)
- * @Serializer\Groups({"permission"})
- */
- private $username;
- /**
- * @ORM\Column(type="string", length=60)
- * @Serializer\Groups({"permission"})
- */
- private $password;
- /**
- * @ORM\Column(type="string", length=100, unique=true)
- * @Serializer\Groups({"permission"})
- */
- private $email;
- /**
- * @ORM\Column(type="boolean")
- * @Serializer\Groups({"permission"})
- */
- private $isActive;
- /**
- * @ORM\Column(type="datetime")
- * @Serializer\Groups({"permission"})
- */
- private $createdAt;
- /**
- * Field used to encode the password
- *
- * @var string
- */
- private $plainPassword;
- /**
- * @Serializer\MaxDepth(1)
- * @ORM\OneToOne(targetEntity="App\Entity\Accounts", inversedBy="user", cascade={"persist", "remove"})
- * @ORM\JoinColumn(nullable=false)
- */
- private $account;
- /**
- * @ORM\ManyToMany(targetEntity="App\Entity\Permission", mappedBy="roleId")
- * @Serializer\Groups({"permission"})
- */
- private $permissionId;
- public function __construct()
- {
- $this->isActive = true;
- $this->permissionId = new ArrayCollection();
- }
- public function setId(int $id): self
- {
- $this->id = $id;
- return $this;
- }
- public function getId(): ?int
- {
- return $this->id;
- }
- public function getUsername(): ?string
- {
- return $this->username;
- }
- public function setUsername(string $username): self
- {
- $this->username = $username;
- return $this;
- }
- public function getPassword(): ?string
- {
- return $this->password;
- }
- public function setPassword(string $password): self
- {
- $this->password = $password;
- return $this;
- }
- public function getEmail(): ?string
- {
- return $this->email;
- }
- public function setEmail(string $email): self
- {
- $this->email = $email;
- return $this;
- }
- public function getIsActive(): ?bool
- {
- return $this->isActive;
- }
- public function setIsActive(bool $isActive): self
- {
- $this->isActive = $isActive;
- return $this;
- }
- public function getSalt()
- {
- return null;
- }
- public function getRoles()
- {
- return array('ROLE_USER');
- }
- /**
- * @ORM\PrePersist()
- */
- public function setCreatedAt()
- {
- $this->createdAt = new \DateTime();
- }
- public function getCreatedAt(): ?\DateTime
- {
- return $this->createdAt;
- }
- public function getPlainPassword(): ?string
- {
- return $this->plainPassword;
- }
- public function setPlainPassword(?string $plainPassword): self
- {
- $this->plainPassword = $plainPassword;
- return $this;
- }
- public function eraseCredentials()
- {
- $this->plainPassword = null;
- }
- /** @see \Serializable::serialize() */
- public function serialize()
- {
- return serialize(array(
- $this->id,
- $this->username,
- $this->password,
- ));
- }
- /** @see \Serializable::unserialize()
- * @param $serialized
- */
- public function unserialize($serialized)
- {
- list (
- $this->id,
- $this->username,
- $this->password,
- ) = unserialize($serialized, array('allowed_classes' => false));
- }
- public function getAccount(): ?Accounts
- {
- return $this->account;
- }
- public function setAccount(Accounts $account): self
- {
- $this->account = $account;
- return $this;
- }
- /**
- * @return Collection|Permission[]
- */
- public function getPermissionId(): Collection
- {
- return $this->permissionId;
- }
- public function addPermissionId(Permission $permissionId): self
- {
- if (!$this->permissionId->contains($permissionId)) {
- $this->permissionId[] = $permissionId;
- $permissionId->addRoleId($this);
- }
- return $this;
- }
- public function removePermissionId(Permission $permissionId): self
- {
- if ($this->permissionId->contains($permissionId)) {
- $this->permissionId->removeElement($permissionId);
- $permissionId->removeRoleId($this);
- }
- return $this;
- }
- }
- /**
- * @Rest\Get("/tests")
- */
- public function test(
- PermissionRepository $permissionRepository,
- UserRepository $userRepository,
- EntityManagerInterface $entity
- )
- {
- $serializer = SerializerBuilder::create()->build();
- $permission = $permissionRepository->find(1);
- $user = $userRepository->find(1);
- return $this->json([
- 'success' => $serializer->serialize($user->getPermissionId(), 'json', SerializationContext::create()->setGroups([
- 'permission'
- ])),
- ]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement