Skip to content

Commit

Permalink
add type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
tambait committed Oct 23, 2020
1 parent 8773923 commit 1734d67
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 116 deletions.
16 changes: 3 additions & 13 deletions src/Util/AdminAclManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
use Symfony\Component\Security\Acl\Model\AclInterface;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;

/**
Expand All @@ -34,11 +33,9 @@ final class AdminAclManipulator implements AdminAclManipulatorInterface
private $maskBuilderClass;

/**
* @param string $maskBuilderClass
*
* @phpstan-param class-string $maskBuilderClass
*/
public function __construct($maskBuilderClass)
public function __construct(string $maskBuilderClass)
{
$this->maskBuilderClass = $maskBuilderClass;
}
Expand Down Expand Up @@ -73,17 +70,10 @@ public function configureAcls(OutputInterface $output, AdminInterface $admin): v

public function addAdminClassAces(
OutputInterface $output,
AclInterface $acl,
MutableAclInterface $acl,
AclSecurityHandlerInterface $securityHandler,
array $roleInformation = []
) {
if (!$acl instanceof MutableAclInterface) {
throw new \TypeError(sprintf(
'Argument 2 passed to "%s()" must implement "%s".',
__METHOD__,
MutableAclInterface::class
));
}
): bool {
if (\count($securityHandler->getAdminPermissions()) > 0) {
$builder = new $this->maskBuilderClass();

Expand Down
10 changes: 4 additions & 6 deletions src/Util/AdminAclManipulatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Security\Acl\Model\AclInterface;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;

/**
* @author Thomas Rabaix <[email protected]>
Expand All @@ -25,10 +25,8 @@ interface AdminAclManipulatorInterface
{
/**
* Batch configure the ACLs for all objects handled by an Admin.
*
* @return void
*/
public function configureAcls(OutputInterface $output, AdminInterface $admin);
public function configureAcls(OutputInterface $output, AdminInterface $admin): void;

/**
* Add the class ACE's to the admin ACL.
Expand All @@ -39,8 +37,8 @@ public function configureAcls(OutputInterface $output, AdminInterface $admin);
*/
public function addAdminClassAces(
OutputInterface $output,
AclInterface $acl,
MutableAclInterface $acl,
AclSecurityHandlerInterface $securityHandler,
array $roleInformation = []
);
): bool;
}
77 changes: 25 additions & 52 deletions src/Util/AdminObjectAclData.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Security\Handler\AclSecurityHandlerInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Security\Acl\Domain\Acl;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;

/**
Expand Down Expand Up @@ -76,22 +74,18 @@ class AdminObjectAclData

/**
* @var string
*
* @phpstan-var class-string
*/
protected $maskBuilderClass;

/**
* @param object $object
* @param string $maskBuilderClass
*
* @phpstan-param class-string $maskBuilderClass
*/
public function __construct(
AdminInterface $admin,
$object,
object $object,
\Traversable $aclUsers,
$maskBuilderClass,
string $maskBuilderClass,
?\Traversable $aclRoles = null
) {
$this->admin = $admin;
Expand All @@ -108,50 +102,40 @@ public function __construct(

/**
* Gets admin.
*
* @return AdminInterface
*/
public function getAdmin()
public function getAdmin(): AdminInterface
{
return $this->admin;
}

/**
* Gets object.
*
* @return object
*/
public function getObject()
public function getObject(): object
{
return $this->object;
}

/**
* Gets ACL users.
*
* @return \Traversable
*/
public function getAclUsers()
public function getAclUsers(): \Traversable
{
return $this->aclUsers;
}

/**
* Gets ACL roles.
*
* @return \Traversable
*/
public function getAclRoles()
public function getAclRoles(): \Traversable
{
return $this->aclRoles;
}

/**
* Sets ACL.
*
* @return AdminObjectAclData
*/
public function setAcl(MutableAclInterface $acl)
public function setAcl(MutableAclInterface $acl): self
{
$this->acl = $acl;

Expand All @@ -160,11 +144,11 @@ public function setAcl(MutableAclInterface $acl)

/**
* Gets ACL.
*
* @return MutableAclInterface
*/
public function getAcl()
public function getAcl(): MutableAclInterface
{
\assert(null !== $this->acl);

return $this->acl;
}

Expand All @@ -173,7 +157,7 @@ public function getAcl()
*
* @return array<string, mixed>
*/
public function getMasks()
public function getMasks(): array
{
return $this->masks;
}
Expand All @@ -183,7 +167,7 @@ public function getMasks()
*
* @return AdminObjectAclData
*/
public function setAclUsersForm(FormInterface $form)
public function setAclUsersForm(FormInterface $form): self
{
$this->aclUsersForm = $form;

Expand All @@ -192,11 +176,11 @@ public function setAclUsersForm(FormInterface $form)

/**
* Gets ACL users form.
*
* @return FormInterface
*/
public function getAclUsersForm()
public function getAclUsersForm(): FormInterface
{
\assert(null !== $this->aclUsersForm);

return $this->aclUsersForm;
}

Expand All @@ -205,7 +189,7 @@ public function getAclUsersForm()
*
* @return AdminObjectAclData
*/
public function setAclRolesForm(FormInterface $form)
public function setAclRolesForm(FormInterface $form): self
{
$this->aclRolesForm = $form;

Expand All @@ -214,30 +198,26 @@ public function setAclRolesForm(FormInterface $form)

/**
* Gets ACL roles form.
*
* @return FormInterface
*/
public function getAclRolesForm()
public function getAclRolesForm(): FormInterface
{
\assert(null !== $this->aclRolesForm);

return $this->aclRolesForm;
}

/**
* Gets permissions.
*
* @return array
*/
public function getPermissions()
public function getPermissions(): array
{
return $this->getSecurityHandler()->getObjectPermissions();
}

/**
* Get permissions that the current user can set.
*
* @return array
*/
public function getUserPermissions()
public function getUserPermissions(): array
{
$permissions = $this->getPermissions();

Expand All @@ -253,39 +233,32 @@ public function getUserPermissions()
return $permissions;
}

public function getOwnerPermissions()
public function getOwnerPermissions(): array
{
return self::$ownerPermissions;
}

/**
* Tests if the current user has the OWNER right.
*
* @return bool
*/
public function isOwner()
public function isOwner(): bool
{
// Only a owner can set MASTER and OWNER ACL
return $this->admin->isGranted('OWNER', $this->object);
}

/**
* Gets security handler.
*
* @return AclSecurityHandlerInterface
*/
public function getSecurityHandler()
public function getSecurityHandler(): AclSecurityHandlerInterface
{
$securityHandler = $this->admin->getSecurityHandler();
\assert($securityHandler instanceof AclSecurityHandlerInterface);

return $securityHandler;
}

/**
* @return array
*/
public function getSecurityInformation()
public function getSecurityInformation(): array
{
return $this->getSecurityHandler()->buildSecurityInformation($this->admin);
}
Expand Down
21 changes: 5 additions & 16 deletions src/Util/AdminObjectAclManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

use Sonata\AdminBundle\Form\Type\AclMatrixType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
Expand Down Expand Up @@ -51,32 +50,26 @@ class AdminObjectAclManipulator
protected $maskBuilderClass;

/**
* @param string $maskBuilderClass
*
* @phpstan-param class-string $maskBuilderClass
*/
public function __construct(FormFactoryInterface $formFactory, $maskBuilderClass)
public function __construct(FormFactoryInterface $formFactory, string $maskBuilderClass)
{
$this->formFactory = $formFactory;
$this->maskBuilderClass = $maskBuilderClass;
}

/**
* Gets mask builder class name.
*
* @return string
*/
public function getMaskBuilderClass()
public function getMaskBuilderClass(): string
{
return $this->maskBuilderClass;
}

/**
* Gets the ACL users form.
*
* @return FormInterface
*/
public function createAclUsersForm(AdminObjectAclData $data)
public function createAclUsersForm(AdminObjectAclData $data): FormInterface
{
$aclValues = $data->getAclUsers();
$formBuilder = $this->formFactory->createNamedBuilder(self::ACL_USERS_FORM_NAME, FormType::class);
Expand All @@ -88,10 +81,8 @@ public function createAclUsersForm(AdminObjectAclData $data)

/**
* Gets the ACL roles form.
*
* @return FormInterface
*/
public function createAclRolesForm(AdminObjectAclData $data)
public function createAclRolesForm(AdminObjectAclData $data): FormInterface
{
$aclValues = $data->getAclRoles();
$formBuilder = $this->formFactory->createNamedBuilder(self::ACL_ROLES_FORM_NAME, FormType::class);
Expand Down Expand Up @@ -195,11 +186,9 @@ protected function buildAcl(AdminObjectAclData $data, FormInterface $form, \Trav
/**
* @param \Traversable<int|string, UserInterface|string> $aclValues
*
* @return FormInterface
*
* @phpstan-param \Traversable<array-key, UserInterface|string> $aclValues
*/
protected function buildForm(AdminObjectAclData $data, FormBuilderInterface $formBuilder, \Traversable $aclValues)
protected function buildForm(AdminObjectAclData $data, FormBuilderInterface $formBuilder, \Traversable $aclValues): FormInterface
{
// Retrieve object identity
$objectIdentity = ObjectIdentity::fromDomainObject($data->getObject());
Expand Down
Loading

0 comments on commit 1734d67

Please sign in to comment.