PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/Util/AdminObjectAclData.php

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