UserAdmin.php 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

declare(strict_types=1);

/*
 * This file is part of the Sonata Project 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 App\Application\Sonata\UserBundle\Admin;

16
use App\Entity\User;
17 18 19 20 21 22
use FOS\UserBundle\Model\UserManagerInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelType;
23
use Sonata\AdminBundle\Route\RouteCollection;
24 25
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\CoreBundle\Form\Type\DatePickerType;
26
use Sonata\UserBundle\Admin\Model\UserAdmin as BaseUserAdmin;
27 28 29 30 31 32 33 34 35 36
use Sonata\UserBundle\Form\Type\SecurityRolesType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormTypeInterface;

class UserAdmin extends BaseUserAdmin
{
37 38 39 40 41
    public function configure()
    {
        parent::configure();
        $this->setTemplate('edit', '@SonataAdmin/base_edit.html.twig');
    }
42

43 44 45 46 47 48 49 50 51 52
    protected function configureRoutes(RouteCollection $collection)
    {
        if ($this->isChild()) {
            $collection->remove('delete');
            return;
        }

        // This is the route configuration as a parent
        $collection->remove('delete');
    }
53 54 55 56 57 58 59 60 61
    /**
     * {@inheritdoc}
     */
    protected function configureListFields(ListMapper $listMapper): void
    {
        $listMapper
            ->addIdentifier('username')
            ->add('email')
            ->add('groups')
62 63 64 65
            ->add('enabled', null, array(
                'label' => 'Activé',
                'editable' => true
            ))
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
            ->add('createdAt')
        ;

        if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
            $listMapper
                ->add('impersonating', 'string', ['template' => '@SonataUser/Admin/Field/impersonating.html.twig'])
            ;
        }
    }

    /**
     * {@inheritdoc}
     */
    protected function configureDatagridFilters(DatagridMapper $filterMapper): void
    {
        $filterMapper
            ->add('id')
            ->add('username')
            ->add('email')
            ->add('groups')
        ;
    }

    /**
     * {@inheritdoc}
     */
    protected function configureShowFields(ShowMapper $showMapper): void
    {
        $showMapper
            ->with('General')
                ->add('username')
                ->add('email')
            ->end()
            ->with('Groups')
100
                ->add('possiblegroups')
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
            ->end()
            ->with('Profile')
                // ->add('dateOfBirth')
                ->add('firstname')
                ->add('lastname')
                // ->add('website')
                // ->add('biography')
                // ->add('gender')
                // ->add('locale')
                // ->add('timezone')
                ->add('phone')
            ->end()
            // ->with('Social')
            //     ->add('facebookUid')
            //     ->add('facebookName')
            //     ->add('twitterUid')
            //     ->add('twitterName')
            //     ->add('gplusUid')
            //     ->add('gplusName')
            // ->end()
121 122 123 124
            // ->with('Security')
            //     ->add('token')
            //     ->add('twoStepVerificationCode')
            // ->end()
125 126 127 128 129 130 131 132
        ;
    }

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper): void
    {
133
        $subject = $this->getSubject();
134 135 136 137 138 139
        // define group zoning
        $formMapper
            ->tab('User')
                ->with('Profile', ['class' => 'col-md-6'])->end()
                ->with('General', ['class' => 'col-md-6'])->end()
                // ->with('Social', ['class' => 'col-md-6'])->end()
140
            ->end();
Julien Jorry committed
141 142
        // if (!($subject->isGranted('ROLE_SUPER_ADMIN') || $subject->isGranted('ROLE_ADMIN_SIEGE'))) {
        $formMapper
143 144 145
                ->tab('Security')
                ->with('Groups', ['class' => 'col-md-8'])->end()
                ->with('Status', ['class' => 'col-md-4'])->end()
146
                // ->with('Keys', ['class' => 'col-md-4'])->end()
147
                // ->with('Roles', ['class' => 'col-md-12'])->end()
148 149
                ->end()
            ;
Julien Jorry committed
150
        // }
151 152 153 154 155 156 157 158 159

        $now = new \DateTime();

        $genderOptions = [
            'choices' => call_user_func([$this->getUserManager()->getClass(), 'getGenderList']),
            'required' => true,
            'translation_domain' => $this->getTranslationDomain(),
        ];

160
        // w: Remove this when dropping support for SF 2.8
161 162 163 164 165 166 167 168 169
        if (method_exists(FormTypeInterface::class, 'setDefaultOptions')) {
            $genderOptions['choices_as_values'] = true;
        }

        // $formMapper->removeGroup('Social');
        $formMapper
            ->tab('User')
                ->with('General')
                    ->add('username')
170 171 172 173 174 175 176 177 178
                    ->add('email');
        if ($this->isGranted('ROLE_SUPER_ADMIN')) {
            $formMapper
                ->add('plainPassword', TextType::class, [
                    'required' => (!$this->getSubject() || null === $this->getSubject()->getId())
                ])
            ;
        }
        $formMapper->end()
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
                ->with('Profile')
                    // ->add('dateOfBirth', DatePickerType::class, [
                    //     'years' => range(1900, $now->format('Y')),
                    //     'dp_min_date' => '1-1-1900',
                    //     'dp_max_date' => $now->format('c'),
                    //     'required' => false,
                    // ])
                    ->add('firstname', null, ['required' => false])
                    ->add('lastname', null, ['required' => false])
                    // ->add('website', UrlType::class, ['required' => false])
                    // ->add('biography', TextType::class, ['required' => false])
                    // ->add('gender', ChoiceType::class, $genderOptions)
                    // ->add('locale', LocaleType::class, ['required' => false])
                    // ->add('timezone', TimezoneType::class, ['required' => false])
                    ->add('phone', null, ['required' => false])
                ->end()
                // ->with('Social')
                //     ->add('facebookUid', null, ['required' => false])
                //     ->add('facebookName', null, ['required' => false])
                //     ->add('twitterUid', null, ['required' => false])
                //     ->add('twitterName', null, ['required' => false])
                //     ->add('gplusUid', null, ['required' => false])
                //     ->add('gplusName', null, ['required' => false])
                // ->end()
203
            ->end();
Julien Jorry committed
204 205
        // if (!($subject->isGranted('ROLE_SUPER_ADMIN') || $subject->isGranted('ROLE_ADMIN_SIEGE'))) {
        $formMapper
206 207 208 209 210
                ->tab('Security')
                    ->with('Status')
                        ->add('enabled', null, ['required' => false])
                    ->end()
            ;
Julien Jorry committed
211 212 213 214 215 216 217 218 219
        $hideOrShowGroupe = ['class' => 'hide'];
        $hideOrShowComptoir = ['class' => 'hide'];
        if (($subject->isGranted('ROLE_GESTION_GROUPE') || $subject->isGranted('ROLE_CONTACT') || $subject->isGranted('ROLE_TRESORIER'))) {
            $hideOrShowGroupe = [];
        }
        if ($subject->isGranted('ROLE_COMPTOIR')) {
            $hideOrShowComptoir = [];
        }
        $formMapper
220
                ->with('Groups')
221 222
                    ->add('possiblegroups', ModelType::class, [
                        'label' => 'Groupes de rôles possibles',
223 224 225 226
                        'required' => false,
                        'expanded' => true,
                        'multiple' => true,
                    ])
227 228 229 230 231 232
                    // @TODO : Si on veut voir le groupe choisit par l'utilisateur
                    // ->add('groups', null, [
                    //     'label' => 'Rôle actuel',
                    //     'required' => false,
                    //     'disabled' => true
                    // ])
Julien Jorry committed
233
                    ->add('groupesgeres', null, [
234 235 236 237
                        'required' => false,
                        'label' => 'Groupe local géré (obligatoire)',
                        'attr' => $hideOrShowGroupe,
                        'label_attr' => $hideOrShowGroupe,
Julien Jorry committed
238 239
                        'multiple' => true,
                        'by_reference' => false
240
                    ])
Julien Jorry committed
241
                    ->add('comptoirsgeres', null, [
242 243 244 245
                        'required' => false,
                        'label' => 'Comptoir géré (obligatoire)',
                        'attr' => $hideOrShowComptoir,
                        'label_attr' => $hideOrShowComptoir,
Julien Jorry committed
246 247
                        'multiple' => true,
                        'by_reference' => false
248 249 250 251 252 253 254 255 256 257 258 259
                    ])
                ->end()
                ->with('Roles')
                    ->add('realRoles', SecurityRolesType::class, [
                        'label' => 'form.label_roles',
                        'expanded' => true,
                        'multiple' => true,
                        'required' => false,
                    ])
                ->end()
            ->end()
            ;
Julien Jorry committed
260
        // }
261 262 263 264
        // ->with('Keys')
        //     ->add('token', null, ['required' => false])
        //     ->add('twoStepVerificationCode', null, ['required' => false])
        // ->end()
265
    }
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280

    public function getBatchActions()
    {
        $actions = parent::getBatchActions();
        unset($actions['delete']);

        return $actions;
    }

    public function getDashboardActions()
    {
        $actions = parent::getDashboardActions();
        unset($actions['list']);
        return $actions;
    }
281 282 283 284 285 286 287 288 289 290 291 292 293 294

    public function preUpdate($user): void
    {
        $em = $this->getConfigurationPool()->getContainer()->get('doctrine')->getManager();

        /* ON EMPECHE ICI DE DESACTIVER LES ROLES SUPER ADMIN OU ADMIN SIEGE */
        if ($user->hasRole('ROLE_SUPER_ADMIN') || $user->hasRole('ROLE_ADMIN_SIEGE')) {
            if (!$user->isEnabled()) {
                $user->setEnabled(1);
                $em->persist($user);
                $em->flush();
            }
        }
    }
295
}