Passed
Push — develop ( 0eae32...8386a6 )
by BENARD
12:17 queued 09:24
created

SecurityEventAdmin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 82
c 1
b 0
f 0
dl 0
loc 128
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configureShowFields() 0 28 1
A configureRouteCollection() 0 6 1
A configureListFields() 0 20 1
A configureDatagridFilters() 0 47 1
1
<?php
2
3
namespace ProjetNormandie\UserBundle\Admin;
4
5
use Sonata\AdminBundle\Admin\AbstractAdmin;
6
use Sonata\AdminBundle\Datagrid\DatagridMapper;
7
use Sonata\AdminBundle\Datagrid\ListMapper;
8
use Sonata\AdminBundle\Route\RouteCollectionInterface;
9
use Sonata\AdminBundle\Show\ShowMapper;
10
use Sonata\DoctrineORMAdminBundle\Filter\DateTimeFilter;
11
use Sonata\DoctrineORMAdminBundle\Filter\StringFilter;
12
use Sonata\Form\Type\DateTimePickerType;
13
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
14
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
15
16
/**
17
 * Admin class for the SecurityEvent entity
18
 */
19
class SecurityEventAdmin extends AbstractAdmin
20
{
21
    protected $baseRouteName = 'pnu_admin_security_event';
22
    protected $baseRoutePattern = 'security-event';
23
24
    /**
25
     * Configure route collection
26
     */
27
    protected function configureRouteCollection(RouteCollectionInterface $collection): void
28
    {
29
        $collection
30
            ->remove('create')
31
            ->remove('delete')
32
            ->remove('edit');
33
    }
34
35
    /**
36
     * Fields to be shown on lists
37
     */
38
    protected function configureListFields(ListMapper $list): void
39
    {
40
        $list
41
            ->addIdentifier('id', null, ['label' => 'ID'])
42
            ->add('user', null, [
43
                'label' => 'User',
44
                'admin_code' => 'sonata.admin.user.user'
45
            ])
46
            ->add('eventType', null, [
47
                'label' => 'Event Type',
48
                'template' => '@ProjetNormandieUser/Admin/security_event_type.html.twig'
49
            ])
50
            ->add('ipAddress', null, ['label' => 'IP Address'])
51
            ->add('createdAt', 'datetime', [
52
                'label' => 'Date/Time',
53
                'format' => 'd/m/Y H:i:s'
54
            ])
55
            ->add('_action', 'actions', [
56
                'actions' => [
57
                    'show' => [],
58
                ]
59
            ]);
60
    }
61
62
    /**
63
     * Fields to be shown on filters
64
     */
65
    protected function configureDatagridFilters(DatagridMapper $filter): void
66
    {
67
        $filter
68
            ->add('id', null, ['label' => 'ID'])
69
            ->add('user', null, [
70
                'label' => 'User',
71
                'field_type' => ModelAutocompleteType::class,
72
                'field_options' => [
73
                    'property' => 'username',
74
                    'admin_code' => 'sonata.admin.user.user',
75
                    'callback' => function ($admin, $property, $value) {
76
                        $datagrid = $admin->getDatagrid();
77
                        $query = $datagrid->getQuery();
78
                        $query
79
                            ->andWhere($query->expr()->orX(
80
                                $query->expr()->like('username', ':username'),
81
                                $query->expr()->like('email', ':username')
82
                            ))
83
                            ->setParameter('username', '%' . $value . '%');
84
                    },
85
                ],
86
            ])
87
            ->add('eventType', StringFilter::class, [
88
                'label' => 'Event Type',
89
                'field_type' => ChoiceType::class,
90
                'field_options' => [
91
                    'choices' => [
92
                        'Password Changed' => 'password_change',
93
                        'Email Changed' => 'email_change',
94
                        'Login Success' => 'login_success',
95
                        'Login Failure' => 'login_failure',
96
                        'Account Locked' => 'account_locked',
97
                        'Registration' => 'registration',
98
                        'Password Reset' => 'password_reset',
99
                    ],
100
                    'required' => false,
101
                    'multiple' => false,
102
                    'expanded' => false,
103
                ],
104
            ])
105
            ->add('ipAddress', null, ['label' => 'IP Address'])
106
            ->add('userAgent', null, ['label' => 'User Agent'])
107
            ->add('createdAt', DateTimeFilter::class, [
108
                'label' => 'Date/Time',
109
                'field_type' => DateTimePickerType::class,
110
                'field_options' => [
111
                    'format' => 'yyyy-MM-dd HH:mm:ss',
112
                ],
113
            ]);
114
    }
115
116
    /**
117
     * Fields to be shown on show action
118
     */
119
    protected function configureShowFields(ShowMapper $show): void
120
    {
121
        $show
122
            ->with('Event Information', ['class' => 'col-md-6'])
123
            ->add('id', null, ['label' => 'ID'])
124
            ->add('eventType', null, [
125
                'label' => 'Event Type',
126
                'template' => '@ProjetNormandieUser/Admin/security_event_type.html.twig'
127
            ])
128
            ->add('createdAt', 'datetime', [
129
                'label' => 'Date/Time',
130
                'format' => 'd/m/Y H:i:s'
131
            ])
132
            ->end()
133
            ->with('User Information', ['class' => 'col-md-6'])
134
            ->add('user', null, [
135
                'label' => 'User',
136
                'admin_code' => 'sonata.admin.user.user'
137
            ])
138
            ->add('ipAddress', null, ['label' => 'IP Address'])
139
            ->add('userAgent', null, ['label' => 'User Agent'])
140
            ->end()
141
            ->with('Event Data', ['class' => 'col-md-12'])
142
            ->add('eventData', null, [
143
                'label' => 'Event Data',
144
                'template' => '@ProjetNormandieUser/Admin/security_event_data.html.twig'
145
            ])
146
            ->end();
147
    }
148
}