AclMatrixType.php 2.4 KB

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