vendor/roothirsch/core-bundle/Entity/User.php line 52

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\CoreBundle\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  6. use Roothirsch\CoreBundle\Entity\Traits\TimetrackedTrait;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Roothirsch\CoreBundle\API\Filter\OrSearchFilter;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11. use Symfony\Component\Serializer\Annotation\Groups;
  12. use Symfony\Component\Serializer\Annotation\MaxDepth;
  13. use Symfony\Component\Validator\Constraints as Assert;
  14. /**
  15. * @ORM\Entity(repositoryClass="Roothirsch\CoreBundle\Repository\UserRepository")
  16. * @ORM\Table(name="user")
  17. * @ApiResource(
  18. * collectionOperations={
  19. * "get"={
  20. * "normalization_context"={"groups"={"list"}}
  21. * },
  22. * "post"
  23. * },
  24. * itemOperations={
  25. * "register"={"method"="PUT", "path"="/users/{id}/register", "requirements"={"id"=".+"}},
  26. * "reset-password"={"method"="PUT", "path"="/users/{id}/reset-password", "requirements"={"id"=".+"}},
  27. * "change-password"={"method"="PUT", "path"="/users/{id}/change-password", "requirements"={"id"=".+"}},
  28. * "get"={"method"="GET", "path"="/users/{id}"},
  29. * "current"={"method"="GET", "path"="/users/current", "normalization_context"={"groups"={"list"}}},
  30. * "put",
  31. * "delete"
  32. * },
  33. * attributes={
  34. * "normalization_context"={"groups"={"read"}, "enable_max_depth"=true},
  35. * "denormalization_context"={"groups"={"write"}},
  36. * "order"={"lastName": "ASC"}
  37. * }
  38. * )
  39. * @ApiResource
  40. * @ApiFilter(BooleanFilter::class, properties={"active"})
  41. * @ApiFilter(OrSearchFilter::class, properties={
  42. * "username": "partial",
  43. * "email": "partial",
  44. * "firstName": "partial",
  45. * "lastName": "partial",
  46. * "company.name": "partial"
  47. * })
  48. */
  49. class User implements UserInterface
  50. {
  51. use TimetrackedTrait;
  52. /**
  53. * @ORM\Id
  54. * @ORM\GeneratedValue
  55. * @ORM\Column(type="integer")
  56. * @Groups({"list", "read"})
  57. */
  58. private $id;
  59. /**
  60. * @ORM\Column(type="string", unique=true)
  61. * @Groups({"list", "read", "write", "company_read", "js_minimal_read"})
  62. */
  63. private $username;
  64. /**
  65. * @ORM\Column(type="string", unique=true)
  66. * @Assert\NotBlank()
  67. * @Assert\Email(message="email")
  68. * @Groups({"list", "read", "write", "company_read", "js_minimal_read"})
  69. */
  70. private $email;
  71. /**
  72. * @ORM\Column(type="string", nullable=true)
  73. * @Groups({"write"})
  74. */
  75. private $password;
  76. /**
  77. * @Groups({"write"})
  78. */
  79. private $plaintextPassword;
  80. /**
  81. * @ORM\Column(type="string", nullable=true)
  82. * @Assert\NotBlank(message="blank")
  83. * @Groups({"list", "read", "write", "company_read", "js_minimal_read"})
  84. */
  85. private $firstName;
  86. /**
  87. * @ORM\Column(type="string", nullable=true)
  88. * @Assert\NotBlank(message="blank")
  89. * @Groups({"list", "read", "write", "company_read", "js_minimal_read"})
  90. */
  91. private $lastName;
  92. /**
  93. * @ORM\Column(type="string", unique=true, nullable=true)
  94. */
  95. private $registrationToken;
  96. /**
  97. * Random token to verify the e-mail and password-reset.
  98. *
  99. * @var string
  100. * @ORM\Column(type="string", unique=true, nullable=true)
  101. */
  102. protected $securityToken;
  103. /**
  104. * @var bool
  105. * @ORM\Column(type="boolean")
  106. * @Groups({"list", "read", "write", "company_read"})
  107. */
  108. private $active = true;
  109. /**
  110. * @var bool
  111. * @ORM\Column(type="boolean")
  112. * @Groups({"list", "read", "write"})
  113. */
  114. private $admin = true;
  115. /**
  116. * @var string
  117. * @Orm\Column(type="string", nullable=true)
  118. * @Groups({"list", "read", "write", "company_read"})
  119. */
  120. private $phone;
  121. /**
  122. * @var string
  123. * @Orm\Column(type="string", nullable=true)
  124. * @Groups({"list", "read", "write", "company_read"})
  125. */
  126. private $fax;
  127. /**
  128. * @var string
  129. * @Orm\Column(type="string", nullable=true)
  130. * @Groups({"list", "read", "write", "company_read"})
  131. */
  132. private $mobile;
  133. /**
  134. * @ORM\ManyToOne(targetEntity="Company", inversedBy="employees", cascade={"persist"}, fetch="EAGER")
  135. * @ORM\JoinColumn(name="employee_of", referencedColumnName="id", onDelete="SET NULL")
  136. * @Groups({"list", "read", "write"})
  137. * @MaxDepth(1)
  138. */
  139. private $company;
  140. /**
  141. * @var ContactValidation
  142. * @ORM\OneToOne(targetEntity="ContactValidation",cascade={"persist","remove"}, mappedBy="user")
  143. * @Groups({"read", "write"})
  144. */
  145. private $contactValidation;
  146. /**
  147. * @ORM\ManyToOne(targetEntity="ContactPerson", inversedBy="users", cascade={"persist"}, fetch="EAGER")
  148. * @ORM\JoinColumn(name="contact_through", referencedColumnName="id")
  149. * @Groups({"read", "write", "list"})
  150. * @MaxDepth(1)
  151. */
  152. private $contact;
  153. /**
  154. * @Orm\Column(type="string", nullable=true)
  155. * @Groups({"list", "read", "write"})
  156. */
  157. private $erpReference;
  158. /**
  159. * @ORM\Column(type="integer", nullable=true)
  160. */
  161. private $newsReadOn;
  162. /**
  163. * @var array
  164. * @Groups({"read"})
  165. */
  166. private $features = [];
  167. /**
  168. * @ORM\ManyToMany(targetEntity=Group::class, inversedBy="users")
  169. */
  170. private $groups;
  171. /**
  172. * @Groups({"list", "read", "write", "js_minimal_read"})
  173. */
  174. private $roles = [];
  175. public function __toString()
  176. {
  177. return $this->getFullName();
  178. }
  179. public function getFullName()
  180. {
  181. return $this->getFirstName() . ' ' . $this->getLastName();
  182. }
  183. public function getGravatar()
  184. {
  185. return 'https://www.gravatar.com/avatar/' . md5(strtolower(trim($this->getEmail())));
  186. }
  187. /**
  188. * @return mixed
  189. */
  190. public function getId()
  191. {
  192. return $this->id;
  193. }
  194. /**
  195. * @param mixed $id
  196. */
  197. public function setId($id)
  198. {
  199. $this->id = $id;
  200. }
  201. /**
  202. * @return string
  203. */
  204. public function getFirstName()
  205. {
  206. return $this->firstName;
  207. }
  208. /**
  209. * @param string $firstName
  210. */
  211. public function setFirstName($firstName)
  212. {
  213. $this->firstName = $firstName;
  214. }
  215. /**
  216. * @return string
  217. */
  218. public function getLastName()
  219. {
  220. return $this->lastName;
  221. }
  222. /**
  223. * @param string $lastName
  224. */
  225. public function setLastName($lastName)
  226. {
  227. $this->lastName = $lastName;
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function getUsername()
  233. {
  234. return $this->username;
  235. }
  236. /**
  237. * {@inheritdoc}
  238. */
  239. public function getPassword()
  240. {
  241. return $this->password;
  242. }
  243. /**
  244. * {@inheritdoc}
  245. */
  246. public function getRegistrationToken()
  247. {
  248. return $this->registrationToken;
  249. }
  250. /**
  251. * {@inheritdoc}
  252. */
  253. public function getRoles()
  254. {
  255. $roles = [];
  256. foreach ($this->groups as $group) {
  257. $roles[] = $group->getName();
  258. }
  259. if ($this->company instanceof Company) {
  260. foreach ($this->company->getGroups() as $group) {
  261. $roles[] = $group->getName();
  262. }
  263. }
  264. // guarantees that a user always has at least one role for security
  265. if (empty($roles) || !array_search('ROLE_USER', $roles)) {
  266. $roles[] = 'ROLE_USER';
  267. }
  268. return $roles;
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function getSalt()
  274. {
  275. }
  276. /**
  277. * {@inheritdoc}
  278. */
  279. public function eraseCredentials()
  280. {
  281. }
  282. /**
  283. * @return mixed
  284. */
  285. public function getEmail()
  286. {
  287. return $this->email;
  288. }
  289. /**
  290. * @param mixed $email
  291. */
  292. public function setEmail($email)
  293. {
  294. $this->email = $email;
  295. $this->username = $email;
  296. }
  297. /**
  298. * @return string
  299. */
  300. public function getSecurityToken()
  301. {
  302. return $this->securityToken;
  303. }
  304. /**
  305. * @param string $securityToken
  306. */
  307. public function setSecurityToken($securityToken)
  308. {
  309. $this->securityToken = $securityToken;
  310. }
  311. /**
  312. * @return mixed
  313. */
  314. public function getActive()
  315. {
  316. return $this->active;
  317. }
  318. /**
  319. * @param mixed $active
  320. */
  321. public function setActive($active)
  322. {
  323. $this->active = $active;
  324. }
  325. /**
  326. * @return mixed
  327. */
  328. public function getAdmin()
  329. {
  330. return $this->admin;
  331. }
  332. /**
  333. * @param mixed $admin
  334. */
  335. public function setAdmin($admin)
  336. {
  337. $this->admin = $admin;
  338. }
  339. /**
  340. * @param mixed $username
  341. */
  342. public function setUsername($username)
  343. {
  344. $this->username = $username;
  345. }
  346. /**
  347. * @param mixed $password
  348. */
  349. public function setPassword($password)
  350. {
  351. $this->password = $password;
  352. }
  353. /**
  354. * @param string $registrationToken
  355. */
  356. public function setRegistrationToken($registrationToken)
  357. {
  358. $this->registrationToken = $registrationToken;
  359. if (empty($this->registrationToken)) {
  360. $this->registrationToken = null;
  361. }
  362. }
  363. /**
  364. * @return Company
  365. */
  366. public function getCompany()
  367. {
  368. return $this->company;
  369. }
  370. public function getCompanyName()
  371. {
  372. if ($this->company) {
  373. return $this->company->getName();
  374. }
  375. }
  376. /**
  377. * @param Company $company
  378. */
  379. public function setCompany($company)
  380. {
  381. $this->company = $company;
  382. }
  383. /**
  384. * @return string
  385. */
  386. public function getPhone(): string
  387. {
  388. return is_null($this->phone) ? '' : $this->phone;
  389. }
  390. /**
  391. * @param string $phone
  392. */
  393. public function setPhone($phone)
  394. {
  395. $this->phone = $phone;
  396. }
  397. /**
  398. * @return string
  399. */
  400. public function getFax(): string
  401. {
  402. return is_null($this->fax) ? '' : $this->fax;
  403. }
  404. /**
  405. * @param string $fax
  406. */
  407. public function setFax($fax)
  408. {
  409. $this->fax = $fax;
  410. }
  411. /**
  412. * @return string
  413. */
  414. public function getMobile(): string
  415. {
  416. return is_null($this->mobile) ? '' : $this->mobile;
  417. }
  418. /**
  419. * @param string $mobile
  420. */
  421. public function setMobile($mobile)
  422. {
  423. $this->mobile = $mobile;
  424. }
  425. /**
  426. * @return ContactValidation
  427. */
  428. public function getContactValidation()
  429. {
  430. return $this->contactValidation;
  431. }
  432. /**
  433. * @param ContactValidation $contactValidation
  434. */
  435. public function setContactValidation($contactValidation)
  436. {
  437. $this->contactValidation = $contactValidation;
  438. }
  439. /**
  440. * @return mixed
  441. */
  442. public function getContact(): ?ContactPerson
  443. {
  444. return $this->contact;
  445. }
  446. /**
  447. * @param mixed $contact
  448. */
  449. public function setContact($contact)
  450. {
  451. $this->contact = $contact;
  452. }
  453. /**
  454. * @return mixed
  455. */
  456. public function getPlaintextPassword()
  457. {
  458. return $this->plaintextPassword;
  459. }
  460. /**
  461. * @param mixed $plaintextPassword
  462. */
  463. public function setPlaintextPassword($plaintextPassword)
  464. {
  465. $this->updatedAt = new \DateTime();
  466. $this->plaintextPassword = $plaintextPassword;
  467. }
  468. /**
  469. * @return mixed
  470. */
  471. public function getErpReference()
  472. {
  473. return $this->erpReference;
  474. }
  475. /**
  476. * @param mixed $erpReference
  477. */
  478. public function setErpReference($erpReference)
  479. {
  480. $this->erpReference = $erpReference;
  481. }
  482. /**
  483. * @return mixed
  484. */
  485. public function getNewsReadOn()
  486. {
  487. return $this->newsReadOn;
  488. }
  489. /**
  490. * @param mixed $newsReadOn
  491. */
  492. public function setNewsReadOn($newsReadOn)
  493. {
  494. $this->newsReadOn = $newsReadOn;
  495. }
  496. /**
  497. * @return array
  498. */
  499. public function getFeatures()
  500. {
  501. return $this->features;
  502. }
  503. /**
  504. * @param array $features
  505. */
  506. public function setFeatures($features)
  507. {
  508. $this->features = $features;
  509. }
  510. /**
  511. * @return ArrayCollection
  512. */
  513. public function getGroups($includeCompanyGroups = false)
  514. {
  515. if ($includeCompanyGroups === false || ! $this->company instanceof Company) {
  516. return $this->groups;
  517. }
  518. return array_merge($this->groups->toArray(), $this->company->getGroups()->toArray());
  519. }
  520. public function hasGroup($groupName, $includeCompanyGroups = false)
  521. {
  522. $groups = $this->getGroups($includeCompanyGroups);
  523. foreach ($groups as $group) {
  524. /** @var Group $group */
  525. if ($group->getName() === $groupName) {
  526. return true;
  527. }
  528. }
  529. return false;
  530. }
  531. public function hasRole($groupName)
  532. {
  533. $groups = [];
  534. foreach($this->getGroups() as $group) {
  535. $groups[] = $group->getName();
  536. }
  537. return array_search($groupName, $groups) !== false;
  538. }
  539. public function __construct()
  540. {
  541. $this->groups = new ArrayCollection();
  542. }
  543. public function addGroup(Group $group): self
  544. {
  545. if (!$this->groups->contains($group)) {
  546. $this->groups[] = $group;
  547. }
  548. return $this;
  549. }
  550. public function removeGroup(Group $group): self
  551. {
  552. $this->groups->removeElement($group);
  553. return $this;
  554. }
  555. public function getDiscount() {
  556. if (is_object($this->company)) {
  557. return $this->company->getDiscount();
  558. }
  559. return 0;
  560. }
  561. }