Symfony2 : 检查旧的ROLLE返回true[英] Symfony2 : Checking for old ROLE returns true

本文是小编为大家收集整理的关于Symfony2 : 检查旧的ROLLE返回true的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用FOSUserBundle用于我的

数据库,并且没有角色用户了.如果我注销,然后再次登录,$user->hasDefaultRole()仍然返回true.我在这里没有看到什么?还是这是一种预期的行为?

推荐答案

fosuserBundle始终将默认角色(ROLE_USER)添加到角色列表中,以确保用户至少始终扮演角色,因此无论您做什么,您都无法将其删除.<<<<<<<<<<<<<<<<

fosuserbundle \ model \ user

/**
 * Returns the user roles
 *
 * @return array The roles
 */
public function getRoles()
{
    $roles = $this->roles;

    foreach ($this->getGroups() as $group) {
        $roles = array_merge($roles, $group->getRoles());
    }

    // we need to make sure to have at least one role
    $roles[] = static::ROLE_DEFAULT;

    return array_unique($roles);
}

fosuserbundle \ model \ userInterface

const ROLE_DEFAULT = 'ROLE_USER';

您也永远不会在数据库中找到ROLE_USER,因为它实际上从未添加过.

fosuserbundle \ model \ user

public function addRole($role)
{
    $role = strtoupper($role);
    if ($role === static::ROLE_DEFAULT) {
        return $this;
    }

    if (!in_array($role, $this->roles, true)) {
        $this->roles[] = $role;
    }

    return $this;
}

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

问题描述

I am using FOSUserBundle for my project. Upon registration, I check with the function below if the user has the default role ROLE_USER that FOSUB gives.

 /**
  * Returns true if user has ROLE_USER
  *
  * @return boolean 
  */
 public function hasDefaultRole() {
     return ($this->hasRole('ROLE_USER'));
 }

If this function returns true, I set up a new account registration form and on submit the roles are changed and ROLE_USER is removed.

EDIT :

$user = $this->container->get('security.context')->getToken()->getUser();
...
$userManager = $this->container->get('fos_user.user_manager');
$user->removeRole("ROLE_USER");
$user->setRoles(array("ROLE_TEACHER", "ROLE_TEACHER_BASIC"));
$user->setStatus(1);
$userManager->updateUser($user);
$this->resetToken($user);

restetToken does this :

 $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
 $this->container->get('security.context')->setToken($token);

I have checked the database and there is no role user anymore. If I logout, and login back again, $user->hasDefaultRole() still returns true. What am I not seeing here? Or is this an expected behaviour?

推荐答案

FOSUserBundle always add the default role (ROLE_USER) to the list of roles to ensure that users always have at least on role, so no matter what you do you won't be able to remove it.

FOSUserBundle\Model\User

/**
 * Returns the user roles
 *
 * @return array The roles
 */
public function getRoles()
{
    $roles = $this->roles;

    foreach ($this->getGroups() as $group) {
        $roles = array_merge($roles, $group->getRoles());
    }

    // we need to make sure to have at least one role
    $roles[] = static::ROLE_DEFAULT;

    return array_unique($roles);
}

FOSUserBundle\Model\UserInterface

const ROLE_DEFAULT = 'ROLE_USER';

Also you will never find the ROLE_USER in your database as it never actually adds it.

FOSUserBundle\Model\User

public function addRole($role)
{
    $role = strtoupper($role);
    if ($role === static::ROLE_DEFAULT) {
        return $this;
    }

    if (!in_array($role, $this->roles, true)) {
        $this->roles[] = $role;
    }

    return $this;
}