PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Ultra/UsuarioBundle/Entity/UltraUser.php

https://gitlab.com/hersan/ultra
PHP | 389 lines | 135 code | 42 blank | 212 comment | 1 complexity | 4eebe33b40b319fa84189e7308a6353d MD5 | raw file
  1. <?php
  2. namespace Ultra\UsuarioBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Symfony\Component\Security\Core\Role\Role;
  5. use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  9. /**
  10. * UltraUser Contiene los usuario del sistema
  11. *
  12. * @author Herminio Heredia Santos <herminio.heredia@hotmail.com>
  13. * @version 1.0
  14. *
  15. * @ORM\Table()
  16. * @ORM\Entity(repositoryClass="Ultra\UsuarioBundle\Entity\UltraUserRepository")
  17. * @UniqueEntity(fields={"username"}, groups={"creation","editing"})
  18. *
  19. */
  20. class UltraUser implements AdvancedUserInterface, \Serializable
  21. {
  22. /**
  23. * @var integer Identifica de forma unica al usuario
  24. *
  25. * @ORM\Column(name="id", type="integer")
  26. * @ORM\Id
  27. * @ORM\GeneratedValue(strategy="AUTO")
  28. */
  29. private $id;
  30. /**
  31. * @var string Representa el nombre de usuario
  32. *
  33. * @Assert\NotBlank(message="Este campo no puede estar vacío", groups={"creation","editing"})
  34. * @Assert\Type(type="string", message="El valor {{ value }} no es de tipo {{ type }}.")
  35. * @ORM\Column(name="username", type="string", length=40, unique=true)
  36. */
  37. private $username;
  38. /**
  39. * @var string contiene la contraseña del usuario
  40. *
  41. * @Assert\NotBlank(message="Este campo no puede estar vacío",groups={"creation"})
  42. * @Assert\Type(type="string", message="El valor {{ value }} no es de tipo {{ type }}.")
  43. * @ORM\Column(name="password", type="string", length=128)
  44. */
  45. private $password;
  46. /**
  47. * @var string semilla para la generación de la contraseña
  48. *
  49. * @Assert\NotBlank(message="Este campo no puede estar vacío")
  50. * @Assert\Type(type="string", message="El valor {{ value }} no es de tipo {{ type }}.")
  51. * @ORM\Column(name="salt", type="string", length=32)
  52. */
  53. private $salt;
  54. /**
  55. * @var boolean determina si la cuenta esta habilitada para acceder a la aplicación
  56. *
  57. * @Assert\NotBlank(message="Este campo no puede estar vacío")
  58. * @ORM\Column(name="is_enabled", type="boolean")
  59. */
  60. private $isEnabled;
  61. /**
  62. * @var
  63. *
  64. * @ORM\ManyToMany(targetEntity="\Ultra\UsuarioBundle\Entity\UltraRole", inversedBy="users")
  65. */
  66. private $roles;
  67. /**
  68. * @var
  69. *
  70. * @ORM\ManyToOne(targetEntity="\Ultra\ControlDocumentoBundle\Entity\Curricula", inversedBy="user")
  71. */
  72. private $curricula;
  73. /**
  74. * Get id
  75. *
  76. * @return integer
  77. */
  78. public function getId()
  79. {
  80. return $this->id;
  81. }
  82. /**
  83. * Set username
  84. *
  85. * @param string $username
  86. * @return UltraUser
  87. */
  88. public function setUsername($username)
  89. {
  90. $this->username = $username;
  91. return $this;
  92. }
  93. /**
  94. * Get username
  95. *
  96. * @return string
  97. */
  98. public function getUsername()
  99. {
  100. return $this->username;
  101. }
  102. /**
  103. * Set password
  104. *
  105. * @param string $password
  106. * @return UltraUser
  107. */
  108. public function setPassword($password)
  109. {
  110. $this->password = $password;
  111. return $this;
  112. }
  113. /**
  114. * Get password
  115. *
  116. * @return string
  117. */
  118. public function getPassword()
  119. {
  120. return $this->password;
  121. }
  122. /**
  123. * Set salt
  124. *
  125. * @param string $salt
  126. * @return UltraUser
  127. */
  128. public function setSalt($salt)
  129. {
  130. $this->salt = $salt;
  131. return $this;
  132. }
  133. /**
  134. * Get salt
  135. *
  136. * @return string
  137. */
  138. public function getSalt()
  139. {
  140. return $this->salt;
  141. }
  142. /**
  143. * Checks whether the user's account has expired.
  144. *
  145. * Internally, if this method returns false, the authentication system
  146. * will throw an AccountExpiredException and prevent login.
  147. *
  148. * @return Boolean true if the user's account is non expired, false otherwise
  149. *
  150. * @see AccountExpiredException
  151. */
  152. public function isAccountNonExpired()
  153. {
  154. return true;
  155. }
  156. /**
  157. * Checks whether the user is locked.
  158. *
  159. * Internally, if this method returns false, the authentication system
  160. * will throw a LockedException and prevent login.
  161. *
  162. * @return Boolean true if the user is not locked, false otherwise
  163. *
  164. * @see LockedException
  165. */
  166. public function isAccountNonLocked()
  167. {
  168. return true;
  169. }
  170. /**
  171. * Checks whether the user's credentials (password) has expired.
  172. *
  173. * Internally, if this method returns false, the authentication system
  174. * will throw a CredentialsExpiredException and prevent login.
  175. *
  176. * @return Boolean true if the user's credentials are non expired, false otherwise
  177. *
  178. * @see CredentialsExpiredException
  179. */
  180. public function isCredentialsNonExpired()
  181. {
  182. return true;
  183. }
  184. /**
  185. * Checks whether the user is enabled.
  186. *
  187. * Internally, if this method returns false, the authentication system
  188. * will throw a DisabledException and prevent login.
  189. *
  190. * @return Boolean true if the user is enabled, false otherwise
  191. *
  192. * @see DisabledException
  193. */
  194. public function isEnabled()
  195. {
  196. return $this->isEnabled;
  197. }
  198. /**
  199. * Returns the roles granted to the user.
  200. *
  201. * <code>
  202. * public function getRoles()
  203. * {
  204. * return array('ROLE_USER');
  205. * }
  206. * </code>
  207. *
  208. * Alternatively, the roles might be stored on a ``roles`` property,
  209. * and populated in any number of different ways when the user object
  210. * is created.
  211. *
  212. * @return Role[] The user roles
  213. */
  214. public function getRoles()
  215. {
  216. return $this->roles->toArray();
  217. }
  218. /**
  219. * Removes sensitive data from the user.
  220. *
  221. * This is important if, at any given point, sensitive information like
  222. * the plain-text password is stored on this object.
  223. */
  224. public function eraseCredentials()
  225. {
  226. }
  227. /**
  228. * Constructor
  229. */
  230. public function __construct()
  231. {
  232. $this->isEnabled = true;
  233. $this->roles = new ArrayCollection();
  234. $this->salt = md5(uniqid('ultra.dsad',true));
  235. }
  236. /**
  237. * Set isEnabled
  238. *
  239. * @param boolean $isEnabled
  240. * @return UltraUser
  241. */
  242. public function setIsEnabled($isEnabled)
  243. {
  244. $this->isEnabled = $isEnabled;
  245. return $this;
  246. }
  247. /**
  248. * Get isEnabled
  249. *
  250. * @return boolean
  251. */
  252. public function getIsEnabled()
  253. {
  254. return $this->isEnabled;
  255. }
  256. /**
  257. * Add roles
  258. *
  259. * @param \Ultra\UsuarioBundle\Entity\UltraRole $roles
  260. * @return UltraUser
  261. */
  262. public function addRole(UltraRole $roles)
  263. {
  264. $this->roles->add($roles);
  265. return $this;
  266. }
  267. /**
  268. * Remove roles
  269. *
  270. * @param \Ultra\UsuarioBundle\Entity\UltraRole $roles
  271. */
  272. public function removeRole(UltraRole $roles)
  273. {
  274. $this->roles->removeElement($roles);
  275. }
  276. public function __toString(){
  277. return $this->getUsername();
  278. }
  279. /**
  280. * (PHP 5 &gt;= 5.1.0)<br/>
  281. * String representation of object
  282. * @link http://php.net/manual/en/serializable.serialize.php
  283. * @return string the string representation of the object or null
  284. */
  285. public function serialize()
  286. {
  287. return \serialize(array(
  288. $this->id,
  289. $this->username,
  290. $this->password,
  291. $this->salt,
  292. ));
  293. }
  294. /**
  295. * (PHP 5 &gt;= 5.1.0)<br/>
  296. * Constructs the object
  297. * @link http://php.net/manual/en/serializable.unserialize.php
  298. * @param string $serialized <p>
  299. * The string representation of the object.
  300. * </p>
  301. * @return void
  302. */
  303. public function unserialize($serialized)
  304. {
  305. list(
  306. $this->id,
  307. $this->username,
  308. $this->password,
  309. $this->salt,
  310. ) = \unserialize($serialized);
  311. }
  312. /**
  313. * Set curricula
  314. *
  315. * @param \Ultra\ControlDocumentoBundle\Entity\Curricula $curricula
  316. * @return UltraUser
  317. */
  318. public function setCurricula(\Ultra\ControlDocumentoBundle\Entity\Curricula $curricula = null)
  319. {
  320. $this->curricula = $curricula;
  321. return $this;
  322. }
  323. /**
  324. * Get curricula
  325. *
  326. * @return \Ultra\ControlDocumentoBundle\Entity\Curricula
  327. */
  328. public function getCurricula()
  329. {
  330. return $this->curricula;
  331. }
  332. /**
  333. * Determina si el usuario cuenta con un Rol
  334. *
  335. * @param $role
  336. * @return bool
  337. */
  338. public function hasRole($role){
  339. $checkRole = function($role)
  340. {
  341. return function($key, UltraRole $roles) use ($role){
  342. return $roles->getRole() == $role;
  343. };
  344. };
  345. return $this->roles->exists($checkRole($role));
  346. }
  347. }