|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2020 Jan Böhmer |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
7
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace App\Controller\Admin; |
|
20
|
|
|
|
|
21
|
|
|
use App\Admin\Field\PasswordField; |
|
22
|
|
|
use App\Entity\User; |
|
23
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
24
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Config\Action; |
|
25
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; |
|
26
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; |
|
27
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; |
|
28
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField; |
|
29
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; |
|
30
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField; |
|
31
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField; |
|
32
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField; |
|
33
|
|
|
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; |
|
34
|
|
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
|
35
|
|
|
|
|
36
|
|
|
class UserCrudController extends AbstractCrudController |
|
37
|
|
|
{ |
|
38
|
|
|
private $encoder; |
|
39
|
|
|
|
|
40
|
|
|
public const USER_ROLE_CHOICES = [ |
|
41
|
|
|
'user.role.access_admin' => 'ROLE_ADMIN', |
|
42
|
|
|
'user.role.edit_user' => 'ROLE_EDIT_USER', |
|
43
|
|
|
'user.role.edit_organisations' => 'ROLE_EDIT_ORGANISATIONS', |
|
44
|
|
|
'user.role.show_payment_orders' => 'ROLE_SHOW_PAYMENT_ORDERS', |
|
45
|
|
|
'user.role.edit_payment_orders' => 'ROLE_EDIT_PAYMENT_ORDERS', |
|
46
|
|
|
'user.role.edit_po_factually' => 'ROLE_PO_FACTUALLY', |
|
47
|
|
|
'user.role.edit_po_mathematically' => 'ROLE_PO_MATHEMATICALLY', |
|
48
|
|
|
'user.role.edit_bank_accounts' => 'ROLE_EDIT_BANK_ACCOUNTS', |
|
49
|
|
|
'user.role.view_audit_logs' => 'ROLE_VIEW_AUDITS', |
|
50
|
|
|
'user.role.export_references' => 'ROLE_EXPORT_REFERENCES', |
|
51
|
|
|
'user.role.manual_confirmation' => 'ROLE_MANUAL_CONFIRMATION', |
|
52
|
|
|
'user.role.show_sepa_exports' => 'ROLE_SHOW_SEPA_EXPORTS', |
|
53
|
|
|
'user.role.book_sepa_exports' => 'ROLE_BOOK_SEPA_EXPORTS', |
|
54
|
|
|
]; |
|
55
|
|
|
|
|
56
|
|
|
public function __construct(UserPasswordHasherInterface $encoder) |
|
57
|
|
|
{ |
|
58
|
|
|
$this->encoder = $encoder; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public static function getEntityFqcn(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return User::class; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public function configureActions(Actions $actions): Actions |
|
67
|
|
|
{ |
|
68
|
|
|
$actions->setPermissions([ |
|
69
|
|
|
Action::EDIT => 'ROLE_EDIT_USER', |
|
70
|
|
|
Action::DELETE => 'ROLE_EDIT_USER', |
|
71
|
|
|
Action::NEW => 'ROLE_EDIT_USER', |
|
72
|
|
|
Action::INDEX => 'ROLE_READ_USER', |
|
73
|
|
|
Action::DETAIL => 'ROLE_READ_USER', |
|
74
|
|
|
]); |
|
75
|
|
|
|
|
76
|
|
|
return parent::configureActions($actions); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function configureCrud(Crud $crud): Crud |
|
80
|
|
|
{ |
|
81
|
|
|
return $crud |
|
82
|
|
|
->setEntityLabelInSingular('user.label') |
|
83
|
|
|
->setEntityLabelInPlural('user.labelp') |
|
84
|
|
|
->setFormOptions([ |
|
85
|
|
|
'validation_groups' => ['Default', 'perm_edit'], |
|
86
|
|
|
]) |
|
87
|
|
|
->setSearchFields(['id', 'username', 'role_description', 'email', 'roles', 'first_name', 'last_name']); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function configureFields(string $pageName): iterable |
|
91
|
|
|
{ |
|
92
|
|
|
return [ |
|
93
|
|
|
//Basic info |
|
94
|
|
|
IntegerField::new('id', 'user.id.label') |
|
95
|
|
|
->hideOnForm(), |
|
96
|
|
|
TextField::new('username', 'user.username.label'), |
|
97
|
|
|
TextField::new('fullName', 'user.fullName.label') |
|
98
|
|
|
->onlyOnIndex(), |
|
99
|
|
|
TextField::new('first_name', 'user.first_name.label') |
|
100
|
|
|
->setRequired(false) |
|
101
|
|
|
->setFormTypeOption('empty_data', '') |
|
102
|
|
|
->hideOnIndex(), |
|
103
|
|
|
TextField::new('last_name', 'user.last_name.label') |
|
104
|
|
|
->setRequired(false) |
|
105
|
|
|
->setFormTypeOption('empty_data', '') |
|
106
|
|
|
->hideOnIndex(), |
|
107
|
|
|
EmailField::new('email', 'user.email.label') |
|
108
|
|
|
->setRequired(false) |
|
109
|
|
|
->setFormTypeOption('empty_data', ''), |
|
110
|
|
|
TextField::new('role_description', 'user.role_description.label') |
|
111
|
|
|
->setRequired(false) |
|
112
|
|
|
->setFormTypeOption('empty_data', ''), |
|
113
|
|
|
ChoiceField::new('roles', 'user.roles.label') |
|
114
|
|
|
->allowMultipleChoices() |
|
115
|
|
|
->setChoices(self::USER_ROLE_CHOICES) |
|
116
|
|
|
->renderExpanded() |
|
117
|
|
|
->renderAsNativeWidget() |
|
118
|
|
|
->hideOnIndex(), |
|
119
|
|
|
|
|
120
|
|
|
//Passowrd panel |
|
121
|
|
|
FormField::addPanel('user.section.password') |
|
122
|
|
|
->setHelp('user.section.password.help') |
|
123
|
|
|
->onlyOnForms(), |
|
124
|
|
|
PasswordField::new('plain_password') |
|
125
|
|
|
->setRequired(Crud::PAGE_NEW === $pageName) |
|
126
|
|
|
->onlyOnForms(), |
|
127
|
|
|
|
|
128
|
|
|
//2FA panel |
|
129
|
|
|
FormField::addPanel('user.section.tfa')->setHelp('user.section.tfa.help'), |
|
130
|
|
|
BooleanField::new('tfa_enabled', 'user.tfa_enabled.label') |
|
131
|
|
|
->setHelp('user.tfa_enabled.help') |
|
132
|
|
|
->renderAsSwitch(false) |
|
133
|
|
|
->setFormTypeOption('disabled', true), |
|
134
|
|
|
]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
private function setUserPlainPassword(User $user): void |
|
138
|
|
|
{ |
|
139
|
|
|
if ($user->getPlainPassword()) { |
|
140
|
|
|
$user->setPassword($this->encoder->hashPassword($user, $user->getPlainPassword())); |
|
141
|
|
|
$user->setPlainPassword(null); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void |
|
146
|
|
|
{ |
|
147
|
|
|
$this->setUserPlainPassword($entityInstance); |
|
148
|
|
|
//Set password before persisting |
|
149
|
|
|
parent::persistEntity($entityManager, $entityInstance); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void |
|
153
|
|
|
{ |
|
154
|
|
|
$this->setUserPlainPassword($entityInstance); |
|
155
|
|
|
parent::updateEntity($entityManager, $entityInstance); |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|