AdminObjectAclData.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. * @param Form $form
  148. *
  149. * @return AdminObjectAclData
  150. *
  151. * @deprecated Deprecated since version 3.0. Use setAclUsersForm() instead
  152. */
  153. public function setForm(Form $form)
  154. {
  155. @trigger_error(
  156. 'setForm() is deprecated since version 3.0 and will be removed in 4.0. '
  157. .'Use setAclUsersForm() instead.',
  158. E_USER_DEPRECATED
  159. );
  160. return $this->setAclUsersForm($form);
  161. }
  162. /**
  163. * Gets form.
  164. *
  165. * @return Form
  166. *
  167. * @deprecated Deprecated since version 3.0. Use getAclUsersForm() instead
  168. */
  169. public function getForm()
  170. {
  171. @trigger_error(
  172. 'getForm() is deprecated since version 3.0 and will be removed in 4.0. '
  173. .'Use getAclUsersForm() instead.',
  174. E_USER_DEPRECATED
  175. );
  176. return $this->getAclUsersForm();
  177. }
  178. /**
  179. * Sets ACL users form.
  180. *
  181. * @param Form $form
  182. *
  183. * @return AdminObjectAclData
  184. */
  185. public function setAclUsersForm(Form $form)
  186. {
  187. $this->aclUsersForm = $form;
  188. return $this;
  189. }
  190. /**
  191. * Gets ACL users form.
  192. *
  193. * @return Form
  194. */
  195. public function getAclUsersForm()
  196. {
  197. return $this->aclUsersForm;
  198. }
  199. /**
  200. * Sets ACL roles form.
  201. *
  202. * @param Form $form
  203. *
  204. * @return AdminObjectAclData
  205. */
  206. public function setAclRolesForm(Form $form)
  207. {
  208. $this->aclRolesForm = $form;
  209. return $this;
  210. }
  211. /**
  212. * Gets ACL roles form.
  213. *
  214. * @return Form
  215. */
  216. public function getAclRolesForm()
  217. {
  218. return $this->aclRolesForm;
  219. }
  220. /**
  221. * Gets permissions.
  222. *
  223. * @return array
  224. */
  225. public function getPermissions()
  226. {
  227. return $this->admin->getSecurityHandler()->getObjectPermissions();
  228. }
  229. /**
  230. * Get permissions that the current user can set.
  231. *
  232. * @return array
  233. */
  234. public function getUserPermissions()
  235. {
  236. $permissions = $this->getPermissions();
  237. if (!$this->isOwner()) {
  238. foreach (self::$ownerPermissions as $permission) {
  239. $key = array_search($permission, $permissions);
  240. if ($key !== false) {
  241. unset($permissions[$key]);
  242. }
  243. }
  244. }
  245. return $permissions;
  246. }
  247. /**
  248. * Tests if the current user has the OWNER right.
  249. *
  250. * @return bool
  251. */
  252. public function isOwner()
  253. {
  254. // Only a owner can set MASTER and OWNER ACL
  255. return $this->admin->isGranted('OWNER', $this->object);
  256. }
  257. /**
  258. * Gets security handler.
  259. *
  260. * @return SecurityHandlerInterface
  261. */
  262. public function getSecurityHandler()
  263. {
  264. return $this->admin->getSecurityHandler();
  265. }
  266. /**
  267. * @return array
  268. */
  269. public function getSecurityInformation()
  270. {
  271. return $this->admin->getSecurityHandler()->buildSecurityInformation($this->admin);
  272. }
  273. /**
  274. * Cache masks.
  275. */
  276. protected function updateMasks()
  277. {
  278. $permissions = $this->getPermissions();
  279. $reflectionClass = new \ReflectionClass(new $this->maskBuilderClass());
  280. $this->masks = array();
  281. foreach ($permissions as $permission) {
  282. $this->masks[$permission] = $reflectionClass->getConstant('MASK_'.$permission);
  283. }
  284. }
  285. }