Search code examples
phpsymfonydoctrine-ormuser-roles

Symfony2 - a User references a roles entity by id but i can't retrieve the user rol when i log in


This is my user entity

<?php

namespace Monse\UsuariosBundle\Entity;

use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;

/**
 * Monse\UsuariosBundle\Entity\Usuario
 *
 * @ORM\Table(name="users")
 * @ORM\Entity
 */
class Usuario implements UserInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /** 
 * @ORM\OneToOne(targetEntity="Monse\ClientesBundle\Entity\Cliente")
 * @ORM\JoinColumn(name="cliente_id", referencedColumnName="id",nullable=true)
*/
    protected $cliente;

    /**
     * @var string $usuario
     *
     * @ORM\Column(name="usuario", type="string", length=255, unique=true)
     */
    protected $usuario;

    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="string", length=255, unique=true)
     */
    protected $email;

    /**
     * @var string $password
     *
     * @ORM\Column(name="password", type="string", length=255)
     */
    protected $password;

    /**
     * @var string $salt
     *
     * @ORM\Column(name="salt", type="string", length=255)
     */
    protected $salt;

    /**
     * @var date $ultimo_ingreso
     *
     * @ORM\Column(name="ultimo_ingreso", type="date")
     */
    protected $ultimo_ingreso;

    /**
     * @var date $fecha_alta
     *
     * @ORM\Column(name="fecha_alta", type="date")
     */
    protected $fecha_alta;

     /**
* @ORM\ManyToOne(targetEntity="Monse\UsuariosBundle\Entity\Rol")
* @ORM\JoinColumn(name="rol", referencedColumnName="id",nullable=false)
*/
    protected $rol;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

public function setCliente(\Monse\ClientesBundle\Entity\Cliente $cliente)
    {
        $this->cliente = $cliente;
    }

    /**
     * Get cliente
     *
     * @return integer 
     */
    public function getCliente()
    {
        return $this->cliente;
    }

    /**
     * Set usuario
     *
     * @param string $usuario
     */
    public function setUsuario($usuario)
    {
        $this->usuario = $usuario;
    }

    /**
     * Get usuario
     *
     * @return string 
     */
    public function getUsuario()
    {
        return $this->usuario;
    }

    /**
     * Set email
     *
     * @param string $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * Get email
     *
     * @return string 
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Set contrasena
     *
     * @param string $contrasena
     */
    public function setPassword($password)
    {
        $this->password = $password;
    }

    /**
     * Get contrasena
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Set salt
     *
     * @param string $salt
     */
    public function setSalt($salt)
    {
        $this->salt = $salt;
    }

    /**
     * Get salt
     *
     * @return string 
     */
    public function getSalt()
    {
        return $this->salt;
    }

    /**
     * Set ultimo_ingreso
     *
     * @param date $ultimoIngreso
     */
    public function setUltimoIngreso($ultimoIngreso)
    {
        $this->ultimo_ingreso = $ultimoIngreso;
    }

    /**
     * Get ultimo_ingreso
     *
     * @return date 
     */
    public function getUltimoIngreso()
    {
        return $this->ultimo_ingreso;
    }

    /**
     * Set fecha_alta
     *
     * @param date $fechaAlta
     */
    public function setFechaAlta($fechaAlta)
    {
        $this->fecha_alta = $fechaAlta;
    }

    /**
     * Get fecha_alta
     *
     * @return date 
     */
    public function getFechaAlta()
    {
        return $this->fecha_alta;
    }

    /**
     * Set rol
     *
     * @param integer $rol
     */
    public function setRol(\Monse\UsuariosBundle\Entity\Rol $rol)
    {
        $this->rol = $rol;
    }

    /**
     * Get rol
     *
     * @return integer 
     */
    public function getRol()
    {
        return $this->rol;
    }

    public function __construct()
    {
        $this->fecha_alta = new \DateTime();
    }

    function equals(\Symfony\Component\Security\Core\User\UserInterface $usuario)
    {
        return $this->getUsuario() == $usuario->getUsername();
    }

    function eraseCredentials()
    {
    }

    function getRoles()
    {
    }

    function getUsername()
    {
        return $this->getUsuario();
    }
}

and this is my roles entity

    <?php

namespace Monse\UsuariosBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\ORM\Mapping as ORM;
/**
 * Monse\UsuariosBundle\Entity\Rol
 *
 * @ORM\Table(name="roles")
 * @ORM\Entity
 */
class Rol implements RoleInterface
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var string $role
     *
     * @ORM\Column(name="rol", type="string", length=255)
     */
    protected $role;

    /**
     * @var string $denominacion
     *
     * @ORM\Column(name="denominacion", type="string", length=255)
     */
    protected $denominacion;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set rol
     *
     * @param string $rol
     */
    public function setRole($role)
    {
        $this->role = $role;
    }

    /**
     * Get rol
     *
     * @return string 
     */
    public function getRole()
    {
        return $this->role;
    }
     public function setDenominacion($denominacion)
    {
        $this->denominacion = $denominacion;
    }

    /**
     * Get denominacion
     *
     * @return string 
     */
    public function getDenominacion()
    {
        return $this->denominacion;
    }

    public function __toString() {
        return $this->denominacion;
    }
}

Please HELP, i don't know what i'm doing wrong?

I need to administrate ROLES, that's why i have the entity.

In the Rol entity

rol has ROLE_ADMIN, ROLE_USER... denominacion is Super Admin, Admin, User, etc... I need to retreive ROL


Solution

  • Function getRoles() in Usuario should return an array of role names. Why yours is empty?

    public function getRoles()
    {
        return $this->roles->map(function($r) { return $r->getRole(); })
            ->toArray();
    }
    

    Usuario has a many to one relation with role. Are you sure this is correct? The above example assumes that you have a collection of roles.