SecurityEventAdmin::configureRoutes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 1
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace ProjetNormandie\UserBundle\Admin;
4
5
use ProjetNormandie\UserBundle\Security\Event\SecurityEventTypeEnum;
6
use Sonata\AdminBundle\Admin\AbstractAdmin;
7
use Sonata\AdminBundle\Datagrid\DatagridMapper;
8
use Sonata\AdminBundle\Datagrid\ListMapper;
9
use Sonata\AdminBundle\Route\RouteCollectionInterface;
10
use Sonata\AdminBundle\Show\ShowMapper;
11
use Doctrine\ORM\EntityManagerInterface;
12
use ProjetNormandie\UserBundle\Entity\SecurityEvent;
13
use Sonata\DoctrineORMAdminBundle\Filter\DateTimeFilter;
14
use Sonata\DoctrineORMAdminBundle\Filter\StringFilter;
15
use Sonata\Form\Type\DateTimePickerType;
16
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
17
use Sonata\AdminBundle\Form\Type\ModelAutocompleteType;
18
19
/**
20
 * Admin class for the SecurityEvent entity
21
 */
22
class SecurityEventAdmin extends AbstractAdmin
23
{
24
    protected $baseRouteName = 'pnu_admin_security_event';
25
    protected $baseRoutePattern = 'security-event';
26
27
    /**
28
     * Configure route collection
29
     */
30
    protected function configureRoutes(RouteCollectionInterface $collection): void
31
    {
32
        $collection
33
            ->remove('create')
34
            ->remove('delete')
35
            ->remove('edit')
36
            ->add('statistics', 'statistics');
37
    }
38
39
    /**
40
     * Fields to be shown on lists
41
     */
42
    protected function configureListFields(ListMapper $list): void
43
    {
44
        $list
45
            ->addIdentifier('id', null, ['label' => 'security_event.list.id'])
46
            ->add('user', null, [
47
                'label' => 'security_event.list.user',
48
                'admin_code' => 'sonata.admin.user.user'
49
            ])
50
            ->add('eventType', null, [
51
                'label' => 'security_event.list.event_type',
52
                'template' => '@ProjetNormandieUser/Admin/security_event_type.html.twig'
53
            ])
54
            ->add('ipAddress', null, ['label' => 'security_event.list.ip_address'])
55
            ->add('createdAt', 'datetime', [
56
                'label' => 'security_event.list.date_time',
57
                'format' => 'd/m/Y H:i:s'
58
            ])
59
            ->add('_action', 'actions', [
60
                'actions' => [
61
                    'show' => [],
62
                ]
63
            ]);
64
    }
65
66
    /**
67
     * Configure batch actions
68
     */
69
    protected function configureBatchActions(array $actions): array
70
    {
71
        unset($actions['delete']);
72
        return $actions;
73
    }
74
75
    /**
76
     * Configure actions for the list view
77
     */
78
    protected function configureActionButtons(array $buttonList, $action, $object = null): array
79
    {
80
        if ($action === 'list') {
81
            $buttonList['statistics'] = [
82
                'template' => '@ProjetNormandieUser/Admin/statistics_button.html.twig'
83
            ];
84
        }
85
86
        return parent::configureActionButtons($buttonList, $action, $object);
87
    }
88
89
    /**
90
     * Fields to be shown on filters
91
     */
92
    protected function configureDatagridFilters(DatagridMapper $filter): void
93
    {
94
        $filter
95
            ->add('id', null, ['label' => 'security_event.filter.id'])
96
            ->add('user', null, [
97
                'label' => 'security_event.filter.user',
98
                'field_type' => ModelAutocompleteType::class,
99
                'field_options' => [
100
                    'property' => 'username',
101
                    'admin_code' => 'sonata.admin.user.user',
102
                    'callback' => function ($admin, $property, $value) {
103
                        $datagrid = $admin->getDatagrid();
104
                        $query = $datagrid->getQuery();
105
                        $query
106
                            ->andWhere($query->expr()->orX(
107
                                $query->expr()->like('username', ':username'),
108
                                $query->expr()->like('email', ':username')
109
                            ))
110
                            ->setParameter('username', '%' . $value . '%');
111
                    },
112
                ],
113
            ])
114
            ->add('eventType', StringFilter::class, [
115
                'label' => 'security_event.filter.event_type',
116
                'field_type' => ChoiceType::class,
117
                'field_options' => [
118
                    'choices' => SecurityEventTypeEnum::getOptionsForForm(),
119
                    'required' => false,
120
                    'multiple' => false,
121
                    'expanded' => false,
122
                    'translation_domain' => 'messages',
123
                ],
124
            ])
125
            ->add('ipAddress', null, ['label' => 'security_event.filter.ip_address'])
126
            ->add('userAgent', null, ['label' => 'security_event.filter.user_agent'])
127
            ->add('createdAt', DateTimeFilter::class, [
128
                'label' => 'security_event.filter.date_time',
129
                'field_type' => DateTimePickerType::class,
130
                'field_options' => [
131
                    'format' => 'yyyy-MM-dd HH:mm:ss',
132
                ],
133
            ]);
134
    }
135
136
    /**
137
     * Fields to be shown on show action
138
     */
139
    protected function configureShowFields(ShowMapper $show): void
140
    {
141
        $show
142
            ->with('security_event_show_event_information', ['class' => 'col-md-6'])
143
            ->add('id', null, ['label' => 'security_event.list.id'])
144
            ->add('eventType', null, [
145
                'label' => 'security_event.list.event_type',
146
                'template' => '@ProjetNormandieUser/Admin/security_event_type.html.twig'
147
            ])
148
            ->add('createdAt', 'datetime', [
149
                'label' => 'security_event.list.date_time',
150
                'format' => 'd/m/Y H:i:s'
151
            ])
152
            ->end()
153
            ->with('security_event_show_user_information', ['class' => 'col-md-6'])
154
            ->add('user', null, [
155
                'label' => 'security_event.list.user',
156
                'admin_code' => 'sonata.admin.user.user'
157
            ])
158
            ->add('ipAddress', null, ['label' => 'security_event.list.ip_address'])
159
            ->add('userAgent', null, ['label' => 'security_event.filter.user_agent'])
160
            ->end()
161
            ->with('security_event_show_event_data', ['class' => 'col-md-12'])
162
            ->add('eventData', null, [
163
                'label' => 'security_event.show.event_data',
164
                'template' => '@ProjetNormandieUser/Admin/security_event_data.html.twig'
165
            ])
166
            ->end();
167
    }
168
}
169