Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Entity;
  4.  
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Symfony\Component\Serializer\Annotation\Groups;
  9.  
  10. /**
  11. * @ORM\Entity(repositoryClass="App\Repository\PermissionRepository")
  12. */
  13. class Permission
  14. {
  15. /**
  16. * @ORM\Id()
  17. * @ORM\GeneratedValue()
  18. * @ORM\Column(type="integer")
  19. */
  20. private $id;
  21.  
  22. /**
  23. * @ORM\Column(type="string", length=40)
  24. */
  25. private $name;
  26.  
  27. /**
  28. * @ORM\ManyToMany(targetEntity="App\Entity\User", inversedBy="permissionId")
  29. * @Groups({"permission"})
  30. */
  31. private $roleId;
  32.  
  33. public function __construct()
  34. {
  35. $this->roleId = new ArrayCollection();
  36. }
  37.  
  38. public function getId(): ?int
  39. {
  40. return $this->id;
  41. }
  42.  
  43. public function getName(): ?string
  44. {
  45. return $this->name;
  46. }
  47.  
  48. public function setName(string $name): self
  49. {
  50. $this->name = $name;
  51.  
  52. return $this;
  53. }
  54.  
  55. /**
  56. * @return Collection|User[]
  57. */
  58. public function getRoleId(): Collection
  59. {
  60. return $this->roleId;
  61. }
  62.  
  63. public function addRoleId(User $roleId): self
  64. {
  65. if (!$this->roleId->contains($roleId)) {
  66. $this->roleId[] = $roleId;
  67. }
  68.  
  69. return $this;
  70. }
  71.  
  72. public function removeRoleId(User $roleId): self
  73. {
  74. if ($this->roleId->contains($roleId)) {
  75. $this->roleId->removeElement($roleId);
  76. }
  77.  
  78. return $this;
  79. }
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. <?php
  94.  
  95. declare(strict_types=1);
  96.  
  97. namespace App\Entity;
  98.  
  99. use Doctrine\Common\Collections\ArrayCollection;
  100. use Doctrine\Common\Collections\Collection;
  101. use Doctrine\ORM\Mapping as ORM;
  102. use JMS\Serializer\Annotation as Serializer;
  103. use Symfony\Component\Security\Core\User\UserInterface;
  104.  
  105. /**
  106. * @ORM\Table(name="users")
  107. * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
  108. *
  109. * @ORM\HasLifecycleCallbacks()
  110. */
  111. class User implements UserInterface, \Serializable
  112. {
  113. /**
  114. * @ORM\Id()
  115. * @ORM\GeneratedValue()
  116. * @ORM\Column(type="integer")
  117. * @Serializer\Groups({"permission"})
  118. */
  119. private $id;
  120.  
  121. /**
  122. * @ORM\Column(type="string", length=50, unique=true)
  123. * @Serializer\Groups({"permission"})
  124. */
  125. private $username;
  126.  
  127. /**
  128. * @ORM\Column(type="string", length=60)
  129. * @Serializer\Groups({"permission"})
  130. */
  131. private $password;
  132.  
  133. /**
  134. * @ORM\Column(type="string", length=100, unique=true)
  135. * @Serializer\Groups({"permission"})
  136. */
  137. private $email;
  138.  
  139. /**
  140. * @ORM\Column(type="boolean")
  141. * @Serializer\Groups({"permission"})
  142. */
  143. private $isActive;
  144.  
  145. /**
  146. * @ORM\Column(type="datetime")
  147. * @Serializer\Groups({"permission"})
  148. */
  149. private $createdAt;
  150.  
  151. /**
  152. * Field used to encode the password
  153. *
  154. * @var string
  155. */
  156. private $plainPassword;
  157.  
  158. /**
  159. * @Serializer\MaxDepth(1)
  160. * @ORM\OneToOne(targetEntity="App\Entity\Accounts", inversedBy="user", cascade={"persist", "remove"})
  161. * @ORM\JoinColumn(nullable=false)
  162. */
  163. private $account;
  164.  
  165. /**
  166. * @ORM\ManyToMany(targetEntity="App\Entity\Permission", mappedBy="roleId")
  167. * @Serializer\Groups({"permission"})
  168. */
  169. private $permissionId;
  170.  
  171. public function __construct()
  172. {
  173. $this->isActive = true;
  174. $this->permissionId = new ArrayCollection();
  175. }
  176.  
  177. public function setId(int $id): self
  178. {
  179. $this->id = $id;
  180.  
  181. return $this;
  182. }
  183.  
  184.  
  185. public function getId(): ?int
  186. {
  187. return $this->id;
  188. }
  189.  
  190. public function getUsername(): ?string
  191. {
  192. return $this->username;
  193. }
  194.  
  195. public function setUsername(string $username): self
  196. {
  197. $this->username = $username;
  198.  
  199. return $this;
  200. }
  201.  
  202. public function getPassword(): ?string
  203. {
  204. return $this->password;
  205. }
  206.  
  207. public function setPassword(string $password): self
  208. {
  209. $this->password = $password;
  210.  
  211. return $this;
  212. }
  213.  
  214. public function getEmail(): ?string
  215. {
  216. return $this->email;
  217. }
  218.  
  219. public function setEmail(string $email): self
  220. {
  221. $this->email = $email;
  222.  
  223. return $this;
  224. }
  225.  
  226. public function getIsActive(): ?bool
  227. {
  228. return $this->isActive;
  229. }
  230.  
  231. public function setIsActive(bool $isActive): self
  232. {
  233. $this->isActive = $isActive;
  234.  
  235. return $this;
  236. }
  237.  
  238. public function getSalt()
  239. {
  240. return null;
  241. }
  242.  
  243. public function getRoles()
  244. {
  245. return array('ROLE_USER');
  246. }
  247.  
  248. /**
  249. * @ORM\PrePersist()
  250. */
  251. public function setCreatedAt()
  252. {
  253. $this->createdAt = new \DateTime();
  254. }
  255.  
  256. public function getCreatedAt(): ?\DateTime
  257. {
  258. return $this->createdAt;
  259. }
  260.  
  261. public function getPlainPassword(): ?string
  262. {
  263. return $this->plainPassword;
  264. }
  265.  
  266. public function setPlainPassword(?string $plainPassword): self
  267. {
  268. $this->plainPassword = $plainPassword;
  269.  
  270. return $this;
  271. }
  272.  
  273. public function eraseCredentials()
  274. {
  275. $this->plainPassword = null;
  276. }
  277.  
  278. /** @see \Serializable::serialize() */
  279. public function serialize()
  280. {
  281. return serialize(array(
  282. $this->id,
  283. $this->username,
  284. $this->password,
  285. ));
  286. }
  287.  
  288. /** @see \Serializable::unserialize()
  289. * @param $serialized
  290. */
  291. public function unserialize($serialized)
  292. {
  293. list (
  294. $this->id,
  295. $this->username,
  296. $this->password,
  297. ) = unserialize($serialized, array('allowed_classes' => false));
  298. }
  299.  
  300. public function getAccount(): ?Accounts
  301. {
  302. return $this->account;
  303. }
  304.  
  305. public function setAccount(Accounts $account): self
  306. {
  307. $this->account = $account;
  308.  
  309. return $this;
  310. }
  311.  
  312. /**
  313. * @return Collection|Permission[]
  314. */
  315. public function getPermissionId(): Collection
  316. {
  317. return $this->permissionId;
  318. }
  319.  
  320. public function addPermissionId(Permission $permissionId): self
  321. {
  322. if (!$this->permissionId->contains($permissionId)) {
  323. $this->permissionId[] = $permissionId;
  324. $permissionId->addRoleId($this);
  325. }
  326.  
  327. return $this;
  328. }
  329.  
  330. public function removePermissionId(Permission $permissionId): self
  331. {
  332. if ($this->permissionId->contains($permissionId)) {
  333. $this->permissionId->removeElement($permissionId);
  334. $permissionId->removeRoleId($this);
  335. }
  336.  
  337. return $this;
  338. }
  339. }
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350. /**
  351. * @Rest\Get("/tests")
  352. */
  353. public function test(
  354. PermissionRepository $permissionRepository,
  355. UserRepository $userRepository,
  356. EntityManagerInterface $entity
  357. )
  358. {
  359. $serializer = SerializerBuilder::create()->build();
  360.  
  361. $permission = $permissionRepository->find(1);
  362. $user = $userRepository->find(1);
  363.  
  364. return $this->json([
  365. 'success' => $serializer->serialize($user->getPermissionId(), 'json', SerializationContext::create()->setGroups([
  366. 'permission'
  367. ])),
  368. ]);
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement