123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 |
- <?php
- /*
- * This file is part of the Sonata Project package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Util;
- use Sonata\AdminBundle\Admin\AdminInterface;
- use Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface;
- use Symfony\Component\Form\Form;
- use Symfony\Component\Security\Acl\Domain\Acl;
- /**
- * AdminObjectAclData holds data manipulated by {@link AdminObjectAclManipulator}.
- *
- * @author Kévin Dunglas <kevin@les-tilleuls.coop>
- */
- class AdminObjectAclData
- {
- /**
- * @var array Permissions managed only by a OWNER
- */
- protected static $ownerPermissions = array('MASTER', 'OWNER');
- /**
- * @var AdminInterface
- */
- protected $admin;
- /**
- * @var mixed
- */
- protected $object;
- /**
- * @var \Traversable Users to set ACL for
- */
- protected $aclUsers;
- /**
- * @var \Traversable Roles to set ACL for
- */
- protected $aclRoles;
- /**
- * @var array Cache of masks
- */
- protected $masks;
- /**
- * @var Form
- */
- protected $aclUsersForm;
- /**
- * @var Form
- */
- protected $aclRolesForm;
- /**
- * @var Acl
- */
- protected $acl;
- /**
- * @var string
- */
- protected $maskBuilderClass;
- /**
- * @param AdminInterface $admin
- * @param mixed $object
- * @param \Traversable $aclUsers
- * @param string $maskBuilderClass
- * @param \Traversable|null $aclRoles
- */
- public function __construct(AdminInterface $admin, $object, \Traversable $aclUsers, $maskBuilderClass, \Traversable $aclRoles = null)
- {
- $this->admin = $admin;
- $this->object = $object;
- $this->aclUsers = $aclUsers;
- $this->aclRoles = (null === $aclRoles) ? new \ArrayIterator() : $aclRoles;
- $this->maskBuilderClass = $maskBuilderClass;
- $this->updateMasks();
- }
- /**
- * Gets admin.
- *
- * @return AdminInterface
- */
- public function getAdmin()
- {
- return $this->admin;
- }
- /**
- * Gets object.
- *
- * @return mixed
- */
- public function getObject()
- {
- return $this->object;
- }
- /**
- * Gets ACL users.
- *
- * @return \Traversable
- */
- public function getAclUsers()
- {
- return $this->aclUsers;
- }
- /**
- * Gets ACL roles.
- *
- * @return \Traversable
- */
- public function getAclRoles()
- {
- return $this->aclRoles;
- }
- /**
- * Sets ACL.
- *
- * @param Acl $acl
- *
- * @return AdminObjectAclData
- */
- public function setAcl(Acl $acl)
- {
- $this->acl = $acl;
- return $this;
- }
- /**
- * Gets ACL.
- *
- * @return Acl
- */
- public function getAcl()
- {
- return $this->acl;
- }
- /**
- * Gets masks.
- *
- * @return array
- */
- public function getMasks()
- {
- return $this->masks;
- }
- /**
- * Sets form.
- *
- * NEXT_MAJOR: remove this method.
- *
- * @param Form $form
- *
- * @return AdminObjectAclData
- *
- * @deprecated Deprecated since version 3.0. Use setAclUsersForm() instead
- */
- public function setForm(Form $form)
- {
- @trigger_error(
- 'setForm() is deprecated since version 3.0 and will be removed in 4.0. '
- .'Use setAclUsersForm() instead.',
- E_USER_DEPRECATED
- );
- return $this->setAclUsersForm($form);
- }
- /**
- * Gets form.
- *
- * NEXT_MAJOR: remove this method.
- *
- * @return Form
- *
- * @deprecated Deprecated since version 3.0. Use getAclUsersForm() instead
- */
- public function getForm()
- {
- @trigger_error(
- 'getForm() is deprecated since version 3.0 and will be removed in 4.0. '
- .'Use getAclUsersForm() instead.',
- E_USER_DEPRECATED
- );
- return $this->getAclUsersForm();
- }
- /**
- * Sets ACL users form.
- *
- * @param Form $form
- *
- * @return AdminObjectAclData
- */
- public function setAclUsersForm(Form $form)
- {
- $this->aclUsersForm = $form;
- return $this;
- }
- /**
- * Gets ACL users form.
- *
- * @return Form
- */
- public function getAclUsersForm()
- {
- return $this->aclUsersForm;
- }
- /**
- * Sets ACL roles form.
- *
- * @param Form $form
- *
- * @return AdminObjectAclData
- */
- public function setAclRolesForm(Form $form)
- {
- $this->aclRolesForm = $form;
- return $this;
- }
- /**
- * Gets ACL roles form.
- *
- * @return Form
- */
- public function getAclRolesForm()
- {
- return $this->aclRolesForm;
- }
- /**
- * Gets permissions.
- *
- * @return array
- */
- public function getPermissions()
- {
- return $this->admin->getSecurityHandler()->getObjectPermissions();
- }
- /**
- * Get permissions that the current user can set.
- *
- * @return array
- */
- public function getUserPermissions()
- {
- $permissions = $this->getPermissions();
- if (!$this->isOwner()) {
- foreach (self::$ownerPermissions as $permission) {
- $key = array_search($permission, $permissions);
- if ($key !== false) {
- unset($permissions[$key]);
- }
- }
- }
- return $permissions;
- }
- /**
- * Tests if the current user has the OWNER right.
- *
- * @return bool
- */
- public function isOwner()
- {
- // Only a owner can set MASTER and OWNER ACL
- return $this->admin->isGranted('OWNER', $this->object);
- }
- /**
- * Gets security handler.
- *
- * @return SecurityHandlerInterface
- */
- public function getSecurityHandler()
- {
- return $this->admin->getSecurityHandler();
- }
- /**
- * @return array
- */
- public function getSecurityInformation()
- {
- return $this->admin->getSecurityHandler()->buildSecurityInformation($this->admin);
- }
- /**
- * Cache masks.
- */
- protected function updateMasks()
- {
- $permissions = $this->getPermissions();
- $reflectionClass = new \ReflectionClass(new $this->maskBuilderClass());
- $this->masks = array();
- foreach ($permissions as $permission) {
- $this->masks[$permission] = $reflectionClass->getConstant('MASK_'.$permission);
- }
- }
- }
|