1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Command; |
15
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
17
|
|
|
use Sonata\AdminBundle\Admin\AdminInterface; |
18
|
|
|
use Sonata\AdminBundle\Admin\Pool; |
19
|
|
|
use Sonata\AdminBundle\Util\ObjectAclManipulatorInterface; |
20
|
|
|
use Symfony\Bridge\Doctrine\RegistryInterface; |
21
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
22
|
|
|
use Symfony\Component\Console\Input\InputOption; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
25
|
|
|
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @final since sonata-project/admin-bundle 3.52 |
29
|
|
|
* |
30
|
|
|
* @author Thomas Rabaix <[email protected]> |
31
|
|
|
*/ |
32
|
|
|
class GenerateObjectAclCommand extends QuestionableCommand |
33
|
|
|
{ |
34
|
|
|
protected static $defaultName = 'sonata:admin:generate-object-acl'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $userModelClass = ''; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var Pool |
43
|
|
|
*/ |
44
|
|
|
private $pool; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* An array of object ACL manipulators indexed by their service ids. |
48
|
|
|
* |
49
|
|
|
* @var ObjectAclManipulatorInterface[] |
50
|
|
|
*/ |
51
|
|
|
private $aclObjectManipulators = []; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var RegistryInterface|ManagerRegistry|null |
55
|
|
|
*/ |
56
|
|
|
private $registry; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param RegistryInterface|ManagerRegistry|null $registry |
60
|
|
|
*/ |
61
|
|
|
public function __construct(Pool $pool, array $aclObjectManipulators, $registry = null) |
62
|
|
|
{ |
63
|
|
|
$this->pool = $pool; |
64
|
|
|
$this->aclObjectManipulators = $aclObjectManipulators; |
65
|
|
|
if (null !== $registry && (!$registry instanceof RegistryInterface && !$registry instanceof ManagerRegistry)) { |
66
|
|
|
if (!$registry instanceof ManagerRegistry) { |
67
|
|
|
@trigger_error(sprintf( |
|
|
|
|
68
|
|
|
'Passing an object that doesn\'t implement %s as argument 3 to %s() is deprecated since' |
69
|
|
|
.' sonata-project/admin-bundle 3.56.', |
70
|
|
|
ManagerRegistry::class, |
71
|
|
|
__METHOD__ |
72
|
|
|
), E_USER_DEPRECATED); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw new \TypeError(sprintf( |
|
|
|
|
76
|
|
|
'Argument 3 passed to %s() must be either an instance of %s or %s, %s given.', |
77
|
|
|
__METHOD__, |
78
|
|
|
RegistryInterface::class, |
79
|
|
|
ManagerRegistry::class, |
80
|
|
|
\is_object($registry) ? \get_class($registry) : \gettype($registry) |
81
|
|
|
)); |
82
|
|
|
} |
83
|
|
|
$this->registry = $registry; |
84
|
|
|
|
85
|
|
|
parent::__construct(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function configure(): void |
89
|
|
|
{ |
90
|
|
|
$this |
91
|
|
|
->setDescription('Install ACL for the objects of the Admin Classes.') |
92
|
|
|
->addOption('object_owner', null, InputOption::VALUE_OPTIONAL, 'If set, the task will set the object owner for each admin.') |
93
|
|
|
->addOption('user_model', 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.') |
94
|
|
|
->addOption('step', null, InputOption::VALUE_NONE, 'If set, the task will ask for each admin if the ACLs need to be generated and what object owner to set, if any.') |
95
|
|
|
; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int |
99
|
|
|
{ |
100
|
|
|
$output->writeln('Welcome to the AdminBundle object ACL generator'); |
101
|
|
|
$output->writeln([ |
|
|
|
|
102
|
|
|
'', |
103
|
|
|
'This command helps you to generate ACL entities for the objects handled by the AdminBundle.', |
104
|
|
|
'', |
105
|
|
|
'If the step option is used, you will be asked if you want to generate the object ACL entities for each Admin.', |
106
|
|
|
'You must use the shortcut notation like <comment>AcmeDemoBundle:User</comment> if you want to set an object owner.', |
107
|
|
|
'', |
108
|
|
|
]); |
109
|
|
|
|
110
|
|
|
if (!$this->registry) { |
111
|
|
|
throw new ServiceNotFoundException('doctrine', static::class, null, [], sprintf( |
112
|
|
|
'The command "%s" has a dependency on a non-existent service "doctrine".', |
113
|
|
|
static::$defaultName |
114
|
|
|
)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($input->getOption('user_model')) { |
118
|
|
|
try { |
119
|
|
|
$this->getUserEntityClass($input, $output); |
|
|
|
|
120
|
|
|
} catch (\Exception $e) { |
121
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
122
|
|
|
|
123
|
|
|
return 1; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (!$this->aclObjectManipulators) { |
|
|
|
|
128
|
|
|
$output->writeln('No manipulators are implemented : <info>ignoring</info>'); |
129
|
|
|
|
130
|
|
|
return 1; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
foreach ($this->pool->getAdminServiceIds() as $id) { |
134
|
|
|
try { |
135
|
|
|
$admin = $this->pool->getInstance($id); |
136
|
|
|
} catch (\Exception $e) { |
137
|
|
|
$output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>'); |
138
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
139
|
|
|
|
140
|
|
|
continue; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if ($input->getOption('step') && !$this->askConfirmation($input, $output, sprintf("<question>Generate ACLs for the object instances handled by \"%s\"?</question>\n", $id), 'no', '?')) { |
|
|
|
|
144
|
|
|
continue; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$securityIdentity = null; |
148
|
|
|
if ($input->getOption('step') && $this->askConfirmation($input, $output, "<question>Set an object owner?</question>\n", 'no', '?')) { |
|
|
|
|
149
|
|
|
$username = $this->askAndValidate($input, $output, 'Please enter the username: ', '', 'Sonata\AdminBundle\Command\Validators::validateUsername'); |
150
|
|
|
|
151
|
|
|
$securityIdentity = new UserSecurityIdentity($username, $this->getUserEntityClass($input, $output)); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
if (!$input->getOption('step') && $input->getOption('object_owner')) { |
154
|
|
|
$securityIdentity = new UserSecurityIdentity($input->getOption('object_owner'), $this->getUserEntityClass($input, $output)); |
|
|
|
|
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$manipulatorId = sprintf('sonata.admin.manipulator.acl.object.%s', $admin->getManagerType()); |
158
|
|
|
if (!$manipulator = $this->aclObjectManipulators[$manipulatorId] ?? null) { |
159
|
|
|
$output->writeln('Admin class is using a manager type that has no manipulator implemented : <info>ignoring</info>'); |
160
|
|
|
|
161
|
|
|
continue; |
162
|
|
|
} |
163
|
|
|
if (!$manipulator instanceof ObjectAclManipulatorInterface) { |
164
|
|
|
$output->writeln(sprintf('The interface "ObjectAclManipulatorInterface" is not implemented for %s: <info>ignoring</info>', \get_class($manipulator))); |
165
|
|
|
|
166
|
|
|
continue; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
\assert($admin instanceof AdminInterface); |
170
|
|
|
$manipulator->batchConfigureAcls($output, $admin, $securityIdentity); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return 0; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
177
|
|
|
{ |
178
|
|
|
parent::initialize($input, $output); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected function getUserModelClass(InputInterface $input, OutputInterface $output): string |
182
|
|
|
{ |
183
|
|
|
if ('' === $this->userEntityClass) { |
184
|
|
|
if ($input->getOption('user_model')) { |
185
|
|
|
[$userBundle, $userModel] = Validators::validateEntityName($input->getOption('user_model')); |
|
|
|
|
186
|
|
|
} else { |
187
|
|
|
[$userBundle, $userModel] = $this->askAndValidate( |
188
|
|
|
$input, |
189
|
|
|
$output, |
190
|
|
|
'Please enter the User Entity shortcut name: ', |
191
|
|
|
'', |
192
|
|
|
'Sonata\AdminBundle\Command\Validators::validateEntityName' |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Entity exists? |
197
|
|
|
if ($this->registry instanceof RegistryInterface) { |
198
|
|
|
$namespace = $this->registry->getEntityNamespace($userBundle); |
199
|
|
|
} else { |
200
|
|
|
$namespace = $this->registry->getAliasNamespace($userBundle); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$this->userEntityClass = sprintf('%s\%s', $namespace, $userModel); |
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $this->userEntityClass; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: