/
UserAdmin.php
223 lines (194 loc) · 6.7 KB
/
UserAdmin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
declare(strict_types=1);
/*
* This file is part of the NucleosUserAdminBundle package.
*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nucleos\UserAdminBundle\Admin\Model;
use DomainException;
use Nucleos\UserAdminBundle\Form\Type\RolesMatrixType;
use Nucleos\UserBundle\Model\LocaleAwareUser;
use Nucleos\UserBundle\Model\UserInterface;
use Nucleos\UserBundle\Model\UserManager;
use Nucleos\UserBundle\Util\UserManipulator;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelType;
use Sonata\AdminBundle\Show\ShowMapper;
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
/**
* @phpstan-extends AbstractAdmin<UserInterface>
*/
abstract class UserAdmin extends AbstractAdmin
{
protected UserManager $userManager;
private ?UserManipulator $userManipulator;
public function __construct(UserManager $userManager, ?UserManipulator $userManipulator = null)
{
parent::__construct();
$this->userManager = $userManager;
$this->userManipulator = $userManipulator;
}
public function preUpdate($object): void
{
if (method_exists($this->userManager, 'updateCanonicalFields')) {
$this->userManager->updateCanonicalFields($object);
}
}
protected function postUpdate(object $object): void
{
$this->updatePassword($object);
}
protected function configureFormOptions(array &$formOptions): void
{
$formOptions['validation_groups'] = ['User', $this->isNewInstance() ? 'Registration' : 'Profile'];
}
protected function configureExportFields(): array
{
// avoid security field to be exported
return array_filter(
parent::configureExportFields(),
static function ($v): bool {
return !\in_array($v, ['password', 'salt'], true);
}
);
}
protected function createNewInstance(): object
{
$instance = $this->userManager->createUser();
// TODO: Find a better way to create editabe form models
// BC layer
try {
$instance->getUsername();
} catch (DomainException $exception) {
$instance->setUsername('');
}
try {
$instance->getEmail();
} catch (DomainException $exception) {
$instance->setEmail('');
}
return $instance;
}
protected function configureListFields(ListMapper $list): void
{
$list
->addIdentifier('username')
->add('email')
->add('groups')
->add('enabled', null, ['editabe' => true])
;
if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
$list
->add('id', 'string', [
'template' => '@NucleosUserAdmin/Admin/Field/impersonating.html.twig',
])
;
}
}
protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add('id')
->add('username')
->add('email')
->add('groups')
;
}
protected function configureShowFields(ShowMapper $show): void
{
$show
->with('general', ['label' => 'show.group_general'])
->add('username')
->add('email')
->end()
->with('groups', ['label' => 'show.group_groups'])
->add('groups')
->end()
;
}
/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
protected function configureFormFields(FormMapper $form): void
{
$form
->tab('user', ['label' => 'form.group_user'])
->with('general', ['class' => 'col-md-6'])->end()
->ifTrue($this->isLocaleAwareSubject())
->with('locale', ['class' => 'col-md-6'])->end()
->ifEnd()
->end()
->tab('security', ['label' => 'form.group_security'])
->with('groups', ['class' => 'col-md-8', 'label' => 'form.group_groups'])->end()
->with('status', ['class' => 'col-md-4', 'label' => 'form.group_status'])->end()
->with('roles', ['class' => 'col-md-12', 'label' => 'form.group_roles'])->end()
->end()
;
$form
->tab('user')
->with('general')
->add('username')
->add('email')
->add('plainPassword', TextType::class, [
'required' => $this->isNewInstance(),
])
->end()
->ifTrue($this->isLocaleAwareSubject())
->with('locale')
->add('locale', LocaleType::class, [
'required' => false,
])
->add('timezone', TimezoneType::class, [
'required' => false,
])
->end()
->ifEnd()
->end()
->tab('security')
->with('status')
->add('enabled', null, ['required' => false])
->end()
->with('groups')
->add('groups', ModelType::class, [
'required' => false,
'expanded' => true,
'multiple' => true,
])
->end()
->with('roles')
->add('roles', RolesMatrixType::class, [
'label' => 'form.label_roles',
'expanded' => true,
'multiple' => true,
'required' => false,
])
->end()
->end()
;
}
private function updatePassword(UserInterface $user): void
{
$plainPassword = $user->getPlainPassword();
if (null === $plainPassword) {
return;
}
$this->userManipulator?->changePassword($user->getUsername(), $plainPassword);
}
private function isNewInstance(): bool
{
return !$this->hasSubject() || null === $this->id($this->getSubject());
}
private function isLocaleAwareSubject(): bool
{
return is_subclass_of($this->getClass(), LocaleAwareUser::class);
}
}