UserAdmin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. namespace Sonata\UserBundle\Admin\Model;
  11. use Sonata\AdminBundle\Admin\Admin;
  12. use Sonata\AdminBundle\Form\FormMapper;
  13. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  14. use Sonata\AdminBundle\Datagrid\ListMapper;
  15. use FOS\UserBundle\Model\UserManagerInterface;
  16. class UserAdmin extends Admin
  17. {
  18. protected $formOptions = array(
  19. 'validation_groups' => 'Profile'
  20. );
  21. /**
  22. * {@inheritdoc}
  23. */
  24. protected function configureListFields(ListMapper $listMapper)
  25. {
  26. $listMapper
  27. ->addIdentifier('username')
  28. ->add('email')
  29. ->add('groups')
  30. ->add('enabled')
  31. ->add('locked')
  32. ->add('createdAt')
  33. ;
  34. if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
  35. $listMapper
  36. ->add('impersonating', 'string', array('template' => 'SonataUserBundle:Admin:Field/impersonating.html.twig'))
  37. ;
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. protected function configureDatagridFilters(DatagridMapper $filterMapper)
  44. {
  45. $filterMapper
  46. ->add('id')
  47. ->add('username')
  48. ->add('locked')
  49. ->add('email')
  50. ->add('groups')
  51. ;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. protected function configureFormFields(FormMapper $formMapper)
  57. {
  58. $formMapper
  59. ->with('General')
  60. ->add('username')
  61. ->add('email')
  62. ->add('plainPassword', 'text', array('required' => false))
  63. ->end()
  64. ->with('Groups')
  65. ->add('groups', 'sonata_type_model', array('required' => false))
  66. ->end()
  67. ->with('Management')
  68. ->add('roles', 'sonata_security_roles', array(
  69. 'expanded' => true,
  70. 'multiple' => true,
  71. 'required' => false
  72. ))
  73. ->add('locked', null, array('required' => false))
  74. ->add('expired', null, array('required' => false))
  75. ->add('enabled', null, array('required' => false))
  76. ->end()
  77. ;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function preUpdate($user)
  83. {
  84. $this->getUserManager()->updateCanonicalFields($user);
  85. $this->getUserManager()->updatePassword($user);
  86. }
  87. /**
  88. * @param UserManagerInterface $userManager
  89. */
  90. public function setUserManager(UserManagerInterface $userManager)
  91. {
  92. $this->userManager = $userManager;
  93. }
  94. /**
  95. * @return UserManagerInterface
  96. */
  97. public function getUserManager()
  98. {
  99. return $this->userManager;
  100. }
  101. }