PageRenderTime 46ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/cuza/Clinic_Recods
PHP | 295 lines | 162 code | 39 blank | 94 comment | 16 complexity | b9eea81775aff8233dbee7aebaf25b24 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\Form\Type\AclMatrixType;
  12. use Symfony\Component\Form\Form;
  13. use Symfony\Component\Form\FormBuilderInterface;
  14. use Symfony\Component\Form\FormFactoryInterface;
  15. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  16. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  17. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  18. use Symfony\Component\Security\Acl\Exception\NoAceFoundException;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. /**
  21. * A manipulator for updating ACL related to an object.
  22. *
  23. * @author Kévin Dunglas <kevin@les-tilleuls.coop>
  24. * @author Baptiste Meyer <baptiste@les-tilleuls.coop>
  25. */
  26. class AdminObjectAclManipulator
  27. {
  28. const ACL_USERS_FORM_NAME = 'acl_users_form';
  29. const ACL_ROLES_FORM_NAME = 'acl_roles_form';
  30. /**
  31. * @var FormFactoryInterface
  32. */
  33. protected $formFactory;
  34. /**
  35. * @var string
  36. */
  37. protected $maskBuilderClass;
  38. /**
  39. * @param FormFactoryInterface $formFactory
  40. * @param string $maskBuilderClass
  41. */
  42. public function __construct(FormFactoryInterface $formFactory, $maskBuilderClass)
  43. {
  44. $this->formFactory = $formFactory;
  45. $this->maskBuilderClass = $maskBuilderClass;
  46. }
  47. /**
  48. * Gets mask builder class name.
  49. *
  50. * @return string
  51. */
  52. public function getMaskBuilderClass()
  53. {
  54. return $this->maskBuilderClass;
  55. }
  56. /**
  57. * Gets the form.
  58. *
  59. * @param AdminObjectAclData $data
  60. *
  61. * @return Form
  62. *
  63. * @deprecated Deprecated since version 3.0. Use createAclUsersForm() instead.
  64. */
  65. public function createForm(AdminObjectAclData $data)
  66. {
  67. trigger_error('createForm() is deprecated since version 3.0. Use createAclUsersForm() instead.', E_USER_DEPRECATED);
  68. return $this->createAclUsersForm($data);
  69. }
  70. /**
  71. * Gets the ACL users form.
  72. *
  73. * @param AdminObjectAclData $data
  74. *
  75. * @return Form
  76. */
  77. public function createAclUsersForm(AdminObjectAclData $data)
  78. {
  79. $aclValues = $data->getAclUsers();
  80. $formBuilder = $this->formFactory->createNamedBuilder(self::ACL_USERS_FORM_NAME, 'form');
  81. $form = $this->buildForm($data, $formBuilder, $aclValues);
  82. $data->setAclUsersForm($form);
  83. return $form;
  84. }
  85. /**
  86. * Gets the ACL roles form.
  87. *
  88. * @param AdminObjectAclData $data
  89. *
  90. * @return Form
  91. */
  92. public function createAclRolesForm(AdminObjectAclData $data)
  93. {
  94. $aclValues = $data->getAclRoles();
  95. $formBuilder = $this->formFactory->createNamedBuilder(self::ACL_ROLES_FORM_NAME, 'form');
  96. $form = $this->buildForm($data, $formBuilder, $aclValues);
  97. $data->setAclRolesForm($form);
  98. return $form;
  99. }
  100. /**
  101. * Updates ACL users.
  102. *
  103. * @param AdminObjectAclData $data
  104. */
  105. public function updateAclUsers(AdminObjectAclData $data)
  106. {
  107. $aclValues = $data->getAclUsers();
  108. $form = $data->getAclUsersForm();
  109. $this->buildAcl($data, $form, $aclValues);
  110. }
  111. /**
  112. * Updates ACL roles.
  113. *
  114. * @param AdminObjectAclData $data
  115. */
  116. public function updateAclRoles(AdminObjectAclData $data)
  117. {
  118. $aclValues = $data->getAclRoles();
  119. $form = $data->getAclRolesForm();
  120. $this->buildAcl($data, $form, $aclValues);
  121. }
  122. /**
  123. * Updates ACl.
  124. *
  125. * @param AdminObjectAclData $data
  126. *
  127. * @deprecated Deprecated since version 3.0. Use updateAclUsers() instead.
  128. */
  129. public function updateAcl(AdminObjectAclData $data)
  130. {
  131. trigger_error('updateAcl() is deprecated since version 3.0. Use updateAclUsers() instead.', E_USER_DEPRECATED);
  132. $this->updateAclUsers($data);
  133. }
  134. /**
  135. * Builds ACL.
  136. *
  137. * @param AdminObjectAclData $data
  138. * @param Form $form
  139. * @param \Traversable $aclValues
  140. */
  141. protected function buildAcl(AdminObjectAclData $data, Form $form, \Traversable $aclValues)
  142. {
  143. $masks = $data->getMasks();
  144. $acl = $data->getAcl();
  145. $matrices = $form->getData();
  146. foreach ($aclValues as $aclValue) {
  147. foreach ($matrices as $key => $matrix) {
  148. if ($aclValue instanceof UserInterface) {
  149. if (array_key_exists('user', $matrix) && $aclValue->getUsername() === $matrix['user']) {
  150. $matrices[$key]['acl_value'] = $aclValue;
  151. }
  152. } elseif (array_key_exists('role', $matrix) && $aclValue === $matrix['role']) {
  153. $matrices[$key]['acl_value'] = $aclValue;
  154. }
  155. }
  156. }
  157. foreach ($matrices as $matrix) {
  158. if (!isset($matrix['acl_value'])) {
  159. continue;
  160. }
  161. $securityIdentity = $this->getSecurityIdentity($matrix['acl_value']);
  162. $maskBuilder = new $this->maskBuilderClass();
  163. foreach ($data->getUserPermissions() as $permission) {
  164. if (isset($matrix[$permission]) && $matrix[$permission] === true) {
  165. $maskBuilder->add($permission);
  166. }
  167. }
  168. // Restore OWNER and MASTER permissions
  169. if (!$data->isOwner()) {
  170. foreach ($data->getOwnerPermissions() as $permission) {
  171. if ($acl->isGranted(array($masks[$permission]), array($securityIdentity))) {
  172. $maskBuilder->add($permission);
  173. }
  174. }
  175. }
  176. $mask = $maskBuilder->get();
  177. $index = null;
  178. $ace = null;
  179. foreach ($acl->getObjectAces() as $currentIndex => $currentAce) {
  180. if ($currentAce->getSecurityIdentity()->equals($securityIdentity)) {
  181. $index = $currentIndex;
  182. $ace = $currentAce;
  183. break;
  184. }
  185. }
  186. if ($ace) {
  187. $acl->updateObjectAce($index, $mask);
  188. } else {
  189. $acl->insertObjectAce($securityIdentity, $mask);
  190. }
  191. }
  192. $data->getSecurityHandler()->updateAcl($acl);
  193. }
  194. /**
  195. * Builds the form.
  196. *
  197. * @param AdminObjectAclData $data
  198. * @param FormBuilderInterface $formBuilder
  199. * @param \Traversable $aclValues
  200. *
  201. * @return Form
  202. */
  203. protected function buildForm(AdminObjectAclData $data, FormBuilderInterface $formBuilder, \Traversable $aclValues)
  204. {
  205. // Retrieve object identity
  206. $objectIdentity = ObjectIdentity::fromDomainObject($data->getObject());
  207. $acl = $data->getSecurityHandler()->getObjectAcl($objectIdentity);
  208. if (!$acl) {
  209. $acl = $data->getSecurityHandler()->createAcl($objectIdentity);
  210. }
  211. $data->setAcl($acl);
  212. $masks = $data->getMasks();
  213. $securityInformation = $data->getSecurityInformation();
  214. foreach ($aclValues as $key => $aclValue) {
  215. $securityIdentity = $this->getSecurityIdentity($aclValue);
  216. $permissions = array();
  217. foreach ($data->getUserPermissions() as $permission) {
  218. try {
  219. $checked = $acl->isGranted(array($masks[$permission]), array($securityIdentity));
  220. } catch (NoAceFoundException $e) {
  221. $checked = false;
  222. }
  223. $attr = array();
  224. if (
  225. self::ACL_ROLES_FORM_NAME === $formBuilder->getName()
  226. && isset($securityInformation[$aclValue])
  227. && array_search($permission, $securityInformation[$aclValue]) !== false
  228. ) {
  229. $attr['disabled'] = 'disabled';
  230. }
  231. $permissions[$permission] = array(
  232. 'required' => false,
  233. 'data' => $checked,
  234. 'disabled' => array_key_exists('disabled', $attr),
  235. 'attr' => $attr,
  236. );
  237. }
  238. $formBuilder->add($key, new AclMatrixType(), array('permissions' => $permissions, 'acl_value' => $aclValue));
  239. }
  240. return $formBuilder->getForm();
  241. }
  242. /**
  243. * Gets a user or a role security identity.
  244. *
  245. * @param string|UserInterface $aclValue
  246. *
  247. * @return RoleSecurityIdentity|UserSecurityIdentity
  248. */
  249. protected function getSecurityIdentity($aclValue)
  250. {
  251. return ($aclValue instanceof UserInterface)
  252. ? UserSecurityIdentity::fromAccount($aclValue)
  253. : new RoleSecurityIdentity($aclValue)
  254. ;
  255. }
  256. }