AdminObjectAclData.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  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 Symfony\Component\Form\Form;
  12. use Symfony\Component\Security\Acl\Domain\Acl;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  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. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  111. */
  112. public function setAcl(Acl $acl)
  113. {
  114. $this->acl = $acl;
  115. return $this;
  116. }
  117. /**
  118. * Gets ACL
  119. *
  120. * @return \Symfony\Component\Security\Acl\Domain\Acl
  121. */
  122. public function getAcl()
  123. {
  124. return $this->acl;
  125. }
  126. /**
  127. * Gets masks
  128. *
  129. * @return array
  130. */
  131. public function getMasks()
  132. {
  133. return $this->masks;
  134. }
  135. /**
  136. * Sets form
  137. *
  138. * @param \Symfony\Component\Form\Form $form
  139. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  140. */
  141. public function setForm(Form $form)
  142. {
  143. $this->form = $form;
  144. return $this;
  145. }
  146. /**
  147. * Gets form
  148. *
  149. * @return \Symfony\Component\Form\Form
  150. */
  151. public function getForm()
  152. {
  153. return $this->form;
  154. }
  155. /**
  156. * Gets permissions
  157. *
  158. * @return array
  159. */
  160. public function getPermissions()
  161. {
  162. return $this->admin->getSecurityHandler()->getObjectPermissions();
  163. }
  164. /**
  165. * Get permissions that the current user can set
  166. *
  167. * @return array
  168. */
  169. public function getUserPermissions()
  170. {
  171. $permissions = $this->getPermissions();
  172. if (!$this->isOwner()) {
  173. foreach (self::$ownerPermissions as $permission) {
  174. $key = array_search($permission, $permissions);
  175. if ($key !== false) {
  176. unset($permissions[$key]);
  177. }
  178. }
  179. }
  180. return $permissions;
  181. }
  182. /**
  183. * Tests if the current user as the OWNER right
  184. *
  185. * @return boolean
  186. */
  187. public function isOwner()
  188. {
  189. // Only a owner can set MASTER and OWNER ACL
  190. return $this->admin->isGranted('OWNER', $this->object);
  191. }
  192. /**
  193. * Gets security handler
  194. *
  195. * @return \Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface
  196. */
  197. public function getSecurityHandler()
  198. {
  199. return $this->admin->getSecurityHandler();
  200. }
  201. }