AdminObjectAclData.php 6.5 KB

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