本文整理汇总了PHP中Symfony\Component\Security\Core\Authentication\Token\TokenInterface::getRoles方法的典型用法代码示例。如果您正苦于以下问题:PHP TokenInterface::getRoles方法的具体用法?PHP TokenInterface::getRoles怎么用?PHP TokenInterface::getRoles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Symfony\Component\Security\Core\Authentication\Token\TokenInterface
的用法示例。
在下文中一共展示了TokenInterface::getRoles方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createUser
public function createUser(TokenInterface $token)
{
$username = $token->getUser();
$attributes = $token->getAttributes();
if (is_null($token->getRoles())) {
$roles = isset($attributes['roles']) ? $attributes['roles'] : null;
unset($attributes['roles']);
} else {
$roles = $token->getRoles();
}
return new SpawnedUser($username, $attributes, $roles);
}
示例2: vote
public function vote(TokenInterface $token, Location $object, array $attributes)
{
if (in_array('ROLE_LOCATION_MODERATOR', $token->getRoles())) {
return VoterInterface::ACCESS_GRANTED;
}
return VoterInterface::ACCESS_ABSTAIN;
}
示例3: vote
public function vote(TokenInterface $token, $group, array $attributes)
{
// check if class of this object is supported by this voter
if (!$this->supportsClass(get_class($group))) {
return VoterInterface::ACCESS_ABSTAIN;
}
// check if the given attribute is covered by this voter
if (!$this->supportsAttribute($attributes[0])) {
return VoterInterface::ACCESS_ABSTAIN;
}
// get current logged in user
$user = $token->getUser();
//allow the token to have ROLE_SUPER_ADMIN before we check the user, for testing
if (in_array(new Role("ROLE_SUPER_ADMIN"), $token->getRoles())) {
return VoterInterface::ACCESS_GRANTED;
}
// make sure there is a user object (i.e. that the user is logged in)
if (!$user instanceof User) {
return VoterInterface::ACCESS_DENIED;
}
switch ($attributes[0]) {
case self::VIEW:
if ($user->hasGroup($group->getName())) {
return VoterInterface::ACCESS_GRANTED;
}
break;
case self::EDIT:
if ($user->hasGroup($group->getName()) && $user->hasRole("ROLE_ADMIN")) {
return VoterInterface::ACCESS_GRANTED;
}
break;
}
return VoterInterface::ACCESS_DENIED;
}
示例4: getRoles
/**
* Returns the roles (an array of string) of the $token.
*
* @todo remove this $method
*
* @param \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token
*
* @return array
*/
public function getRoles(TokenInterface $token)
{
$roles = array();
foreach ($token->getRoles() as $role) {
$roles[] = $role->getRole();
}
return $roles;
}
示例5: isAdmin
protected function isAdmin(TokenInterface $token)
{
foreach ($token->getRoles() as $role) {
if (PlatformRoles::ADMIN === $role->getRole()) {
return true;
}
}
return false;
}
示例6: getVariables
private function getVariables(TokenInterface $token, $object)
{
if (null !== $this->roleHierarchy) {
$roles = $this->roleHierarchy->getReachableRoles($token->getRoles());
} else {
$roles = $token->getRoles();
}
$variables = array('token' => $token, 'user' => $token->getUser(), 'object' => $object, 'roles' => array_map(function ($role) {
return $role->getRole();
}, $roles), 'trust_resolver' => $this->trustResolver);
// this is mainly to propose a better experience when the expression is used
// in an access control rule, as the developer does not know that it's going
// to be handled by this voter
if ($object instanceof Request) {
$variables['request'] = $object;
}
return $variables;
}
示例7: onAuthenticationSuccess
/**
* @param Request $request
* @param TokenInterface $token
*
* @return RedirectResponse
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
foreach ($token->getRoles() as $role) {
if ('ROLE_ADMIN' == $role->getRole()) {
return new RedirectResponse($this->router->generate('admin_start'));
}
}
return new RedirectResponse($this->router->generate('homepage'));
}
示例8: isIddqd
protected function isIddqd(TokenInterface $token)
{
foreach ($token->getRoles() as $role) {
if (in_array($role->getRole(), $this->iddqdAliases, true)) {
return true;
}
}
return false;
}
示例9: isIddqd
protected function isIddqd(TokenInterface $token)
{
foreach ($token->getRoles() as $role) {
if ('IS_IDDQD' === $role->getRole()) {
return true;
}
}
return false;
}
示例10: authenticate
public function authenticate(TokenInterface $token)
{
$user = $token->getUser();
if ($user) {
$authenticatedToken = new WordpressUserToken($token->getRoles());
$authenticatedToken->setUser($user);
return $authenticatedToken;
}
throw new AuthenticationException('Wordpress authentication failed.');
}
示例11: getRoles
/**
* Retrieves roles from user and appends SwitchUserRole if original token contained one.
*
* @param UserInterface $user The user
* @param TokenInterface $token The token
*
* @return array The user roles
*/
private function getRoles(UserInterface $user, TokenInterface $token)
{
$roles = $user->getRoles();
foreach ($token->getRoles() as $role) {
if ($role instanceof SwitchUserRole) {
$roles[] = $role;
break;
}
}
return $roles;
}
示例12: hasRole
/**
* Utility function to find role in token
*
* @param TokenInterface $token
* @param $role
* @return bool
*/
public static function hasRole(TokenInterface $token, $role)
{
if (is_string($role)) {
$role = new Role($role);
}
foreach ($token->getRoles() as $tokenRole) {
if ($role->getRole() == $tokenRole->getRole()) {
return true;
}
}
}
示例13: getObjectIdsForRole
/**
*
* @param TokenInterface $token - Array of ACL objects to check
* @param string $inputRole - Requested Role
*
* @return array
*/
public function getObjectIdsForRole(TokenInterface $token, $inputRole)
{
$object_ids = array();
$reachable = $this->getRoleHierarchy()->getReachableRoles($token->getRoles());
foreach ($token->getUser()->getAcls() as $acl) {
// found an object id for this role
if ($acl->getType()->equal($inputRole) || $this->findInMap($acl, $reachable)) {
$object_ids[] = $acl->getObjectId();
}
}
return $object_ids;
}
示例14: voteOnAttribute
/**
* Perform a single access check operation on a given attribute, subject and token.
*
* @param string $attribute
* @param mixed $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/* @var $subject SecuredAccessInterface */
if ($subject->isUsernameAllowed($token->getUsername(), $attribute)) {
return true;
}
foreach ($token->getRoles() as $role) {
if ($subject->isRoleAllowed($role->getRole(), $attribute)) {
return true;
}
}
return false;
}
示例15:
function it_should_log_event(FilterControllerEvent $event, ApiControllerInterface $ctrl, LoggerInterface $logger, SecurityContext $security, Request $request, TokenInterface $token)
{
$logger->info('API call', Argument::type('array'))->shouldBeCalled();
$token->getRoles()->willReturn([]);
$token->getUsername()->willReturn('anon.');
$security->getToken()->willReturn($token);
$this->setContext($security);
$this->setLogger($logger);
$request->getRequestUri()->willReturn('/api/user/1');
$request->getMethod()->willReturn('GET');
$event->getController()->willReturn([$ctrl, 'someAction']);
$event->getRequest()->willReturn($request);
$this->onKernelController($event);
}