]>
Commit | Line | Data |
---|---|---|
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 | $user->setPlainPassword($pwdForm->get('new_password')->getData()); | |
61 | $userManager->updateUser($user, true); | |
62 | ||
63 | $this->get('session')->getFlashBag()->add( | |
64 | 'notice', | |
65 | 'Password updated' | |
66 | ); | |
67 | ||
68 | return $this->redirect($this->generateUrl('config').'#set4'); | |
69 | } | |
70 | ||
71 | // handle changing user information | |
72 | $userForm = $this->createForm(UserInformationType::class, $user, array( | |
73 | 'validation_groups' => array('Profile'), | |
74 | 'action' => $this->generateUrl('config').'#set3', | |
75 | )); | |
76 | $userForm->handleRequest($request); | |
77 | ||
78 | if ($userForm->isValid()) { | |
79 | $userManager->updateUser($user, true); | |
80 | ||
81 | $this->get('session')->getFlashBag()->add( | |
82 | 'notice', | |
83 | 'Information updated' | |
84 | ); | |
85 | ||
86 | return $this->redirect($this->generateUrl('config').'#set3'); | |
87 | } | |
88 | ||
89 | // handle rss information | |
90 | $rssForm = $this->createForm(RssType::class, $config, array('action' => $this->generateUrl('config').'#set2')); | |
91 | $rssForm->handleRequest($request); | |
92 | ||
93 | if ($rssForm->isValid()) { | |
94 | $em->persist($config); | |
95 | $em->flush(); | |
96 | ||
97 | $this->get('session')->getFlashBag()->add( | |
98 | 'notice', | |
99 | 'RSS information updated' | |
100 | ); | |
101 | ||
102 | return $this->redirect($this->generateUrl('config').'#set2'); | |
103 | } | |
104 | ||
105 | // handle tagging rule | |
106 | $taggingRule = new TaggingRule(); | |
107 | $newTaggingRule = $this->createForm(TaggingRuleType::class, $taggingRule, array('action' => $this->generateUrl('config').'#set5')); | |
108 | $newTaggingRule->handleRequest($request); | |
109 | ||
110 | if ($newTaggingRule->isValid()) { | |
111 | $taggingRule->setConfig($config); | |
112 | $em->persist($taggingRule); | |
113 | $em->flush(); | |
114 | ||
115 | $this->get('session')->getFlashBag()->add( | |
116 | 'notice', | |
117 | 'Tagging rules updated' | |
118 | ); | |
119 | ||
120 | return $this->redirect($this->generateUrl('config').'#set5'); | |
121 | } | |
122 | ||
123 | // handle adding new user | |
124 | $newUser = $userManager->createUser(); | |
125 | // enable created user by default | |
126 | $newUser->setEnabled(true); | |
127 | $newUserForm = $this->createForm(NewUserType::class, $newUser, array( | |
128 | 'validation_groups' => array('Profile'), | |
129 | 'action' => $this->generateUrl('config').'#set6', | |
130 | )); | |
131 | $newUserForm->handleRequest($request); | |
132 | ||
133 | if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) { | |
134 | $userManager->updateUser($newUser, true); | |
135 | ||
136 | $config = new Config($newUser); | |
137 | $config->setTheme($this->getParameter('wallabag_core.theme')); | |
138 | $config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page')); | |
139 | $config->setRssLimit($this->getParameter('wallabag_core.rss_limit')); | |
140 | $config->setLanguage($this->getParameter('wallabag_core.language')); | |
141 | ||
142 | $em->persist($config); | |
143 | ||
144 | $em->flush(); | |
145 | ||
146 | $this->get('session')->getFlashBag()->add( | |
147 | 'notice', | |
148 | $this->get('translator')->trans('User "%username%" added', array('%username%' => $newUser->getUsername())) | |
149 | ); | |
150 | ||
151 | return $this->redirect($this->generateUrl('config').'#set6'); | |
152 | } | |
153 | ||
154 | return $this->render('WallabagCoreBundle:Config:index.html.twig', array( | |
155 | 'form' => array( | |
156 | 'config' => $configForm->createView(), | |
157 | 'rss' => $rssForm->createView(), | |
158 | 'pwd' => $pwdForm->createView(), | |
159 | 'user' => $userForm->createView(), | |
160 | 'new_user' => $newUserForm->createView(), | |
161 | 'new_tagging_rule' => $newTaggingRule->createView(), | |
162 | ), | |
163 | 'rss' => array( | |
164 | 'username' => $user->getUsername(), | |
165 | 'token' => $config->getRssToken(), | |
166 | ), | |
167 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | |
168 | )); | |
169 | } | |
170 | ||
171 | /** | |
172 | * @param Request $request | |
173 | * | |
174 | * @Route("/generate-token", name="generate_token") | |
175 | * | |
176 | * @return RedirectResponse|JsonResponse | |
177 | */ | |
178 | public function generateTokenAction(Request $request) | |
179 | { | |
180 | $config = $this->getConfig(); | |
181 | $config->setRssToken(Utils::generateToken()); | |
182 | ||
183 | $em = $this->getDoctrine()->getManager(); | |
184 | $em->persist($config); | |
185 | $em->flush(); | |
186 | ||
187 | if ($request->isXmlHttpRequest()) { | |
188 | return new JsonResponse(array('token' => $config->getRssToken())); | |
189 | } | |
190 | ||
191 | $this->get('session')->getFlashBag()->add( | |
192 | 'notice', | |
193 | 'RSS token updated' | |
194 | ); | |
195 | ||
196 | return $this->redirect($this->generateUrl('config').'#set2'); | |
197 | } | |
198 | ||
199 | /** | |
200 | * Deletes a tagging rule and redirect to the config homepage. | |
201 | * | |
202 | * @param TaggingRule $rule | |
203 | * | |
204 | * @Route("/tagging-rule/delete/{id}", requirements={"id" = "\d+"}, name="delete_tagging_rule") | |
205 | * | |
206 | * @return RedirectResponse | |
207 | */ | |
208 | public function deleteTaggingRuleAction(TaggingRule $rule) | |
209 | { | |
210 | if ($this->getUser()->getId() != $rule->getConfig()->getUser()->getId()) { | |
211 | throw $this->createAccessDeniedException('You can not access this tagging ryle.'); | |
212 | } | |
213 | ||
214 | $em = $this->getDoctrine()->getManager(); | |
215 | $em->remove($rule); | |
216 | $em->flush(); | |
217 | ||
218 | $this->get('session')->getFlashBag()->add( | |
219 | 'notice', | |
220 | 'Tagging rule deleted' | |
221 | ); | |
222 | ||
223 | return $this->redirect($this->generateUrl('config').'#set5'); | |
224 | } | |
225 | ||
226 | /** | |
227 | * Retrieve config for the current user. | |
228 | * If no config were found, create a new one. | |
229 | * | |
230 | * @return Wallabag\CoreBundle\Entity\Config | |
231 | */ | |
232 | private function getConfig() | |
233 | { | |
234 | $config = $this->getDoctrine() | |
235 | ->getRepository('WallabagCoreBundle:Config') | |
236 | ->findOneByUser($this->getUser()); | |
237 | ||
238 | if (!$config) { | |
239 | $config = new Config($this->getUser()); | |
240 | } | |
241 | ||
242 | return $config; | |
243 | } | |
244 | } |