1 | <?php |
||
19 | class User implements UserInterface, \Serializable |
||
20 | { |
||
21 | const ROLE_USER = 'ROLE_USER'; |
||
22 | |||
23 | /** |
||
24 | * @ORM\Id() |
||
25 | * @ORM\GeneratedValue() |
||
26 | * @ORM\Column(type="integer") |
||
27 | */ |
||
28 | private $id; |
||
29 | |||
30 | /** |
||
31 | * @var $profile UserProfile |
||
32 | * @ORM\OneToOne(targetEntity="App\Entity\UserProfile", mappedBy="user", cascade={"persist", "remove"}) |
||
33 | */ |
||
34 | private $profile; |
||
35 | |||
36 | /** |
||
37 | * @ORM\Column(type="string", length=255, unique=true) |
||
38 | */ |
||
39 | public $email; |
||
40 | |||
41 | /** |
||
42 | * @ORM\Column(type="string", length=255, unique=true) |
||
43 | */ |
||
44 | public $username; |
||
45 | |||
46 | /** |
||
47 | * @ORM\Column(type="string", length=255, nullable=true) |
||
48 | */ |
||
49 | private $roles; |
||
50 | |||
51 | /** |
||
52 | * @Exclude |
||
53 | */ |
||
54 | public $plainPassword; |
||
55 | |||
56 | /** |
||
57 | * @Exclude |
||
58 | * @ORM\Column(type="string", length=64) |
||
59 | */ |
||
60 | private $password; |
||
61 | |||
62 | 22 | public function __construct() |
|
67 | |||
68 | 8 | public function getProfile(): UserProfile |
|
69 | { |
||
70 | 8 | return $this->profile; |
|
71 | } |
||
72 | |||
73 | 2 | public function getId(): ?int |
|
77 | |||
78 | 22 | public function addRole(string $role): self |
|
79 | { |
||
80 | 22 | if (array_search($role, $this->getRoles()) === false) { |
|
81 | 3 | return $this->setRoles( |
|
82 | 3 | array_merge($this->getRoles(), [$role]) |
|
83 | ); |
||
84 | } |
||
85 | |||
86 | 22 | return $this; |
|
87 | } |
||
88 | |||
89 | 1 | public function removeRole(string $role): self |
|
101 | |||
102 | 3 | private function setRoles(array $roles): self |
|
103 | { |
||
104 | 3 | if (!count($roles)) { |
|
105 | 1 | $this->roles = null; |
|
106 | 1 | return $this; |
|
107 | } |
||
108 | |||
109 | 3 | $this->roles = json_encode($roles); |
|
110 | |||
111 | 3 | return $this; |
|
112 | } |
||
113 | |||
114 | 22 | public function getRoles(): array |
|
128 | |||
129 | 22 | private function getDefaultRoles() |
|
133 | |||
134 | 4 | public function setPassword($password, UserPasswordEncoderInterface $passwordEncoder): self |
|
141 | |||
142 | 1 | public function isPasswordValid($password, UserPasswordEncoderInterface $passwordEncoder): bool |
|
146 | |||
147 | 1 | public function getPassword(): ?string |
|
151 | |||
152 | 5 | public function getSalt(): ?string |
|
156 | |||
157 | 3 | public function getUsername(): ?string |
|
161 | |||
162 | 1 | public function eraseCredentials(): self |
|
168 | |||
169 | 1 | public function serialize(): string |
|
178 | |||
179 | 1 | public function unserialize($serialized): self |
|
190 | } |