SecurityEventAdmin::configureShowFields()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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