PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sonata-project/admin-bundle/Util/AdminObjectAclData.php

https://gitlab.com/cuza/Clinic_Recods
PHP | 319 lines | 123 code | 42 blank | 154 comment | 2 complexity | d1f691f4836e1d4e10b8dda9460c9a24 MD5 | raw file
  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. * Cache masks.
  64. */
  65. protected function updateMasks()
  66. {
  67. $permissions = $this->getPermissions();
  68. $reflectionClass = new \ReflectionClass(new $this->maskBuilderClass());
  69. $this->masks = array();
  70. foreach ($permissions as $permission) {
  71. $this->masks[$permission] = $reflectionClass->getConstant('MASK_'.$permission);
  72. }
  73. }
  74. /**
  75. * @param AdminInterface $admin
  76. * @param mixed $object
  77. * @param \Traversable $aclUsers
  78. * @param string $maskBuilderClass
  79. * @param \Traversable|null $aclRoles
  80. */
  81. public function __construct(AdminInterface $admin, $object, \Traversable $aclUsers, $maskBuilderClass, \Traversable $aclRoles = null)
  82. {
  83. $this->admin = $admin;
  84. $this->object = $object;
  85. $this->aclUsers = $aclUsers;
  86. $this->aclRoles = (null === $aclRoles) ? new \ArrayIterator() : $aclRoles;
  87. $this->maskBuilderClass = $maskBuilderClass;
  88. $this->updateMasks();
  89. }
  90. /**
  91. * Gets admin.
  92. *
  93. * @return AdminInterface
  94. */
  95. public function getAdmin()
  96. {
  97. return $this->admin;
  98. }
  99. /**
  100. * Gets object.
  101. *
  102. * @return mixed
  103. */
  104. public function getObject()
  105. {
  106. return $this->object;
  107. }
  108. /**
  109. * Gets ACL users.
  110. *
  111. * @return \Traversable
  112. */
  113. public function getAclUsers()
  114. {
  115. return $this->aclUsers;
  116. }
  117. /**
  118. * Gets ACL roles.
  119. *
  120. * @return \Traversable
  121. */
  122. public function getAclRoles()
  123. {
  124. return $this->aclRoles;
  125. }
  126. /**
  127. * Sets ACL.
  128. *
  129. * @param Acl $acl
  130. *
  131. * @return AdminObjectAclData
  132. */
  133. public function setAcl(Acl $acl)
  134. {
  135. $this->acl = $acl;
  136. return $this;
  137. }
  138. /**
  139. * Gets ACL.
  140. *
  141. * @return Acl
  142. */
  143. public function getAcl()
  144. {
  145. return $this->acl;
  146. }
  147. /**
  148. * Gets masks.
  149. *
  150. * @return array
  151. */
  152. public function getMasks()
  153. {
  154. return $this->masks;
  155. }
  156. /**
  157. * Sets form.
  158. *
  159. * @param Form $form
  160. *
  161. * @return AdminObjectAclData
  162. *
  163. * @deprecated Deprecated since version 3.0. Use setAclUsersForm() instead.
  164. */
  165. public function setForm(Form $form)
  166. {
  167. trigger_error('setForm() is deprecated since version 3.0. Use setAclUsersForm() instead.', E_USER_DEPRECATED);
  168. return $this->setAclUsersForm($form);
  169. }
  170. /**
  171. * Gets form.
  172. *
  173. * @return Form
  174. *
  175. * @deprecated Deprecated since version 3.0. Use getAclUsersForm() instead.
  176. */
  177. public function getForm()
  178. {
  179. trigger_error('getForm() is deprecated since version 3.0. Use getAclUsersForm() instead.', E_USER_DEPRECATED);
  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 as 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. }