]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ConfigController.php
* public registration
[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 Symfony\Component\HttpFoundation\JsonResponse;
9 use Wallabag\CoreBundle\Entity\Config;
10 use Wallabag\CoreBundle\Entity\User;
11 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
12 use Wallabag\CoreBundle\Form\Type\UserInformationType;
13 use Wallabag\CoreBundle\Form\Type\NewUserType;
14 use Wallabag\CoreBundle\Form\Type\RssType;
15 use Wallabag\CoreBundle\Tools\Utils;
16
17 class ConfigController extends Controller
18 {
19 /**
20 * @param Request $request
21 *
22 * @Route("/config", name="config")
23 */
24 public function indexAction(Request $request)
25 {
26 $em = $this->getDoctrine()->getManager();
27 $config = $this->getConfig();
28 $userManager = $this->container->get('fos_user.user_manager');
29 $user = $this->getUser();
30
31 // handle basic config detail (this form is defined as a service)
32 $configForm = $this->createForm('config', $config);
33 $configForm->handleRequest($request);
34
35 if ($configForm->isValid()) {
36 $em->persist($config);
37 $em->flush();
38
39 // switch active theme
40 $activeTheme = $this->get('liip_theme.active_theme');
41 $activeTheme->setName($config->getTheme());
42
43 $this->get('session')->getFlashBag()->add(
44 'notice',
45 'Config saved'
46 );
47
48 return $this->redirect($this->generateUrl('config'));
49 }
50
51 // handle changing password
52 $pwdForm = $this->createForm(new ChangePasswordType());
53 $pwdForm->handleRequest($request);
54
55 if ($pwdForm->isValid()) {
56 $user->setPlainPassword($pwdForm->get('new_password')->getData());
57 $userManager->updateUser($user, true);
58
59 $this->get('session')->getFlashBag()->add(
60 'notice',
61 'Password updated'
62 );
63
64 return $this->redirect($this->generateUrl('config'));
65 }
66
67 // handle changing user information
68 $userForm = $this->createForm(new UserInformationType(), $user, array('validation_groups' => array('Profile')));
69 $userForm->handleRequest($request);
70
71 if ($userForm->isValid()) {
72 $userManager->updateUser($user, true);
73
74 $this->get('session')->getFlashBag()->add(
75 'notice',
76 'Information updated'
77 );
78
79 return $this->redirect($this->generateUrl('config'));
80 }
81
82 // handle rss information
83 $rssForm = $this->createForm(new RssType(), $config);
84 $rssForm->handleRequest($request);
85
86 if ($rssForm->isValid()) {
87 $em->persist($config);
88 $em->flush();
89
90 $this->get('session')->getFlashBag()->add(
91 'notice',
92 'RSS information updated'
93 );
94
95 return $this->redirect($this->generateUrl('config'));
96 }
97
98 // handle adding new user
99 $newUser = $userManager->createUser();
100 // enable created user by default
101 $newUser->setEnabled(true);
102 $newUserForm = $this->createForm(new NewUserType(), $newUser, array('validation_groups' => array('Profile')));
103 $newUserForm->handleRequest($request);
104
105 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
106 $userManager->updateUser($newUser, true);
107
108 $config = new Config($newUser);
109 $config->setTheme($this->container->getParameter('theme'));
110 $config->setItemsPerPage($this->container->getParameter('items_on_page'));
111 $config->setRssLimit($this->container->getParameter('rss_limit'));
112 $config->setLanguage($this->container->getParameter('language'));
113
114 $em->persist($config);
115
116 $em->flush();
117
118 $this->get('session')->getFlashBag()->add(
119 'notice',
120 sprintf('User "%s" added', $newUser->getUsername())
121 );
122
123 return $this->redirect($this->generateUrl('config'));
124 }
125
126 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
127 'form' => array(
128 'config' => $configForm->createView(),
129 'rss' => $rssForm->createView(),
130 'pwd' => $pwdForm->createView(),
131 'user' => $userForm->createView(),
132 'new_user' => $newUserForm->createView(),
133 ),
134 'rss' => array(
135 'username' => $user->getUsername(),
136 'token' => $config->getRssToken(),
137 ),
138 ));
139 }
140
141 /**
142 * @param Request $request
143 *
144 * @Route("/generate-token", name="generate_token")
145 *
146 * @return JsonResponse
147 */
148 public function generateTokenAction(Request $request)
149 {
150 $config = $this->getConfig();
151 $config->setRssToken(Utils::generateToken());
152
153 $em = $this->getDoctrine()->getManager();
154 $em->persist($config);
155 $em->flush();
156
157 if ($request->isXmlHttpRequest()) {
158 return new JsonResponse(array('token' => $config->getRssToken()));
159 }
160
161 return $request->headers->get('referer') ? $this->redirect($request->headers->get('referer')) : $this->redirectToRoute('config');
162 }
163
164 /**
165 * Retrieve config for the current user.
166 * If no config were found, create a new one.
167 *
168 * @return Wallabag\CoreBundle\Entity\Config
169 */
170 private function getConfig()
171 {
172 $config = $this->getDoctrine()
173 ->getRepository('WallabagCoreBundle:Config')
174 ->findOneByUser($this->getUser());
175
176 if (!$config) {
177 $config = new Config($this->getUser());
178 }
179
180 return $config;
181 }
182 }