AclMatrixType.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /*
  3. * This file is part of the Sonata 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. */
  11. namespace Sonata\AdminBundle\Form\Type;
  12. use Symfony\Component\Form\AbstractType;
  13. use Symfony\Component\Form\FormBuilderInterface;
  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. public function setDefaultOptions(OptionsResolverInterface $resolver)
  40. {
  41. $resolver->setRequired(array(
  42. 'permissions',
  43. 'acl_value',
  44. ));
  45. $resolver->setAllowedTypes(array(
  46. 'permissions' => 'array',
  47. 'acl_value' => array('string', '\Symfony\Component\Security\Core\User\UserInterface'),
  48. ));
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function getName()
  54. {
  55. return 'sonata_type_acl_matrix';
  56. }
  57. }