Dinamic Roles in Symfony2

0

Good I have that piece of code, I have the class "Users" and the class "Roles", in this fragment the getRole () method, perfectly gets the user name of the class users, at this point return 'ROLE_' strtoupper ($ this-> user-> getUsername ()); returning perfectly the ROLE_NOMBREDEUSUARIO , but I want to return the role, then I put getRol , and it does not work for me, it returns the id of the users. In the Users class I have the getRol () method

Could someone help me?

namespace AppBundle \ Entity;

use Symfony\Component\Security\Core\Role\RoleInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class Roles implements RoleInterface
{
    private $user;

    public function __construct(UserInterface $user)
    {
        $this->user = $user;
    }

    public function getRole()
    {
        return 'ROLE_' . strtoupper($this->user->getUsername());
    }
}
    
asked by R. Tony 14.10.2017 в 07:42
source

1 answer

0

You will need to configure a little more that entity to work as I think you want ...

use Symfony\Component\Security\Core\Role\RoleInterface;
use Symfony\Component\Security\Core\User\UserInterface;

class Roles implements RoleInterface
{
    private $user;

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

    public function __construct(UserInterface $user)
    {
        $this->user = $user;
    }

    public function getRole()
    {
        return $this->role;
    }
}

Not to mention that your entire class needs to configure the ORM mapping through notations (if applicable) or in the yml file that you have created (in which case I highly recommend that you change to annotations)

    
answered by 14.10.2017 в 23:29