summaryrefslogtreecommitdiffstats
path: root/Core/Role
diff options
context:
space:
mode:
authorJohannes M. Schmitt <schmittjoh@gmail.com>2011-01-26 21:34:11 +0100
committerFabien Potencier <fabien.potencier@gmail.com>2011-01-26 22:23:20 +0100
commitbebc09870cb0a7720e2c6a8c5c74585e69e8bb24 (patch)
tree0c399647cdbe504be405017e7cc04c70c53482f2 /Core/Role
parentc85f3d708d2c9b00d73ca1234ccfaf50336d94b1 (diff)
downloadsymfony-security-bebc09870cb0a7720e2c6a8c5c74585e69e8bb24.zip
symfony-security-bebc09870cb0a7720e2c6a8c5c74585e69e8bb24.tar.gz
symfony-security-bebc09870cb0a7720e2c6a8c5c74585e69e8bb24.tar.bz2
namespace changes
Symfony\Component\Security -> Symfony\Component\Security\Core Symfony\Component\Security\Acl remains unchanged Symfony\Component\HttpKernel\Security -> Symfony\Component\Security\Http
Diffstat (limited to 'Core/Role')
-rw-r--r--Core/Role/Role.php41
-rw-r--r--Core/Role/RoleHierarchy.php77
-rw-r--r--Core/Role/RoleHierarchyInterface.php32
-rw-r--r--Core/Role/RoleInterface.php35
-rw-r--r--Core/Role/SwitchUserRole.php48
5 files changed, 233 insertions, 0 deletions
diff --git a/Core/Role/Role.php b/Core/Role/Role.php
new file mode 100644
index 0000000..20e4fd5
--- /dev/null
+++ b/Core/Role/Role.php
@@ -0,0 +1,41 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.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.potencier@symfony-project.com>
+ */
+class Role implements RoleInterface
+{
+ protected $role;
+
+ /**
+ * Constructor.
+ *
+ * @param string $role The role name
+ */
+ public function __construct($role)
+ {
+ $this->role = (string) $role;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getRole()
+ {
+ return $this->role;
+ }
+}
diff --git a/Core/Role/RoleHierarchy.php b/Core/Role/RoleHierarchy.php
new file mode 100644
index 0000000..9556801
--- /dev/null
+++ b/Core/Role/RoleHierarchy.php
@@ -0,0 +1,77 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.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.potencier@symfony-project.com>
+ */
+class RoleHierarchy implements RoleHierarchyInterface
+{
+ protected $hierarchy;
+ protected $map;
+
+ /**
+ * Constructor.
+ *
+ * @param array $hierarchy An array defining the hierarchy
+ */
+ public function __construct(array $hierarchy)
+ {
+ $this->hierarchy = $hierarchy;
+
+ $this->buildRoleMap();
+ }
+
+ /**
+ * Returns an array of all roles reachable by the given ones.
+ *
+ * @param RoleInterface[] $roles An array of RoleInterface instances
+ *
+ * @return RoleInterface[] An array of RoleInterface instances
+ */
+ 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;
+ $this->map[$main] = array_unique(array_merge($this->map[$main], $this->hierarchy[$role]));
+ $additionalRoles = array_merge($additionalRoles, array_diff($this->hierarchy[$role], $visited));
+ }
+ }
+ }
+}
diff --git a/Core/Role/RoleHierarchyInterface.php b/Core/Role/RoleHierarchyInterface.php
new file mode 100644
index 0000000..9f5cd5d
--- /dev/null
+++ b/Core/Role/RoleHierarchyInterface.php
@@ -0,0 +1,32 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.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.potencier@symfony-project.com>
+ */
+interface RoleHierarchyInterface
+{
+ /**
+ * Returns an array of all reachable roles.
+ *
+ * Reachable roles are the roles directly assigned but also all roles that
+ * are transitively reachable from them in the role hierarchy.
+ *
+ * @param array $roles An array of directly assigned roles
+ *
+ * @return array An array of all reachable roles
+ */
+ function getReachableRoles(array $roles);
+}
diff --git a/Core/Role/RoleInterface.php b/Core/Role/RoleInterface.php
new file mode 100644
index 0000000..923a933
--- /dev/null
+++ b/Core/Role/RoleInterface.php
@@ -0,0 +1,35 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.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 an at least one AccessDecisionManager.
+ *
+ * @author Fabien Potencier <fabien.potencier@symfony-project.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
+ */
+ function getRole();
+}
diff --git a/Core/Role/SwitchUserRole.php b/Core/Role/SwitchUserRole.php
new file mode 100644
index 0000000..589129c
--- /dev/null
+++ b/Core/Role/SwitchUserRole.php
@@ -0,0 +1,48 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.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.potencier@symfony-project.com>
+ */
+class SwitchUserRole extends Role
+{
+ protected $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;
+ }
+}