浏览代码

Refactor orm/mongodb admin code

Emmanuel Vella 13 年之前
父节点
当前提交
ba824dc658

+ 2 - 39
Admin/Document/GroupAdmin.php

@@ -11,45 +11,8 @@
 
 namespace Sonata\UserBundle\Admin\Document;
 
-use Sonata\AdminBundle\Admin\Admin;
-use Sonata\AdminBundle\Form\FormMapper;
-use Sonata\AdminBundle\Datagrid\DatagridMapper;
-use Sonata\AdminBundle\Datagrid\ListMapper;
-use Sonata\AdminBundle\Route\RouteCollection;
+use Sonata\UserBundle\Admin\Model\GroupAdmin as BaseGroupAdmin;
 
-class GroupAdmin extends Admin
+class GroupAdmin extends BaseGroupAdmin
 {
-    protected $formOptions = array(
-        'validation_groups' => 'Registration'
-    );
-
-    public function getNewInstance()
-    {
-        $class = $this->getClass();
-
-        return new $class('', array());
-    }
-
-    protected function configureListFields(ListMapper $listMapper)
-    {
-        $listMapper
-            ->addIdentifier('name')
-            ->add('roles')
-        ;
-    }
-
-    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
-    {
-        $datagridMapper
-            ->add('name')
-        ;
-    }
-
-    protected function configureFormFields(FormMapper $formMapper)
-    {
-        $formMapper
-            ->add('name')
-            ->add('roles', 'sonata_security_roles', array( 'multiple' => true, 'required' => false))
-        ;
-    }
 }

+ 2 - 75
Admin/Document/UserAdmin.php

@@ -11,81 +11,8 @@
 
 namespace Sonata\UserBundle\Admin\Document;
 
-use Sonata\AdminBundle\Admin\Admin;
-use Sonata\AdminBundle\Form\FormMapper;
-use Sonata\AdminBundle\Datagrid\DatagridMapper;
-use Sonata\AdminBundle\Datagrid\ListMapper;
-use Sonata\AdminBundle\Route\RouteCollection;
-use Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder;
+use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;
 
-use FOS\UserBundle\Model\UserManagerInterface;
-
-class UserAdmin extends Admin
+class UserAdmin extends BaseUserAdmin
 {
-    protected $formOptions = array(
-        'validation_groups' => 'Profile'
-    );
-
-    protected function configureListFields(ListMapper $listMapper)
-    {
-        $listMapper
-            ->addIdentifier('username')
-            ->add('email')
-            ->add('enabled')
-            ->add('locked')
-            ->add('createdAt')
-        ;
-
-        if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
-            $listMapper
-                ->add('impersonating', 'string', array('template' => 'SonataUserBundle:Admin:Field/impersonating.html.twig'))
-            ;
-        }
-    }
-
-    protected function configureDatagridFilters(DatagridMapper $filterMapper)
-    {
-        $filterMapper
-            ->add('username')
-            ->add('locked')
-            ->add('email')
-            ->add('id')
-        ;
-    }
-
-    protected function configureFormFields(FormMapper $formMapper)
-    {
-        $formMapper
-            ->with('General')
-                ->add('username')
-                ->add('email')
-                ->add('plainPassword', 'text', array('required' => false))
-            ->end()
-            ->with('Groups')
-                ->add('groups', 'sonata_type_model', array('required' => false))
-            ->end()
-            ->with('Management')
-                ->add('roles', 'sonata_security_roles', array( 'multiple' => true, 'required' => false))
-                ->add('locked', null, array('required' => false))
-                ->add('expired', null, array('required' => false))
-                ->add('enabled', null, array('required' => false))
-            ->end()
-        ;
-    }
-
-    public function preUpdate($user)
-    {
-        $this->getUserManager()->updateCanonicalFields($user);
-        $this->getUserManager()->updatePassword($user);
-    }
-
-    public function setUserManager(UserManagerInterface $userManager)
-    {
-        $this->userManager = $userManager;
-    }
-
-    public function getUserManager()
-    {
-        return $this->userManager;
-    }
 }

+ 2 - 42
Admin/Entity/GroupAdmin.php

@@ -11,48 +11,8 @@
 
 namespace Sonata\UserBundle\Admin\Entity;
 
-use Sonata\AdminBundle\Admin\Admin;
-use Sonata\AdminBundle\Form\FormMapper;
-use Sonata\AdminBundle\Datagrid\DatagridMapper;
-use Sonata\AdminBundle\Datagrid\ListMapper;
-use Sonata\AdminBundle\Route\RouteCollection;
+use Sonata\UserBundle\Admin\Model\GroupAdmin as BaseGroupAdmin;
 
-class GroupAdmin extends Admin
+class GroupAdmin extends BaseGroupAdmin
 {
-    protected $formOptions = array(
-        'validation_groups' => 'Registration'
-    );
-
-    public function getNewInstance()
-    {
-        $class = $this->getClass();
-
-        return new $class('', array());
-    }
-
-    protected function configureListFields(ListMapper $listMapper)
-    {
-        $listMapper
-            ->addIdentifier('name')
-            ->add('roles')
-        ;
-    }
-
-    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
-    {
-        $datagridMapper
-            ->add('name')
-        ;
-    }
-    protected function configureFormFields(FormMapper $formMapper)
-    {
-        $formMapper
-            ->add('name')
-            ->add('roles', 'sonata_security_roles', array(
-                'expanded' => true,
-                'multiple' => true,
-                'required' => false
-            ))
-        ;
-    }
 }

+ 4 - 73
Admin/Entity/UserAdmin.php

@@ -11,88 +11,19 @@
 
 namespace Sonata\UserBundle\Admin\Entity;
 
-use Sonata\AdminBundle\Admin\Admin;
+use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;
 use Sonata\AdminBundle\Form\FormMapper;
-use Sonata\AdminBundle\Datagrid\DatagridMapper;
-use Sonata\AdminBundle\Datagrid\ListMapper;
-use Sonata\AdminBundle\Route\RouteCollection;
-use Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder;
 
-use FOS\UserBundle\Model\UserManagerInterface;
-
-class UserAdmin extends Admin
+class UserAdmin extends BaseUserAdmin
 {
-    protected $formOptions = array(
-        'validation_groups' => 'Profile'
-    );
-
-    protected function configureListFields(ListMapper $listMapper)
-    {
-        $listMapper
-            ->addIdentifier('username')
-            ->add('email')
-            ->add('groups')
-            ->add('enabled')
-            ->add('locked')
-            ->add('createdAt')
-        ;
-
-        if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
-            $listMapper
-                ->add('impersonating', 'string', array('template' => 'SonataUserBundle:Admin:Field/impersonating.html.twig'))
-            ;
-        }
-    }
-
-    protected function configureDatagridFilters(DatagridMapper $filterMapper)
-    {
-        $filterMapper
-            ->add('username')
-            ->add('locked')
-            ->add('email')
-            ->add('groups')
-            ->add('id')
-        ;
-    }
-
     protected function configureFormFields(FormMapper $formMapper)
     {
+        parent::configureFormFields($formMapper);
+
         $formMapper
-            ->with('General')
-                ->add('username')
-                ->add('email')
-                ->add('plainPassword', 'text', array('required' => false))
-            ->end()
-            ->with('Groups')
-                ->add('groups', 'sonata_type_model', array('required' => false))
-            ->end()
             ->with('Management')
-                ->add('roles', 'sonata_security_roles', array(
-                    'expanded' => true,
-                    'multiple' => true,
-                    'required' => false
-                ))
-                ->add('locked', null, array('required' => false))
-                ->add('expired', null, array('required' => false))
-                ->add('enabled', null, array('required' => false))
                 ->add('credentialsExpired', null, array('required' => false))
             ->end()
         ;
     }
-
-    public function preUpdate($user)
-    {
-        $this->getUserManager()->updateCanonicalFields($user);
-        $this->getUserManager()->updatePassword($user);
-    }
-
-    public function setUserManager(UserManagerInterface $userManager)
-    {
-        $this->userManager = $userManager;
-    }
-
-    public function getUserManager()
-    {
-        return $this->userManager;
-    }
 }

+ 57 - 0
Admin/Model/GroupAdmin.php

@@ -0,0 +1,57 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\UserBundle\Admin\Model;
+
+use Sonata\AdminBundle\Admin\Admin;
+use Sonata\AdminBundle\Form\FormMapper;
+use Sonata\AdminBundle\Datagrid\DatagridMapper;
+use Sonata\AdminBundle\Datagrid\ListMapper;
+
+class GroupAdmin extends Admin
+{
+    protected $formOptions = array(
+        'validation_groups' => 'Registration'
+    );
+
+    public function getNewInstance()
+    {
+        $class = $this->getClass();
+
+        return new $class('', array());
+    }
+
+    protected function configureListFields(ListMapper $listMapper)
+    {
+        $listMapper
+            ->addIdentifier('name')
+            ->add('roles')
+        ;
+    }
+
+    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
+    {
+        $datagridMapper
+            ->add('name')
+        ;
+    }
+    protected function configureFormFields(FormMapper $formMapper)
+    {
+        $formMapper
+            ->add('name')
+            ->add('roles', 'sonata_security_roles', array(
+                'expanded' => true,
+                'multiple' => true,
+                'required' => false
+            ))
+        ;
+    }
+}

+ 95 - 0
Admin/Model/UserAdmin.php

@@ -0,0 +1,95 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\UserBundle\Admin\Model;
+
+use Sonata\AdminBundle\Admin\Admin;
+use Sonata\AdminBundle\Form\FormMapper;
+use Sonata\AdminBundle\Datagrid\DatagridMapper;
+use Sonata\AdminBundle\Datagrid\ListMapper;
+
+use FOS\UserBundle\Model\UserManagerInterface;
+
+class UserAdmin extends Admin
+{
+    protected $formOptions = array(
+        'validation_groups' => 'Profile'
+    );
+
+    protected function configureListFields(ListMapper $listMapper)
+    {
+        $listMapper
+            ->addIdentifier('username')
+            ->add('email')
+            ->add('groups')
+            ->add('enabled')
+            ->add('locked')
+            ->add('createdAt')
+        ;
+
+        if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
+            $listMapper
+                ->add('impersonating', 'string', array('template' => 'SonataUserBundle:Admin:Field/impersonating.html.twig'))
+            ;
+        }
+    }
+
+    protected function configureDatagridFilters(DatagridMapper $filterMapper)
+    {
+        $filterMapper
+            ->add('id')
+            ->add('username')
+            ->add('locked')
+            ->add('email')
+            ->add('groups')
+        ;
+    }
+
+    protected function configureFormFields(FormMapper $formMapper)
+    {
+        $formMapper
+            ->with('General')
+                ->add('username')
+                ->add('email')
+                ->add('plainPassword', 'text', array('required' => false))
+            ->end()
+            ->with('Groups')
+                ->add('groups', 'sonata_type_model', array('required' => false))
+            ->end()
+            ->with('Management')
+                ->add('roles', 'sonata_security_roles', array(
+                    'expanded' => true,
+                    'multiple' => true,
+                    'required' => false
+                ))
+                ->add('locked', null, array('required' => false))
+                ->add('expired', null, array('required' => false))
+                ->add('enabled', null, array('required' => false))
+            ->end()
+        ;
+    }
+
+    public function preUpdate($user)
+    {
+        $this->getUserManager()->updateCanonicalFields($user);
+        $this->getUserManager()->updatePassword($user);
+    }
+
+    public function setUserManager(UserManagerInterface $userManager)
+    {
+        $this->userManager = $userManager;
+    }
+
+    public function getUserManager()
+    {
+        return $this->userManager;
+    }
+}

+ 4 - 0
Resources/translations/SonataUserBundle.en.xliff

@@ -110,6 +110,10 @@
                 <source>filter.label_id</source>
                 <target>ID</target>
             </trans-unit>
+            <trans-unit id="filter.label_groups">
+                <source>filter.label_groups</source>
+                <target>Groups</target>
+            </trans-unit>
             <trans-unit id="list.label_username">
                 <source>list.label_username</source>
                 <target>Username</target>

+ 4 - 0
Resources/translations/SonataUserBundle.fr.xliff

@@ -106,6 +106,10 @@
                 <source>filter.label_id</source>
                 <target>ID</target>
             </trans-unit>
+            <trans-unit id="filter.label_groups">
+                <source>filter.label_groups</source>
+                <target>Groupes</target>
+            </trans-unit>
             <trans-unit id="list.label_username">
                 <source>list.label_username</source>
                 <target>Nom d'utilisateur</target>