Completed
Push — master ( ecc42f...bcb453 )
by Paweł
15s queued 11s
created

UserAdmin::prePersist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Admin;
6
7
use App\Model\UserInterface;
8
use Sonata\AdminBundle\Admin\AbstractAdmin;
9
use Sonata\AdminBundle\Datagrid\DatagridMapper;
10
use Sonata\AdminBundle\Datagrid\ListMapper;
11
use Sonata\AdminBundle\Form\FormMapper;
12
use Sonata\AdminBundle\Show\ShowMapper;
13
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
14
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
15
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
16
17
final class UserAdmin extends AbstractAdmin
18
{
19
    private $encoder;
20
21
    public function preUpdate($object)
22
    {
23
        parent::preUpdate($object);
24
25
        $this->handleUserPassword($object);
26
    }
27
28
    public function prePersist($object)
29
    {
30
        parent::prePersist($object);
31
32
        $this->handleUserPassword($object);
33
    }
34
35
    private function handleUserPassword(UserInterface $user)
36
    {
37
        if (null === ($plainPassword = $user->getPlainPassword())) {
38
            return;
39
        }
40
41
        $encoded = $this->encoder->encodePassword($user, $plainPassword);
42
        $user->setPassword($encoded);
43
    }
44
45
    public function setEncoder(UserPasswordEncoderInterface $encoder)
46
    {
47
        $this->encoder = $encoder;
48
    }
49
50
    protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
51
    {
52
        $datagridMapper
53
            ->add('id')
54
            ->add('email')
55
            ->add('firstName')
56
            ->add('lastName')
57
            ->add('roles')
58
            ;
59
    }
60
61
    protected function configureListFields(ListMapper $listMapper): void
62
    {
63
        $listMapper
64
            ->add('id')
65
            ->add('email')
66
            ->add('firstName')
67
            ->add('lastName')
68
            ->add('courses')
69
            ->add('roles')
70
            ->add('_action', null, [
71
                'actions' => [
72
                    'show' => [],
73
                    'edit' => [],
74
                    'delete' => [],
75
                ],
76
            ]);
77
    }
78
79
    protected function configureFormFields(FormMapper $formMapper): void
80
    {
81
        $container = $this->getConfigurationPool()->getContainer();
82
        $roles = $container->getParameter('security.role_hierarchy.roles');
83
84
        $formMapper
85
            ->add('email')
86
            ->add('firstName')
87
            ->add('lastName')
88
            ->add('courses')
89
            ->add('plainPassword', PasswordType::class, ['label' => 'Password', 'required' => false])
90
            ->add('roles', ChoiceType::class, array(
91
                    'choices' => self::flattenRoles($roles),
92
                    'multiple' => true,
93
                )
94
            )
95
            ;
96
    }
97
98
    protected function configureShowFields(ShowMapper $showMapper): void
99
    {
100
        $showMapper
101
            ->add('id')
102
            ->add('firstName')
103
            ->add('lastName')
104
            ->add('courses')
105
            ->add('email')
106
            ->add('roles')
107
            ;
108
    }
109
110
    protected static function flattenRoles(array $rolesHierarchy): array
111
    {
112
        $flatRoles = [];
113
        foreach ($rolesHierarchy as $roles) {
114
            if (empty($roles)) {
115
                continue;
116
            }
117
118
            foreach ($roles as $role) {
119
                if (!isset($flatRoles[$role])) {
120
                    $flatRoles[$role] = $role;
121
                }
122
            }
123
        }
124
125
        return $flatRoles;
126
    }
127
}
128