Guest User

Untitled

a guest
May 1st, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.35 KB | None | 0 0
  1. namespace BackendBundleRepository;
  2.  
  3. use BackendBundleEntityUsers;
  4. use SymfonyBridgeDoctrineSecurityUserUserLoaderInterface;
  5. use SymfonyComponentSecurityCoreUserUserInterface;
  6. use SymfonyComponentSecurityCoreExceptionUsernameNotFoundException;
  7. use DoctrineORMEntityRepository;
  8.  
  9.  
  10. class UserRepository extends EntityRepository implements UserLoaderInterface {
  11.  
  12. public function loadUserByUsername($username) {
  13. return $this->createQueryBuilder('u')
  14. //$user = $this->createQueryBuilder('u')
  15. ->where('u.nick = :nick OR u.email = :email')
  16. ->setParameter('nick', $username)
  17. ->setParameter('email', $username)
  18. ->getQuery()
  19. ->getOneOrNullResult();
  20.  
  21. // if (null === $user) {
  22. // $message = sprintf(
  23. // 'Unable to find an active admin AppBundle:User object identified by "%s".', $username
  24. // );
  25. // throw new UsernameNotFoundException($message);
  26. // }
  27. //
  28. // return $user;
  29. }
  30.  
  31. }
  32.  
  33. use BackendBundleEntityUsers;
  34. use BackendBundleEntityUserRepository;
  35. use SymfonyBundleFrameworkBundleControllerController;
  36. use SymfonyComponentHttpFoundationRequest;
  37. use SymfonyComponentHttpFoundationSessionSession;
  38. use BackendBundleRepositoryUserRepository;
  39. /**
  40. * User controller.
  41. *
  42. */
  43. class LoginController extends Controller {
  44.  
  45. private $session;
  46.  
  47. public function __construct() {
  48. $this->session = new Session();
  49. }
  50.  
  51. public function loginAction(Request $request) { //autentificamos usuarios usando los metodos de symfony de autentificación
  52. $authenticationUtils = $this->get("security.authentication_utils");
  53.  
  54. $error = $authenticationUtils->getLastAuthenticationError();
  55. $lastUsername = $authenticationUtils->getLastUsername();
  56.  
  57.  
  58.  
  59. return $this->render('AppBundle:User:login.html.twig', array(
  60. "error" => $error,
  61. "last_username" => $lastUsername
  62. ));
  63. }
  64.  
  65. Security.yml
  66. security:
  67. encoders:
  68. BackendBundleEntityUsers:
  69. algorithm: bcrypt
  70. cost: 6
  71.  
  72. # http://symfony.com/doc/current/security.html#b-configuring-how-users-are- loaded
  73. providers:
  74. user_db_provider:
  75. entity:
  76. class: BackendBundle:Users
  77. #property: email
  78.  
  79.  
  80. firewalls:
  81. # disables authentication for assets and the profiler, adapt it according to your needs
  82. dev:
  83. pattern: ^/(_(profiler|wdt)|css|images|js)/
  84. security: false
  85.  
  86. main:
  87. anonymous: ~
  88. provider: user_db_provider
  89. form_login:
  90. login_path: /login
  91. check_path: /login_check
  92. default_target_path: inscritos_index
  93. always_use_default_target_path: true
  94. logout:
  95. path: logout
  96. target: /
  97.  
  98. // BackendBundle/Entity/Users.php
  99.  
  100. namespace BackendBundleEntity;
  101.  
  102. use SymfonyComponentSecurityCoreUserUserInterface;
  103. use DoctrineCommonCollectionsArrayCollection;
  104. use SymfonyComponentValidatorConstraints as Assert;
  105. use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
  106. use SymfonyComponentSecurityCoreValidatorConstraints as SecurityAssert;
  107. use SymfonyComponentSecurityCoreUserAdvancedUserInterface;
  108.  
  109. /**
  110. * Users
  111. * @ORMEntity(repositoryClass="BackendBundleRepositoryUserRepository")
  112. * @UniqueEntity(fields={"email"}, message="¡Este correo ya esta registrado!")
  113. * @UniqueEntity(fields={"nick"}, message="¡Nick ya esta registrado!")
  114. */
  115. class Users implements AdvancedUserInterface, Serializable {
  116.  
  117. /**
  118. * @var integer
  119. */
  120. private $id;
  121.  
  122. /**
  123. * @var string
  124. */
  125. private $role;
  126.  
  127. /**
  128. * @var string
  129. * @AssertEmail(checkMX=true)
  130. */
  131. private $email;
  132.  
  133. /**
  134. * @var string
  135. * @AssertNotBlank(message = "Por favor, escribe el nombre")
  136. */
  137. private $name;
  138.  
  139. /**
  140. * @var string
  141. * @AssertNotBlank(message = "Por favor, escribe los apellidos")
  142. */
  143. private $surname;
  144.  
  145. /**
  146. * @var string
  147. * @AssertNotBlank(message = "No puedes dejar el password vacio")
  148. * @AssertLength(min = 6)
  149. */
  150. private $password;
  151.  
  152. /**
  153. * @var string
  154. */
  155. private $nick;
  156.  
  157.  
  158. /**
  159. * @var boolean
  160. */
  161. private $isActive = false;
  162.  
  163.  
  164. /**
  165. * @var string
  166. */
  167. private $bloqueo = 'no';
  168.  
  169. /**
  170. * @var string
  171. */
  172. private $razonBloqueo;
  173.  
  174.  
  175. /**
  176. * Constructor
  177. */
  178. public function __construct() {
  179. $this->entry = new DoctrineCommonCollectionsArrayCollection();
  180. $this->usuarioValida = new DoctrineCommonCollectionsArrayCollection();
  181. $this->preferenciasUser = new DoctrineCommonCollectionsArrayCollection();
  182.  
  183. // $this->isActive = true; //????? ojo!!! no esta claro si debe estar o no
  184. // may not be needed, see section on salt below
  185. // $this->salt = md5(uniqid('', true));
  186. }
  187.  
  188. //auth
  189. public function getUsername() {
  190. return $this->email;
  191. }
  192.  
  193. public function getSalt() {
  194. return null;
  195. }
  196.  
  197. public function getRoles() {
  198. return array($this->getRole());
  199. }
  200.  
  201. public function eraseCredentials() {
  202.  
  203. }
  204.  
  205. //end auth
  206.  
  207. /**
  208. * Set id
  209. *
  210. * @param integer $id
  211. *
  212. * @return Users
  213. */
  214. public function setId($id) {
  215. $this->id = $id;
  216.  
  217. return $this;
  218. }
  219.  
  220. /**
  221. * Get id
  222. *
  223. * @return integer
  224. */
  225. public function getId() {
  226. return $this->id;
  227. }
  228.  
  229. /**
  230. * Set role
  231. *
  232. * @param string $role
  233. *
  234. * @return Users
  235. */
  236. public function setRole($role) {
  237. $this->role = $role;
  238.  
  239. return $this;
  240. }
  241.  
  242. /**
  243. * Get role
  244. *
  245. * @return string
  246. */
  247. public function getRole() {
  248. return $this->role;
  249. }
  250.  
  251. /**
  252. * Set email
  253. *
  254. * @param string $email
  255. *
  256. * @return Users
  257. */
  258. public function setEmail($email) {
  259. $this->email = $email;
  260.  
  261. return $this;
  262. }
  263.  
  264. /**
  265. * Get email
  266. *
  267. * @return string
  268. */
  269. public function getEmail() {
  270. return $this->email;
  271. }
  272.  
  273. /**
  274. * Set name
  275. *
  276. * @param string $name
  277. *
  278. * @return Users
  279. */
  280. public function setName($name) {
  281. $this->name = $name;
  282.  
  283. return $this;
  284. }
  285.  
  286. /**
  287. * Get name
  288. *
  289. * @return string
  290. */
  291. public function getName() {
  292. return $this->name;
  293. }
  294.  
  295. /**
  296. * Set surname
  297. *
  298. * @param string $surname
  299. *
  300. * @return Users
  301. */
  302. public function setSurname($surname) {
  303. $this->surname = $surname;
  304.  
  305. return $this;
  306. }
  307.  
  308. /**
  309. * Get surname
  310. *
  311. * @return string
  312. */
  313. public function getSurname() {
  314. return $this->surname;
  315. }
  316.  
  317. /**
  318. * Set password
  319. *
  320. * @param string $password
  321. *
  322. * @return Users
  323. */
  324. public function setPassword($password) {
  325. $this->password = $password;
  326.  
  327. return $this;
  328. }
  329.  
  330. /**
  331. * Get password
  332. *
  333. * @return string
  334. */
  335. public function getPassword() {
  336. return $this->password;
  337. }
  338.  
  339. /**
  340. * Set nick
  341. *
  342. * @param string $nick
  343. *
  344. * @return Users
  345. */
  346. public function setNick($nick) {
  347. $this->nick = $nick;
  348.  
  349. return $this;
  350. }
  351.  
  352. /**
  353. * Get nick
  354. *
  355. * @return string
  356. */
  357. public function getNick() {
  358. return $this->nick;
  359. }
  360.  
  361.  
  362. /**
  363. * Set isActive
  364. *
  365. * @param boolean $isActive
  366. *
  367. * @return Users
  368. */
  369. public function setIsActive($isActive) {
  370. $this->isActive = $isActive;
  371.  
  372. return $this;
  373. }
  374.  
  375. /**
  376. * Get isActive
  377. *
  378. * @return boolean
  379. */
  380. public function getIsActive() {
  381. return $this->isActive;
  382. }
  383.  
  384.  
  385. /**
  386. * Set bloqueo
  387. *
  388. * @param string $bloqueo
  389. *
  390. * @return Users
  391. */
  392. public function setBloqueo($bloqueo) {
  393. $this->bloqueo = $bloqueo;
  394.  
  395. return $this;
  396. }
  397.  
  398. /**
  399. * Get bloqueo
  400. *
  401. * @return string
  402. */
  403. public function getBloqueo() {
  404. return $this->bloqueo;
  405. }
  406.  
  407. /**
  408. * Set razonBloqueo
  409. *
  410. * @param string $razonBloqueo
  411. *
  412. * @return Users
  413. */
  414. public function setRazonBloqueo($razonBloqueo) {
  415. $this->razonBloqueo = $razonBloqueo;
  416.  
  417. return $this;
  418. }
  419.  
  420. /**
  421. * Get razonBloqueo
  422. *
  423. * @return string
  424. */
  425. public function getRazonBloqueo() {
  426. return $this->razonBloqueo;
  427. }
  428.  
  429.  
  430.  
  431.  
  432. public function isAccountNonExpired()
  433. {
  434. return true;
  435. }
  436.  
  437. public function isAccountNonLocked()
  438. {
  439. return true;
  440. }
  441.  
  442. public function isCredentialsNonExpired()
  443. {
  444. return true;
  445. }
  446.  
  447. public function isEnabled()
  448. {
  449. return $this->isActive;
  450. }
  451.  
  452.  
  453. /** @see Serializable::serialize() */
  454. public function serialize() {
  455. return serialize(array(
  456. $this->id,
  457. //$this->username,
  458. $this->email,
  459. $this->password,
  460. $this->isActive,
  461. // see section on salt below
  462. // $this->salt,
  463. ));
  464. }
  465.  
  466. /** @see Serializable::unserialize() */
  467.  
  468. public function unserialize($serialized)
  469. {
  470. list (
  471. $this->id,
  472. //$this->username,
  473. $this->email,
  474. $this->password,
  475. $this->isActive,
  476. ) = unserialize($serialized);
  477. }
  478.  
  479. }
Add Comment
Please, Sign In to add comment