Issues (655)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Maker/AdminMaker.php (4 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Maker;
15
16
use Sonata\AdminBundle\Command\Validators;
17
use Sonata\AdminBundle\Manipulator\ServicesManipulator;
18
use Sonata\AdminBundle\Model\ModelManagerInterface;
19
use Symfony\Bundle\MakerBundle\ConsoleStyle;
20
use Symfony\Bundle\MakerBundle\DependencyBuilder;
21
use Symfony\Bundle\MakerBundle\Generator;
22
use Symfony\Bundle\MakerBundle\InputConfiguration;
23
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
24
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails;
25
use Symfony\Component\Console\Command\Command;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\DependencyInjection\Container;
30
31
/**
32
 * @author Gaurav Singh Faujdar <[email protected]>
33
 */
34
final class AdminMaker extends AbstractMaker
35
{
36
    /**
37
     * @var string
38
     */
39
    private $projectDirectory;
40
41
    /**
42
     * @var string[]
43
     */
44
    private $availableModelManagers;
45
46
    /**
47
     * @var string
48
     */
49
    private $skeletonDirectory;
50
51
    /**
52
     * @var string
53
     */
54
    private $modelClass;
55
56
    /**
57
     * @var string
58
     */
59
    private $modelClassBasename;
60
61
    /**
62
     * @var string
63
     */
64
    private $adminClassBasename;
65
66
    /**
67
     * @var string
68
     */
69
    private $controllerClassBasename;
70
71
    /**
72
     * @var string
73
     */
74
    private $managerType;
75
76
    /**
77
     * @var ModelManagerInterface
78
     */
79
    private $modelManager;
80
81
    public function __construct($projectDirectory, array $modelManagers = [])
82
    {
83
        $this->projectDirectory = $projectDirectory;
84
        $this->availableModelManagers = $modelManagers;
85
        $this->skeletonDirectory = sprintf('%s/../Resources/skeleton', __DIR__);
86
    }
87
88
    public static function getCommandName(): string
89
    {
90
        return 'make:sonata:admin';
91
    }
92
93
    public function configureCommand(Command $command, InputConfiguration $inputConfig): void
94
    {
95
        $command
96
            ->setDescription('Generates an admin class based on the given model class')
97
            ->addArgument('model', InputArgument::REQUIRED, 'The fully qualified model class')
98
            ->addOption('admin', 'a', InputOption::VALUE_OPTIONAL, 'The admin class basename')
99
            ->addOption('controller', 'c', InputOption::VALUE_OPTIONAL, 'The controller class basename')
100
            ->addOption('manager', 'm', InputOption::VALUE_OPTIONAL, 'The model manager type')
101
            ->addOption('services', 's', InputOption::VALUE_OPTIONAL, 'The services YAML file', 'services.yaml')
102
            ->addOption('id', 'i', InputOption::VALUE_OPTIONAL, 'The admin service ID');
103
104
        $inputConfig->setArgumentAsNonInteractive('model');
105
    }
106
107
    public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
108
    {
109
        $io->section('Welcome to the Sonata Admin');
110
        $this->modelClass = $io->ask(
111
            'The fully qualified model class',
112
            $input->getArgument('model'),
0 ignored issues
show
It seems like $input->getArgument('model') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string>; however, Symfony\Component\Consol...yle\SymfonyStyle::ask() does only seem to accept string|null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
113
            [Validators::class, 'validateClass']
114
        );
115
        $this->modelClassBasename = current(\array_slice(explode('\\', $this->modelClass), -1));
116
117
        $this->adminClassBasename = $io->ask(
118
            'The admin class basename',
119
            $input->getOption('admin') ?: sprintf('%sAdmin', $this->modelClassBasename),
120
            [Validators::class, 'validateAdminClassBasename']
121
        );
122
        if (\count($this->availableModelManagers) > 1) {
123
            $managerTypes = array_keys($this->availableModelManagers);
124
            $this->managerType = $io->choice('The manager type', $managerTypes, $managerTypes[0]);
125
126
            $input->setOption('manager', $this->managerType);
127
        }
128
        if ($io->confirm('Do you want to generate a controller?', false)) {
129
            $this->controllerClassBasename = $io->ask(
130
                'The controller class basename',
131
                $input->getOption('controller') ?: sprintf('%sAdminController', $this->modelClassBasename),
132
                [Validators::class, 'validateControllerClassBasename']
133
            );
134
            $input->setOption('controller', $this->controllerClassBasename);
135
        }
136
        $input->setOption('services', false);
137
        if ($io->confirm('Do you want to update the services YAML configuration file?', true)) {
138
            $path = sprintf('%s/config/', $this->projectDirectory);
139
            $servicesFile = $io->ask(
140
                'The services YAML configuration file',
141
                is_file($path.'admin.yaml') ? 'admin.yaml' : 'services.yaml',
142
                [Validators::class, 'validateServicesFile']
143
            );
144
            $id = $io->ask(
145
                'The admin service ID',
146
                $this->getAdminServiceId($this->adminClassBasename),
147
                [Validators::class, 'validateServiceId']
148
            );
149
            $input->setOption('services', $servicesFile);
150
            $input->setOption('id', $id);
151
        }
152
        $input->setArgument('model', $this->modelClass);
153
        $input->setOption('admin', $this->adminClassBasename);
154
    }
155
156
    /**
157
     * Configure any library dependencies that your maker requires.
158
     */
159
    public function configureDependencies(DependencyBuilder $dependencies): void
160
    {
161
    }
162
163
    /**
164
     * Called after normal code generation: allows you to do anything.
165
     */
166
    public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
167
    {
168
        $this->configure($input);
169
170
        $adminClassNameDetails = $generator->createClassNameDetails(
171
            $this->adminClassBasename,
172
            'Admin\\',
173
            'Admin'
174
        );
175
176
        $adminClassFullName = $adminClassNameDetails->getFullName();
177
        $this->generateAdmin($io, $generator, $adminClassNameDetails);
178
179
        $controllerClassFullName = '';
180
        if ($this->controllerClassBasename) {
181
            $controllerClassNameDetails = $generator->createClassNameDetails(
182
                $this->controllerClassBasename,
183
                'Controller\\',
184
                'Controller'
185
            );
186
187
            $this->generateController($io, $generator, $controllerClassNameDetails);
188
189
            $controllerClassFullName = $controllerClassNameDetails->getFullName();
190
        }
191
192
        $this->generateService($input, $io, $adminClassFullName, $controllerClassFullName);
193
    }
194
195
    private function getAdminServiceId(string $adminClassBasename): string
196
    {
197
        return Container::underscore(sprintf(
198
            'admin.%s',
199
            str_replace('\\', '.', 'Admin' === substr($adminClassBasename, -5) ?
200
                substr($adminClassBasename, 0, -5) : $adminClassBasename)
201
        ));
202
    }
203
204
    private function generateService(
205
        InputInterface $input,
206
        ConsoleStyle $io,
207
        string $adminClassFullName,
208
        string $controllerClassFullName
209
    ): void {
210
        if ($servicesFile = $input->getOption('services')) {
211
            $file = sprintf('%s/config/%s', $this->projectDirectory, $servicesFile);
212
            $servicesManipulator = new ServicesManipulator($file);
213
            $controllerName = $this->controllerClassBasename ? $controllerClassFullName : '~';
214
215
            $id = $input->getOption('id') ?:
216
                $this->getAdminServiceId($this->adminClassBasename);
217
218
            $servicesManipulator->addResource(
219
                $id,
220
                $this->modelClass,
221
                $adminClassFullName,
222
                $controllerName,
223
                substr($this->managerType, \strlen('sonata.admin.manager.'))
224
            );
225
226
            $io->writeln(sprintf(
227
                '%sThe service "<info>%s</info>" has been appended to the file <info>"%s</info>".',
228
                PHP_EOL,
229
                $id,
230
                realpath($file)
231
            ));
232
        }
233
    }
234
235
    private function generateController(
236
        ConsoleStyle $io,
237
        Generator $generator,
238
        ClassNameDetails $controllerClassNameDetails
239
    ): void {
240
        $controllerClassFullName = $controllerClassNameDetails->getFullName();
241
        $generator->generateClass(
242
            $controllerClassFullName,
243
            sprintf('%s/AdminController.tpl.php', $this->skeletonDirectory),
244
            []
245
        );
246
        $generator->writeChanges();
247
        $io->writeln(sprintf(
248
            '%sThe controller class "<info>%s</info>" has been generated under the file "<info>%s</info>".',
249
            PHP_EOL,
250
            $controllerClassNameDetails->getShortName(),
251
            $controllerClassFullName
252
        ));
253
    }
254
255
    private function generateAdmin(
256
        ConsoleStyle $io,
257
        Generator $generator,
258
        ClassNameDetails $adminClassNameDetails
259
    ): void {
260
        $adminClassFullName = $adminClassNameDetails->getFullName();
261
262
        $fields = $this->modelManager->getExportFields($this->modelClass);
263
        $fieldString = '';
264
        foreach ($fields as $field) {
265
            $fieldString = $fieldString.sprintf('%12s', '')."->add('".$field."')".PHP_EOL;
266
        }
267
268
        $fieldString .= sprintf('%12s', '');
269
270
        $generator->generateClass(
271
            $adminClassFullName,
272
            sprintf('%s/Admin.tpl.php', $this->skeletonDirectory),
273
            ['fields' => $fieldString]
274
        );
275
276
        $generator->writeChanges();
277
278
        $io->writeln(sprintf(
279
            '%sThe admin class "<info>%s</info>" has been generated under the file "<info>%s</info>".',
280
            PHP_EOL,
281
            $adminClassNameDetails->getShortName(),
282
            $adminClassFullName
283
        ));
284
    }
285
286
    private function configure(InputInterface $input): void
287
    {
288
        $this->modelClass = Validators::validateClass($input->getArgument('model'));
0 ignored issues
show
It seems like $input->getArgument('model') targeting Symfony\Component\Consol...nterface::getArgument() can also be of type array<integer,string> or null; however, Sonata\AdminBundle\Comma...dators::validateClass() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
289
        $this->modelClassBasename = (new \ReflectionClass($this->modelClass))->getShortName();
290
        $this->adminClassBasename = Validators::validateAdminClassBasename(
291
            $input->getOption('admin') ?: sprintf('%sAdmin', $this->modelClassBasename)
292
        );
293
294
        if ($this->controllerClassBasename = $input->getOption('controller')) {
0 ignored issues
show
Documentation Bug introduced by
It seems like $input->getOption('controller') can also be of type array<integer,string> or boolean. However, the property $controllerClassBasename is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
295
            $this->controllerClassBasename = Validators::validateControllerClassBasename($this->controllerClassBasename);
296
        }
297
298
        if (0 === \count($this->availableModelManagers)) {
299
            throw new \InvalidArgumentException('There are no model managers registered.');
300
        }
301
302
        $this->managerType = $input->getOption('manager') ?: array_keys($this->availableModelManagers)[0];
303
        $this->modelManager = $this->availableModelManagers[$this->managerType] ?: current($this->availableModelManagers);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->availableModelMan...availableModelManagers) of type string or false is incompatible with the declared type object<Sonata\AdminBundl...\ModelManagerInterface> of property $modelManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
304
    }
305
}
306