]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ConfigController.php
Handle password change
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / ConfigController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7 use Symfony\Component\HttpFoundation\Request;
8 use Wallabag\CoreBundle\Entity\Config;
9 use Wallabag\CoreBundle\Form\Type\ConfigType;
10 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
11
12 class ConfigController extends Controller
13 {
14 /**
15 * @param Request $request
16 *
17 * @Route("/config", name="config")
18 */
19 public function indexAction(Request $request)
20 {
21 $em = $this->getDoctrine()->getManager();
22 $config = $this->getConfig();
23
24 // handle basic config detail
25 $configForm = $this->createForm(new ConfigType(), $config);
26 $configForm->handleRequest($request);
27
28 if ($configForm->isValid()) {
29
30 $em->persist($config);
31 $em->flush();
32
33 $this->get('session')->getFlashBag()->add(
34 'notice',
35 'Config saved'
36 );
37
38 return $this->redirect($this->generateUrl('config'));
39 }
40
41 // handle changing password
42 $pwdForm = $this->createForm(new ChangePasswordType());
43 $pwdForm->handleRequest($request);
44
45 if ($pwdForm->isValid()) {
46 $user = $this->getUser();
47 $user->setPassword($pwdForm->get('new_password')->getData());
48 $em->persist($user);
49 $em->flush();
50
51 $this->get('session')->getFlashBag()->add(
52 'notice',
53 'Password updated'
54 );
55
56 return $this->redirect($this->generateUrl('config'));
57 }
58
59 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
60 'configForm' => $configForm->createView(),
61 'pwdForm' => $pwdForm->createView(),
62 ));
63 }
64
65 /**
66 * Retrieve config for the current user.
67 * If no config were found, create a new one.
68 *
69 * @return Wallabag\CoreBundle\Entity\Config
70 */
71 private function getConfig()
72 {
73 $config = $this->getDoctrine()
74 ->getRepository('WallabagCoreBundle:Config')
75 ->findOneByUser($this->getUser());
76
77 if (!$config) {
78 $config = new Config($this->getUser());
79 }
80
81 return $config;
82 }
83 }