diff options
Diffstat (limited to 'src/Wallabag/CoreBundle/Controller/ConfigController.php')
-rw-r--r-- | src/Wallabag/CoreBundle/Controller/ConfigController.php | 157 |
1 files changed, 146 insertions, 11 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index b999c539..9257ab18 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php | |||
@@ -2,12 +2,14 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\CoreBundle\Controller; | 3 | namespace Wallabag\CoreBundle\Controller; |
4 | 4 | ||
5 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 5 | use PragmaRX\Recovery\Recovery as BackupCodes; |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
7 | use Symfony\Component\HttpFoundation\JsonResponse; | 7 | use Symfony\Component\HttpFoundation\JsonResponse; |
8 | use Symfony\Component\HttpFoundation\RedirectResponse; | 8 | use Symfony\Component\HttpFoundation\RedirectResponse; |
9 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; | 10 | use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
11 | use Symfony\Component\Routing\Annotation\Route; | ||
12 | use Symfony\Component\Validator\Constraints\Locale as LocaleConstraint; | ||
11 | use Wallabag\CoreBundle\Entity\Config; | 13 | use Wallabag\CoreBundle\Entity\Config; |
12 | use Wallabag\CoreBundle\Entity\TaggingRule; | 14 | use Wallabag\CoreBundle\Entity\TaggingRule; |
13 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; | 15 | use Wallabag\CoreBundle\Form\Type\ChangePasswordType; |
@@ -45,7 +47,7 @@ class ConfigController extends Controller | |||
45 | $activeTheme = $this->get('liip_theme.active_theme'); | 47 | $activeTheme = $this->get('liip_theme.active_theme'); |
46 | $activeTheme->setName($config->getTheme()); | 48 | $activeTheme->setName($config->getTheme()); |
47 | 49 | ||
48 | $this->get('session')->getFlashBag()->add( | 50 | $this->addFlash( |
49 | 'notice', | 51 | 'notice', |
50 | 'flashes.config.notice.config_saved' | 52 | 'flashes.config.notice.config_saved' |
51 | ); | 53 | ); |
@@ -67,7 +69,7 @@ class ConfigController extends Controller | |||
67 | $userManager->updateUser($user, true); | 69 | $userManager->updateUser($user, true); |
68 | } | 70 | } |
69 | 71 | ||
70 | $this->get('session')->getFlashBag()->add('notice', $message); | 72 | $this->addFlash('notice', $message); |
71 | 73 | ||
72 | return $this->redirect($this->generateUrl('config') . '#set4'); | 74 | return $this->redirect($this->generateUrl('config') . '#set4'); |
73 | } | 75 | } |
@@ -82,7 +84,7 @@ class ConfigController extends Controller | |||
82 | if ($userForm->isSubmitted() && $userForm->isValid()) { | 84 | if ($userForm->isSubmitted() && $userForm->isValid()) { |
83 | $userManager->updateUser($user, true); | 85 | $userManager->updateUser($user, true); |
84 | 86 | ||
85 | $this->get('session')->getFlashBag()->add( | 87 | $this->addFlash( |
86 | 'notice', | 88 | 'notice', |
87 | 'flashes.config.notice.user_updated' | 89 | 'flashes.config.notice.user_updated' |
88 | ); | 90 | ); |
@@ -98,7 +100,7 @@ class ConfigController extends Controller | |||
98 | $em->persist($config); | 100 | $em->persist($config); |
99 | $em->flush(); | 101 | $em->flush(); |
100 | 102 | ||
101 | $this->get('session')->getFlashBag()->add( | 103 | $this->addFlash( |
102 | 'notice', | 104 | 'notice', |
103 | 'flashes.config.notice.rss_updated' | 105 | 'flashes.config.notice.rss_updated' |
104 | ); | 106 | ); |
@@ -130,7 +132,7 @@ class ConfigController extends Controller | |||
130 | $em->persist($taggingRule); | 132 | $em->persist($taggingRule); |
131 | $em->flush(); | 133 | $em->flush(); |
132 | 134 | ||
133 | $this->get('session')->getFlashBag()->add( | 135 | $this->addFlash( |
134 | 'notice', | 136 | 'notice', |
135 | 'flashes.config.notice.tagging_rules_updated' | 137 | 'flashes.config.notice.tagging_rules_updated' |
136 | ); | 138 | ); |
@@ -152,12 +154,124 @@ class ConfigController extends Controller | |||
152 | ], | 154 | ], |
153 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | 155 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), |
154 | 'wallabag_url' => $this->getParameter('domain_name'), | 156 | 'wallabag_url' => $this->getParameter('domain_name'), |
155 | 'enabled_users' => $this->get('wallabag_user.user_repository') | 157 | 'enabled_users' => $this->get('wallabag_user.user_repository')->getSumEnabledUsers(), |
156 | ->getSumEnabledUsers(), | ||
157 | ]); | 158 | ]); |
158 | } | 159 | } |
159 | 160 | ||
160 | /** | 161 | /** |
162 | * Enable 2FA using email. | ||
163 | * | ||
164 | * @Route("/config/otp/email", name="config_otp_email") | ||
165 | */ | ||
166 | public function otpEmailAction() | ||
167 | { | ||
168 | if (!$this->getParameter('twofactor_auth')) { | ||
169 | return $this->createNotFoundException('two_factor not enabled'); | ||
170 | } | ||
171 | |||
172 | $user = $this->getUser(); | ||
173 | |||
174 | $user->setGoogleAuthenticatorSecret(null); | ||
175 | $user->setBackupCodes(null); | ||
176 | $user->setEmailTwoFactor(true); | ||
177 | |||
178 | $this->container->get('fos_user.user_manager')->updateUser($user, true); | ||
179 | |||
180 | $this->addFlash( | ||
181 | 'notice', | ||
182 | 'flashes.config.notice.otp_enabled' | ||
183 | ); | ||
184 | |||
185 | return $this->redirect($this->generateUrl('config') . '#set3'); | ||
186 | } | ||
187 | |||
188 | /** | ||
189 | * Enable 2FA using OTP app, user will need to confirm the generated code from the app. | ||
190 | * | ||
191 | * @Route("/config/otp/app", name="config_otp_app") | ||
192 | */ | ||
193 | public function otpAppAction() | ||
194 | { | ||
195 | if (!$this->getParameter('twofactor_auth')) { | ||
196 | return $this->createNotFoundException('two_factor not enabled'); | ||
197 | } | ||
198 | |||
199 | $user = $this->getUser(); | ||
200 | $secret = $this->get('scheb_two_factor.security.google_authenticator')->generateSecret(); | ||
201 | |||
202 | $user->setGoogleAuthenticatorSecret($secret); | ||
203 | $user->setEmailTwoFactor(false); | ||
204 | |||
205 | $backupCodes = (new BackupCodes())->toArray(); | ||
206 | $backupCodesHashed = array_map( | ||
207 | function ($backupCode) { | ||
208 | return password_hash($backupCode, PASSWORD_DEFAULT); | ||
209 | }, | ||
210 | $backupCodes | ||
211 | ); | ||
212 | |||
213 | $user->setBackupCodes($backupCodesHashed); | ||
214 | |||
215 | $this->container->get('fos_user.user_manager')->updateUser($user, true); | ||
216 | |||
217 | return $this->render('WallabagCoreBundle:Config:otp_app.html.twig', [ | ||
218 | 'backupCodes' => $backupCodes, | ||
219 | 'qr_code' => $this->get('scheb_two_factor.security.google_authenticator')->getQRContent($user), | ||
220 | ]); | ||
221 | } | ||
222 | |||
223 | /** | ||
224 | * Cancelling 2FA using OTP app. | ||
225 | * | ||
226 | * @Route("/config/otp/app/cancel", name="config_otp_app_cancel") | ||
227 | */ | ||
228 | public function otpAppCancelAction() | ||
229 | { | ||
230 | if (!$this->getParameter('twofactor_auth')) { | ||
231 | return $this->createNotFoundException('two_factor not enabled'); | ||
232 | } | ||
233 | |||
234 | $user = $this->getUser(); | ||
235 | $user->setGoogleAuthenticatorSecret(null); | ||
236 | $user->setBackupCodes(null); | ||
237 | |||
238 | $this->container->get('fos_user.user_manager')->updateUser($user, true); | ||
239 | |||
240 | return $this->redirect($this->generateUrl('config') . '#set3'); | ||
241 | } | ||
242 | |||
243 | /** | ||
244 | * Validate OTP code. | ||
245 | * | ||
246 | * @param Request $request | ||
247 | * | ||
248 | * @Route("/config/otp/app/check", name="config_otp_app_check") | ||
249 | */ | ||
250 | public function otpAppCheckAction(Request $request) | ||
251 | { | ||
252 | $isValid = $this->get('scheb_two_factor.security.google_authenticator')->checkCode( | ||
253 | $this->getUser(), | ||
254 | $request->get('_auth_code') | ||
255 | ); | ||
256 | |||
257 | if (true === $isValid) { | ||
258 | $this->addFlash( | ||
259 | 'notice', | ||
260 | 'flashes.config.notice.otp_enabled' | ||
261 | ); | ||
262 | |||
263 | return $this->redirect($this->generateUrl('config') . '#set3'); | ||
264 | } | ||
265 | |||
266 | $this->addFlash( | ||
267 | 'two_factor', | ||
268 | 'scheb_two_factor.code_invalid' | ||
269 | ); | ||
270 | |||
271 | return $this->redirect($this->generateUrl('config_otp_app')); | ||
272 | } | ||
273 | |||
274 | /** | ||
161 | * @param Request $request | 275 | * @param Request $request |
162 | * | 276 | * |
163 | * @Route("/generate-token", name="generate_token") | 277 | * @Route("/generate-token", name="generate_token") |
@@ -177,7 +291,7 @@ class ConfigController extends Controller | |||
177 | return new JsonResponse(['token' => $config->getRssToken()]); | 291 | return new JsonResponse(['token' => $config->getRssToken()]); |
178 | } | 292 | } |
179 | 293 | ||
180 | $this->get('session')->getFlashBag()->add( | 294 | $this->addFlash( |
181 | 'notice', | 295 | 'notice', |
182 | 'flashes.config.notice.rss_token_updated' | 296 | 'flashes.config.notice.rss_token_updated' |
183 | ); | 297 | ); |
@@ -202,7 +316,7 @@ class ConfigController extends Controller | |||
202 | $em->remove($rule); | 316 | $em->remove($rule); |
203 | $em->flush(); | 317 | $em->flush(); |
204 | 318 | ||
205 | $this->get('session')->getFlashBag()->add( | 319 | $this->addFlash( |
206 | 'notice', | 320 | 'notice', |
207 | 'flashes.config.notice.tagging_rules_deleted' | 321 | 'flashes.config.notice.tagging_rules_deleted' |
208 | ); | 322 | ); |
@@ -268,7 +382,7 @@ class ConfigController extends Controller | |||
268 | break; | 382 | break; |
269 | } | 383 | } |
270 | 384 | ||
271 | $this->get('session')->getFlashBag()->add( | 385 | $this->addFlash( |
272 | 'notice', | 386 | 'notice', |
273 | 'flashes.config.notice.' . $type . '_reset' | 387 | 'flashes.config.notice.' . $type . '_reset' |
274 | ); | 388 | ); |
@@ -330,6 +444,27 @@ class ConfigController extends Controller | |||
330 | } | 444 | } |
331 | 445 | ||
332 | /** | 446 | /** |
447 | * Change the locale for the current user. | ||
448 | * | ||
449 | * @param Request $request | ||
450 | * @param string $language | ||
451 | * | ||
452 | * @Route("/locale/{language}", name="changeLocale") | ||
453 | * | ||
454 | * @return \Symfony\Component\HttpFoundation\RedirectResponse | ||
455 | */ | ||
456 | public function setLocaleAction(Request $request, $language = null) | ||
457 | { | ||
458 | $errors = $this->get('validator')->validate($language, (new LocaleConstraint())); | ||
459 | |||
460 | if (0 === \count($errors)) { | ||
461 | $request->getSession()->set('_locale', $language); | ||
462 | } | ||
463 | |||
464 | return $this->redirect($request->headers->get('referer', $this->generateUrl('homepage'))); | ||
465 | } | ||
466 | |||
467 | /** | ||
333 | * Remove all tags for given tags and a given user and cleanup orphan tags. | 468 | * Remove all tags for given tags and a given user and cleanup orphan tags. |
334 | * | 469 | * |
335 | * @param array $tags | 470 | * @param array $tags |