使用FosUserBundle分配角色 [英] Assign a Role with FosUserBundle

查看:98
本文介绍了使用FosUserBundle分配角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的是Symfony的新手,我正在尝试使用FosUserBundle向用户注册ROLE,但我无法管理该操作. 实际上,我还集成了PUGXMultiUserBundle,以便针对两个不同的角色使用两种不同的形式. 谁能帮我吗?

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?

预先感谢

-更新-

我报告我的代码是为了清楚说明.我使用 PUGXMultiUserBundle

I report my code in order to explain clearly. I create this files with the guide of 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');
    }
}

很抱歉这个愚蠢的问题,但这是我的第一个项目,我感到有些不对劲.

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

-更新-已解决-

我在此处找到了解决方案 抱歉,这个问题很冗长,我没有被Google找到.

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

推荐答案

将角色(例如ROLE_ADMIN)分配给用户的方法是这样的.

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

您还可以在User Entity类的构造函数中设置角色,如下所示:

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

请记住,在构造函数中设置角色意味着将其保存在数据库中(除非您进行持久化和刷新),并且该角色将应用于每个用户.
如果您还有其他问题,请通知我.

PS:如果您使用Sonata 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

这篇关于使用FosUserBundle分配角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆