]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/ConfigController.php
Add tests
[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\JsonResponse;
8 use Symfony\Component\HttpFoundation\RedirectResponse;
9 use Symfony\Component\HttpFoundation\Request;
10 use Wallabag\CoreBundle\Entity\Config;
11 use Wallabag\CoreBundle\Entity\TaggingRule;
12 use Wallabag\CoreBundle\Form\Type\ConfigType;
13 use Wallabag\CoreBundle\Form\Type\ChangePasswordType;
14 use Wallabag\CoreBundle\Form\Type\NewUserType;
15 use Wallabag\CoreBundle\Form\Type\RssType;
16 use Wallabag\CoreBundle\Form\Type\TaggingRuleType;
17 use Wallabag\CoreBundle\Form\Type\UserInformationType;
18 use Wallabag\CoreBundle\Tools\Utils;
19 use Wallabag\UserBundle\Entity\User;
20
21 class ConfigController extends Controller
22 {
23 /**
24 * @param Request $request
25 *
26 * @Route("/config", name="config")
27 */
28 public function indexAction(Request $request)
29 {
30 $em = $this->getDoctrine()->getManager();
31 $config = $this->getConfig();
32 $userManager = $this->container->get('fos_user.user_manager');
33 $user = $this->getUser();
34
35 // handle basic config detail (this form is defined as a service)
36 $configForm = $this->createForm(ConfigType::class, $config, array('action' => $this->generateUrl('config')));
37 $configForm->handleRequest($request);
38
39 if ($configForm->isValid()) {
40 $em->persist($config);
41 $em->flush();
42
43 // switch active theme
44 $activeTheme = $this->get('liip_theme.active_theme');
45 $activeTheme->setName($config->getTheme());
46
47 $this->get('session')->getFlashBag()->add(
48 'notice',
49 'Config saved. Some parameters will be considered after disconnection.'
50 );
51
52 return $this->redirect($this->generateUrl('config'));
53 }
54
55 // handle changing password
56 $pwdForm = $this->createForm(ChangePasswordType::class, null, array('action' => $this->generateUrl('config').'#set4'));
57 $pwdForm->handleRequest($request);
58
59 if ($pwdForm->isValid()) {
60 if ($this->get('craue_config')->get('demo_mode_enabled') && $this->get('craue_config')->get('demo_mode_username') === $user->getUsername()) {
61 $message = 'In demonstration mode, you can\'t change password for this user.';
62 } else {
63 $message = 'Password updated';
64
65 $user->setPlainPassword($pwdForm->get('new_password')->getData());
66 $userManager->updateUser($user, true);
67 }
68
69 $this->get('session')->getFlashBag()->add('notice', $message);
70
71 return $this->redirect($this->generateUrl('config').'#set4');
72 }
73
74 // handle changing user information
75 $userForm = $this->createForm(UserInformationType::class, $user, array(
76 'validation_groups' => array('Profile'),
77 'action' => $this->generateUrl('config').'#set3',
78 ));
79 $userForm->handleRequest($request);
80
81 if ($userForm->isValid()) {
82 $userManager->updateUser($user, true);
83
84 $this->get('session')->getFlashBag()->add(
85 'notice',
86 'Information updated'
87 );
88
89 return $this->redirect($this->generateUrl('config').'#set3');
90 }
91
92 // handle rss information
93 $rssForm = $this->createForm(RssType::class, $config, array('action' => $this->generateUrl('config').'#set2'));
94 $rssForm->handleRequest($request);
95
96 if ($rssForm->isValid()) {
97 $em->persist($config);
98 $em->flush();
99
100 $this->get('session')->getFlashBag()->add(
101 'notice',
102 'RSS information updated'
103 );
104
105 return $this->redirect($this->generateUrl('config').'#set2');
106 }
107
108 // handle tagging rule
109 $taggingRule = new TaggingRule();
110 $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, array('action' => $this->generateUrl('config').'#set5'));
111 $newTaggingRule->handleRequest($request);
112
113 if ($newTaggingRule->isValid()) {
114 $taggingRule->setConfig($config);
115 $em->persist($taggingRule);
116 $em->flush();
117
118 $this->get('session')->getFlashBag()->add(
119 'notice',
120 'Tagging rules updated'
121 );
122
123 return $this->redirect($this->generateUrl('config').'#set5');
124 }
125
126 // handle adding new user
127 $newUser = $userManager->createUser();
128 // enable created user by default
129 $newUser->setEnabled(true);
130 $newUserForm = $this->createForm(NewUserType::class, $newUser, array(
131 'validation_groups' => array('Profile'),
132 'action' => $this->generateUrl('config').'#set6',
133 ));
134 $newUserForm->handleRequest($request);
135
136 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
137 $userManager->updateUser($newUser, true);
138
139 $config = new Config($newUser);
140 $config->setTheme($this->getParameter('wallabag_core.theme'));
141 $config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page'));
142 $config->setRssLimit($this->getParameter('wallabag_core.rss_limit'));
143 $config->setLanguage($this->getParameter('wallabag_core.language'));
144
145 $em->persist($config);
146
147 $em->flush();
148
149 $this->get('session')->getFlashBag()->add(
150 'notice',
151 $this->get('translator')->trans('User "%username%" added', array('%username%' => $newUser->getUsername()))
152 );
153
154 return $this->redirect($this->generateUrl('config').'#set6');
155 }
156
157 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
158 'form' => array(
159 'config' => $configForm->createView(),
160 'rss' => $rssForm->createView(),
161 'pwd' => $pwdForm->createView(),
162 'user' => $userForm->createView(),
163 'new_user' => $newUserForm->createView(),
164 'new_tagging_rule' => $newTaggingRule->createView(),
165 ),
166 'rss' => array(
167 'username' => $user->getUsername(),
168 'token' => $config->getRssToken(),
169 ),
170 'twofactor_auth' => $this->getParameter('twofactor_auth'),
171 ));
172 }
173
174 /**
175 * @param Request $request
176 *
177 * @Route("/generate-token", name="generate_token")
178 *
179 * @return RedirectResponse|JsonResponse
180 */
181 public function generateTokenAction(Request $request)
182 {
183 $config = $this->getConfig();
184 $config->setRssToken(Utils::generateToken());
185
186 $em = $this->getDoctrine()->getManager();
187 $em->persist($config);
188 $em->flush();
189
190 if ($request->isXmlHttpRequest()) {
191 return new JsonResponse(array('token' => $config->getRssToken()));
192 }
193
194 $this->get('session')->getFlashBag()->add(
195 'notice',
196 'RSS token updated'
197 );
198
199 return $this->redirect($this->generateUrl('config').'#set2');
200 }
201
202 /**
203 * Deletes a tagging rule and redirect to the config homepage.
204 *
205 * @param TaggingRule $rule
206 *
207 * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule")
208 *
209 * @return RedirectResponse
210 */
211 public function deleteTaggingRuleAction(TaggingRule $rule)
212 {
213 if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) {
214 throw $this->createAccessDeniedException('You can not access this tagging ryle.');
215 }
216
217 $em = $this->getDoctrine()->getManager();
218 $em->remove($rule);
219 $em->flush();
220
221 $this->get('session')->getFlashBag()->add(
222 'notice',
223 'Tagging rule deleted'
224 );
225
226 return $this->redirect($this->generateUrl('config').'#set5');
227 }
228
229 /**
230 * Retrieve config for the current user.
231 * If no config were found, create a new one.
232 *
233 * @return Wallabag\CoreBundle\Entity\Config
234 */
235 private function getConfig()
236 {
237 $config = $this->getDoctrine()
238 ->getRepository('WallabagCoreBundle:Config')
239 ->findOneByUser($this->getUser());
240
241 if (!$config) {
242 $config = new Config($this->getUser());
243 }
244
245 return $config;
246 }
247 }