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 26, 2020
1 parent 8773923 commit 6ba37c5
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 164 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;
}
106 changes: 19 additions & 87 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 @@ -82,16 +80,13 @@ class AdminObjectAclData
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 @@ -106,138 +101,85 @@ public function __construct(
$this->updateMasks();
}

/**
* 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;

return $this;
}

/**
* Gets ACL.
*
* @return MutableAclInterface
*/
public function getAcl()
public function getAcl(): ?MutableAclInterface
{
return $this->acl;
}

/**
* Gets masks.
*
* @return array<string, mixed>
*/
public function getMasks()
public function getMasks(): array
{
return $this->masks;
}

/**
* Sets ACL users form.
*
* @return AdminObjectAclData
*/
public function setAclUsersForm(FormInterface $form)
public function setAclUsersForm(FormInterface $form): self
{
$this->aclUsersForm = $form;

return $this;
}

/**
* Gets ACL users form.
*
* @return FormInterface
*/
public function getAclUsersForm()
public function getAclUsersForm(): ?FormInterface
{
return $this->aclUsersForm;
}

/**
* Sets ACL roles form.
*
* @return AdminObjectAclData
*/
public function setAclRolesForm(FormInterface $form)
public function setAclRolesForm(FormInterface $form): self
{
$this->aclRolesForm = $form;

return $this;
}

/**
* Gets ACL roles form.
*
* @return FormInterface
*/
public function getAclRolesForm()
public function getAclRolesForm(): ?FormInterface
{
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 +195,29 @@ 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
32 changes: 6 additions & 26 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,23 @@ 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 +78,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 All @@ -101,9 +89,6 @@ public function createAclRolesForm(AdminObjectAclData $data)
return $form;
}

/**
* Updates ACL users.
*/
public function updateAclUsers(AdminObjectAclData $data): void
{
$aclValues = $data->getAclUsers();
Expand All @@ -112,9 +97,6 @@ public function updateAclUsers(AdminObjectAclData $data): void
$this->buildAcl($data, $form, $aclValues);
}

/**
* Updates ACL roles.
*/
public function updateAclRoles(AdminObjectAclData $data): void
{
$aclValues = $data->getAclRoles();
Expand Down Expand Up @@ -195,11 +177,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 Expand Up @@ -258,7 +238,7 @@ protected function buildForm(AdminObjectAclData $data, FormBuilderInterface $for
*
* @return RoleSecurityIdentity|UserSecurityIdentity
*/
protected function getSecurityIdentity($aclValue)
protected function getSecurityIdentity($aclValue): SecurityIdentityInterface
{
return ($aclValue instanceof UserInterface)
? UserSecurityIdentity::fromAccount($aclValue)
Expand Down
Loading

0 comments on commit 6ba37c5

Please sign in to comment.