Assign a Role with FosUserBundle

冷暖自知 提交于 2019-12-03 13:10:11

问题


I'm really new to Symfony, I'm trying to register a ROLE to a User with FosUserBundle but I can't manage how to do it. Actually I integrated also PUGXMultiUserBundle in order to have two different form for two different roles. Can anyone help me?

Thanks in advance

--UPDATE--

I report my code in order to explain clearly. I create this files with the guide of PUGXMultiUserBundle

This is my entity:

//C:\BitNami\wampstack-5.4.23-0\frameworks\symfony\src\Acme\ManagementBundle\Entity\UserGroundStation.php
<?php

namespace Acme\ManagementBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use PUGX\MultiUserBundle\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="user_GroundStation")
 * @UniqueEntity(fields = "username", targetClass = "Acme\ManagementBundle\Entity\User", message="fos_user.username.already_used")
 * @UniqueEntity(fields = "email", targetClass = "Acme\ManagementBundle\Entity\User", message="fos_user.email.already_used")
 */
class UserGroundStation extends User
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

And this is the controller

C:\BitNami\wampstack-5.4.23-0\frameworks\symfony\src\Acme\ManagementBundle\ControllerRegistrationController.php
<?php

namespace Acme\ManagementBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class RegistrationController extends Controller
{
    public function registerUserGroundStationAction()
    {
        return $this->container
                    ->get('pugx_multi_user.registration_manager')
                    ->register('Acme\ManagementBundle\Entity\UserGroundStation');
    }
}

Sorry for the dumb question, but it's my first project and I feel a bit misplace.

--UPDATE--SOLVED--

I found the solution here Sorry for the redundance of the question, I didn't find it by Google.


回答1:


A way to assign a role, for example ROLE_ADMIN, to a user goes like this.

//Get the enity manager
$em = $this->getDoctrine()->getManager();
//Get the user with name admin
$user= $em->getRepository("<your vendor>\<your bundle>\Entity\User")->findBy(Array("name" => "admin"));
//Set the admin role
$user->addRole("ROLE_ADMIN");
//Save it to the database
$em->persist($user);
$em->flush();

You can also set the role in the constructor of the User Entity class, which goes like this:

public function __construct()
{
    parent::__construct();
    $this->addRole("ROLE_ADMIN");
}

Remember that setting the role in the constructor means that's it is not saved in the database(unless your persist and flush) and that it is applied to every user.
If you have some additional questions please let me know.

PS: if you use the Sonata Admin bundle the user's role can be set with a form located in the Admin section




回答2:


This can be one with the console as well:

Symfony 2

php app/console fos:user:promote theusername ROLE_ADMIN

Symfony 3

php bin/console fos:user:promote theusername ROLE_ADMIN

You can also activate/deactivate and various other things with the CLI.

http://symfony.com/doc/current/bundles/FOSUserBundle/command_line_tools.html



来源:https://stackoverflow.com/questions/20659377/assign-a-role-with-fosuserbundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!