AclMatrixType.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\Form\Type;
  11. use Symfony\Component\Form\AbstractType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. /**
  17. * This type define an ACL matrix.
  18. *
  19. * @author Samuel Roze <samuel@sroze.io>
  20. * @author Baptiste Meyer <baptiste@les-tilleuls.coop>
  21. */
  22. class AclMatrixType extends AbstractType
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function buildForm(FormBuilderInterface $builder, array $options)
  28. {
  29. $aclValueType = $options['acl_value'] instanceof UserInterface ? 'user' : 'role';
  30. $aclValueData = $options['acl_value'] instanceof UserInterface ? $options['acl_value']->getUsername() : $options['acl_value'];
  31. $builder->add($aclValueType, 'hidden', array('data' => $aclValueData));
  32. foreach ($options['permissions'] as $permission => $attributes) {
  33. $builder->add($permission, 'checkbox', $attributes);
  34. }
  35. }
  36. /**
  37. * {@inheritdoc}
  38. *
  39. * @todo Remove it when bumping requirements to SF 2.7+
  40. */
  41. public function setDefaultOptions(OptionsResolverInterface $resolver)
  42. {
  43. $this->configureOptions($resolver);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function configureOptions(OptionsResolver $resolver)
  49. {
  50. $resolver->setRequired(array(
  51. 'permissions',
  52. 'acl_value',
  53. ));
  54. if (method_exists($resolver, 'setDefined')) {
  55. $resolver->setAllowedTypes('permissions', 'array');
  56. $resolver->setAllowedTypes('acl_value', array('string', '\Symfony\Component\Security\Core\User\UserInterface'));
  57. } else {
  58. $resolver->setAllowedTypes(array(
  59. 'permissions' => 'array',
  60. 'acl_value' => array('string', '\Symfony\Component\Security\Core\User\UserInterface'),
  61. ));
  62. }
  63. }
  64. /**
  65. * {@inheritdoc}
  66. *
  67. * @todo Remove when dropping Symfony <2.8 support
  68. */
  69. public function getName()
  70. {
  71. return $this->getBlockPrefix();
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getBlockPrefix()
  77. {
  78. return 'sonata_type_acl_matrix';
  79. }
  80. }