Guest User

Untitled

a guest
Jun 19th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. <?php // src/Application/UserBundle/Entity/User.php
  2.  
  3. namespace Application\UserBundle\Entity;
  4.  
  5. use Symfony\Component\Security\User\AccountInterface,
  6. Symfony\Component\Security\Encoder\MessageDigestPasswordEncoder;
  7.  
  8. use Doctrine\Common\Collections\ArrayCollection;
  9.  
  10. /**
  11. * @orm:Entity(repositoryClass="Application\UserBundle\Entity\UserRepository")
  12. * @orm:Table(name="user",
  13. * uniqueConstraints={
  14. * @orm:UniqueConstraint(name="username_idx", columns={"username"}),
  15. * @orm:UniqueConstraint(name="email_idx", columns={"email"})
  16. * }
  17. * )
  18. * @orm:HasLifecycleCallbacks
  19. */
  20. class User implements AccountInterface
  21. {
  22. /**
  23. * @orm:Column(name="id", type="integer")
  24. * @orm:Id
  25. * @orm:GeneratedValue(strategy="AUTO")
  26. * @var integer
  27. */
  28. protected $id;
  29.  
  30. /**
  31. * @orm:Column(name="username", type="string", length="32")
  32. * @validation:Min(3)
  33. * @validation:Max(32)
  34. * @validation:NotBlank
  35. * @var string
  36. */
  37. protected $username;
  38.  
  39. /**
  40. * @orm:Column(name="email", type="string", length="256")
  41. * @validation:Email
  42. * @validation:NotBlank
  43. * @var string
  44. */
  45. protected $email;
  46.  
  47. /**
  48. * @orm:Column(name="salt", type="string", length="32")
  49. * @var string
  50. */
  51. protected $salt;
  52.  
  53. /**
  54. * @orm:Column(name="password", type="string", length="40")
  55. * @validation:NotBlank
  56. * @var string
  57. */
  58. protected $password;
  59.  
  60. /**
  61. * @orm:Column(name="activation_key", type="string", length="32", nullable="true")
  62. * @var \DateTime
  63. */
  64. protected $activationKey;
  65.  
  66. /**
  67. * @orm:Column(name="activation", type="datetime", nullable="true")
  68. * @var \DateTime
  69. */
  70. protected $activation;
  71.  
  72. /**
  73. * @orm:Column(name="last_login", type="datetime", nullable="true")
  74. * @var \DateTime
  75. */
  76. protected $lastLogin;
  77.  
  78. /**
  79. * @orm:Column(name="created", type="datetime")
  80. * @validation:NotBlank
  81. * @var \DateTime
  82. */
  83. protected $created;
  84.  
  85. /**
  86. * @orm:Column(name="updated", type="datetime")
  87. * @validation:NotBlank
  88. * @var \DateTime
  89. */
  90. protected $updated;
  91.  
  92. /**
  93. * Constructor.
  94. */
  95. public function __construct()
  96. {
  97. $this->created = $this->updated = new \DateTime('now');
  98. }
  99.  
  100. /**
  101. * @return integer
  102. */
  103. public function getId()
  104. {
  105. return $this->id;
  106. }
  107.  
  108. /**
  109. * @return string
  110. */
  111. public function __toString()
  112. {
  113. return $this->getUsername();
  114. }
  115.  
  116. /**
  117. * @return string
  118. */
  119. public function getUsername()
  120. {
  121. return $this->username;
  122. }
  123.  
  124. /**
  125. * @param string $username
  126. */
  127. public function setUsername($username)
  128. {
  129. $this->username = $username;
  130. }
  131.  
  132. /**
  133. * @return string
  134. */
  135. public function getEmail()
  136. {
  137. return $this->email;
  138. }
  139.  
  140. /**
  141. * @param string $email
  142. */
  143. public function setEmail($email)
  144. {
  145. $this->email = $email;
  146. }
  147.  
  148. /**
  149. * @return string
  150. */
  151. public function getPassword()
  152. {
  153. return $this->password;
  154. }
  155.  
  156. /**
  157. * @param string $password
  158. */
  159. public function setPassword($password)
  160. {
  161. $encoder = new MessageDigestPasswordEncoder('sha1');
  162. $password = $encoder->encodePassword($password, $this->getSalt());
  163.  
  164. $this->password = $password;
  165. }
  166.  
  167. /**
  168. * @return string
  169. */
  170. public function getActivationKey()
  171. {
  172. if (null === $this->activationKey) {
  173. $this->activationKey = md5(sprintf(
  174. '%s_%d_%s_%f_%s_%d',
  175. uniqid(),
  176. rand(0, 99999),
  177. $this->getUsername(),
  178. microtime(true),
  179. $this->getEmail(),
  180. rand(99999, 999999)
  181. ));
  182. }
  183.  
  184. return $this->activationKey;
  185. }
  186.  
  187. /**
  188. * @return \DateTime
  189. */
  190. public function getActivation()
  191. {
  192. return $this->activation;
  193. }
  194.  
  195. /**
  196. * @param \DateTime $activation
  197. */
  198. public function setActivation(\DateTime $activation)
  199. {
  200. $this->activation = $activation;
  201. }
  202.  
  203. /**
  204. * @return \DateTime
  205. */
  206. public function isActivated()
  207. {
  208. return (boolean) $this->activation;
  209. }
  210.  
  211. /**
  212. * @return \DateTime
  213. */
  214. public function getLastLogin()
  215. {
  216. return $this->lastLogin;
  217. }
  218.  
  219. /**
  220. * @param \DateTime $lastLogin
  221. */
  222. public function setLastLogin(\DateTime $lastLogin)
  223. {
  224. $this->lastLogin = $lastLogin;
  225. }
  226.  
  227. /**
  228. * @return \DateTime
  229. */
  230. public function getCreated()
  231. {
  232. return $this->created;
  233. }
  234.  
  235. /**
  236. * @return \DateTime
  237. */
  238. public function getUpdated()
  239. {
  240. return $this->updated;
  241. }
  242.  
  243. /**
  244. * @orm:PreUpdate
  245. */
  246. public function update()
  247. {
  248. $this->updated = new \DateTime('now');
  249. }
  250.  
  251. // AccountInterface
  252.  
  253. /**
  254. * @return string
  255. */
  256. public function getSalt()
  257. {
  258. if (null === $this->salt) {
  259. $this->salt = md5(sprintf(
  260. '%s_%d_%f',
  261. uniqid(),
  262. rand(0, 99999),
  263. microtime(true)
  264. ));
  265. }
  266.  
  267. return $this->salt;
  268. }
  269.  
  270. /**
  271. * @return array
  272. */
  273. public function getRoles()
  274. {
  275. return array('ROLE_USER', 'ROLE_ADMIN');
  276. }
  277.  
  278. /**
  279. * @return void
  280. */
  281. public function eraseCredentials()
  282. {
  283. $this->roles = null;
  284. }
  285. }
Add Comment
Please, Sign In to add comment