1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\UserBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Mapping as ORM; |
6
|
|
|
use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUserInterface; |
7
|
|
|
use Ramsey\Uuid\Uuid; |
8
|
|
|
use Starkerxp\StructureBundle\Entity\AbstractEntity; |
9
|
|
|
use Starkerxp\StructureBundle\Entity\UserInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* User |
13
|
|
|
* |
14
|
|
|
* @ORM\Table(name="user", indexes={ |
15
|
|
|
* @ORM\Index(columns={"created_at"}), |
16
|
|
|
* @ORM\Index(columns={"updated_at"}) |
17
|
|
|
* }) |
18
|
|
|
* @ORM\Entity(repositoryClass="Starkerxp\UserBundle\Repository\UserRepository") |
19
|
|
|
*/ |
20
|
|
|
class User extends AbstractEntity implements JWTUserInterface, UserInterface |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var UserRole |
25
|
|
|
* |
26
|
|
|
* @ORM\OneToOne(targetEntity="UserRole", cascade={"persist"}) |
27
|
|
|
* @ORM\JoinColumn(name="roles", referencedColumnName="id", nullable=true) |
28
|
|
|
*/ |
29
|
|
|
protected $roles; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
* |
34
|
|
|
* @ORM\Column(name="salt", type="string", length=255, nullable=true) |
35
|
|
|
*/ |
36
|
|
|
protected $salt; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
* |
41
|
|
|
* @ORM\Column(name="password", type="string", length=255) |
42
|
|
|
*/ |
43
|
|
|
protected $password; |
44
|
|
|
protected $plainPassword; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
* |
49
|
|
|
* @ORM\Column(name="email", type="string", length=255, nullable=false, unique=true) |
50
|
|
|
*/ |
51
|
|
|
protected $email; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* User constructor. |
55
|
|
|
* @param array $roles |
56
|
|
|
* @param string $email |
57
|
|
|
*/ |
58
|
|
|
public function __construct($email, $roles) |
59
|
|
|
{ |
60
|
|
|
$this->email = $email; |
61
|
|
|
$this->setRoles($roles); |
62
|
|
|
$uuid = Uuid::uuid4(); |
63
|
|
|
$this->salt = $uuid->toString(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function createFromPayload($username, array $payload) |
67
|
|
|
{ |
68
|
|
|
return new self( |
69
|
|
|
$username, |
70
|
|
|
$payload['roles'] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getRoles() |
75
|
|
|
{ |
76
|
|
|
/** @var \Starkerxp\UserBundle\Entity\UserRole $roles */ |
77
|
|
|
if (empty($this->roles)) { |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $this->roles->getRole(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param array $roles |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function setRoles($roles) |
90
|
|
|
{ |
91
|
|
|
if ($roles instanceof UserRole || empty($this->roles)) { |
92
|
|
|
$this->roles = new UserRole($roles); |
93
|
|
|
|
94
|
|
|
return true; |
95
|
|
|
} |
96
|
|
|
$this->roles->setRole($roles); |
97
|
|
|
|
98
|
|
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getPassword() |
102
|
|
|
{ |
103
|
|
|
return $this->password; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $password |
108
|
|
|
*/ |
109
|
|
|
public function setPassword($password) |
110
|
|
|
{ |
111
|
|
|
$this->password = $password; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getSalt() |
115
|
|
|
{ |
116
|
|
|
return $this->salt; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getUsername() |
120
|
|
|
{ |
121
|
|
|
return $this->email; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function eraseCredentials() |
125
|
|
|
{ |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $email |
131
|
|
|
*/ |
132
|
|
|
public function setEmail($email) |
133
|
|
|
{ |
134
|
|
|
$this->email = $email; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return mixed |
139
|
|
|
*/ |
140
|
|
|
public function getPlainPassword() |
141
|
|
|
{ |
142
|
|
|
return $this->plainPassword; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param mixed $plainPassword |
147
|
|
|
*/ |
148
|
|
|
public function setPlainPassword($plainPassword) |
149
|
|
|
{ |
150
|
|
|
$this->plainPassword = $plainPassword; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
|
154
|
|
|
} |
|
|
|
|
155
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.