/Entity/User.php

https://github.com/brainwood/OrkestraApplicationBundle · PHP · 344 lines · 152 code · 46 blank · 146 comment · 2 complexity · f7ea4c63851961ea609c6b8c49fbe22f MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the OrkestraApplicationBundle package.
  4. *
  5. * Copyright (c) Orkestra Community
  6. *
  7. * For the full copyright and license information, please view the LICENSE file
  8. * that was distributed with this source code.
  9. */
  10. namespace Orkestra\Bundle\ApplicationBundle\Entity;
  11. use Orkestra\Bundle\ApplicationBundle\Model\GroupInterface;
  12. use Orkestra\Bundle\ApplicationBundle\Model\PreferencesInterface;
  13. use Orkestra\Bundle\ApplicationBundle\Model\UserInterface;
  14. use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  15. use Doctrine\Common\Collections\ArrayCollection;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Orkestra\Common\Entity\AbstractEntity;
  18. /**
  19. * User Entity
  20. *
  21. * Defines an authenticated user
  22. *
  23. * @ORM\Table(name="orkestra_users")
  24. * @ORM\Entity(repositoryClass="Orkestra\Bundle\ApplicationBundle\Repository\UserRepository")
  25. */
  26. class User extends AbstractEntity implements UserInterface, AdvancedUserInterface, \Serializable
  27. {
  28. /**
  29. * @ORM\Column(name="username", type="string", length=100, unique=true)
  30. */
  31. private $username;
  32. /**
  33. * @ORM\Column(name="salt", type="string", length=40)
  34. */
  35. private $salt;
  36. /**
  37. * @ORM\Column(name="password", type="string", length=255)
  38. */
  39. private $password;
  40. /**
  41. * @ORM\Column(name="first_name", type="string", length=60)
  42. */
  43. private $firstName;
  44. /**
  45. * @ORM\Column(name="last_name", type="string", length=60)
  46. */
  47. private $lastName;
  48. /**
  49. * @ORM\Column(name="email", type="string", length=100, unique=true, nullable=true)
  50. */
  51. private $email;
  52. /**
  53. * @ORM\Column(name="expired", type="boolean")
  54. */
  55. private $expired = false;
  56. /**
  57. * @ORM\Column(name="locked", type="boolean")
  58. */
  59. private $locked = false;
  60. /**
  61. * @ORM\ManyToMany(targetEntity="Orkestra\Bundle\ApplicationBundle\Entity\Group", inversedBy="users")
  62. * @ORM\JoinTable(name="orkestra_user_groups",
  63. * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
  64. * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
  65. * )
  66. */
  67. private $groups;
  68. /**
  69. * @ORM\OneToOne(targetEntity="Orkestra\Bundle\ApplicationBundle\Entity\Preferences", mappedBy="user", cascade={"persist"})
  70. */
  71. private $preferences;
  72. /**
  73. *
  74. */
  75. public function __construct()
  76. {
  77. $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
  78. $this->groups = new ArrayCollection();
  79. }
  80. /**
  81. * @return string
  82. */
  83. public function __toString()
  84. {
  85. return sprintf('%s %s', $this->getFirstName(), $this->getLastName());
  86. }
  87. /**
  88. * @param $email
  89. */
  90. public function setEmail($email)
  91. {
  92. $this->email = $email;
  93. }
  94. /**
  95. * @return mixed
  96. */
  97. public function getEmail()
  98. {
  99. return $this->email;
  100. }
  101. /**
  102. * @param $username
  103. */
  104. public function setUsername($username)
  105. {
  106. $this->username = $username;
  107. }
  108. /**
  109. * @param $password
  110. */
  111. public function setPassword($password)
  112. {
  113. $this->password = $password;
  114. }
  115. /**
  116. * @param $firstName
  117. */
  118. public function setFirstName($firstName)
  119. {
  120. $this->firstName = $firstName;
  121. }
  122. /**
  123. * @return mixed
  124. */
  125. public function getFirstName()
  126. {
  127. return $this->firstName;
  128. }
  129. /**
  130. * @param $lastName
  131. */
  132. public function setLastName($lastName)
  133. {
  134. $this->lastName = $lastName;
  135. }
  136. /**
  137. * @return mixed
  138. */
  139. public function getLastName()
  140. {
  141. return $this->lastName;
  142. }
  143. /**
  144. * @param GroupInterface $group
  145. */
  146. public function addGroup(GroupInterface $group)
  147. {
  148. $this->groups[] = $group;
  149. }
  150. /**
  151. * @param $groups
  152. */
  153. public function setGroups($groups)
  154. {
  155. if ($groups instanceof Group) {
  156. $this->groups = new ArrayCollection(array($groups));
  157. } elseif ($groups === null) {
  158. $this->groups = new ArrayCollection();
  159. } elseif (is_array($groups)) {
  160. $this->groups = new ArrayCollection($groups);
  161. } else {
  162. $this->groups = $groups;
  163. }
  164. }
  165. /**
  166. * @return ArrayCollection
  167. */
  168. public function getGroups()
  169. {
  170. return $this->groups;
  171. }
  172. /**
  173. * @param GroupInterface $group
  174. *
  175. * @return ArrayCollection
  176. */
  177. public function removeGroup(GroupInterface $group)
  178. {
  179. $this->groups->removeElement($group);
  180. return $this->groups;
  181. }
  182. /**
  183. * @param PreferencesInterface $preferences
  184. */
  185. public function setPreferences(PreferencesInterface $preferences)
  186. {
  187. $this->preferences = $preferences;
  188. $this->preferences->setUser($this);
  189. }
  190. /**
  191. * @return mixed
  192. */
  193. public function getPreferences()
  194. {
  195. return $this->preferences;
  196. }
  197. #region AdvancedUserInterface members
  198. /**
  199. * @return array
  200. */
  201. public function getRoles()
  202. {
  203. return $this->groups->toArray();
  204. }
  205. /**
  206. * @param UserInterface $user
  207. *
  208. * @return bool
  209. */
  210. public function equals(UserInterface $user)
  211. {
  212. return $user->getUsername() === $this->username;
  213. }
  214. /**
  215. *
  216. */
  217. public function eraseCredentials()
  218. {
  219. }
  220. /**
  221. * @return mixed
  222. */
  223. public function getUsername()
  224. {
  225. return $this->username;
  226. }
  227. /**
  228. * @return string
  229. */
  230. public function getSalt()
  231. {
  232. return $this->salt;
  233. }
  234. /**
  235. * @return mixed
  236. */
  237. public function getPassword()
  238. {
  239. return $this->password;
  240. }
  241. /**
  242. * @return bool
  243. */
  244. public function isAccountNonExpired()
  245. {
  246. return !$this->expired;
  247. }
  248. /**
  249. * @return bool
  250. */
  251. public function isAccountNonLocked()
  252. {
  253. return !$this->locked;
  254. }
  255. /**
  256. * @return bool
  257. */
  258. public function isCredentialsNonExpired()
  259. {
  260. return true;
  261. }
  262. /**
  263. * @return bool
  264. */
  265. public function isEnabled()
  266. {
  267. return $this->active;
  268. }
  269. /**
  270. * @return bool
  271. */
  272. public function isExpired()
  273. {
  274. return $this->expired;
  275. }
  276. /**
  277. * @return bool
  278. */
  279. public function isLocked()
  280. {
  281. return $this->locked;
  282. }
  283. #endregion
  284. /**
  285. * @return string
  286. */
  287. public function serialize()
  288. {
  289. return \json_encode(array($this->username, $this->id));
  290. }
  291. /**
  292. * @param string $serialized
  293. */
  294. public function unserialize($serialized)
  295. {
  296. list($this->username, $this->id) = json_decode($serialized);
  297. }
  298. }