Ver Mensaje Individual
  #17 (permalink)  
Antiguo 28/09/2012, 08:19
merysmarcano
 
Fecha de Ingreso: septiembre-2012
Mensajes: 19
Antigüedad: 11 años, 7 meses
Puntos: 0
Respuesta: Como obtener los datos de sessión en twig

hola Maycol. Gracias por responder.

A continuación pongo el código de las entidades usuario y roles.

Role
Código PHP:
Ver original
  1. <?php
  2. // symfony/src/Unid/OtcBundle/Entity/Role.php
  3. namespace Unid\OtcBundle\Entity;
  4.  
  5.  
  6. use Symfony\Component\Security\Core\Role\RoleInterface;
  7. use Doctrine\ORM\Mapping as ORM;
  8.  
  9. /**
  10.  * @ORM\Entity
  11.  * @ORM\Table(name="admin_roles")
  12.  */
  13. class Role implements RoleInterface
  14. {
  15.     /**
  16.      * @ORM\Id
  17.      * @ORM\Column(type="integer")
  18.      * @ORM\GeneratedValue(strategy="AUTO")
  19.      */
  20.     protected $id;
  21.  
  22.     /**
  23.      * @ORM\Column(name="nombre", type="string", length="255")
  24.      */
  25.     protected $name;
  26.  
  27.     /**
  28.      * Get id
  29.      *
  30.      * @return integer
  31.      */
  32.     public function getId()
  33.     {
  34.         return $this->id;
  35.     }
  36.  
  37.     /**
  38.      * Set name
  39.      *
  40.      * @param string $name
  41.      */
  42.     public function setName($name)
  43.     {
  44.         $this->name = $name;
  45.     }
  46.  
  47.     /**
  48.      * Get name
  49.      *
  50.      * @return string
  51.      */
  52.     public function getName()
  53.     {
  54.         return $this->name;
  55.     }
  56.  
  57.     public function getRole() {
  58.         return $this->getName();
  59.     }
  60.  
  61.     public function __toString() {
  62.         return $this->getRole();
  63.     }
  64. }

User:

Código PHP:
Ver original
  1. <?php
  2. // proyecto/src/Unid/OtcBundle/Entity/User.php
  3. namespace Unid\OtcBundle\Entity;
  4.  
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Doctrine\ORM\Mapping as ORM;
  7. /**
  8.   * @ORM\Entity
  9.   * @ORM\Table(name="admin_user")
  10.   */
  11. class User implements UserInterface
  12. {
  13.     /**
  14.      * @var integer $id
  15.      *
  16.      * @ORM\Column(name="id", type="integer")
  17.      * @ORM\Id
  18.      * @ORM\GeneratedValue(strategy="AUTO")
  19.      */
  20.     private $id;
  21.  
  22.     /**
  23.     * @ORM\Column(type="string", length="255")
  24.     */
  25.     protected $username;
  26.  
  27.     /**
  28.      * @ORM\Column(name="password", type="string", length="255")
  29.      */
  30.     protected $password;
  31.  
  32.     /**
  33.      * @ORM\Column(name="salt", type="string", length="255")
  34.      */
  35.     protected $salt;
  36.  
  37.     /**
  38.      * se utilizó user_roles para no hacer conflicto al aplicar ->toArray en getRoles()
  39.      * @ORM\ManyToMany(targetEntity="Role")
  40.      * @ORM\JoinTable(name="user_role",
  41.      *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
  42.      *     inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
  43.      * )
  44.      */
  45.     protected $user_roles;
  46.  
  47.     public function __construct()
  48.     {
  49.         $this->user_roles = new \Doctrine\Common\Collections\ArrayCollection();
  50.     }
  51.  
  52.     /**
  53.      * Get id
  54.      *
  55.      * @return integer
  56.      */
  57.     public function getId()
  58.     {
  59.         return $this->id;
  60.     }
  61.  
  62.     /**
  63.      * Set username
  64.      *
  65.      * @param string $username
  66.      */
  67.     public function setUsername($username)
  68.     {
  69.         $this->username = $username;
  70.     }
  71.  
  72.     /**
  73.      * Get username
  74.      *
  75.      * @return string
  76.      */
  77.     public function getUsername()
  78.     {
  79.         return $this->username;
  80.     }
  81.  
  82.     /**
  83.      * Set password
  84.      *
  85.      * @param string $password
  86.      */
  87.     public function setPassword($password)
  88.     {
  89.         $this->password = $password;
  90.     }
  91.  
  92.     /**
  93.      * Get password
  94.      *
  95.      * @return string
  96.      */
  97.     public function getPassword()
  98.     {
  99.         return $this->password;
  100.     }
  101.  
  102.     /**
  103.      * Set salt
  104.      *
  105.      * @param string $salt
  106.      */
  107.     public function setSalt($salt)
  108.     {
  109.         $this->salt = $salt;
  110.     }
  111.  
  112.     /**
  113.      * Get salt
  114.      *
  115.      * @return string
  116.      */
  117.     public function getSalt()
  118.     {
  119.         return $this->salt;
  120.     }
  121.  
  122.     /**
  123.      * Add user_roles
  124.      *
  125.      * @param Unid\OtcBundle\Entity\Role $userRoles
  126.      */
  127.     public function addRole(\Unid\OtcBundle\Entity\Role $userRoles)
  128.     {
  129.         $this->user_roles[] = $userRoles;
  130.     }
  131.  
  132.     public function setUserRoles($roles) {
  133.         $this->user_roles = $roles;
  134.     }
  135.  
  136.     /**
  137.      * Get user_roles
  138.      *
  139.      * @return Doctrine\Common\Collections\Collection
  140.      */
  141.     public function getUserRoles()
  142.     {
  143.         return $this->user_roles;
  144.     }
  145.  
  146.     /**
  147.      * Get roles
  148.      *
  149.      * @return Doctrine\Common\Collections\Collection
  150.      */
  151.     public function getRoles()
  152.     {
  153.         return $this->user_roles->toArray(); //IMPORTANTE: el mecanismo de seguridad de Sf2 requiere ésto como un array
  154.     }
  155.  
  156.     /**
  157.      * Compares this user to another to determine if they are the same.
  158.      *
  159.      * @param UserInterface $user The user
  160.      * @return boolean True if equal, false othwerwise.
  161.      */
  162.     public function equals(UserInterface $user) {
  163.         return md5($this->getUsername()) == md5($user->getUsername());
  164.  
  165.     }
  166.  
  167.     /**
  168.      * Erases the user credentials.
  169.      */
  170.     public function eraseCredentials() {
  171.  
  172.     }
  173. }

Agradezco su ayuda y su tiempo.