Skip to content

Commit

Permalink
Add support for sonata 4
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Sep 10, 2021
1 parent fe063fd commit 1b3c4b9
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 179 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"ext-json": "*",
"nucleos/user-bundle": "^1.9",
"psr/log": "^1.0",
"sonata-project/admin-bundle": "^3.90",
"sonata-project/admin-bundle": "^3.90 || ^4.0",
"sonata-project/doctrine-extensions": "^1.5.1",
"symfony/config": "^4.4",
"symfony/dependency-injection": "^4.4",
Expand Down Expand Up @@ -56,7 +56,7 @@
"doctrine/orm": "^2.7",
"ergebnis/composer-normalize": "^2.0.1",
"knplabs/knp-menu": "^2.3 || ^3.0",
"sonata-project/doctrine-orm-admin-bundle": "^3.13",
"sonata-project/doctrine-orm-admin-bundle": "^3.13 || ^4.0",
"symfony/asset": "^4.4",
"symfony/browser-kit": "^4.4",
"symfony/doctrine-bridge": "^4.4",
Expand Down
36 changes: 33 additions & 3 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,27 @@ parameters:
path: src/Security/EditableRolesBuilder.php

-
message: "#^Strict comparison using \\=\\=\\= between null and Sonata\\\\AdminBundle\\\\Security\\\\Handler\\\\SecurityHandlerInterface will always evaluate to false\\.$#"
message: "#^Call to function is_array\\(\\) with array\\<array\\(\\?'admin' \\=\\> string, 'label' \\=\\> string, 'roles' \\=\\> array\\<int, string\\>, 'route' \\=\\> string, 'route_absolute' \\=\\> bool, 'route_params' \\=\\> array\\<string, string\\>\\)\\> will always evaluate to true\\.$#"
count: 1
path: src/Security/EditableRolesBuilder.php
path: src/Security/RolesBuilder/AdminRolesBuilder.php

-
message: "#^Method Nucleos\\\\UserAdminBundle\\\\Security\\\\RolesBuilder\\\\AdminRolesBuilder\\:\\:getRoles\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: src/Security/RolesBuilder/AdminRolesBuilder.php

-
message: "#^Strict comparison using \\=\\=\\= between null and Sonata\\\\AdminBundle\\\\Security\\\\Handler\\\\SecurityHandlerInterface will always evaluate to false\\.$#"
message: "#^Parameter \\#1 \\$admin of method Nucleos\\\\UserAdminBundle\\\\Security\\\\RolesBuilder\\\\AdminRolesBuilder\\:\\:getAdminLabel\\(\\) expects Sonata\\\\AdminBundle\\\\Admin\\\\AdminInterface\\<Nucleos\\\\UserBundle\\\\Model\\\\UserInterface\\>, Sonata\\\\AdminBundle\\\\Admin\\\\AdminInterface\\<object\\> given\\.$#"
count: 1
path: src/Security/RolesBuilder/AdminRolesBuilder.php

-
message: "#^Parameter \\#1 \\$admin of method Nucleos\\\\UserAdminBundle\\\\Security\\\\RolesBuilder\\\\AdminRolesBuilder\\:\\:getGroupLabel\\(\\) expects Sonata\\\\AdminBundle\\\\Admin\\\\AdminInterface\\<Nucleos\\\\UserBundle\\\\Model\\\\UserInterface\\>, Sonata\\\\AdminBundle\\\\Admin\\\\AdminInterface\\<object\\> given\\.$#"
count: 1
path: src/Security/RolesBuilder/AdminRolesBuilder.php

-
message: "#^Parameter \\#1 \\$admin of method Nucleos\\\\UserAdminBundle\\\\Security\\\\RolesBuilder\\\\AdminRolesBuilder\\:\\:isMaster\\(\\) expects Sonata\\\\AdminBundle\\\\Admin\\\\AdminInterface\\<Nucleos\\\\UserBundle\\\\Model\\\\UserInterface\\>, Sonata\\\\AdminBundle\\\\Admin\\\\AdminInterface\\<object\\> given\\.$#"
count: 1
path: src/Security/RolesBuilder/AdminRolesBuilder.php

Expand Down Expand Up @@ -115,8 +125,28 @@ parameters:
count: 1
path: tests/Action/LoginActionTest.php

-
message: "#^Method Nucleos\\\\UserAdminBundle\\\\Tests\\\\Fixtures\\\\PoolMockFactory\\:\\:create\\(\\) has parameter \\$adminServiceIds with generic interface Sonata\\\\AdminBundle\\\\Admin\\\\AdminInterface but does not specify its types\\: T$#"
count: 1
path: tests/Fixtures/PoolMockFactory.php

-
message: "#^Call to static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertTrue\\(\\) with true and non\\-empty\\-string will always evaluate to true\\.$#"
count: 1
path: tests/Resources/XliffTest.php

-
message: "#^Parameter \\#3 \\$options of class Sonata\\\\AdminBundle\\\\SonataConfiguration constructor expects array\\('confirm_exit' \\=\\> bool, 'default_group' \\=\\> string, 'default_icon' \\=\\> string, 'default_label_catalogue' \\=\\> string, 'dropdown_number_groups_per_colums' \\=\\> int, 'form_type' \\=\\> string, 'html5_validate' \\=\\> bool, 'javascripts' \\=\\> array\\<int, string\\>, \\.\\.\\.\\), array\\(\\) given\\.$#"
count: 3
path: tests/Security/Authorization/Voter/EditableRolesBuilderTest.php

-
message: "#^Parameter \\#3 \\$options of class Sonata\\\\AdminBundle\\\\SonataConfiguration constructor expects array\\('confirm_exit' \\=\\> bool, 'default_group' \\=\\> string, 'default_icon' \\=\\> string, 'default_label_catalogue' \\=\\> string, 'dropdown_number_groups_per_colums' \\=\\> int, 'form_type' \\=\\> string, 'html5_validate' \\=\\> bool, 'javascripts' \\=\\> array\\<int, string\\>, \\.\\.\\.\\), array\\(\\) given\\.$#"
count: 1
path: tests/Security/RolesBuilder/AdminRolesBuilderTest.php

-
message: "#^Parameter \\#3 \\$options of class Sonata\\\\AdminBundle\\\\SonataConfiguration constructor expects array\\('confirm_exit' \\=\\> bool, 'default_group' \\=\\> string, 'default_icon' \\=\\> string, 'default_label_catalogue' \\=\\> string, 'dropdown_number_groups_per_colums' \\=\\> int, 'form_type' \\=\\> string, 'html5_validate' \\=\\> bool, 'javascripts' \\=\\> array\\<int, string\\>, \\.\\.\\.\\), array\\('role_super_admin' \\=\\> 'ROLE_SUPER_ADMIN', 'role_admin' \\=\\> 'ROLE_SONATA_ADMIN'\\) given\\.$#"
count: 1
path: tests/Security/RolesBuilder/SecurityRolesBuilderTest.php

32 changes: 2 additions & 30 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.6.4@97fe86c4e158b5a57c5150aa5055c38b5a809aab">
<files psalm-version="4.10.0@916b098b008f6de4543892b1e0651c1c3b92cbfa">
<file src="src/Action/LoginAction.php">
<TooManyArguments occurrences="1">
<code>dispatch</code>
</TooManyArguments>
</file>
<file src="src/Admin/Model/GroupAdmin.php">
<NonInvariantDocblockPropertyType occurrences="1">
<code>$formOptions</code>
</NonInvariantDocblockPropertyType>
</file>
<file src="src/Admin/Model/UserAdmin.php">
<PossiblyNullArgument occurrences="1">
<code>$this-&gt;getSubject()</code>
</PossiblyNullArgument>
</file>
<file src="src/Controller/UserCRUDController.php">
<TooManyArguments occurrences="1">
<code>dispatch</code>
Expand All @@ -33,11 +23,6 @@
<code>$formBuilder</code>
</ParamNameMismatch>
</file>
<file src="src/Security/Authorization/Voter/UserAclVoter.php">
<ParamNameMismatch occurrences="1">
<code>$subject</code>
</ParamNameMismatch>
</file>
<file src="src/Security/EditableRolesBuilder.php">
<LessSpecificImplementedReturnType occurrences="1">
<code>array</code>
Expand All @@ -61,18 +46,13 @@
<InvalidReturnType occurrences="1">
<code>string[]</code>
</InvalidReturnType>
<PossiblyNullArgument occurrences="1">
<code>$admin-&gt;getParent()</code>
</PossiblyNullArgument>
</file>
<file src="src/Security/RolesBuilder/SecurityRolesBuilder.php">
<InvalidArgument occurrences="6">
<InvalidArgument occurrences="4">
<code>$childRoles</code>
<code>$childRoles</code>
<code>$childRoles</code>
<code>$role</code>
<code>$securityRoles</code>
<code>$securityRoles</code>
</InvalidArgument>
<InvalidArrayOffset occurrences="2">
<code>$securityRoles[$role]</code>
Expand All @@ -81,13 +61,5 @@
<InvalidOperand occurrences="1">
<code>$role</code>
</InvalidOperand>
<InvalidReturnStatement occurrences="2">
<code>$securityRoles</code>
<code>$securityRoles</code>
</InvalidReturnStatement>
<InvalidReturnType occurrences="2">
<code>mixed[]</code>
<code>mixed[]</code>
</InvalidReturnType>
</file>
</files>
22 changes: 6 additions & 16 deletions src/Admin/Model/UserAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\Form\FormBuilderInterface;

/**
* @phpstan-extends AbstractAdmin<\Nucleos\UserBundle\Model\UserInterface>
Expand All @@ -52,26 +51,17 @@ public function __construct($code, $class, $baseControllerName, UserManagerInter
$this->userManager = $userManager;
}

public function getFormBuilder(): FormBuilderInterface
{
$this->formOptions['data_class'] = $this->getClass();

$options = $this->formOptions;
$options['validation_groups'] = $this->isNewInstance() ? 'Registration' : 'Profile';

$formBuilder = $this->getFormContractor()->getFormBuilder($this->getUniqid(), $options);

$this->defineFormBuilder($formBuilder);

return $formBuilder;
}

public function preUpdate($object): void
{
$this->userManager->updateCanonicalFields($object);
$this->userManager->updatePassword($object);
}

protected function configureFormOptions(array &$formOptions): void
{
$formOptions['validation_groups'] = $this->isNewInstance() ? 'Registration' : 'Profile';
}

protected function configureExportFields(): array
{
// avoid security field to be exported
Expand Down Expand Up @@ -211,7 +201,7 @@ protected function configureFormFields(FormMapper $form): void

private function isNewInstance(): bool
{
return !$this->hasSubject() || null === $this->getSubject() || null === $this->id($this->getSubject());
return !$this->hasSubject() || null === $this->id($this->getSubject());
}

private function isLocaleAwareSubject(): bool
Expand Down
3 changes: 2 additions & 1 deletion src/Controller/UserCRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
use Nucleos\UserBundle\NucleosUserEvents;
use Sonata\AdminBundle\Controller\CRUDController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
* @phpstan-extends CRUDController<\Nucleos\UserBundle\Model\UserInterface>
*/
class UserCRUDController extends CRUDController
{
protected function preDelete(Request $request, $object)
protected function preDelete(Request $request, $object): ?Response
{
$this->getEventDispatcher()->dispatch(new AccountDeletionEvent($object, $request), NucleosUserEvents::ACCOUNT_DELETION);

Expand Down
4 changes: 3 additions & 1 deletion src/Resources/config/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
ref('security.token_storage'),
ref('security.authorization_checker'),
ref('sonata.admin.pool'),
ref('sonata.admin.configuration'),
'%security.role_hierarchy.roles%',
])
->call('setTranslator', [
Expand All @@ -50,13 +51,14 @@
->args([
ref('security.authorization_checker'),
ref('sonata.admin.pool'),
ref('sonata.admin.configuration'),
ref('translator'),
])

->set('nucleos_user_admin.security_roles_builder', SecurityRolesBuilder::class)
->args([
ref('security.authorization_checker'),
ref('sonata.admin.pool'),
ref('sonata.admin.configuration'),
ref('translator'),
'%security.role_hierarchy.roles%',
])
Expand Down
14 changes: 9 additions & 5 deletions src/Security/EditableRolesBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Exception;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\SonataConfiguration;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -36,6 +37,11 @@ final class EditableRolesBuilder implements EditableRolesBuilderInterface
*/
private $pool;

/**
* @var SonataConfiguration
*/
private $configuration;

/**
* @var TranslatorInterface|null
*/
Expand All @@ -50,11 +56,13 @@ public function __construct(
TokenStorageInterface $tokenStorage,
AuthorizationCheckerInterface $authorizationChecker,
Pool $pool,
SonataConfiguration $configuration,
array $rolesHierarchy = []
) {
$this->tokenStorage = $tokenStorage;
$this->authorizationChecker = $authorizationChecker;
$this->pool = $pool;
$this->configuration = $configuration;
$this->rolesHierarchy = $rolesHierarchy;
}

Expand Down Expand Up @@ -87,7 +95,7 @@ function ($role, $isMaster) use ($domain, &$roles): void {
);

$isMaster = $this->authorizationChecker->isGranted(
$this->pool->getOption('role_super_admin', 'ROLE_SUPER_ADMIN')
$this->configuration->getOption('role_super_admin', 'ROLE_SUPER_ADMIN')
);

// get roles from the service container
Expand Down Expand Up @@ -146,10 +154,6 @@ private function iterateAdminRoles(callable $func): void
$isMaster = $admin->isGranted('MASTER');
$securityHandler = $admin->getSecurityHandler();

if (null === $securityHandler) {
continue;
}

// TODO get the base role from the admin or security handler
$baseRole = $securityHandler->getBaseRole($admin);

Expand Down
16 changes: 10 additions & 6 deletions src/Security/RolesBuilder/AdminRolesBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\SonataConfiguration;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand All @@ -30,6 +31,11 @@ final class AdminRolesBuilder implements AdminRolesBuilderInterface
*/
private $pool;

/**
* @var SonataConfiguration
*/
private $configuration;

/**
* @var TranslatorInterface
*/
Expand All @@ -43,10 +49,12 @@ final class AdminRolesBuilder implements AdminRolesBuilderInterface
public function __construct(
AuthorizationCheckerInterface $authorizationChecker,
Pool $pool,
SonataConfiguration $configuration,
TranslatorInterface $translator
) {
$this->authorizationChecker = $authorizationChecker;
$this->pool = $pool;
$this->configuration = $configuration;
$this->translator = $translator;
}

Expand Down Expand Up @@ -86,10 +94,6 @@ public function getRoles(string $domain = null): array
$admin = $this->pool->getInstance($id);
$securityHandler = $admin->getSecurityHandler();

if (null === $securityHandler) {
continue;
}

$baseRole = $securityHandler->getBaseRole($admin);

foreach (array_keys($admin->getSecurityInformation()) as $key) {
Expand Down Expand Up @@ -135,7 +139,7 @@ private function getGroupLabel(AdminInterface $admin): string
}
}

if (null !== $admin->getParent() && $admin->getParent() !== $admin) {
if ($admin->isChild()) {
return $this->getGroupLabel($admin->getParent());
}

Expand Down Expand Up @@ -169,7 +173,7 @@ private function isMaster(AdminInterface $admin): bool
{
return $admin->isGranted('MASTER')
|| $admin->isGranted('OPERATOR')
|| $this->authorizationChecker->isGranted($this->pool->getOption('role_super_admin'));
|| $this->authorizationChecker->isGranted($this->configuration->getOption('role_super_admin'));
}

private function translateRole(string $role, ?string $domain): string
Expand Down
14 changes: 7 additions & 7 deletions src/Security/RolesBuilder/SecurityRolesBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Nucleos\UserAdminBundle\Security\RolesBuilder;

use Sonata\AdminBundle\Admin\Pool;
use Sonata\AdminBundle\SonataConfiguration;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand All @@ -25,9 +25,9 @@ final class SecurityRolesBuilder implements ExpandableRolesBuilderInterface
private $authorizationChecker;

/**
* @var Pool
* @var SonataConfiguration
*/
private $pool;
private $configuration;

/**
* @var TranslatorInterface
Expand All @@ -44,12 +44,12 @@ final class SecurityRolesBuilder implements ExpandableRolesBuilderInterface
*/
public function __construct(
AuthorizationCheckerInterface $authorizationChecker,
Pool $pool,
SonataConfiguration $configuration,
TranslatorInterface $translator,
array $rolesHierarchy = []
) {
$this->authorizationChecker = $authorizationChecker;
$this->pool = $pool;
$this->configuration = $configuration;
$this->translator = $translator;
$this->rolesHierarchy = $rolesHierarchy;
}
Expand Down Expand Up @@ -108,8 +108,8 @@ private function getHierarchy(): array
// @phpstan-ignore-next-line
return array_merge(
[
$this->pool->getOption('role_super_admin') => [],
$this->pool->getOption('role_admin') => [],
$this->configuration->getOption('role_super_admin') => [],
$this->configuration->getOption('role_admin') => [],
],
$this->rolesHierarchy
);
Expand Down
Loading

0 comments on commit 1b3c4b9

Please sign in to comment.