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