AdminObjectAclManipulator.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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(
  68. 'createForm() is deprecated since version 3.0 and will be removed in 4.0. '
  69. .'Use createAclUsersForm() instead.',
  70. E_USER_DEPRECATED
  71. );
  72. return $this->createAclUsersForm($data);
  73. }
  74. /**
  75. * Gets the ACL users form.
  76. *
  77. * @param AdminObjectAclData $data
  78. *
  79. * @return Form
  80. */
  81. public function createAclUsersForm(AdminObjectAclData $data)
  82. {
  83. $aclValues = $data->getAclUsers();
  84. $formBuilder = $this->formFactory->createNamedBuilder(self::ACL_USERS_FORM_NAME, 'form');
  85. $form = $this->buildForm($data, $formBuilder, $aclValues);
  86. $data->setAclUsersForm($form);
  87. return $form;
  88. }
  89. /**
  90. * Gets the ACL roles form.
  91. *
  92. * @param AdminObjectAclData $data
  93. *
  94. * @return Form
  95. */
  96. public function createAclRolesForm(AdminObjectAclData $data)
  97. {
  98. $aclValues = $data->getAclRoles();
  99. $formBuilder = $this->formFactory->createNamedBuilder(self::ACL_ROLES_FORM_NAME, 'form');
  100. $form = $this->buildForm($data, $formBuilder, $aclValues);
  101. $data->setAclRolesForm($form);
  102. return $form;
  103. }
  104. /**
  105. * Updates ACL users.
  106. *
  107. * @param AdminObjectAclData $data
  108. */
  109. public function updateAclUsers(AdminObjectAclData $data)
  110. {
  111. $aclValues = $data->getAclUsers();
  112. $form = $data->getAclUsersForm();
  113. $this->buildAcl($data, $form, $aclValues);
  114. }
  115. /**
  116. * Updates ACL roles.
  117. *
  118. * @param AdminObjectAclData $data
  119. */
  120. public function updateAclRoles(AdminObjectAclData $data)
  121. {
  122. $aclValues = $data->getAclRoles();
  123. $form = $data->getAclRolesForm();
  124. $this->buildAcl($data, $form, $aclValues);
  125. }
  126. /**
  127. * Updates ACl.
  128. *
  129. * @param AdminObjectAclData $data
  130. *
  131. * @deprecated Deprecated since version 3.0. Use updateAclUsers() instead
  132. */
  133. public function updateAcl(AdminObjectAclData $data)
  134. {
  135. @trigger_error(
  136. 'updateAcl() is deprecated since version 3.0 and will be removed in 4.0.'
  137. .'Use updateAclUsers() instead.',
  138. E_USER_DEPRECATED
  139. );
  140. $this->updateAclUsers($data);
  141. }
  142. /**
  143. * Builds ACL.
  144. *
  145. * @param AdminObjectAclData $data
  146. * @param Form $form
  147. * @param \Traversable $aclValues
  148. */
  149. protected function buildAcl(AdminObjectAclData $data, Form $form, \Traversable $aclValues)
  150. {
  151. $masks = $data->getMasks();
  152. $acl = $data->getAcl();
  153. $matrices = $form->getData();
  154. foreach ($aclValues as $aclValue) {
  155. foreach ($matrices as $key => $matrix) {
  156. if ($aclValue instanceof UserInterface) {
  157. if (array_key_exists('user', $matrix) && $aclValue->getUsername() === $matrix['user']) {
  158. $matrices[$key]['acl_value'] = $aclValue;
  159. }
  160. } elseif (array_key_exists('role', $matrix) && $aclValue === $matrix['role']) {
  161. $matrices[$key]['acl_value'] = $aclValue;
  162. }
  163. }
  164. }
  165. foreach ($matrices as $matrix) {
  166. if (!isset($matrix['acl_value'])) {
  167. continue;
  168. }
  169. $securityIdentity = $this->getSecurityIdentity($matrix['acl_value']);
  170. $maskBuilder = new $this->maskBuilderClass();
  171. foreach ($data->getUserPermissions() as $permission) {
  172. if (isset($matrix[$permission]) && $matrix[$permission] === true) {
  173. $maskBuilder->add($permission);
  174. }
  175. }
  176. // Restore OWNER and MASTER permissions
  177. if (!$data->isOwner()) {
  178. foreach ($data->getOwnerPermissions() as $permission) {
  179. if ($acl->isGranted(array($masks[$permission]), array($securityIdentity))) {
  180. $maskBuilder->add($permission);
  181. }
  182. }
  183. }
  184. $mask = $maskBuilder->get();
  185. $index = null;
  186. $ace = null;
  187. foreach ($acl->getObjectAces() as $currentIndex => $currentAce) {
  188. if ($currentAce->getSecurityIdentity()->equals($securityIdentity)) {
  189. $index = $currentIndex;
  190. $ace = $currentAce;
  191. break;
  192. }
  193. }
  194. if ($ace) {
  195. $acl->updateObjectAce($index, $mask);
  196. } else {
  197. $acl->insertObjectAce($securityIdentity, $mask);
  198. }
  199. }
  200. $data->getSecurityHandler()->updateAcl($acl);
  201. }
  202. /**
  203. * Builds the form.
  204. *
  205. * @param AdminObjectAclData $data
  206. * @param FormBuilderInterface $formBuilder
  207. * @param \Traversable $aclValues
  208. *
  209. * @return Form
  210. */
  211. protected function buildForm(AdminObjectAclData $data, FormBuilderInterface $formBuilder, \Traversable $aclValues)
  212. {
  213. // Retrieve object identity
  214. $objectIdentity = ObjectIdentity::fromDomainObject($data->getObject());
  215. $acl = $data->getSecurityHandler()->getObjectAcl($objectIdentity);
  216. if (!$acl) {
  217. $acl = $data->getSecurityHandler()->createAcl($objectIdentity);
  218. }
  219. $data->setAcl($acl);
  220. $masks = $data->getMasks();
  221. $securityInformation = $data->getSecurityInformation();
  222. foreach ($aclValues as $key => $aclValue) {
  223. $securityIdentity = $this->getSecurityIdentity($aclValue);
  224. $permissions = array();
  225. foreach ($data->getUserPermissions() as $permission) {
  226. try {
  227. $checked = $acl->isGranted(array($masks[$permission]), array($securityIdentity));
  228. } catch (NoAceFoundException $e) {
  229. $checked = false;
  230. }
  231. $attr = array();
  232. if (
  233. self::ACL_ROLES_FORM_NAME === $formBuilder->getName()
  234. && isset($securityInformation[$aclValue])
  235. && array_search($permission, $securityInformation[$aclValue]) !== false
  236. ) {
  237. $attr['disabled'] = 'disabled';
  238. }
  239. $permissions[$permission] = array(
  240. 'required' => false,
  241. 'data' => $checked,
  242. 'disabled' => array_key_exists('disabled', $attr),
  243. 'attr' => $attr,
  244. );
  245. }
  246. // NEXT_MAJOR: remove when dropping Symfony <2.8 support
  247. $type = method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ?
  248. 'Sonata\AdminBundle\Form\Type\AclMatrixType' :
  249. new AclMatrixType();
  250. $formBuilder->add($key, $type, array('permissions' => $permissions, 'acl_value' => $aclValue));
  251. }
  252. return $formBuilder->getForm();
  253. }
  254. /**
  255. * Gets a user or a role security identity.
  256. *
  257. * @param string|UserInterface $aclValue
  258. *
  259. * @return RoleSecurityIdentity|UserSecurityIdentity
  260. */
  261. protected function getSecurityIdentity($aclValue)
  262. {
  263. return ($aclValue instanceof UserInterface)
  264. ? UserSecurityIdentity::fromAccount($aclValue)
  265. : new RoleSecurityIdentity($aclValue)
  266. ;
  267. }
  268. }