AdminObjectAclData.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Util;
  11. use Sonata\AdminBundle\Admin\AdminInterface;
  12. use Symfony\Component\Form\Form;
  13. use Symfony\Component\Security\Acl\Domain\Acl;
  14. /**
  15. * AdminObjectAclData holds data manipulated by {@link AdminObjectAclManipulator}.
  16. *
  17. * @author Kévin Dunglas <kevin@les-tilleuls.coop>
  18. */
  19. class AdminObjectAclData
  20. {
  21. /**
  22. * @var array Permissions managed only by a OWNER
  23. */
  24. protected static $ownerPermissions = array('MASTER', 'OWNER');
  25. /**
  26. * @var \Sonata\AdminBundle\Admin\AdminInterface
  27. */
  28. protected $admin;
  29. /**
  30. * @var mixed
  31. */
  32. protected $object;
  33. /**
  34. * @var array Users to set ACL for
  35. */
  36. protected $aclUsers;
  37. /**
  38. * @var array Cache of masks
  39. */
  40. protected $masks;
  41. /**
  42. * @var \Symfony\Component\Form\Form
  43. */
  44. protected $form;
  45. /**
  46. * @var \Symfony\Component\Security\Acl\Domain\Acl
  47. */
  48. protected $acl;
  49. /**
  50. * @var string
  51. */
  52. protected $maskBuilderClass;
  53. /**
  54. * Cache masks.
  55. */
  56. protected function updateMasks()
  57. {
  58. $permissions = $this->getPermissions();
  59. $reflectionClass = new \ReflectionClass(new $this->maskBuilderClass());
  60. $this->masks = array();
  61. foreach ($permissions as $permission) {
  62. $this->masks[$permission] = $reflectionClass->getConstant('MASK_'.$permission);
  63. }
  64. }
  65. /**
  66. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  67. * @param mixed $object
  68. * @param \Traversable $aclUsers
  69. * @param string $maskBuilderClass
  70. */
  71. public function __construct(AdminInterface $admin, $object, \Traversable $aclUsers, $maskBuilderClass)
  72. {
  73. $this->admin = $admin;
  74. $this->object = $object;
  75. $this->aclUsers = $aclUsers;
  76. $this->maskBuilderClass = $maskBuilderClass;
  77. $this->updateMasks();
  78. }
  79. /**
  80. * Gets admin.
  81. *
  82. * @return \Sonata\AdminBundle\Admin\AdminInterface
  83. */
  84. public function getAdmin()
  85. {
  86. return $this->admin;
  87. }
  88. /**
  89. * Gets object.
  90. *
  91. * @return mixed
  92. */
  93. public function getObject()
  94. {
  95. return $this->object;
  96. }
  97. /**
  98. * Gets ACL users.
  99. *
  100. * @return array
  101. */
  102. public function getAclUsers()
  103. {
  104. return $this->aclUsers;
  105. }
  106. /**
  107. * Sets ACL.
  108. *
  109. * @param \Symfony\Component\Security\Acl\Domain\Acl $acl
  110. *
  111. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  112. */
  113. public function setAcl(Acl $acl)
  114. {
  115. $this->acl = $acl;
  116. return $this;
  117. }
  118. /**
  119. * Gets ACL.
  120. *
  121. * @return \Symfony\Component\Security\Acl\Domain\Acl
  122. */
  123. public function getAcl()
  124. {
  125. return $this->acl;
  126. }
  127. /**
  128. * Gets masks.
  129. *
  130. * @return array
  131. */
  132. public function getMasks()
  133. {
  134. return $this->masks;
  135. }
  136. /**
  137. * Sets form.
  138. *
  139. * @param \Symfony\Component\Form\Form $form
  140. *
  141. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  142. */
  143. public function setForm(Form $form)
  144. {
  145. $this->form = $form;
  146. return $this;
  147. }
  148. /**
  149. * Gets form.
  150. *
  151. * @return \Symfony\Component\Form\Form
  152. */
  153. public function getForm()
  154. {
  155. return $this->form;
  156. }
  157. /**
  158. * Gets permissions.
  159. *
  160. * @return array
  161. */
  162. public function getPermissions()
  163. {
  164. return $this->admin->getSecurityHandler()->getObjectPermissions();
  165. }
  166. /**
  167. * Get permissions that the current user can set.
  168. *
  169. * @return array
  170. */
  171. public function getUserPermissions()
  172. {
  173. $permissions = $this->getPermissions();
  174. if (!$this->isOwner()) {
  175. foreach (self::$ownerPermissions as $permission) {
  176. $key = array_search($permission, $permissions);
  177. if ($key !== false) {
  178. unset($permissions[$key]);
  179. }
  180. }
  181. }
  182. return $permissions;
  183. }
  184. /**
  185. * Tests if the current user as the OWNER right.
  186. *
  187. * @return bool
  188. */
  189. public function isOwner()
  190. {
  191. // Only a owner can set MASTER and OWNER ACL
  192. return $this->admin->isGranted('OWNER', $this->object);
  193. }
  194. /**
  195. * Gets security handler.
  196. *
  197. * @return \Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface
  198. */
  199. public function getSecurityHandler()
  200. {
  201. return $this->admin->getSecurityHandler();
  202. }
  203. }