问题描述
我正在使用FOSUserBundle用于我的
Restettoken这样做: 我已经检查了数据库 $token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->container->get('security.context')->setToken($token);
推荐答案
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; }
问题描述
I am using FOSUserBundle for my symfony2 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; }