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
|
|
|
* NEXT_MAJOR: Rename to `$userModelClass`. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $userEntityClass = ''; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var Pool |
45
|
|
|
*/ |
46
|
|
|
private $pool; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* An array of object ACL manipulators indexed by their service ids. |
50
|
|
|
* |
51
|
|
|
* @var ObjectAclManipulatorInterface[] |
52
|
|
|
*/ |
53
|
|
|
private $aclObjectManipulators = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ManagerRegistry|null |
57
|
|
|
*/ |
58
|
|
|
private $registry; |
59
|
|
|
|
60
|
|
|
public function __construct(Pool $pool, array $aclObjectManipulators, ?ManagerRegistry $registry = null) |
61
|
|
|
{ |
62
|
|
|
$this->pool = $pool; |
63
|
|
|
$this->aclObjectManipulators = $aclObjectManipulators; |
64
|
|
|
$this->registry = $registry; |
65
|
|
|
|
66
|
|
|
parent::__construct(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function configure(): void |
70
|
|
|
{ |
71
|
|
|
$this |
72
|
|
|
->setDescription('Install ACL for the objects of the Admin Classes.') |
73
|
|
|
->addOption('object_owner', null, InputOption::VALUE_OPTIONAL, 'If set, the task will set the object owner for each admin.') |
74
|
|
|
// NEXT_MAJOR: Remove "user_entity" option. |
75
|
|
|
->addOption('user_entity', null, InputOption::VALUE_OPTIONAL, '<error>DEPRECATED</error> Use <comment>user_model</comment> option instead.') |
76
|
|
|
->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.') |
77
|
|
|
->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.') |
78
|
|
|
; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function execute(InputInterface $input, OutputInterface $output): int |
82
|
|
|
{ |
83
|
|
|
$output->writeln('Welcome to the AdminBundle object ACL generator'); |
84
|
|
|
$output->writeln([ |
|
|
|
|
85
|
|
|
'', |
86
|
|
|
'This command helps you to generate ACL entities for the objects handled by the AdminBundle.', |
87
|
|
|
'', |
88
|
|
|
'If the step option is used, you will be asked if you want to generate the object ACL entities for each Admin.', |
89
|
|
|
'You must use the shortcut notation like <comment>AcmeDemoBundle:User</comment> if you want to set an object owner.', |
90
|
|
|
'', |
91
|
|
|
]); |
92
|
|
|
|
93
|
|
|
if (!$this->registry) { |
94
|
|
|
throw new ServiceNotFoundException('doctrine', static::class, null, [], sprintf( |
95
|
|
|
'The command "%s" has a dependency on a non-existent service "doctrine".', |
96
|
|
|
static::$defaultName |
97
|
|
|
)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($input->getOption('user_model')) { |
101
|
|
|
try { |
102
|
|
|
$this->getUserEntityClass($input, $output); |
|
|
|
|
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
105
|
|
|
|
106
|
|
|
return 1; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (!$this->aclObjectManipulators) { |
|
|
|
|
111
|
|
|
$output->writeln('No manipulators are implemented : <info>ignoring</info>'); |
112
|
|
|
|
113
|
|
|
return 1; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
foreach ($this->pool->getAdminServiceIds() as $id) { |
117
|
|
|
try { |
118
|
|
|
$admin = $this->pool->getInstance($id); |
119
|
|
|
} catch (\Exception $e) { |
120
|
|
|
$output->writeln('<error>Warning : The admin class cannot be initiated from the command line</error>'); |
121
|
|
|
$output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
122
|
|
|
|
123
|
|
|
continue; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if ($input->getOption('step') && !$this->askConfirmation($input, $output, sprintf("<question>Generate ACLs for the object instances handled by \"%s\"?</question>\n", $id), 'no', '?')) { |
127
|
|
|
continue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$securityIdentity = null; |
131
|
|
|
if ($input->getOption('step') && $this->askConfirmation($input, $output, "<question>Set an object owner?</question>\n", 'no', '?')) { |
132
|
|
|
$username = $this->askAndValidate($input, $output, 'Please enter the username: ', '', 'Sonata\AdminBundle\Command\Validators::validateUsername'); |
133
|
|
|
|
134
|
|
|
$securityIdentity = new UserSecurityIdentity($username, $this->getUserEntityClass($input, $output)); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
if (!$input->getOption('step') && $input->getOption('object_owner')) { |
137
|
|
|
$securityIdentity = new UserSecurityIdentity($input->getOption('object_owner'), $this->getUserEntityClass($input, $output)); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$manipulatorId = sprintf('sonata.admin.manipulator.acl.object.%s', $admin->getManagerType()); |
141
|
|
|
if (!$manipulator = $this->aclObjectManipulators[$manipulatorId] ?? null) { |
142
|
|
|
$output->writeln('Admin class is using a manager type that has no manipulator implemented : <info>ignoring</info>'); |
143
|
|
|
|
144
|
|
|
continue; |
145
|
|
|
} |
146
|
|
|
if (!$manipulator instanceof ObjectAclManipulatorInterface) { |
147
|
|
|
$output->writeln(sprintf('The interface "ObjectAclManipulatorInterface" is not implemented for %s: <info>ignoring</info>', \get_class($manipulator))); |
148
|
|
|
|
149
|
|
|
continue; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
\assert($admin instanceof AdminInterface); |
153
|
|
|
$manipulator->batchConfigureAcls($output, $admin, $securityIdentity); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return 0; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
160
|
|
|
{ |
161
|
|
|
parent::initialize($input, $output); |
162
|
|
|
|
163
|
|
|
// NEXT_MAJOR: Remove the following conditional block. |
164
|
|
|
if (null !== $input->getOption('user_entity')) { |
165
|
|
|
$output->writeln([ |
|
|
|
|
166
|
|
|
'Option <comment>user_entity</comment> is deprecated since sonata-project/admin-bundle 3.69 and will be removed in version 4.0.' |
167
|
|
|
.' Use <comment>user_model</comment> option instead.', |
168
|
|
|
'', |
169
|
|
|
]); |
170
|
|
|
|
171
|
|
|
@trigger_error( |
|
|
|
|
172
|
|
|
'Option "user_entity" is deprecated since sonata-project/admin-bundle 3.69 and will be removed in version 4.0.' |
173
|
|
|
.' Use "user_model" option instead.', |
174
|
|
|
E_USER_DEPRECATED |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
if (null === $input->getOption('user_model')) { |
178
|
|
|
$input->setOption('user_model', $input->getOption('user_entity')); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
protected function getUserModelClass(InputInterface $input, OutputInterface $output): string |
184
|
|
|
{ |
185
|
|
|
return $this->getUserEntityClass($input, $output); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* NEXT_MAJOR: Remove this method and move its body to `getUserModelClass()`. |
190
|
|
|
* |
191
|
|
|
* @deprecated since sonata-project/admin-bundle 3.69. Use `getUserModelClass()` instead. |
192
|
|
|
* |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
protected function getUserEntityClass(InputInterface $input, OutputInterface $output) |
196
|
|
|
{ |
197
|
|
|
if (self::class !== static::class) { |
198
|
|
|
@trigger_error(sprintf( |
|
|
|
|
199
|
|
|
'Method %s() is deprecated since sonata-project/admin-bundle 3.69 and will be removed in version 4.0.' |
200
|
|
|
.' Use %s::getUserModelClass() instead.', |
201
|
|
|
__METHOD__, |
202
|
|
|
__CLASS__ |
203
|
|
|
), E_USER_DEPRECATED); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
if ('' === $this->userEntityClass) { |
207
|
|
|
if ($input->getOption('user_model')) { |
208
|
|
|
[$userBundle, $userModel] = Validators::validateEntityName($input->getOption('user_model')); |
|
|
|
|
209
|
|
|
} else { |
210
|
|
|
[$userBundle, $userModel] = $this->askAndValidate( |
211
|
|
|
$input, |
212
|
|
|
$output, |
213
|
|
|
'Please enter the User Entity shortcut name: ', |
214
|
|
|
'', |
215
|
|
|
'Sonata\AdminBundle\Command\Validators::validateEntityName' |
216
|
|
|
); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// Entity exists? |
220
|
|
|
if ($this->registry instanceof RegistryInterface) { |
221
|
|
|
$namespace = $this->registry->getEntityNamespace($userBundle); |
222
|
|
|
} else { |
223
|
|
|
$namespace = $this->registry->getAliasNamespace($userBundle); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
$this->userEntityClass = sprintf('%s\%s', $namespace, $userModel); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $this->userEntityClass; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: