Completed
Push — master ( e79dfa...ffecda )
by Benjamin
02:16
created

AdminAdmin   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 158
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A createQuery() 0 20 2
A configureDatagridFilters() 0 20 1
B configureListFields() 0 30 1
A configureFormFields() 0 55 2
A preUpdate() 0 4 1
A setUserManager() 0 4 1
A getUserManager() 0 4 1
1
<?php
2
3
namespace Alpixel\Bundle\UserBundle\Admin;
4
5
use Alpixel\Bundle\UserBundle\Entity\Admin as AdminEntity;
6
use FOS\UserBundle\Model\UserManagerInterface;
7
use Sonata\AdminBundle\Admin\Admin;
8
use Sonata\AdminBundle\Datagrid\DatagridMapper;
9
use Sonata\AdminBundle\Datagrid\ListMapper;
10
use Sonata\AdminBundle\Form\FormMapper;
11
12
class AdminAdmin extends Admin
13
{
14
    public function createQuery($context = 'list')
15
    {
16
        $container       = $this->getConfigurationPool()->getContainer();
17
        $securityContext = $container->get('security.context');
18
19
        $query = parent::createQuery($context);
20
21
        if (!$securityContext->isGranted('ROLE_ADMIN')) {
22
            $andx = $query->expr()->andx();
23
24
            $andx->add($query->expr()->notlike($query->getRootAlias().'.roles', ':nope'));
25
            $andx->add($query->expr()->notlike($query->getRootAlias().'.roles', ':nope2'));
26
27
            $query->andWhere($andx);
28
            $query->setParameter('nope', '%ROLE_ADMIN%');
29
            $query->setParameter('nope2', '%ROLE_SUPER_ADMIN%');
30
        }
31
32
        return $query;
33
    }
34
35
    /**
36
     * @param DatagridMapper $datagridMapper
37
     */
38
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
39
    {
40
        $datagridMapper
41
            ->add('id', null, array(
42
                'label' => 'ID',
43
            ))
44
            ->add('username', null, array(
45
                'label' => 'Nom d\'utilisateur',
46
            ))
47
            ->add('firstname', null, array(
48
                'label' => 'Prénom',
49
            ))
50
            ->add('lastname', null, array(
51
                'label' => 'Nom',
52
            ))
53
            ->add('email', null, array(
54
                'label' => 'Email',
55
            ))
56
        ;
57
    }
58
59
    /**
60
     * @param ListMapper $listMapper
61
     */
62
    protected function configureListFields(ListMapper $listMapper)
63
    {
64
        $listMapper
65
            ->addIdentifier('id', null, array(
66
                'label' => 'ID',
67
            ))
68
            ->addIdentifier('username', null, array(
69
                'label' => 'Nom d\'utilisateur',
70
            ))
71
            ->addIdentifier('firstname', null, array(
72
                'label' => 'Prénom',
73
            ))
74
            ->addIdentifier('lastname', null, array(
75
                'label' => 'Nom',
76
            ))
77
            ->add('email', null, array(
78
                'label' => 'Email',
79
            ))
80
            ->add('created', null, array(
81
                'label' => 'Date de création',
82
            ))
83
            ->add('lastLogin', null, array(
84
                'label' => 'Dernier login',
85
            ))
86
            ->add('roles', null, array(
87
                'label'     => 'Permissions',
88
                'template'  => 'UserBundle:admin:fields/list_roles.html.twig',
89
            ))
90
        ;
91
    }
92
93
    /**
94
     * @param FormMapper $formMapper
95
     */
96
    protected function configureFormFields(FormMapper $formMapper)
97
    {
98
        $container       = $this->getConfigurationPool()->getContainer();
99
        $securityContext = $container->get('security.context');
100
101
        if ($securityContext->isGranted('ROLE_ADMIN')) {
102
            $roles = array(
103
                'ROLE_USER'             => AdminEntity::getRoleString('ROLE_USER'),
104
                'ROLE_MODERATOR'        => AdminEntity::getRoleString('ROLE_MODERATOR'),
105
                'ROLE_MODERATOR_LEADER' => AdminEntity::getRoleString('ROLE_MODERATOR_LEADER'),
106
                'ROLE_SUPER_ADMIN'      => AdminEntity::getRoleString('ROLE_SUPER_ADMIN'),
107
            );
108
        } else {
109
            $roles = array(
110
                'ROLE_USER'             => AdminEntity::getRoleString('ROLE_USER'),
111
                'ROLE_MODERATOR'        => AdminEntity::getRoleString('ROLE_MODERATOR'),
112
            );
113
        }
114
        $formMapper
115
            ->with('Informations personnelles', array(
116
                'description' => '',
117
                'class'       => 'col-md-8',
118
            ))
119
                ->add('username', null, array(
120
                    'label' => 'Nom d\'utilisateur',
121
                ))
122
                ->add('email', null, array(
123
                    'label' => 'Email',
124
                ))
125
                ->add('firstname', null, array(
126
                    'label' => 'Prénom',
127
                ))
128
                ->add('lastname', null, array(
129
                    'label' => 'Nom',
130
                ))
131
            ->end()
132
            ->with('Paramétrage', array(
133
                'description' => '',
134
                'class'       => 'col-md-4',
135
            ))
136
                ->add('enabled', null, array(
137
                    'label' => 'Compte actif',
138
                ))
139
                ->add('roles', 'choice', array(
140
                    'multiple'  => true,
141
                    'choices'   => $roles,
142
                    'label'     => 'Permissions',
143
                ))
144
                ->add('plainPassword', 'text', array(
145
                    'label'     => 'Changer le mot de passe',
146
                    'required'  => false,
147
                ))
148
            ->end()
149
        ;
150
    }
151
152
    public function preUpdate($user)
153
    {
154
        $this->getUserManager()->updateUser($user);
155
    }
156
157
    public function setUserManager(UserManagerInterface $userManager)
158
    {
159
        $this->userManager = $userManager;
160
    }
161
162
    /**
163
     * @return UserManagerInterface
164
     */
165
    public function getUserManager()
166
    {
167
        return $this->userManager;
168
    }
169
}
170