You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

User.php 5.2KB

3 jaren geleden
3 jaren geleden
3 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace Lc\SovBundle\Model\User;
  3. use Doctrine\Common\Collections\Collection;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Lc\SovBundle\Doctrine\EntityInterface;
  6. use Lc\SovBundle\Model\Newsletter\NewsletterInterface;
  7. use Symfony\Component\Security\Core\User\UserInterface;
  8. /**
  9. * @ORM\MappedSuperclass()
  10. */
  11. abstract class User implements EntityInterface, UserInterface
  12. {
  13. /**
  14. * @ORM\Column(type="string", length=180, unique=true)
  15. */
  16. protected $email;
  17. /**
  18. * @ORM\Column(type="json")
  19. */
  20. protected $roles = [];
  21. /**
  22. * @var string The hashed password
  23. * @ORM\Column(type="string")
  24. */
  25. protected $password;
  26. /**
  27. * @ORM\Column(type="string", length=255, nullable=true)
  28. */
  29. protected $lastname;
  30. /**
  31. * @ORM\Column(type="string", length=255, nullable=true)
  32. */
  33. protected $firstname;
  34. /**
  35. * @ORM\Column(type="boolean")
  36. */
  37. protected $isVerified = false;
  38. /**
  39. * @ORM\ManyToMany(targetEntity="Lc\SovBundle\Model\User\GroupUserInterface", inversedBy="users")
  40. */
  41. protected $groupUsers;
  42. // isUserInGroupVip
  43. public function isInGroupUserVip()
  44. {
  45. return $this->isInGroupByDevAlias('vip') ;
  46. }
  47. // isUserInGroup
  48. public function isInGroupUser(GroupUserInterface $groupUser)
  49. {
  50. return $this->isInGroupByDevAlias($groupUser->getDevAlias());
  51. }
  52. // isUserInGroupByDevAlias
  53. public function isInGroupByDevAlias(string $groupUserDevAlias)
  54. {
  55. $groupUsers = $this->getGroupUsers();
  56. foreach($groupUsers as $groupUser) {
  57. if($groupUser->getDevAlias() == $groupUserDevAlias) {
  58. return true;
  59. }
  60. }
  61. return false;
  62. }
  63. public function isSubscribedToNewsletter(NewsletterInterface $newsletter)
  64. {
  65. return $this->getNewsletters()->contains($newsletter);
  66. }
  67. public function getEmail(): ?string
  68. {
  69. return $this->email;
  70. }
  71. public function setEmail(string $email): self
  72. {
  73. $this->email = $email;
  74. return $this;
  75. }
  76. /**
  77. * A visual identifier that represents this user.
  78. *
  79. * @see UserInterface
  80. */
  81. public function getUsername(): string
  82. {
  83. return (string)$this->email;
  84. }
  85. /**
  86. * @see UserInterface
  87. */
  88. public function getRoles(): array
  89. {
  90. $roles = $this->roles;
  91. // guarantee every user at least has ROLE_USER
  92. $roles[] = 'ROLE_USER';
  93. return array_unique($roles);
  94. }
  95. public function setRoles(array $roles): self
  96. {
  97. $this->roles = $roles;
  98. return $this;
  99. }
  100. public function hasRole($role)
  101. {
  102. return in_array(strtoupper($role), $this->getRoles(), true);
  103. }
  104. /**
  105. * @see UserInterface
  106. */
  107. public function getPassword(): string
  108. {
  109. return (string)$this->password;
  110. }
  111. public function setPassword(string $password): self
  112. {
  113. $this->password = $password;
  114. return $this;
  115. }
  116. public function generatePassword($length = 8): string
  117. {
  118. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  119. $count = mb_strlen($chars);
  120. for ($i = 0, $password = ''; $i < $length; $i++) {
  121. $index = rand(0, $count - 1);
  122. $password .= mb_substr($chars, $index, 1);
  123. }
  124. return $password;
  125. }
  126. /**
  127. * @see UserIn
  128. */
  129. public function getSalt()
  130. {
  131. // not needed when using the "bcrypt" algorithm in security.yaml
  132. }
  133. /**
  134. * @see UserInterface
  135. */
  136. public function eraseCredentials()
  137. {
  138. // If you store any temporary, sensitive data on the user, clear it here
  139. // $this->plainPassword = null;
  140. }
  141. public function getLastname(): ?string
  142. {
  143. return $this->lastname;
  144. }
  145. public function setLastname(?string $lastname): self
  146. {
  147. $this->lastname = $lastname;
  148. return $this;
  149. }
  150. public function getFirstname(): ?string
  151. {
  152. return $this->firstname;
  153. }
  154. public function setFirstname(?string $firstname): self
  155. {
  156. $this->firstname = $firstname;
  157. return $this;
  158. }
  159. public function getName(): ?string
  160. {
  161. return $this->getFirstname().' '.strtoupper($this->getLastname());
  162. }
  163. public function isVerified(): bool
  164. {
  165. return $this->isVerified;
  166. }
  167. public function setIsVerified(bool $isVerified): self
  168. {
  169. $this->isVerified = $isVerified;
  170. return $this;
  171. }
  172. /**
  173. * @return Collection|GroupUserInterface[]
  174. */
  175. public function getGroupUsers(): Collection
  176. {
  177. return $this->groupUsers;
  178. }
  179. public function addGroupUser(GroupUserInterface $groupUser): self
  180. {
  181. if (!$this->groupUsers->contains($groupUser)) {
  182. $this->groupUsers[] = $groupUser;
  183. $groupUser->addUser($this);
  184. }
  185. return $this;
  186. }
  187. public function removeGroupUser(GroupUserInterface $groupUser): self
  188. {
  189. if ($this->groupUsers->contains($groupUser)) {
  190. $this->groupUsers->removeElement($groupUser);
  191. $groupUser->removeUser($this);
  192. }
  193. return $this;
  194. }
  195. }