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