AdminObjectAclData.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 \Traversable Users to set ACL for
  35. */
  36. protected $aclUsers;
  37. /**
  38. * @var \Traversable Roles to set ACL for
  39. */
  40. protected $aclRoles;
  41. /**
  42. * @var array Cache of masks
  43. */
  44. protected $masks;
  45. /**
  46. * @var \Symfony\Component\Form\Form
  47. */
  48. protected $aclUsersForm;
  49. /**
  50. * @var \Symfony\Component\Form\Form
  51. */
  52. protected $aclRolesForm;
  53. /**
  54. * @var \Symfony\Component\Security\Acl\Domain\Acl
  55. */
  56. protected $acl;
  57. /**
  58. * @var string
  59. */
  60. protected $maskBuilderClass;
  61. /**
  62. * Cache masks.
  63. */
  64. protected function updateMasks()
  65. {
  66. $permissions = $this->getPermissions();
  67. $reflectionClass = new \ReflectionClass(new $this->maskBuilderClass());
  68. $this->masks = array();
  69. foreach ($permissions as $permission) {
  70. $this->masks[$permission] = $reflectionClass->getConstant('MASK_'.$permission);
  71. }
  72. }
  73. /**
  74. * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  75. * @param mixed $object
  76. * @param \Traversable $aclUsers
  77. * @param string $maskBuilderClass
  78. * @param \Traversable|null $aclRoles
  79. */
  80. public function __construct(
  81. AdminInterface $admin, $object,
  82. \Traversable $aclUsers,
  83. $maskBuilderClass,
  84. \Traversable $aclRoles = null
  85. ) {
  86. $this->admin = $admin;
  87. $this->object = $object;
  88. $this->aclUsers = $aclUsers;
  89. $this->aclRoles = (null === $aclRoles) ? new \ArrayIterator() : $aclRoles;
  90. $this->maskBuilderClass = $maskBuilderClass;
  91. $this->updateMasks();
  92. }
  93. /**
  94. * Gets admin.
  95. *
  96. * @return \Sonata\AdminBundle\Admin\AdminInterface
  97. */
  98. public function getAdmin()
  99. {
  100. return $this->admin;
  101. }
  102. /**
  103. * Gets object.
  104. *
  105. * @return mixed
  106. */
  107. public function getObject()
  108. {
  109. return $this->object;
  110. }
  111. /**
  112. * Gets ACL users.
  113. *
  114. * @return \Traversable
  115. */
  116. public function getAclUsers()
  117. {
  118. return $this->aclUsers;
  119. }
  120. /**
  121. * Gets ACL roles.
  122. *
  123. * @return \Traversable
  124. */
  125. public function getAclRoles()
  126. {
  127. return $this->aclRoles;
  128. }
  129. /**
  130. * Sets ACL.
  131. *
  132. * @param \Symfony\Component\Security\Acl\Domain\Acl $acl
  133. *
  134. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  135. */
  136. public function setAcl(Acl $acl)
  137. {
  138. $this->acl = $acl;
  139. return $this;
  140. }
  141. /**
  142. * Gets ACL.
  143. *
  144. * @return \Symfony\Component\Security\Acl\Domain\Acl
  145. */
  146. public function getAcl()
  147. {
  148. return $this->acl;
  149. }
  150. /**
  151. * Gets masks.
  152. *
  153. * @return array
  154. */
  155. public function getMasks()
  156. {
  157. return $this->masks;
  158. }
  159. /**
  160. * Sets form.
  161. *
  162. * @param \Symfony\Component\Form\Form $form
  163. *
  164. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  165. *
  166. * @deprecated Deprecated since version 2.4. Use setAclUsersForm() instead.
  167. */
  168. public function setForm(Form $form)
  169. {
  170. trigger_error('setForm() is deprecated since version 2.4. Use setAclUsersForm() instead.', E_USER_DEPRECATED);
  171. return $this->setAclUsersForm($form);
  172. }
  173. /**
  174. * Gets form.
  175. *
  176. * @return \Symfony\Component\Form\Form
  177. *
  178. * @deprecated Deprecated since version 2.4. Use getAclUsersForm() instead.
  179. */
  180. public function getForm()
  181. {
  182. trigger_error('getForm() is deprecated since version 2.4. Use getAclUsersForm() instead.', E_USER_DEPRECATED);
  183. return $this->getAclUsersForm();
  184. }
  185. /**
  186. * Sets ACL users form.
  187. *
  188. * @param \Symfony\Component\Form\Form $form
  189. *
  190. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  191. */
  192. public function setAclUsersForm(Form $form)
  193. {
  194. $this->aclUsersForm = $form;
  195. return $this;
  196. }
  197. /**
  198. * Gets ACL users form.
  199. *
  200. * @return \Symfony\Component\Form\Form
  201. */
  202. public function getAclUsersForm()
  203. {
  204. return $this->aclUsersForm;
  205. }
  206. /**
  207. * Sets ACL roles form.
  208. *
  209. * @param \Symfony\Component\Form\Form $form
  210. *
  211. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  212. */
  213. public function setAclRolesForm(Form $form)
  214. {
  215. $this->aclRolesForm = $form;
  216. return $this;
  217. }
  218. /**
  219. * Gets ACL roles form.
  220. *
  221. * @return \Symfony\Component\Form\Form
  222. */
  223. public function getAclRolesForm()
  224. {
  225. return $this->aclRolesForm;
  226. }
  227. /**
  228. * Gets permissions.
  229. *
  230. * @return array
  231. */
  232. public function getPermissions()
  233. {
  234. return $this->admin->getSecurityHandler()->getObjectPermissions();
  235. }
  236. /**
  237. * Get permissions that the current user can set.
  238. *
  239. * @return array
  240. */
  241. public function getUserPermissions()
  242. {
  243. $permissions = $this->getPermissions();
  244. if (!$this->isOwner()) {
  245. foreach (self::$ownerPermissions as $permission) {
  246. $key = array_search($permission, $permissions);
  247. if ($key !== false) {
  248. unset($permissions[$key]);
  249. }
  250. }
  251. }
  252. return $permissions;
  253. }
  254. /**
  255. * Tests if the current user as the OWNER right.
  256. *
  257. * @return bool
  258. */
  259. public function isOwner()
  260. {
  261. // Only a owner can set MASTER and OWNER ACL
  262. return $this->admin->isGranted('OWNER', $this->object);
  263. }
  264. /**
  265. * Gets security handler.
  266. *
  267. * @return \Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface
  268. */
  269. public function getSecurityHandler()
  270. {
  271. return $this->admin->getSecurityHandler();
  272. }
  273. /**
  274. * @return array
  275. */
  276. public function getSecurityInformation()
  277. {
  278. return $this->admin->getSecurityHandler()->buildSecurityInformation($this->admin);
  279. }
  280. }