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        | https://github.com/symfony/symfony/pull/22821#issuecomment-302935588

Commits
-------

c783e1e forward compatibility with Symfony 4
This commit is contained in:
Nicolas Grekas 2017-05-21 19:30:27 +02:00
commit bb2a2e2a32

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\SecurityBundle\DataCollector;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Role\Role;
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@ -110,6 +111,14 @@ class SecurityDataCollector extends DataCollector implements LateDataCollectorIn
// fail silently when the logout URL cannot be generated
}
$extractRoles = function ($role) {
if (!$role instanceof RoleInterface && !$role instanceof Role) {
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)));
}
return $role->getRole();
};
$this->data = array(
'enabled' => true,
'authenticated' => $token->isAuthenticated(),
@ -117,8 +126,8 @@ class SecurityDataCollector extends DataCollector implements LateDataCollectorIn
'token_class' => $this->hasVarDumper ? new ClassStub(get_class($token)) : get_class($token),
'logout_url' => $logoutUrl,
'user' => $token->getUsername(),
'roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $assignedRoles),
'inherited_roles' => array_map(function (RoleInterface $role) { return $role->getRole(); }, $inheritedRoles),
'roles' => array_map($extractRoles, $assignedRoles),
'inherited_roles' => array_map($extractRoles, $inheritedRoles),
'supports_role_hierarchy' => null !== $this->roleHierarchy,
);
}