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 | * @ORM\Column(type="string", length=255, unique=true) |
||
32 | */ |
||
33 | public $email; |
||
34 | |||
35 | /** |
||
36 | * @ORM\Column(type="string", length=255, unique=true) |
||
37 | */ |
||
38 | public $username; |
||
39 | |||
40 | /** |
||
41 | * @ORM\Column(type="string", length=255, nullable=true) |
||
42 | */ |
||
43 | private $roles; |
||
44 | |||
45 | /** |
||
46 | * @Exclude |
||
47 | */ |
||
48 | public $plainPassword; |
||
49 | |||
50 | /** |
||
51 | * @Exclude |
||
52 | * @ORM\Column(type="string", length=64) |
||
53 | */ |
||
54 | private $password; |
||
55 | |||
56 | 10 | public function __construct() |
|
57 | { |
||
58 | 10 | $this->addRole(self::ROLE_USER); |
|
59 | 10 | } |
|
60 | |||
61 | 1 | public function getId(): ?int |
|
65 | |||
66 | 10 | public function addRole(string $role): self |
|
67 | { |
||
68 | 10 | if (array_search($role, $this->getRoles()) === false) { |
|
69 | 3 | return $this->setRoles( |
|
70 | 3 | array_merge($this->getRoles(), [$role]) |
|
71 | ); |
||
72 | } |
||
73 | |||
74 | 10 | return $this; |
|
75 | } |
||
76 | |||
77 | 1 | public function removeRole(string $role): self |
|
78 | { |
||
79 | 1 | $roles = $this->getRoles(); |
|
80 | 1 | $foundedRoleKey = array_search($role, $roles); |
|
81 | |||
82 | 1 | if ($foundedRoleKey !== false) { |
|
83 | 1 | unset($roles[$foundedRoleKey]); |
|
84 | 1 | return $this->setRoles($roles); |
|
85 | } |
||
86 | |||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | 3 | private function setRoles(array $roles): self |
|
91 | { |
||
92 | 3 | if (!count($roles)) { |
|
93 | 1 | $this->roles = null; |
|
94 | 1 | return $this; |
|
95 | } |
||
96 | |||
97 | 3 | $this->roles = json_encode($roles); |
|
98 | |||
99 | 3 | return $this; |
|
100 | } |
||
101 | |||
102 | 10 | public function getRoles(): array |
|
103 | { |
||
104 | 10 | if (!$this->roles) { |
|
105 | 10 | return $this->getDefaultRoles(); |
|
106 | } |
||
107 | |||
108 | 3 | $roles = (array)json_decode($this->roles); |
|
109 | |||
110 | 3 | if (!count($roles)) { |
|
111 | return $this->getDefaultRoles(); |
||
112 | } |
||
113 | |||
114 | 3 | return array_values($roles); |
|
115 | } |
||
116 | |||
117 | 10 | private function getDefaultRoles() |
|
118 | { |
||
119 | 10 | return [self::ROLE_USER]; |
|
120 | } |
||
121 | |||
122 | 2 | public function setPassword($password, UserPasswordEncoderInterface $passwordEncoder): self |
|
123 | { |
||
124 | 2 | $this->plainPassword = $password; |
|
125 | 2 | $this->password = $passwordEncoder->encodePassword($this, $this->plainPassword); |
|
126 | |||
127 | 2 | return $this; |
|
128 | } |
||
129 | |||
130 | 1 | public function isPasswordValid($password, UserPasswordEncoderInterface $passwordEncoder): bool |
|
131 | { |
||
132 | 1 | return $passwordEncoder->isPasswordValid($this, $password); |
|
133 | } |
||
134 | |||
135 | 1 | public function getPassword(): ?string |
|
139 | |||
140 | 3 | public function getSalt(): ?string |
|
144 | |||
145 | 3 | public function getUsername(): ?string |
|
149 | |||
150 | 1 | public function eraseCredentials(): self |
|
151 | { |
||
152 | 1 | $this->plainPassword = null; |
|
153 | |||
154 | 1 | return $this; |
|
155 | } |
||
156 | |||
157 | 1 | public function serialize(): string |
|
166 | |||
167 | 1 | public function unserialize($serialized): self |
|
178 | } |