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 App\Services\UserSystem\EnforceTFARedirectHelper; |
||
24 | use App\Tests\Services\UserSystem\EnforceTFARedirectHelperTest; |
||
0 ignored issues
–
show
|
|||
25 | use Doctrine\ORM\EntityManagerInterface; |
||
26 | use EasyCorp\Bundle\EasyAdminBundle\Config\Action; |
||
27 | use EasyCorp\Bundle\EasyAdminBundle\Config\Actions; |
||
28 | use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; |
||
29 | use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; |
||
30 | use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField; |
||
31 | use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; |
||
32 | use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField; |
||
33 | use EasyCorp\Bundle\EasyAdminBundle\Field\FormField; |
||
34 | use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField; |
||
35 | use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; |
||
36 | use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface; |
||
37 | |||
38 | class UserCrudController extends AbstractCrudController |
||
39 | { |
||
40 | private $encoder; |
||
41 | private $TFARedirectHelper; |
||
42 | |||
43 | public const USER_ROLE_CHOICES = [ |
||
44 | 'user.role.access_admin' => 'ROLE_ADMIN', |
||
45 | 'user.role.edit_user' => 'ROLE_EDIT_USER', |
||
46 | 'user.role.edit_organisations' => 'ROLE_EDIT_ORGANISATIONS', |
||
47 | 'user.role.show_payment_orders' => 'ROLE_SHOW_PAYMENT_ORDERS', |
||
48 | 'user.role.edit_payment_orders' => 'ROLE_EDIT_PAYMENT_ORDERS', |
||
49 | 'user.role.edit_po_factually' => 'ROLE_PO_FACTUALLY', |
||
50 | 'user.role.edit_po_mathematically' => 'ROLE_PO_MATHEMATICALLY', |
||
51 | 'user.role.edit_bank_accounts' => 'ROLE_EDIT_BANK_ACCOUNTS', |
||
52 | 'user.role.view_audit_logs' => 'ROLE_VIEW_AUDITS', |
||
53 | 'user.role.export_references' => 'ROLE_EXPORT_REFERENCES', |
||
54 | 'user.role.manual_confirmation' => 'ROLE_MANUAL_CONFIRMATION', |
||
55 | 'user.role.show_sepa_exports' => 'ROLE_SHOW_SEPA_EXPORTS', |
||
56 | 'user.role.book_sepa_exports' => 'ROLE_BOOK_SEPA_EXPORTS', |
||
57 | ]; |
||
58 | |||
59 | public function __construct(UserPasswordHasherInterface $encoder, EnforceTFARedirectHelper $TFARedirectHelper) |
||
60 | { |
||
61 | $this->encoder = $encoder; |
||
62 | $this->TFARedirectHelper = $TFARedirectHelper; |
||
63 | } |
||
64 | |||
65 | public static function getEntityFqcn(): string |
||
66 | { |
||
67 | return User::class; |
||
68 | } |
||
69 | |||
70 | public function configureActions(Actions $actions): Actions |
||
71 | { |
||
72 | $actions->setPermissions([ |
||
73 | Action::EDIT => 'ROLE_EDIT_USER', |
||
74 | Action::DELETE => 'ROLE_EDIT_USER', |
||
75 | Action::NEW => 'ROLE_EDIT_USER', |
||
76 | Action::INDEX => 'ROLE_READ_USER', |
||
77 | Action::DETAIL => 'ROLE_READ_USER', |
||
78 | ]); |
||
79 | |||
80 | return parent::configureActions($actions); |
||
81 | } |
||
82 | |||
83 | public function configureCrud(Crud $crud): Crud |
||
84 | { |
||
85 | return $crud |
||
86 | ->setEntityLabelInSingular('user.label') |
||
87 | ->setEntityLabelInPlural('user.labelp') |
||
88 | ->setFormOptions([ |
||
89 | 'validation_groups' => ['Default', 'perm_edit'], |
||
90 | ]) |
||
91 | ->setSearchFields(['id', 'username', 'role_description', 'email', 'roles', 'first_name', 'last_name']); |
||
92 | } |
||
93 | |||
94 | public function configureFields(string $pageName): iterable |
||
95 | { |
||
96 | return [ |
||
97 | //Basic info |
||
98 | IntegerField::new('id', 'user.id.label') |
||
99 | ->hideOnForm(), |
||
100 | TextField::new('username', 'user.username.label'), |
||
101 | TextField::new('fullName', 'user.fullName.label') |
||
102 | ->onlyOnIndex(), |
||
103 | TextField::new('first_name', 'user.first_name.label') |
||
104 | ->setRequired(false) |
||
105 | ->setFormTypeOption('empty_data', '') |
||
106 | ->hideOnIndex(), |
||
107 | TextField::new('last_name', 'user.last_name.label') |
||
108 | ->setRequired(false) |
||
109 | ->setFormTypeOption('empty_data', '') |
||
110 | ->hideOnIndex(), |
||
111 | EmailField::new('email', 'user.email.label') |
||
112 | ->setRequired(false) |
||
113 | ->setFormTypeOption('empty_data', ''), |
||
114 | TextField::new('role_description', 'user.role_description.label') |
||
115 | ->setRequired(false) |
||
116 | ->setFormTypeOption('empty_data', ''), |
||
117 | BooleanField::new('disabled', 'user.disabled.label') |
||
118 | ->setRequired(false) |
||
119 | ->renderAsSwitch(false) |
||
120 | ->hideOnIndex(), |
||
121 | BooleanField::new('password_change_needed', 'user.password_change_needed.label') |
||
122 | ->setRequired(false) |
||
123 | ->renderAsSwitch(false) |
||
124 | ->hideOnIndex(), |
||
125 | |||
126 | ChoiceField::new('roles', 'user.roles.label') |
||
127 | ->allowMultipleChoices() |
||
128 | ->setChoices(self::USER_ROLE_CHOICES) |
||
129 | ->renderExpanded() |
||
130 | ->renderAsNativeWidget() |
||
131 | ->hideOnIndex(), |
||
132 | |||
133 | //Passowrd panel |
||
134 | FormField::addPanel('user.section.password') |
||
135 | ->setHelp('user.section.password.help') |
||
136 | ->onlyOnForms(), |
||
137 | PasswordField::new('plain_password') |
||
138 | ->setRequired(Crud::PAGE_NEW === $pageName) |
||
139 | ->onlyOnForms(), |
||
140 | |||
141 | //2FA panel |
||
142 | FormField::addPanel('user.section.tfa')->setHelp('user.section.tfa.help'), |
||
143 | BooleanField::new('tfa_enabled', 'user.tfa_enabled.label') |
||
144 | ->setHelp('user.tfa_enabled.help') |
||
145 | ->renderAsSwitch(false) |
||
146 | ->setFormTypeOption('disabled', true), |
||
147 | ]; |
||
148 | } |
||
149 | |||
150 | private function setUserPlainPassword(User $user): void |
||
151 | { |
||
152 | if ($user->getPlainPassword()) { |
||
153 | $user->setPassword($this->encoder->hashPassword($user, $user->getPlainPassword())); |
||
154 | $user->setPlainPassword(null); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | public function persistEntity(EntityManagerInterface $entityManager, $entityInstance): void |
||
159 | { |
||
160 | $this->setUserPlainPassword($entityInstance); |
||
161 | //Set password before persisting |
||
162 | parent::persistEntity($entityManager, $entityInstance); |
||
163 | } |
||
164 | |||
165 | public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void |
||
166 | { |
||
167 | $this->setUserPlainPassword($entityInstance); |
||
168 | parent::updateEntity($entityManager, $entityInstance); |
||
169 | } |
||
170 | } |
||
171 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths