用FosUserBundle指定一个角色[英] Assign a Role with FosUserBundle

本文是小编为大家收集整理的关于用FosUserBundle指定一个角色的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我真的是Symfony的新手,我正在尝试用FosuserBundle注册角色,但我无法管理如何做. 实际上,我还集成了pugxmultiuserbundle,以使两种不同的角色具有两种不同的形式. 谁能帮我吗?

预先感谢

- 更新 -

我报告了我的代码,以便清楚地解释.我用 pugxmultiuserbundle

这是我的实体:

//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;
}

这是控制器

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');
    }
}

对不起这个愚蠢的问题,但这是我的第一个项目,我感到有些失误.

- 更新 - 已解决 -

我找到了解决方案在这里 抱歉,这个问题的冗余,我没有找到Google.

推荐答案

一种将角色(例如角色_ADMIN)分配给用户的方法.

//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();

您还可以在用户实体类的构造函数中设置角色,例如:

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

请记住,在构造函数中设置角色意味着它是不是保存在数据库中的(除非您的持久和齐平)并将其应用于每个用户.
如果您还有一些其他问题,请告诉我.

PS:如果您使用Sonata Admin Bundle,则可以在Admin部分中设置用户的角色

其他推荐答案

这也可以是控制台的:

Symfony 2

php app/console fos:user:promote theusername ROLE_ADMIN

Symfony 3

php bin/console fos:user:promote theusername ROLE_ADMIN

您还可以使用CLI激活/停用和其他各种东西.

command_line_tools.html

本文地址:https://www.itbaoku.cn/post/2090755.html

问题描述

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.

推荐答案

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

其他推荐答案

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