Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace BackendBundleRepository;
- use BackendBundleEntityUsers;
- use SymfonyBridgeDoctrineSecurityUserUserLoaderInterface;
- use SymfonyComponentSecurityCoreUserUserInterface;
- use SymfonyComponentSecurityCoreExceptionUsernameNotFoundException;
- use DoctrineORMEntityRepository;
- class UserRepository extends EntityRepository implements UserLoaderInterface {
- public function loadUserByUsername($username) {
- return $this->createQueryBuilder('u')
- //$user = $this->createQueryBuilder('u')
- ->where('u.nick = :nick OR u.email = :email')
- ->setParameter('nick', $username)
- ->setParameter('email', $username)
- ->getQuery()
- ->getOneOrNullResult();
- // if (null === $user) {
- // $message = sprintf(
- // 'Unable to find an active admin AppBundle:User object identified by "%s".', $username
- // );
- // throw new UsernameNotFoundException($message);
- // }
- //
- // return $user;
- }
- }
- use BackendBundleEntityUsers;
- use BackendBundleEntityUserRepository;
- use SymfonyBundleFrameworkBundleControllerController;
- use SymfonyComponentHttpFoundationRequest;
- use SymfonyComponentHttpFoundationSessionSession;
- use BackendBundleRepositoryUserRepository;
- /**
- * User controller.
- *
- */
- class LoginController extends Controller {
- private $session;
- public function __construct() {
- $this->session = new Session();
- }
- public function loginAction(Request $request) { //autentificamos usuarios usando los metodos de symfony de autentificación
- $authenticationUtils = $this->get("security.authentication_utils");
- $error = $authenticationUtils->getLastAuthenticationError();
- $lastUsername = $authenticationUtils->getLastUsername();
- return $this->render('AppBundle:User:login.html.twig', array(
- "error" => $error,
- "last_username" => $lastUsername
- ));
- }
- Security.yml
- security:
- encoders:
- BackendBundleEntityUsers:
- algorithm: bcrypt
- cost: 6
- # http://symfony.com/doc/current/security.html#b-configuring-how-users-are- loaded
- providers:
- user_db_provider:
- entity:
- class: BackendBundle:Users
- #property: email
- firewalls:
- # disables authentication for assets and the profiler, adapt it according to your needs
- dev:
- pattern: ^/(_(profiler|wdt)|css|images|js)/
- security: false
- main:
- anonymous: ~
- provider: user_db_provider
- form_login:
- login_path: /login
- check_path: /login_check
- default_target_path: inscritos_index
- always_use_default_target_path: true
- logout:
- path: logout
- target: /
- // BackendBundle/Entity/Users.php
- namespace BackendBundleEntity;
- use SymfonyComponentSecurityCoreUserUserInterface;
- use DoctrineCommonCollectionsArrayCollection;
- use SymfonyComponentValidatorConstraints as Assert;
- use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
- use SymfonyComponentSecurityCoreValidatorConstraints as SecurityAssert;
- use SymfonyComponentSecurityCoreUserAdvancedUserInterface;
- /**
- * Users
- * @ORMEntity(repositoryClass="BackendBundleRepositoryUserRepository")
- * @UniqueEntity(fields={"email"}, message="¡Este correo ya esta registrado!")
- * @UniqueEntity(fields={"nick"}, message="¡Nick ya esta registrado!")
- */
- class Users implements AdvancedUserInterface, Serializable {
- /**
- * @var integer
- */
- private $id;
- /**
- * @var string
- */
- private $role;
- /**
- * @var string
- * @AssertEmail(checkMX=true)
- */
- private $email;
- /**
- * @var string
- * @AssertNotBlank(message = "Por favor, escribe el nombre")
- */
- private $name;
- /**
- * @var string
- * @AssertNotBlank(message = "Por favor, escribe los apellidos")
- */
- private $surname;
- /**
- * @var string
- * @AssertNotBlank(message = "No puedes dejar el password vacio")
- * @AssertLength(min = 6)
- */
- private $password;
- /**
- * @var string
- */
- private $nick;
- /**
- * @var boolean
- */
- private $isActive = false;
- /**
- * @var string
- */
- private $bloqueo = 'no';
- /**
- * @var string
- */
- private $razonBloqueo;
- /**
- * Constructor
- */
- public function __construct() {
- $this->entry = new DoctrineCommonCollectionsArrayCollection();
- $this->usuarioValida = new DoctrineCommonCollectionsArrayCollection();
- $this->preferenciasUser = new DoctrineCommonCollectionsArrayCollection();
- // $this->isActive = true; //????? ojo!!! no esta claro si debe estar o no
- // may not be needed, see section on salt below
- // $this->salt = md5(uniqid('', true));
- }
- //auth
- public function getUsername() {
- return $this->email;
- }
- public function getSalt() {
- return null;
- }
- public function getRoles() {
- return array($this->getRole());
- }
- public function eraseCredentials() {
- }
- //end auth
- /**
- * Set id
- *
- * @param integer $id
- *
- * @return Users
- */
- public function setId($id) {
- $this->id = $id;
- return $this;
- }
- /**
- * Get id
- *
- * @return integer
- */
- public function getId() {
- return $this->id;
- }
- /**
- * Set role
- *
- * @param string $role
- *
- * @return Users
- */
- public function setRole($role) {
- $this->role = $role;
- return $this;
- }
- /**
- * Get role
- *
- * @return string
- */
- public function getRole() {
- return $this->role;
- }
- /**
- * Set email
- *
- * @param string $email
- *
- * @return Users
- */
- public function setEmail($email) {
- $this->email = $email;
- return $this;
- }
- /**
- * Get email
- *
- * @return string
- */
- public function getEmail() {
- return $this->email;
- }
- /**
- * Set name
- *
- * @param string $name
- *
- * @return Users
- */
- public function setName($name) {
- $this->name = $name;
- return $this;
- }
- /**
- * Get name
- *
- * @return string
- */
- public function getName() {
- return $this->name;
- }
- /**
- * Set surname
- *
- * @param string $surname
- *
- * @return Users
- */
- public function setSurname($surname) {
- $this->surname = $surname;
- return $this;
- }
- /**
- * Get surname
- *
- * @return string
- */
- public function getSurname() {
- return $this->surname;
- }
- /**
- * Set password
- *
- * @param string $password
- *
- * @return Users
- */
- public function setPassword($password) {
- $this->password = $password;
- return $this;
- }
- /**
- * Get password
- *
- * @return string
- */
- public function getPassword() {
- return $this->password;
- }
- /**
- * Set nick
- *
- * @param string $nick
- *
- * @return Users
- */
- public function setNick($nick) {
- $this->nick = $nick;
- return $this;
- }
- /**
- * Get nick
- *
- * @return string
- */
- public function getNick() {
- return $this->nick;
- }
- /**
- * Set isActive
- *
- * @param boolean $isActive
- *
- * @return Users
- */
- public function setIsActive($isActive) {
- $this->isActive = $isActive;
- return $this;
- }
- /**
- * Get isActive
- *
- * @return boolean
- */
- public function getIsActive() {
- return $this->isActive;
- }
- /**
- * Set bloqueo
- *
- * @param string $bloqueo
- *
- * @return Users
- */
- public function setBloqueo($bloqueo) {
- $this->bloqueo = $bloqueo;
- return $this;
- }
- /**
- * Get bloqueo
- *
- * @return string
- */
- public function getBloqueo() {
- return $this->bloqueo;
- }
- /**
- * Set razonBloqueo
- *
- * @param string $razonBloqueo
- *
- * @return Users
- */
- public function setRazonBloqueo($razonBloqueo) {
- $this->razonBloqueo = $razonBloqueo;
- return $this;
- }
- /**
- * Get razonBloqueo
- *
- * @return string
- */
- public function getRazonBloqueo() {
- return $this->razonBloqueo;
- }
- public function isAccountNonExpired()
- {
- return true;
- }
- public function isAccountNonLocked()
- {
- return true;
- }
- public function isCredentialsNonExpired()
- {
- return true;
- }
- public function isEnabled()
- {
- return $this->isActive;
- }
- /** @see Serializable::serialize() */
- public function serialize() {
- return serialize(array(
- $this->id,
- //$this->username,
- $this->email,
- $this->password,
- $this->isActive,
- // see section on salt below
- // $this->salt,
- ));
- }
- /** @see Serializable::unserialize() */
- public function unserialize($serialized)
- {
- list (
- $this->id,
- //$this->username,
- $this->email,
- $this->password,
- $this->isActive,
- ) = unserialize($serialized);
- }
- }
Add Comment
Please, Sign In to add comment