无法使用数据库为角色分配角色(角色数组为空)

时间:2014-04-30 07:59:27

标签: php arrays database symfony doctrine-orm

我没有使用FOS UserBundle并尝试使用实体类实现安全性(防火墙)。 我已经按照symfony2主网站中的步骤(从数据库加载用户)。但是当我使用我的用户名和密码登录并尝试打印分配给他们的角色时,它显示为空

[roles:Acme\UserBundle\Entity\User:private] =>
        Doctrine\Common\Collections\ArrayCollection Object (
        [_elements:Doctrine\Common\Collections\ArrayCollection:private] =>
             Array ( ) ) 

我的用户实体类是

<?php
namespace Acme\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

use Acme\UserBundle\Entity\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Acme\UserBundle\Entity\Role;
/**
* Acme\Bundle\UserRegistrationBundle\Entity\User
*
* @ORM\Table(name="acme_users")
* @ORM\Entity(repositoryClass="Acme\UserBundle\Entity\UserRepository")
*/

class User implements UserInterface, \Serializable
{
/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @ORM\Column(type="string", length=25, unique=true)
 */
private $fname;

 /**
 * @ORM\Column(type="string", length=25, unique=true)
 */
private $lname;
/**
 * @ORM\Column(type="string", length=60, unique=true)
 */
private $email;
    /**
 * @ORM\Column(type="string", length=64)
 */
private $password;
    /**
 * @ORM\Column(type="string", length=20)
 */
private $gender;
/**
 * @ORM\Column(type="string", length=25, unique=true)
 */
private $profession;
    /**
 * @ORM\Column(type="date")
 */
private $date_of_birth;
/**
 * @ORM\Column(type="integer")
*/
private $country_id;
    /**
 * @ORM\Column(type="integer")
*/
private $state_id;
        /**
 * @ORM\Column(type="integer")
*/
private $city_id;
    /**
 * @ORM\Column(type="string", length=20)
 */
private $phone_number;
   /**
 * @ORM\Column(name="status", type="boolean")
 */
private $status;
        /**
 * @ORM\Column(type="integer")
*/
private $is_lock;
        /**
 * @ORM\Column(type="integer")
*/
private $failed_attempt;

/**
 * @ORM\Column(type="string", length=32)
 */

private $salt;

 /**
  * @ORM\ManyToMany(targetEntity="Role",inversedBy="users")
  * @var ArrayCollection $roles;
   */
private $roles;


public function __construct()
{
    $this->status = true;
    $this->salt = md5(uniqid(null, true));

    $this->roles = new ArrayCollection();

}
/**
  * Get roles (array)
  *
  * @return array
  */

public function getRoles()
{ 
    return $this->roles->toArray();

}


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

/**
 * Set fname
 *
 * @param string $fname
 * @return User
 */
public function setFname($fname)
{
    $this->fname = $fname;

    return $this;
}

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

/**
 * Set lname
 *
 * @param string $lname
 * @return User
 */
public function setLname($lname)
{
    $this->lname = $lname;

    return $this;
}

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

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

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set gender
 *
 * @param string $gender
 * @return User
 */
public function setGender($gender)
{
    $this->gender = $gender;

    return $this;
}

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

/**
 * Set profession
 *
 * @param string $profession
 * @return User
 */
public function setProfession($profession)
{
    $this->profession = $profession;

    return $this;
}

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

/**
 * Set date_of_birth
 *
 * @param \DateTime $dateOfBirth
 * @return User
 */
public function setDateOfBirth($dateOfBirth)
{
    $this->date_of_birth = $dateOfBirth;

    return $this;
}

/**
 * Get date_of_birth
 *
 * @return \DateTime 
 */
public function getDateOfBirth()
{
    return $this->date_of_birth;
}

/**
 * Set country_id
 *
 * @param integer $countryId
 * @return User
 */
public function setCountryId($countryId)
{
    $this->country_id = $countryId;

    return $this;
}

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

/**
 * Set state_id
 *
 * @param integer $stateId
 * @return User
 */
public function setStateId($stateId)
{
    $this->state_id = $stateId;

    return $this;
}

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

/**
 * Set city_id
 *
 * @param integer $cityId
 * @return User
 */
public function setCityId($cityId)
{
    $this->city_id = $cityId;

    return $this;
}

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

/**
 * Set phone_number
 *
 * @param string $phoneNumber
 * @return User
 */
public function setPhoneNumber($phoneNumber)
{
    $this->phone_number = $phoneNumber;

    return $this;
}

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

/**
 * Set status
 *
 * @param boolean $status
 * @return User
 */
public function setStatus($status)
{
    $this->status = $status;

    return $this;
}

/**
 * Get status
 *
 * @return boolean 
 */
public function getStatus()
{
    return $this->status;
}

/**
 * Set is_lock
 *
 * @param integer $isLock
 * @return User
 */
public function setIsLock($isLock)
{
    $this->is_lock = $isLock;

    return $this;
}

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

/**
 * Set failed_attempt
 *
 * @param integer $failedAttempt
 * @return User
 */
public function setFailedAttempt($failedAttempt)
{
    $this->failed_attempt = $failedAttempt;

    return $this;
}

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

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

    return $this;
}

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

/**
 * Add roles
 *
 * @param \Autograph\UserBundle\Entity\Role $roles
 * @return User
 */
public function addRole(\Autograph\UserBundle\Entity\Role $roles)
{
    $this->roles[] = $roles;

    return $this;
}

/**
 * Remove roles
 *
 * @param \Autograph\UserBundle\Entity\Role $roles
 */
public function removeRole(\Autograph\UserBundle\Entity\Role $roles)
{
    $this->roles->removeElement($roles);
}

public function eraseCredentials() {

}

public function getUsername() {

}

public function serialize() {


}

public function unserialize($serialized) {


}
}

我的角色实体类是

<?php

// src/Acme/UserBundle/Entity/Role.php
namespace Acme\UserBundle\Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="acme_roles")
 * @ORM\Entity()
 */
 class Role implements RoleInterface
{
/**
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

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

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

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
 * 
 * @var ArrayCollection $users
 */
private $users;


public function __construct()
{
    $this->users = new ArrayCollection();

}

/**
 * @see RoleInterface
 */
public function getRole()
{
  return $this->role;
 }
/**
 * Set role
 *
 * @param string $role
 * @return Role
 */
public function setRole($role)
{
    $this->role = $role;

    return $this;
}

// ... getters and setters for each property

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

/**
 * Set name
 *
 * @param string $name
 * @return Role
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

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


/**
 * Add users
 *
 * @param \Autograph\UserBundle\Entity\User $users
 * @return Role
 */
public function addUser(\Autograph\UserBundle\Entity\User $users)
{
    $this->users[] = $users;

    return $this;
}

/**
 * Remove users
 *
 * @param \Autograph\UserBundle\Entity\User $users
 */
public function removeUser(\Autograph\UserBundle\Entity\User $users)
{
    $this->users->removeElement($users);
}

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

security.yml是

 security:
     encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        Acme\UserBundle\Entity\User: sha512

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

providers:
    in_memory:
        memory:
            users:
                user:  { password: userpass, roles: [ 'ROLE_USER' ] }
                admin: { password: adminpass, roles: [ 'ROLE_ADMIN' ] }
    our_database_users:
         entity: { class: AcmeUserBundle:User, property:email}

firewalls:
    dev:
        pattern:  ^/(_(profiler|wdt)|css|images|js)/
        security: false

    login:
        pattern:  ^/demo/secured/login$
        security: false

    secured_area:
        pattern:    ^/
        form_login:
            check_path: _security_check
            login_path: _demo_login
        logout:
            path:   _demo_logout
            target: _demo
        anonymous: ~
        #http_basic:
        #    realm: "Secured Demo Area"

access_control:
    #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }

请告诉我这方面的错误。为什么不分配角色......

0 个答案:

没有答案