2012-08-21 11:35:09 +00:00
|
|
|
<?php
|
|
|
|
namespace KekRozsak\SecurityBundle\Command;
|
|
|
|
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
|
|
|
|
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
|
|
|
|
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
|
2012-08-30 11:09:36 +00:00
|
|
|
use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
|
2012-08-21 11:35:09 +00:00
|
|
|
|
|
|
|
class SecurityInitCommand extends ContainerAwareCommand
|
|
|
|
{
|
|
|
|
private $verbose;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*/
|
|
|
|
protected function configure()
|
|
|
|
{
|
|
|
|
$this
|
|
|
|
->setName('kekrozsak:security:init')
|
|
|
|
->setDescription('Initializes ACLs')
|
|
|
|
->setHelp(<<<EOF
|
|
|
|
The <info>%command.full_name%</info> command fills up the ACL tables with default ACLs.
|
|
|
|
|
|
|
|
<info>php %command.full_name%</info>
|
|
|
|
|
|
|
|
ACL lists are currently hard-coded.
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
parent::initialize($input, $output);
|
|
|
|
$this->verbose = $input->getOption('verbose');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see Command
|
|
|
|
*/
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
|
{
|
|
|
|
$container = $this->getContainer();
|
|
|
|
|
|
|
|
$securityContext = $container->get('security.context');
|
|
|
|
$aclProvider = $container->get('security.acl.provider');
|
|
|
|
|
2012-08-30 11:09:36 +00:00
|
|
|
$classNames = array(
|
2012-08-30 19:52:53 +00:00
|
|
|
'newsClass' => 'KekRozsak\\FrontBundle\\Entity\\News',
|
|
|
|
'articlesClass' => 'KekRozsak\\FrontBundle\\Entity\\Articles',
|
2012-08-30 11:09:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$securityIdentity = new RoleSecurityIdentity('ROLE_ADMIN');
|
2012-08-30 19:52:53 +00:00
|
|
|
foreach ($classNames as $id => $className) {
|
|
|
|
$objectIdentity = new ObjectIdentity($id, $className);
|
2012-08-30 11:09:36 +00:00
|
|
|
try {
|
|
|
|
$acl = $aclProvider->findAcl($objectIdentity, array($securityIdentity));
|
|
|
|
} catch (AclNotFoundException $e) {
|
|
|
|
$acl = $aclProvider->createAcl($objectIdentity);
|
|
|
|
}
|
2012-08-30 19:52:53 +00:00
|
|
|
$acl->insertClassAce($securityIdentity, MaskBuilder::MASK_OWNER);
|
2012-08-30 11:09:36 +00:00
|
|
|
$aclProvider->updateAcl($acl);
|
|
|
|
}
|
2012-08-21 11:35:09 +00:00
|
|
|
}
|
|
|
|
}
|