Fosuser捆绑包 - 为用户设置角色

时间:2016-12-09 20:42:44

标签: symfony fosuserbundle

我想为用户设置角色。我使用下面的代码:

    $user = this->getDoctrine()->getRepository('BlogBundle:User')->findOneById('11');
    $em = $this->getDoctrine()->getManager();
    $t=array('ROLE_ADMIN');
    $user->setRoles($t);
    $em->persist($user);
    $em->flush();
    return new Response('okk');

当我设置'ROLE_ADMIN'时一切正常,但是当我设置'ROLE_USER'时,数据库单元格为空。为什么呢?

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

users表中的roles列应该有

ROLE_USER

仅适用于角色为if ($this->get('security.context')->isGranted('ROLE_USER')) { // the user has the ROLE_USER role }

的用户

程序化检查方式。

检查用户是否具有特定角色(包含ROLE_USER)

$user->getRoles()

还有

ROLE_USER

另一种方法来仔细检查使用是否真的有php app/console fos:user:promote

ROLE_USER

出现提示时,输入用户名,然后输入import java.time.LocalDate val df = Seq(("05:00"), ("22:30")).toDF("time") df.show val todaydate = LocalDate.now().toString() val df2 = df.select(concat(lit(todaydate+ " "),df.col("time"))).toDF("datetime"); df2.show

它应该说明

  

用户" example@example.com"已经有了#34; ROLE_USER"作用。

答案 1 :(得分:0)

在FOS用户捆绑包中,所有用户都具有ROLE_USER角色。添加/保存此角色将是多余的。

如果您要在FOSUserBundle的代码库中查找该角色,则该角色在ROLE_DEFAULT类中的别名为UserInterface