Merge branch 'master' of w00d5t0ck.info:blueroses
commit
8309843958
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Application\Migrations;
|
||||
|
||||
use Doctrine\DBAL\Migrations\AbstractMigration,
|
||||
Doctrine\DBAL\Schema\Schema;
|
||||
|
||||
/**
|
||||
* Auto-generated Migration: Please modify to your need!
|
||||
*/
|
||||
class Version20120815091637 extends AbstractMigration
|
||||
{
|
||||
public function up(Schema $schema)
|
||||
{
|
||||
// this up() migration is autogenerated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
|
||||
|
||||
$this->addSql("CREATE TABLE role_hierarchy (parent_role_id INT NOT NULL, child_role_id INT NOT NULL, INDEX IDX_AB8EFB72A44B56EA (parent_role_id), INDEX IDX_AB8EFB72B4B76AB7 (child_role_id), PRIMARY KEY(parent_role_id, child_role_id)) ENGINE = InnoDB");
|
||||
$this->addSql("ALTER TABLE role_hierarchy ADD CONSTRAINT FK_AB8EFB72A44B56EA FOREIGN KEY (parent_role_id) REFERENCES roles (id)");
|
||||
$this->addSql("ALTER TABLE role_hierarchy ADD CONSTRAINT FK_AB8EFB72B4B76AB7 FOREIGN KEY (child_role_id) REFERENCES roles (id)");
|
||||
$this->addSql("ALTER TABLE roles DROP admin, DROP superAdmin");
|
||||
}
|
||||
|
||||
public function down(Schema $schema)
|
||||
{
|
||||
// this down() migration is autogenerated, please modify it to your needs
|
||||
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");
|
||||
|
||||
$this->addSql("DROP TABLE role_hierarchy");
|
||||
$this->addSql("ALTER TABLE roles ADD admin TINYINT(1) NOT NULL, ADD superAdmin TINYINT(1) NOT NULL");
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
namespace KekRozsak\SecurityBundle\DependencyInjection\Compiler;
|
||||
|
||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
class OverrideServiceCompilerPass implements CompilerPassInterface
|
||||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
$definition = $container->getDefinition('security.role_hierarchy');
|
||||
$definition->setClass('KekRozsak\SecurityBundle\Service\RoleHierarchy');
|
||||
$definition->setArguments(array(new Reference('doctrine')));
|
||||
}
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace KekRozsak\SecurityBundle\Service;
|
||||
|
||||
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
|
||||
use Symfony\Bridge\Doctrine\RegistryInterface;
|
||||
|
||||
class RoleHierarchy implements RoleHierarchyInterface
|
||||
{
|
||||
private $hierarchy;
|
||||
private $roleRepo;
|
||||
private $map;
|
||||
|
||||
public function __construct(RegistryInterface $doctrine)
|
||||
{
|
||||
$this->hierarchy = array();
|
||||
$this->roleRepo = $doctrine->getRepository('KekRozsakSecurityBundle:Role');
|
||||
|
||||
$this->buildRoleMap();
|
||||
}
|
||||
|
||||
public function getReachableRoles(array $roles)
|
||||
{
|
||||
$reachableRoles = array();
|
||||
foreach ($roles as $role) {
|
||||
if (!isset($this->map[$role->getRole()])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->map[$role->getRole()] as $r) {
|
||||
if (($childRole = $this->roleRepo->findOneByName($r)) !== null) {
|
||||
$reachableRoles[] = $childRole;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $reachableRoles;
|
||||
}
|
||||
|
||||
private function buildRoleMap()
|
||||
{
|
||||
$this->map = array();
|
||||
$roles = $this->roleRepo->findAll();
|
||||
foreach ($roles as $mainRole) {
|
||||
$main = $mainRole->getRole();
|
||||
$this->map[$main] = array();
|
||||
foreach ($mainRole->getInheritedRoles() as $childRole) {
|
||||
$this->map[$main][] = $childRole->getRole();
|
||||
// TODO: This is one-level only. Get as deep as possible.
|
||||
// BEWARE OF RECURSIVE NESTING!
|
||||
foreach ($childRole->getInheritedRoles() as $grandchildRole) {
|
||||
$this->map[$main][] = $grandchildRole->getRole();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue