在类 vendor\friendsofsymfony\user-bundle\Model\User.php 据说
/**
* Never use this to check if this user has access to anything!
*
* Use the SecurityContext, or an implementation of AccessDecisionManager
* instead, e.g.
*
* $securityContext->isGranted('ROLE_USER');
*
* @param string $role
*
* @return boolean
*/
public function hasRole($role)
{
return in_array(strtoupper($role), $this->getRoles(), true);
}
但 isGranted 返回实际用户的角色
$data = $form->getData();
$user = new User();
$user->setUsername($data->getUsername());
$user->setExpiresAt($data->getExpiresAt());
$user->setName($data->getName());
$user->setPassword($data->getPassword());
$user->setEmail($data->getEmail());
$user->setCredentialsExpired($data->isCredentialsExpired());
$user->setRoles(array('ROLE_NEW'));
$em->persist($user);
$em->flush();
// return false
var_dump(
$this->get('security.context')>isGranted('ROLE_NEW',$user));
如何检查特定用户的“isGranted”?