3 namespace Wallabag\CoreBundle\Controller
;
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\UserBundle\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
;
17 class ConfigController
extends Controller
20 * @param Request $request
22 * @Route("/config", name="config")
24 public function indexAction(Request
$request)
26 $em = $this->getDoctrine()->getManager();
27 $config = $this->getConfig();
28 $userManager = $this->container
->get('fos_user.user_manager');
29 $user = $this->getUser();
31 // handle basic config detail (this form is defined as a service)
32 $configForm = $this->createForm('config', $config);
33 $configForm->handleRequest($request);
35 if ($configForm->isValid()) {
36 $em->persist($config);
39 // switch active theme
40 $activeTheme = $this->get('liip_theme.active_theme');
41 $activeTheme->setName($config->getTheme());
43 $this->get('session')->getFlashBag()->add(
45 'Config saved. Some parameters will be considered after disconnection.'
48 return $this->redirect($this->generateUrl('config'));
51 // handle changing password
52 $pwdForm = $this->createForm(new ChangePasswordType());
53 $pwdForm->handleRequest($request);
55 if ($pwdForm->isValid()) {
56 $user->setPlainPassword($pwdForm->get('new_password')->getData());
57 $userManager->updateUser($user, true);
59 $this->get('session')->getFlashBag()->add(
64 return $this->redirect($this->generateUrl('config'));
67 // handle changing user information
68 $userForm = $this->createForm(new UserInformationType(), $user, array('validation_groups' => array('Profile')));
69 $userForm->handleRequest($request);
71 if ($userForm->isValid()) {
72 $userManager->updateUser($user, true);
74 $this->get('session')->getFlashBag()->add(
79 return $this->redirect($this->generateUrl('config'));
82 // handle rss information
83 $rssForm = $this->createForm(new RssType(), $config);
84 $rssForm->handleRequest($request);
86 if ($rssForm->isValid()) {
87 $em->persist($config);
90 $this->get('session')->getFlashBag()->add(
92 'RSS information updated'
95 return $this->redirect($this->generateUrl('config'));
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);
105 if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
106 $userManager->updateUser($newUser, true);
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'));
114 $em->persist($config);
118 $this->get('session')->getFlashBag()->add(
120 sprintf('User "%s" added', $newUser->getUsername())
123 return $this->redirect($this->generateUrl('config'));
126 return $this->render('WallabagCoreBundle:Config:index.html.twig', array(
128 'config' => $configForm->createView(),
129 'rss' => $rssForm->createView(),
130 'pwd' => $pwdForm->createView(),
131 'user' => $userForm->createView(),
132 'new_user' => $newUserForm->createView(),
135 'username' => $user->getUsername(),
136 'token' => $config->getRssToken(),
142 * @param Request $request
144 * @Route("/generate-token", name="generate_token")
146 * @return JsonResponse
148 public function generateTokenAction(Request
$request)
150 $config = $this->getConfig();
151 $config->setRssToken(Utils
::generateToken());
153 $em = $this->getDoctrine()->getManager();
154 $em->persist($config);
157 if ($request->isXmlHttpRequest()) {
158 return new JsonResponse(array('token' => $config->getRssToken()));
161 return $request->headers
->get('referer') ? $this->redirect($request->headers
->get('referer')) : $this->redirectToRoute('config');
165 * Retrieve config for the current user.
166 * If no config were found, create a new one.
168 * @return Wallabag\CoreBundle\Entity\Config
170 private function getConfig()
172 $config = $this->getDoctrine()
173 ->getRepository('WallabagCoreBundle:Config')
174 ->findOneByUser($this->getUser());
177 $config = new Config($this->getUser());