Foros del Web » Programando para Internet » PHP » Symfony »

[SOLUCIONADO] Problema con Roles

Estas en el tema de Problema con Roles en el foro de Symfony en Foros del Web. Buenas a todos. Antes de empezar dar gracias a cualquiera que me pueda hechar una mano. He de decir que aun soy novato con symfony2 ...
  #1 (permalink)  
Antiguo 20/10/2014, 06:39
 
Fecha de Ingreso: agosto-2013
Ubicación: Oyón/Alava
Mensajes: 23
Antigüedad: 10 años, 8 meses
Puntos: 0
Problema con Roles

Buenas a todos.

Antes de empezar dar gracias a cualquiera que me pueda hechar una mano.

He de decir que aun soy novato con symfony2 y me esta costando un poquito hacer las cosas.

Bueno expongo el problema.

Estoy intentando implementar ROLES atraves de la base de datos usando doctrine2, el problema viene cuando estoy implementando el RoleInterface y añado el metodo getRole(), Pues al iniciar sesión me devuelve un error.

He aquí el error.
Código:
FatalErrorException: Error: Call to a member function getRole() on a non-object in /home/DOMAINS/www.wolfrc.dev/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Role/RoleHierarchy.php line 43
Ahora paso a mostrar como estan las entidad y sus relaciones


Entidad Usuario:
Código PHP:
namespace WolfrcUsuarioBundleEntity;

use 
DoctrineORMMapping as ORM;
use 
SymfonyComponentSecurityCoreUserUserInterface;
use 
DoctrineCommonCollectionsArrayCollection;

/**
 * Usuario
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Wolfrc\UsuarioBundle\Entity\UsuarioRepository")
 */
class Usuario implements UserInterface
{
    
/**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    
private $id;

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

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

    
/**
     * @var string
     *
     * @ORM\Column(name="dni", type="string", length=9)
     */
    
private $dni;

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

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

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

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

    
/**
     * @var string
     *
     * @ORM\Column(name="ciudad", type="string", length=255)
     */
    
private $ciudad;
    
    
/**
     * @ORM\ManyToMany(targetEntity="Wolfrc\UsuarioBundle\Entity\Rol", inversedBy="usuarios")
     */
    
private $roles;

//    function equals(UserInterfaces $usuario)
//    {
//        return $this->$this->getEmail() == $usuario->getEmail();
//    }
    
    
public function __construct() {
        
$this->roles = new ArrayCollection();
    }
    
    
    function 
eraseCredentials()
    {
        
    }
    
    function 
getRoles()
    {
        return 
$this->roles->toArray();
    }
    
    function 
getUsername()
    {
        return 
$this->getEmail();
    }

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

    
/**
     * Set nombre
     *
     * @param string $nombre
     * @return Usuario
     */
    
public function setNombre($nombre)
    {
        
$this->nombre $nombre;

        return 
$this;
    }

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

    
/**
     * Set apellidos
     *
     * @param string $apellidos
     * @return Usuario
     */
    
public function setApellidos($apellidos)
    {
        
$this->apellidos $apellidos;

        return 
$this;
    }

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

    
/**
     * Set dni
     *
     * @param string $dni
     * @return Usuario
     */
    
public function setDni($dni)
    {
        
$this->dni $dni;

        return 
$this;
    }

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

    
/**
     * Set password
     *
     * @param string $password
     * @return Usuario
     */
    
public function setPassword($password)
    {
        
$this->password $password;

        return 
$this;
    }

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

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

        return 
$this;
    }

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

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

        return 
$this;
    }

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

    
/**
     * Set direccion
     *
     * @param string $direccion
     * @return Usuario
     */
    
public function setDireccion($direccion)
    {
        
$this->direccion $direccion;

        return 
$this;
    }

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

    
/**
     * Set ciudad
     *
     * @param string $ciudad
     * @return Usuario
     */
    
public function setCiudad($ciudad)
    {
        
$this->ciudad $ciudad;

        return 
$this;
    }

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

    
/**
     * Add roles
     *
     * @param \Wolfrc\UsuarioBundle\Entity\Rol $roles
     * @return Usuario
     */
    
public function addRole(WolfrcUsuarioBundleEntityRol $roles)
    {
        
$this->roles[] = $roles;

        return 
$this;
    }

    
/**
     * Remove roles
     *
     * @param \Wolfrc\UsuarioBundle\Entity\Rol $roles
     */
    
public function removeRole(WolfrcUsuarioBundleEntityRol $roles)
    {
        
$this->roles->removeElement($roles);
    }

Entidad Rol:
Código PHP:
namespace WolfrcUsuarioBundleEntity;

use 
SymfonyComponentSecurityCoreRoleRoleInterface;
use 
DoctrineORMMapping as ORM;

/**
 * Rol
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Wolfrc\UsuarioBundle\Entity\RolRepository")
 */
class Rol implements RoleInterface
{
    
/**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    
private $id;

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

    
/**
     *
     * @ORM\ManyToMany(targetEntity="Wolfrc\UsuarioBundle\Entity\Usuario", mappedBy="roles")
     */
    
private $usuarios;
    
    
    public function 
__construct() {
        
$this->usuarios = new DoctrineCommonCollectionsArrayCollection();
    }
    
    
/**
     * Get id
     *
     * @return integer 
     */
    
public function getId()
    {
        return 
$this->id;
    }

    
/**
     * Set nombre
     *
     * @param string $nombre
     * @return Rol
     */
    
public function setNombre($nombre)
    {
        
$this->nombre $nombre;

        return 
$this;
    }

    
/**
     * Get nombre
     *
     * @return string 
     */
    
public function getNombre()
    {
        return 
$this->nombre;
    }
    
    public function 
getRole(){
        return 
$this->getNombre();
    }

    
/**
     * Add usuarios
     *
     * @param \Wolfrc\UsuarioBundle\Entity\Usuario $usuarios
     * @return Rol
     */
    
public function addUsuario(WolfrcUsuarioBundleEntityUsuario $usuarios)
    {
        
$this->usuarios[] = $usuarios;

        return 
$this;
    }

    
/**
     * Remove usuarios
     *
     * @param \Wolfrc\UsuarioBundle\Entity\Usuario $usuarios
     */
    
public function removeUsuario(WolfrcUsuarioBundleEntityUsuario $usuarios)
    {
        
$this->usuarios->removeElement($usuarios);
    }

    
/**
     * Get usuarios
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    
public function getUsuarios()
    {
        return 
$this->usuarios;
    }

Me gustaría saber porque me ha salido el error puesto que he estado investigando y no me ha dejado nada claro.

Decir que el error solamente lo lanza estando logueado.
La version de symfony es la 2.5.5

Gracias a todos los que me puedan ayudar.
  #2 (permalink)  
Antiguo 20/10/2014, 13:26
Avatar de hhs
hhs
Colaborador
 
Fecha de Ingreso: junio-2013
Ubicación: México
Mensajes: 2.995
Antigüedad: 10 años, 10 meses
Puntos: 379
Respuesta: Problema con Roles

La clase Usuario también tiene que implementar la interfaz Serializable. ve el ejemplo en la documentación
__________________
Saludos
About me
Laraveles
A class should have only one reason to change.
  #3 (permalink)  
Antiguo 21/10/2014, 06:38
Avatar de GeekGirl  
Fecha de Ingreso: julio-2014
Mensajes: 423
Antigüedad: 9 años, 9 meses
Puntos: 44
Respuesta: Problema con Roles

Como dice hhs, deberías añadir la implementación para serializar:

Código PHP:
<?php
    
class Usuario implements UserInterfaceSerializable 
?>
Y crear los métodos serialize y unserialize dentro de tu entidad. Por ejemplo:

Código PHP:
<?php
    
public function serialize()
    {
        return 
serialize(array(
            
$this->id,
        ));
    }

    public function 
unserialize($serialized)
    {
        list (
            
$this->id,
            ) = 
unserialize($serialized);
    }
?>
Con eso debería funcionar.
Saludos :)
  #4 (permalink)  
Antiguo 23/10/2014, 04:11
 
Fecha de Ingreso: agosto-2013
Ubicación: Oyón/Alava
Mensajes: 23
Antigüedad: 10 años, 8 meses
Puntos: 0
Respuesta: Problema con Roles

Gracias a los dos me ha servido para solucionar el problema.

Etiquetas: roles
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 23:09.