AdminObjectAclData.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 \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. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  134. */
  135. public function setAcl(Acl $acl)
  136. {
  137. $this->acl = $acl;
  138. return $this;
  139. }
  140. /**
  141. * Gets ACL
  142. *
  143. * @return \Symfony\Component\Security\Acl\Domain\Acl
  144. */
  145. public function getAcl()
  146. {
  147. return $this->acl;
  148. }
  149. /**
  150. * Gets masks
  151. *
  152. * @return array
  153. */
  154. public function getMasks()
  155. {
  156. return $this->masks;
  157. }
  158. /**
  159. * Sets form
  160. *
  161. * @param \Symfony\Component\Form\Form $form
  162. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  163. *
  164. * @deprecated Deprecated since version 2.4. Use setAclUsersForm() instead.
  165. */
  166. public function setForm(Form $form)
  167. {
  168. trigger_error('setForm() is deprecated since version 2.4. Use setAclUsersForm() instead.', E_USER_DEPRECATED);
  169. return $this->setAclUsersForm($form);
  170. }
  171. /**
  172. * Gets form
  173. *
  174. * @return \Symfony\Component\Form\Form
  175. *
  176. * @deprecated Deprecated since version 2.4. Use getAclUsersForm() instead.
  177. */
  178. public function getForm()
  179. {
  180. trigger_error('getForm() is deprecated since version 2.4. Use getAclUsersForm() instead.', E_USER_DEPRECATED);
  181. return $this->getAclUsersForm();
  182. }
  183. /**
  184. * Sets ACL users form
  185. *
  186. * @param \Symfony\Component\Form\Form $form
  187. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  188. */
  189. public function setAclUsersForm(Form $form)
  190. {
  191. $this->aclUsersForm = $form;
  192. return $this;
  193. }
  194. /**
  195. * Gets ACL users form
  196. *
  197. * @return \Symfony\Component\Form\Form
  198. */
  199. public function getAclUsersForm()
  200. {
  201. return $this->aclUsersForm;
  202. }
  203. /**
  204. * Sets ACL roles form
  205. *
  206. * @param \Symfony\Component\Form\Form $form
  207. * @return \Sonata\AdminBundle\Util\AdminObjectAclData
  208. */
  209. public function setAclRolesForm(Form $form)
  210. {
  211. $this->aclRolesForm = $form;
  212. return $this;
  213. }
  214. /**
  215. * Gets ACL roles form
  216. *
  217. * @return \Symfony\Component\Form\Form
  218. */
  219. public function getAclRolesForm()
  220. {
  221. return $this->aclRolesForm;
  222. }
  223. /**
  224. * Gets permissions
  225. *
  226. * @return array
  227. */
  228. public function getPermissions()
  229. {
  230. return $this->admin->getSecurityHandler()->getObjectPermissions();
  231. }
  232. /**
  233. * Get permissions that the current user can set
  234. *
  235. * @return array
  236. */
  237. public function getUserPermissions()
  238. {
  239. $permissions = $this->getPermissions();
  240. if (!$this->isOwner()) {
  241. foreach (self::$ownerPermissions as $permission) {
  242. $key = array_search($permission, $permissions);
  243. if ($key !== false) {
  244. unset($permissions[$key]);
  245. }
  246. }
  247. }
  248. return $permissions;
  249. }
  250. /**
  251. * Tests if the current user as the OWNER right
  252. *
  253. * @return boolean
  254. */
  255. public function isOwner()
  256. {
  257. // Only a owner can set MASTER and OWNER ACL
  258. return $this->admin->isGranted('OWNER', $this->object);
  259. }
  260. /**
  261. * Gets security handler
  262. *
  263. * @return \Sonata\AdminBundle\Security\Handler\SecurityHandlerInterface
  264. */
  265. public function getSecurityHandler()
  266. {
  267. return $this->admin->getSecurityHandler();
  268. }
  269. /**
  270. * @return array
  271. */
  272. public function getSecurityInformation()
  273. {
  274. return $this->admin->getSecurityHandler()->buildSecurityInformation($this->admin);
  275. }
  276. }