This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Role;
/**
* Role is a simple implementation of a RoleInterface where the role is a
* string.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Role implements RoleInterface
{
private $role;
/**
* Constructor.
*
* @param string $role The role name
*/
public function __construct($role)
{
$this->role = (string) $role;
}
/**
* {@inheritdoc}
*/
public function getRole()
{
return $this->role;
}
}

View File

@@ -0,0 +1,81 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Role;
/**
* RoleHierarchy defines a role hierarchy.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class RoleHierarchy implements RoleHierarchyInterface
{
private $hierarchy;
protected $map;
/**
* Constructor.
*
* @param array $hierarchy An array defining the hierarchy
*/
public function __construct(array $hierarchy)
{
$this->hierarchy = $hierarchy;
$this->buildRoleMap();
}
/**
* {@inheritdoc}
*/
public function getReachableRoles(array $roles)
{
$reachableRoles = $roles;
foreach ($roles as $role) {
if (!isset($this->map[$role->getRole()])) {
continue;
}
foreach ($this->map[$role->getRole()] as $r) {
$reachableRoles[] = new Role($r);
}
}
return $reachableRoles;
}
protected function buildRoleMap()
{
$this->map = array();
foreach ($this->hierarchy as $main => $roles) {
$this->map[$main] = $roles;
$visited = array();
$additionalRoles = $roles;
while ($role = array_shift($additionalRoles)) {
if (!isset($this->hierarchy[$role])) {
continue;
}
$visited[] = $role;
foreach ($this->hierarchy[$role] as $roleToAdd) {
$this->map[$main][] = $roleToAdd;
}
foreach (array_diff($this->hierarchy[$role], $visited) as $additionalRole) {
$additionalRoles[] = $additionalRole;
}
}
$this->map[$main] = array_unique($this->map[$main]);
}
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Role;
/**
* RoleHierarchyInterface is the interface for a role hierarchy.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface RoleHierarchyInterface
{
/**
* Returns an array of all reachable roles by the given ones.
*
* Reachable roles are the roles directly assigned but also all roles that
* are transitively reachable from them in the role hierarchy.
*
* @param RoleInterface[] $roles An array of directly assigned roles
*
* @return RoleInterface[] An array of all reachable roles
*/
public function getReachableRoles(array $roles);
}

View File

@@ -0,0 +1,35 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Role;
/**
* RoleInterface represents a role granted to a user.
*
* A role must either have a string representation or it needs to be explicitly
* supported by at least one AccessDecisionManager.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface RoleInterface
{
/**
* Returns the role.
*
* This method returns a string representation whenever possible.
*
* When the role cannot be represented with sufficient precision by a
* string, it should return null.
*
* @return string|null A string representation of the role, or null
*/
public function getRole();
}

View File

@@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Security\Core\Role;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
/**
* SwitchUserRole is used when the current user temporarily impersonates
* another one.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SwitchUserRole extends Role
{
private $source;
/**
* Constructor.
*
* @param string $role The role as a string
* @param TokenInterface $source The original token
*/
public function __construct($role, TokenInterface $source)
{
parent::__construct($role);
$this->source = $source;
}
/**
* Returns the original Token.
*
* @return TokenInterface The original TokenInterface instance
*/
public function getSource()
{
return $this->source;
}
}