Total Complexity | 40 |
Total Lines | 339 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like UserSettingsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use UserSettingsController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
57 | class UserSettingsController extends AbstractController |
||
58 | { |
||
59 | protected bool $demo_mode; |
||
60 | |||
61 | /** |
||
62 | * @var EventDispatcher|EventDispatcherInterface |
||
63 | */ |
||
64 | protected $eventDispatcher; |
||
65 | |||
66 | public function __construct(bool $demo_mode, EventDispatcherInterface $eventDispatcher) |
||
67 | { |
||
68 | $this->demo_mode = $demo_mode; |
||
69 | $this->eventDispatcher = $eventDispatcher; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @Route("/2fa_backup_codes", name="show_backup_codes") |
||
74 | */ |
||
75 | public function showBackupCodes() |
||
76 | { |
||
77 | $user = $this->getUser(); |
||
78 | |||
79 | //When user change its settings, he should be logged in fully. |
||
80 | $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
||
81 | |||
82 | if (!$user instanceof User) { |
||
83 | return new RuntimeException('This controller only works only for Part-DB User objects!'); |
||
84 | } |
||
85 | |||
86 | if (empty($user->getBackupCodes())) { |
||
87 | $this->addFlash('error', 'tfa_backup.no_codes_enabled'); |
||
88 | |||
89 | throw new RuntimeException('You do not have any backup codes enabled, therefore you can not view them!'); |
||
90 | } |
||
91 | |||
92 | return $this->render('users/backup_codes.html.twig', [ |
||
93 | 'user' => $user, |
||
94 | ]); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * @Route("/u2f_delete", name="u2f_delete", methods={"DELETE"}) |
||
99 | */ |
||
100 | public function removeU2FToken(Request $request, EntityManagerInterface $entityManager, BackupCodeManager $backupCodeManager): RedirectResponse |
||
101 | { |
||
102 | if ($this->demo_mode) { |
||
103 | throw new RuntimeException('You can not do 2FA things in demo mode'); |
||
104 | } |
||
105 | |||
106 | $user = $this->getUser(); |
||
107 | |||
108 | //When user change its settings, he should be logged in fully. |
||
109 | $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
||
110 | |||
111 | if (!$user instanceof User) { |
||
112 | throw new RuntimeException('This controller only works only for Part-DB User objects!'); |
||
113 | } |
||
114 | |||
115 | if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) { |
||
116 | //Handle U2F key removal |
||
117 | if ($request->request->has('key_id')) { |
||
118 | $key_id = $request->request->get('key_id'); |
||
119 | $key_repo = $entityManager->getRepository(U2FKey::class); |
||
120 | /** @var U2FKey|null $u2f */ |
||
121 | $u2f = $key_repo->find($key_id); |
||
122 | if (null === $u2f) { |
||
123 | $this->addFlash('danger', 'tfa_u2f.u2f_delete.not_existing'); |
||
124 | |||
125 | return $this->redirectToRoute('user_settings'); |
||
126 | } |
||
127 | |||
128 | //User can only delete its own U2F keys |
||
129 | if ($u2f->getUser() !== $user) { |
||
130 | $this->addFlash('danger', 'tfa_u2f.u2f_delete.access_denied'); |
||
131 | |||
132 | return $this->redirectToRoute('user_settings'); |
||
133 | } |
||
134 | |||
135 | $backupCodeManager->disableBackupCodesIfUnused($user); |
||
136 | $entityManager->remove($u2f); |
||
137 | $entityManager->flush(); |
||
138 | $this->addFlash('success', 'tfa.u2f.u2f_delete.success'); |
||
139 | |||
140 | $security_event = new SecurityEvent($user); |
||
141 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::U2F_REMOVED); |
||
142 | } else if ($request->request->has('webauthn_key_id')) { |
||
143 | $key_id = $request->request->get('webauthn_key_id'); |
||
144 | $key_repo = $entityManager->getRepository(WebauthnKey::class); |
||
145 | /** @var WebauthnKey|null $key */ |
||
146 | $key = $key_repo->find($key_id); |
||
147 | if (null === $key) { |
||
148 | $this->addFlash('error', 'tfa_u2f.u2f_delete.not_existing'); |
||
149 | |||
150 | return $this->redirectToRoute('user_settings'); |
||
151 | } |
||
152 | |||
153 | //User can only delete its own U2F keys |
||
154 | if ($key->getUser() !== $user) { |
||
155 | $this->addFlash('error', 'tfa_u2f.u2f_delete.access_denied'); |
||
156 | |||
157 | return $this->redirectToRoute('user_settings'); |
||
158 | } |
||
159 | |||
160 | $backupCodeManager->disableBackupCodesIfUnused($user); |
||
161 | $entityManager->remove($key); |
||
162 | $entityManager->flush(); |
||
163 | $this->addFlash('success', 'tfa.u2f.u2f_delete.success'); |
||
164 | |||
165 | $security_event = new SecurityEvent($user); |
||
166 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::U2F_REMOVED); |
||
167 | } |
||
168 | } else { |
||
169 | $this->addFlash('error', 'csfr_invalid'); |
||
170 | } |
||
171 | |||
172 | return $this->redirectToRoute('user_settings'); |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * @Route("/invalidate_trustedDevices", name="tfa_trustedDevices_invalidate", methods={"DELETE"}) |
||
177 | * |
||
178 | * @return RuntimeException|RedirectResponse |
||
179 | */ |
||
180 | public function resetTrustedDevices(Request $request, EntityManagerInterface $entityManager) |
||
181 | { |
||
182 | if ($this->demo_mode) { |
||
183 | throw new RuntimeException('You can not do 2FA things in demo mode'); |
||
184 | } |
||
185 | |||
186 | $user = $this->getUser(); |
||
187 | |||
188 | //When user change its settings, he should be logged in fully. |
||
189 | $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); |
||
190 | |||
191 | if (!$user instanceof User) { |
||
192 | return new RuntimeException('This controller only works only for Part-DB User objects!'); |
||
193 | } |
||
194 | |||
195 | if ($this->isCsrfTokenValid('devices_reset'.$user->getId(), $request->request->get('_token'))) { |
||
196 | $user->invalidateTrustedDeviceTokens(); |
||
197 | $entityManager->flush(); |
||
198 | $this->addFlash('success', 'tfa_trustedDevice.invalidate.success'); |
||
199 | |||
200 | $security_event = new SecurityEvent($user); |
||
201 | $this->eventDispatcher->dispatch($security_event, SecurityEvents::TRUSTED_DEVICE_RESET); |
||
202 | } else { |
||
203 | $this->addFlash('error', 'csfr_invalid'); |
||
204 | } |
||
205 | |||
206 | return $this->redirectToRoute('user_settings'); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @Route("/settings", name="user_settings") |
||
211 | * |
||
212 | * @return RedirectResponse|Response |
||
213 | */ |
||
214 | public function userSettings(Request $request, EntityManagerInterface $em, UserPasswordHasherInterface $passwordEncoder, GoogleAuthenticator $googleAuthenticator, BackupCodeManager $backupCodeManager, FormFactoryInterface $formFactory, UserAvatarHelper $avatarHelper) |
||
396 | ], |
||
397 | ]); |
||
398 | } |
||
399 | } |
||
400 |