Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb2a2e2

Browse files
bug #22830 [SecurityBundle] forward compatibility with Symfony 4 (xabbuh)
This PR was merged into the 3.4 branch. Discussion ---------- [SecurityBundle] forward compatibility with Symfony 4 | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | #22821 (comment) Commits ------- c783e1e forward compatibility with Symfony 4
2 parents 8308b32 + c783e1e commit bb2a2e2

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\SecurityBundle\DataCollector;
1313

1414
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
15+
use Symfony\Component\Security\Core\Role\Role;
1516
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
1617
use Symfony\Component\HttpFoundation\Request;
1718
use Symfony\Component\HttpFoundation\Response;
@@ -110,15 +111,23 @@ public function collect(Request $request, Response $response, \Exception $except
110111
// fail silently when the logout URL cannot be generated
111112
}
112113

114+
$extractRoles = function ($role) {
115+
if (!$role instanceof RoleInterface && !$role instanceof Role) {
116+
throw new \InvalidArgumentException(sprintf('Roles must be instances of %s or %s (%s given).', RoleInterface::class, Role::class, is_object($role) ? get_class($role) : gettype($role)));
117+
}
118+
119+
return $role->getRole();
120+
};
121+
113122
$this->data = array(
114123
'enabled' => true,
115124
'authenticated' => $token->isAuthenticated(),
116125
'token' => $token,
117126
'token_class' => $this->hasVarDumper ? new ClassStub(get_class($token)) : get_class($token),
118127
'logout_url' => $logoutUrl,
119128
'user' => $token->getUsername(),
120-
'roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $assignedRoles),
121-
'inherited_roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles),
129+
'roles' => array_map($extractRoles, $assignedRoles),
130+
'inherited_roles' => array_map($extractRoles, $inheritedRoles),
122131
'supports_role_hierarchy' => null !== $this->roleHierarchy,
123132
);
124133
}

0 commit comments

Comments
 (0)