forked from sonata-project/SonataAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenerateObjectAclCommand.php
133 lines (113 loc) · 5.81 KB
/
GenerateObjectAclCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/*
* This file is part of the Sonata package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Command;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\Output;
use Sonata\AdminBundle\Util\ObjectAclManipulatorInterface;
class GenerateObjectAclCommand extends ContainerAwareCommand
{
/**
* @var string
*/
protected $userEntityClass = '';
/**
* {@inheritDoc}
*/
public function configure()
{
$this
->setName('sonata:admin:generate-object-acl')
->setDescription('Install ACL for the objects of the Admin Classes.')
->addOption('object_owner', null, InputOption::VALUE_OPTIONAL, 'If set, the task will set the object owner for each admin.')
->addOption('user_entity', null, InputOption::VALUE_OPTIONAL, 'Shortcut notation like <comment>AcmeDemoBundle:User</comment>. If not set, it will be asked the first time an object owner is set.')
->addOption('step', null, InputOption::VALUE_NONE, 'If set, the task will ask for each admin if the ACLs need to be generated and if and what object owner to set.')
;
}
/**
* {@inheritDoc}
*/
public function execute(InputInterface $input, OutputInterface $output)
{
/** @var $dialog \Symfony\Component\Console\Helper\DialogHelper */
$dialog = $this->getHelperSet()->get('dialog');
$output->writeln('Welcome to the AdminBundle object ACL generator');
$output->writeln(array(
'',
'This command helps you generate ACL entities for the objects handled by the AdminBundle.',
'',
'If the step option is used, you will be asked for each Admin to generate the object ACL entities.',
'You must use the shortcut notation like <comment>AcmeDemoBundle:User</comment> if you want to set an object owner.',
''
));
if ($input->getOption('user_entity')) {
try {
$this->getUserEntityClass($input, $output);
} catch (\Exception $e) {
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
return;
}
}
foreach ($this->getContainer()->get('sonata.admin.pool')->getAdminServiceIds() as $id) {
try {
$admin = $this->getContainer()->get($id);
} catch (\Exception $e) {
$output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>');
$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
continue;
}
if ($input->getOption('step') && !$dialog->askConfirmation($output, sprintf("<question>Generate ACLs for the object instances handled by \"%s\"?</question>\n", $id), false)) {
continue;
}
$securityIdentity = null;
if ($input->getOption('step') && $dialog->askConfirmation($output,"<question>Set an object owner?</question>\n", false)) {
$username = $dialog->askAndValidate($output, 'Please enter the username: ', 'Sonata\AdminBundle\Command\Validators::validateUsername');
$securityIdentity = new UserSecurityIdentity($username, $this->getUserEntityClass($input, $output));
}
if (!$input->getOption('step') && $input->getOption('object_owner')) {
$securityIdentity = new UserSecurityIdentity($input->getOption('object_owner'), $this->getUserEntityClass($input, $output));
}
$manipulatorId = sprintf('sonata.admin.manipulator.acl.object.%s', $admin->getManagerType());
if (!$this->getContainer()->has($manipulatorId)) {
$output->writeln('Admin class is using a manager type that has no manipulator implemented : <info>ignoring</info>');
continue;
}
$manipulator = $this->getContainer()->get($manipulatorId);
if (!$manipulator instanceof ObjectAclManipulatorInterface) {
$output->writeln(sprintf('The interface "ObjectAclManipulatorInterface" is not implemented for %s: <info>ignoring</info>', get_class($manipulator)));
continue;
}
$manipulator->batchConfigureAcls($output, $admin, $securityIdentity);
}
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*
* @return string
*/
protected function getUserEntityClass(InputInterface $input, OutputInterface $output)
{
if ($this->userEntityClass === '') {
if ($input->getOption('user_entity')) {
list($userBundle, $userEntity) = Validators::validateEntityName($input->getOption('user_entity'));
$this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
} else {
list($userBundle, $userEntity) = $this->getHelperSet()->get('dialog')->askAndValidate($output, 'Please enter the User Entity shortcut name: ', 'Sonata\AdminBundle\Command\Validators::validateEntityName');
// Entity exists?
$this->userEntityClass = $this->getContainer()->get('doctrine')->getEntityNamespace($userBundle).'\\'.$userEntity;
}
}
return $this->userEntityClass;
}
}