Guest User

Untitled

a guest
Dec 19th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.23 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppEntity;
  4.  
  5. use DoctrineORMMapping as ORM;
  6. use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
  7. use SymfonyComponentSecurityCoreUserUserInterface;
  8.  
  9. /**
  10. * User
  11. *
  12. * @ORMEntity
  13. * @UniqueEntity(fields="email", message="Email already taken")
  14. * @ORMHasLifecycleCallbacks()
  15. */
  16. class User implements UserInterface
  17. {
  18. use MappingUserTrait;
  19.  
  20. /**
  21. * @param Person $person
  22. */
  23. public function addPeople(Person $person)
  24. {
  25. $this->people->add($person);
  26. $person->setOwner($this);
  27. }
  28.  
  29. /**
  30. * @param Person $person
  31. */
  32. public function removePeople(Person $person)
  33. {
  34. $this->people->removeElement($person);
  35. }
  36. }
  37.  
  38. <?php
  39.  
  40. namespace AppEntityMapping;
  41.  
  42. use AppEntityPerson;
  43. use DoctrineCommonCollectionsArrayCollection;
  44. use DoctrineORMMapping as ORM;
  45. use SymfonyComponentValidatorConstraints as Assert;
  46.  
  47. /**
  48. * User
  49. *
  50. * @ORMTable()
  51. */
  52. trait UserTrait
  53. {
  54. /**
  55. * @var int
  56. *
  57. * @ORMColumn(name="id", type="integer")
  58. * @ORMId
  59. * @ORMGeneratedValue(strategy="AUTO")
  60. */
  61. private $id;
  62.  
  63. /**
  64. * @var string
  65. *
  66. * @ORMColumn(name="hash", type="string")
  67. */
  68. private $hash;
  69.  
  70. /**
  71. * @var bool
  72. *
  73. * @ORMColumn(name="is_active", type="boolean")
  74. */
  75. private $isActive;
  76.  
  77. /**
  78. * @var DateTime
  79. *
  80. * @ORMColumn(name="updated_at", type="datetime")
  81. */
  82. private $updatedAt;
  83.  
  84. /**
  85. * @var DateTime
  86. *
  87. * @ORMColumn(name="created_at", type="datetime")
  88. */
  89. private $createdAt;
  90.  
  91. /**
  92. * @var string
  93. * @ORMColumn(name="email", type="string")
  94. * @AssertNotBlank
  95. * @AssertEmail
  96. */
  97. private $email;
  98.  
  99. /**
  100. * @var DoctrineCommonCollectionsCollection
  101. *
  102. * @ORMOneToMany(targetEntity="AppEntityPerson", mappedBy="owner", cascade={"persist"})
  103. */
  104. private $people;
  105.  
  106. /**
  107. * @var string
  108. * @AssertNotBlank
  109. * @AssertLength(max=4096)
  110. */
  111. private $plainPassword;
  112.  
  113. /**
  114. * Constructor
  115. */
  116. public function __construct()
  117. {
  118. $this->people = new ArrayCollection();
  119. $this->roles = ['ROLE_USER'];
  120. $this->isActive = 1;
  121. }
  122.  
  123. /**
  124. * @return int
  125. */
  126. public function getId(): int
  127. {
  128. return $this->id;
  129. }
  130.  
  131. /**
  132. * @param int $id
  133. */
  134. public function setId(int $id): void
  135. {
  136. $this->id = $id;
  137. }
  138.  
  139. /**
  140. * @return bool
  141. */
  142. public function isActive(): bool
  143. {
  144. return $this->isActive;
  145. }
  146.  
  147. /**
  148. * @param bool $isActive
  149. */
  150. public function setIsActive(bool $isActive): void
  151. {
  152. $this->isActive = $isActive;
  153. }
  154.  
  155. /**
  156. * @return string
  157. */
  158. public function getEmail()
  159. {
  160. return $this->email;
  161. }
  162.  
  163. /**
  164. * @param string $email
  165. */
  166. public function setEmail(string $email): void
  167. {
  168. $this->email = $email;
  169. }
  170.  
  171. /**
  172. * @return DoctrineCommonCollectionsCollection
  173. */
  174. public function getPeople(): DoctrineCommonCollectionsCollection
  175. {
  176. return $this->people;
  177. }
  178.  
  179. /**
  180. * @param DoctrineCommonCollectionsCollection $people
  181. */
  182. public function setPeople(DoctrineCommonCollectionsCollection $people): void
  183. {
  184. $this->people = $people;
  185. }
  186.  
  187. /**
  188. * @return string
  189. */
  190. public function getPlainPassword()
  191. {
  192. return $this->plainPassword;
  193. }
  194.  
  195. /**
  196. * @param string $plainPassword
  197. */
  198. public function setPlainPassword(string $plainPassword): void
  199. {
  200. $this->plainPassword = $plainPassword;
  201. }
  202.  
  203. /**
  204. * Implementation of UserInterface: Get password hash.
  205. * @return string
  206. */
  207. public function getPassword()
  208. {
  209. return $this->hash;
  210. }
  211.  
  212. /**
  213. * Implementation of UserInterface
  214. */
  215. public function eraseCredentials()
  216. {
  217. $this->plainPassword = null;
  218. }
  219.  
  220. /**
  221. * Implementation of UserInterface
  222. */
  223. public function getUsername()
  224. {
  225. return $this->email;
  226. }
  227.  
  228. public function getSalt()
  229. {
  230. return null;
  231. }
  232.  
  233. public function getRoles()
  234. {
  235. return ["User"];
  236. }
  237.  
  238. /**
  239. * @return string
  240. */
  241. public function getHash(): string
  242. {
  243. return $this->hash;
  244. }
  245.  
  246. /**
  247. * @param string $hash
  248. */
  249. public function setHash(string $hash): void
  250. {
  251. $this->hash = $hash;
  252. }
  253.  
  254. /**
  255. * @ORMPrePersist
  256. * @ORMPreUpdate
  257. */
  258. public function updatedTimestamps(): void
  259. {
  260. $dateTimeNow = new DateTime('now');
  261. $this->setUpdatedAt($dateTimeNow);
  262. if ($this->getCreatedAt() === null) {
  263. $this->setCreatedAt($dateTimeNow);
  264. }
  265. }
  266.  
  267. /**
  268. * @return DateTime
  269. */
  270. public function getUpdatedAt(): DateTime
  271. {
  272. return $this->updatedAt;
  273. }
  274.  
  275. /**
  276. * @param DateTime $updatedAt
  277. */
  278. public function setUpdatedAt(DateTime $updatedAt): void
  279. {
  280. $this->updatedAt = $updatedAt;
  281. }
  282.  
  283. /**
  284. * @return DateTime
  285. */
  286. public function getCreatedAt()
  287. {
  288. return $this->createdAt;
  289. }
  290.  
  291. /**
  292. * @param DateTime $createdAt
  293. */
  294. public function setCreatedAt(DateTime $createdAt): void
  295. {
  296. $this->createdAt = $createdAt;
  297. }
  298.  
  299. }
  300.  
  301. <?php
  302.  
  303. namespace AppEntity;
  304.  
  305. use DoctrineORMMapping as ORM;
  306.  
  307. /**
  308. * Person
  309. *
  310. * @ORMEntity
  311. */
  312. class Person
  313. {
  314. use MappingPersonTrait;
  315. }
  316.  
  317. <?php
  318.  
  319. namespace AppEntityMapping;
  320.  
  321. use DoctrineORMMapping as ORM;
  322.  
  323. use SymfonyComponentValidatorConstraints as Assert;
  324.  
  325.  
  326. /**
  327. * Person
  328. *
  329. * @ORMTable()
  330. */
  331. trait PersonTrait
  332. {
  333. /**
  334. * @var int
  335. *
  336. * @ORMColumn(name="id", type="integer")
  337. * @ORMId
  338. * @ORMGeneratedValue(strategy="AUTO")
  339. */
  340. private $id;
  341.  
  342. /**
  343. * @var string
  344. *
  345. * @ORMColumn(name="family_name", type="string")
  346. * @AssertNotBlank
  347. */
  348. private $familyName;
  349.  
  350. /**
  351. * @var string
  352. *
  353. * @ORMColumn(name="given_name", type="string")
  354. * @AssertNotBlank
  355. */
  356. private $givenName;
  357.  
  358. /**
  359. * @var string
  360. *
  361. * @ORMColumn(name="title", type="string")
  362. */
  363. private $title;
  364.  
  365. /**
  366. * @var DateTime
  367. *
  368. * @ORMColumn(name="birth_date", type="datetime")
  369. */
  370. private $birthDate;
  371.  
  372. /**
  373. * @var string
  374. *
  375. * @ORMColumn(name="salutation", type="string")
  376. */
  377. private $salutation;
  378.  
  379. /**
  380. * @var string
  381. *
  382. * @ORMColumn(name="gender", type="string")
  383. */
  384. private $gender;
  385.  
  386. /**
  387. * @var AppEntityUser
  388. *
  389. * @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="people")
  390. * @ORMJoinColumns({
  391. * @ORMJoinColumn(name="owner_id", referencedColumnName="id")
  392. * })
  393. */
  394. private $owner;
  395.  
  396. /**
  397. * @return string
  398. */
  399. public function getFamilyName()
  400. {
  401. return $this->familyName;
  402. }
  403.  
  404. /**
  405. * @param string $familyName
  406. */
  407. public function setFamilyName($familyName)
  408. {
  409. var_dump($familyName);
  410. $this->familyName = $familyName;
  411. }
  412.  
  413. /**
  414. * @return string
  415. */
  416. public function getGivenName()
  417. {
  418. return $this->givenName;
  419. }
  420.  
  421. /**
  422. * @param string $givenName
  423. */
  424. public function setGivenName($givenName)
  425. {
  426. $this->givenName = $givenName;
  427. }
  428.  
  429. /**
  430. * @return string
  431. */
  432. public function getTitle(): string
  433. {
  434. return $this->title;
  435. }
  436.  
  437. /**
  438. * @param string $title
  439. */
  440. public function setTitle(string $title): void
  441. {
  442. $this->title = $title;
  443. }
  444.  
  445. /**
  446. * @return DateTime
  447. */
  448. public function getBirthDate(): DateTime
  449. {
  450. return $this->birthDate;
  451. }
  452.  
  453. /**
  454. * @param DateTime $birthDate
  455. */
  456. public function setBirthDate(DateTime $birthDate): void
  457. {
  458. $this->birthDate = $birthDate;
  459. }
  460.  
  461. /**
  462. * @return string
  463. */
  464. public function getSalutation(): string
  465. {
  466. return $this->salutation;
  467. }
  468.  
  469. /**
  470. * @param string $salutation
  471. */
  472. public function setSalutation(string $salutation): void
  473. {
  474. $this->salutation = $salutation;
  475. }
  476.  
  477. /**
  478. * @return string
  479. */
  480. public function getGender(): string
  481. {
  482. return $this->gender;
  483. }
  484.  
  485. /**
  486. * @param string $gender
  487. */
  488. public function setGender(string $gender): void
  489. {
  490. $this->gender = $gender;
  491. }
  492.  
  493. /**
  494. * @return AppEntityUser
  495. */
  496. public function getOwner(): AppEntityUser
  497. {
  498. return $this->owner;
  499. }
  500.  
  501. /**
  502. * @param AppEntityUser $owner
  503. */
  504. public function setOwner(AppEntityUser $owner): void
  505. {
  506. $this->owner = $owner;
  507. }
  508. }
  509.  
  510. <?php
  511.  
  512. namespace AppForm;
  513.  
  514.  
  515. use AppEntityUser;
  516. use SymfonyComponentFormAbstractType;
  517. use SymfonyComponentFormExtensionCoreTypeCollectionType;
  518. use SymfonyComponentFormExtensionCoreTypeEmailType;
  519. use SymfonyComponentFormExtensionCoreTypePasswordType;
  520. use SymfonyComponentFormExtensionCoreTypeRepeatedType;
  521. use SymfonyComponentFormFormBuilderInterface;
  522. use SymfonyComponentOptionsResolverOptionsResolver;
  523.  
  524. class UserType extends AbstractType
  525. {
  526.  
  527.  
  528. public function buildForm(FormBuilderInterface $builder, array $options)
  529. {
  530. $builder
  531. ->add('email', EmailType::class)
  532. ->add('plainPassword', PasswordType::class)
  533. ->add(
  534. "people",
  535. CollectionType::class,
  536. [
  537. 'entry_type' => PersonType::class,
  538. 'allow_add' => true,
  539. 'by_reference' => false,
  540. ]
  541. );
  542. }
  543.  
  544. public function configureOptions(OptionsResolver $resolver)
  545. {
  546. $resolver->setDefaults(
  547. [
  548. 'data_class' => User::class,
  549. 'csrf_protection' => false
  550. ]
  551. );
  552. }
  553.  
  554. }
  555.  
  556. <?php
  557.  
  558. namespace AppForm;
  559.  
  560.  
  561. use AppEntityPerson;
  562. use SymfonyComponentFormAbstractType;
  563. use SymfonyComponentFormExtensionCoreTypeTextType;
  564. use SymfonyComponentFormFormBuilderInterface;
  565. use SymfonyComponentOptionsResolverOptionsResolver;
  566.  
  567. class PersonType extends AbstractType
  568. {
  569.  
  570.  
  571. public function buildForm(FormBuilderInterface $builder, array $options)
  572. {
  573. $builder
  574. ->add('familyName', TextType::class)
  575. ->add('givenName', TextType::class);
  576. }
  577.  
  578. public function configureOptions(OptionsResolver $resolver)
  579. {
  580. $resolver->setDefaults(
  581. [
  582. 'data_class' => Person::class,
  583. 'csrf_protection' => false,
  584. ]
  585. );
  586. }
  587.  
  588. }
  589.  
  590. /**
  591. * @Route("/register")
  592. * @param Request $request
  593. * @param UserPasswordEncoderInterface $passwordEncoder
  594. */
  595. public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder)
  596. {
  597. $user = new User();
  598. $form = $this->createForm(UserType::class, $user);
  599. $form->submit($request->request->all());
  600.  
  601. if ($form->isSubmitted() && $form->isValid()) {
  602. $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
  603. $user->setHash($password);
  604.  
  605. $entityManager = $this->getDoctrine()->getManager();
  606. $entityManager->persist($user);
  607. $entityManager->flush();
  608. return new Response("ok", 300);
  609. }
  610. return new Response("not ok", 500);
  611.  
  612. }
Add Comment
Please, Sign In to add comment