UserAdmin.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\UserBundle\Admin\Model;
  11. use FOS\UserBundle\Model\UserManagerInterface;
  12. use Sonata\AdminBundle\Admin\AbstractAdmin;
  13. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  14. use Sonata\AdminBundle\Datagrid\ListMapper;
  15. use Sonata\AdminBundle\Form\FormMapper;
  16. use Sonata\AdminBundle\Show\ShowMapper;
  17. class UserAdmin extends AbstractAdmin
  18. {
  19. /**
  20. * @var UserManagerInterface
  21. */
  22. protected $userManager;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function getFormBuilder()
  27. {
  28. $this->formOptions['data_class'] = $this->getClass();
  29. $options = $this->formOptions;
  30. $options['validation_groups'] = (!$this->getSubject() || is_null($this->getSubject()->getId())) ? 'Registration' : 'Profile';
  31. $formBuilder = $this->getFormContractor()->getFormBuilder($this->getUniqid(), $options);
  32. $this->defineFormBuilder($formBuilder);
  33. return $formBuilder;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getExportFields()
  39. {
  40. // avoid security field to be exported
  41. return array_filter(parent::getExportFields(), function ($v) {
  42. return !in_array($v, array('password', 'salt'));
  43. });
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function preUpdate($user)
  49. {
  50. $this->getUserManager()->updateCanonicalFields($user);
  51. $this->getUserManager()->updatePassword($user);
  52. }
  53. /**
  54. * @param UserManagerInterface $userManager
  55. */
  56. public function setUserManager(UserManagerInterface $userManager)
  57. {
  58. $this->userManager = $userManager;
  59. }
  60. /**
  61. * @return UserManagerInterface
  62. */
  63. public function getUserManager()
  64. {
  65. return $this->userManager;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. protected function configureListFields(ListMapper $listMapper)
  71. {
  72. $listMapper
  73. ->addIdentifier('username')
  74. ->add('email')
  75. ->add('groups')
  76. ->add('enabled', null, array('editable' => true))
  77. ->add('locked', null, array('editable' => true))
  78. ->add('createdAt')
  79. ;
  80. if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
  81. $listMapper
  82. ->add('impersonating', 'string', array('template' => 'SonataUserBundle:Admin:Field/impersonating.html.twig'))
  83. ;
  84. }
  85. }
  86. /**
  87. * {@inheritdoc}
  88. */
  89. protected function configureDatagridFilters(DatagridMapper $filterMapper)
  90. {
  91. $filterMapper
  92. ->add('id')
  93. ->add('username')
  94. ->add('locked')
  95. ->add('email')
  96. ->add('groups')
  97. ;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. protected function configureShowFields(ShowMapper $showMapper)
  103. {
  104. $showMapper
  105. ->with('General')
  106. ->add('username')
  107. ->add('email')
  108. ->end()
  109. ->with('Groups')
  110. ->add('groups')
  111. ->end()
  112. ->with('Profile')
  113. ->add('dateOfBirth')
  114. ->add('firstname')
  115. ->add('lastname')
  116. ->add('website')
  117. ->add('biography')
  118. ->add('gender')
  119. ->add('locale')
  120. ->add('timezone')
  121. ->add('phone')
  122. ->end()
  123. ->with('Social')
  124. ->add('facebookUid')
  125. ->add('facebookName')
  126. ->add('twitterUid')
  127. ->add('twitterName')
  128. ->add('gplusUid')
  129. ->add('gplusName')
  130. ->end()
  131. ->with('Security')
  132. ->add('token')
  133. ->add('twoStepVerificationCode')
  134. ->end()
  135. ;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. protected function configureFormFields(FormMapper $formMapper)
  141. {
  142. // define group zoning
  143. $formMapper
  144. ->tab('User')
  145. ->with('Profile', array('class' => 'col-md-6'))->end()
  146. ->with('General', array('class' => 'col-md-6'))->end()
  147. ->with('Social', array('class' => 'col-md-6'))->end()
  148. ->end()
  149. ->tab('Security')
  150. ->with('Status', array('class' => 'col-md-4'))->end()
  151. ->with('Groups', array('class' => 'col-md-4'))->end()
  152. ->with('Keys', array('class' => 'col-md-4'))->end()
  153. ->with('Roles', array('class' => 'col-md-12'))->end()
  154. ->end()
  155. ;
  156. $now = new \DateTime();
  157. // NEXT_MAJOR: Keep FQCN when bumping Symfony requirement to 2.8+.
  158. if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
  159. $textType = 'Symfony\Component\Form\Extension\Core\Type\TextType';
  160. $datePickerType = 'Sonata\CoreBundle\Form\Type\DatePickerType';
  161. $urlType = 'Symfony\Component\Form\Extension\Core\Type\UrlType';
  162. $userGenderType = 'Sonata\UserBundle\Form\Type\UserGenderListType';
  163. $localeType = 'Symfony\Component\Form\Extension\Core\Type\LocaleType';
  164. $timezoneType = 'Symfony\Component\Form\Extension\Core\Type\TimezoneType';
  165. $modelType = 'Sonata\AdminBundle\Form\Type\ModelType';
  166. $securityRolesType = 'Sonata\UserBundle\Form\Type\SecurityRolesType';
  167. } else {
  168. $textType = 'text';
  169. $datePickerType = 'sonata_type_date_picker';
  170. $urlType = 'url';
  171. $userGenderType = 'sonata_user_gender';
  172. $localeType = 'locale';
  173. $timezoneType = 'timezone';
  174. $modelType = 'sonata_type_model';
  175. $securityRolesType = 'sonata_security_roles';
  176. }
  177. $formMapper
  178. ->tab('User')
  179. ->with('General')
  180. ->add('username')
  181. ->add('email')
  182. ->add('plainPassword', $textType, array(
  183. 'required' => (!$this->getSubject() || is_null($this->getSubject()->getId())),
  184. ))
  185. ->end()
  186. ->with('Profile')
  187. ->add('dateOfBirth', $datePickerType, array(
  188. 'years' => range(1900, $now->format('Y')),
  189. 'dp_min_date' => '1-1-1900',
  190. 'dp_max_date' => $now->format('c'),
  191. 'required' => false,
  192. ))
  193. ->add('firstname', null, array('required' => false))
  194. ->add('lastname', null, array('required' => false))
  195. ->add('website', $urlType, array('required' => false))
  196. ->add('biography', $textType, array('required' => false))
  197. ->add('gender', $userGenderType, array(
  198. 'required' => true,
  199. 'translation_domain' => $this->getTranslationDomain(),
  200. ))
  201. ->add('locale', $localeType, array('required' => false))
  202. ->add('timezone', $timezoneType, array('required' => false))
  203. ->add('phone', null, array('required' => false))
  204. ->end()
  205. ->with('Social')
  206. ->add('facebookUid', null, array('required' => false))
  207. ->add('facebookName', null, array('required' => false))
  208. ->add('twitterUid', null, array('required' => false))
  209. ->add('twitterName', null, array('required' => false))
  210. ->add('gplusUid', null, array('required' => false))
  211. ->add('gplusName', null, array('required' => false))
  212. ->end()
  213. ->end()
  214. ->tab('Security')
  215. ->with('Status')
  216. ->add('locked', null, array('required' => false))
  217. ->add('expired', null, array('required' => false))
  218. ->add('enabled', null, array('required' => false))
  219. ->add('credentialsExpired', null, array('required' => false))
  220. ->end()
  221. ->with('Groups')
  222. ->add('groups', $modelType, array(
  223. 'required' => false,
  224. 'expanded' => true,
  225. 'multiple' => true,
  226. ))
  227. ->end()
  228. ->with('Roles')
  229. ->add('realRoles', $securityRolesType, array(
  230. 'label' => 'form.label_roles',
  231. 'expanded' => true,
  232. 'multiple' => true,
  233. 'required' => false,
  234. ))
  235. ->end()
  236. ->with('Keys')
  237. ->add('token', null, array('required' => false))
  238. ->add('twoStepVerificationCode', null, array('required' => false))
  239. ->end()
  240. ->end()
  241. ;
  242. }
  243. }