]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Controller/SiteCredentialController.php
548de74486287c3076311338065cabcd660ede50
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Controller / SiteCredentialController.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Controller;
4
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
6 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7 use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8 use Symfony\Component\HttpFoundation\Request;
9 use Wallabag\CoreBundle\Entity\SiteCredential;
10 use Wallabag\UserBundle\Entity\User;
11
12 /**
13 * SiteCredential controller.
14 *
15 * @Route("/site-credentials")
16 */
17 class SiteCredentialController extends Controller
18 {
19 /**
20 * Lists all User entities.
21 *
22 * @Route("/", name="site_credentials_index")
23 * @Method("GET")
24 */
25 public function indexAction()
26 {
27 $this->isSiteCredentialsEnabled();
28
29 $credentials = $this->get('wallabag_core.site_credential_repository')->findByUser($this->getUser());
30
31 return $this->render('WallabagCoreBundle:SiteCredential:index.html.twig', [
32 'credentials' => $credentials,
33 ]);
34 }
35
36 /**
37 * Creates a new site credential entity.
38 *
39 * @Route("/new", name="site_credentials_new")
40 * @Method({"GET", "POST"})
41 *
42 * @param Request $request
43 *
44 * @return \Symfony\Component\HttpFoundation\Response
45 */
46 public function newAction(Request $request)
47 {
48 $this->isSiteCredentialsEnabled();
49
50 $credential = new SiteCredential($this->getUser());
51
52 $form = $this->createForm('Wallabag\CoreBundle\Form\Type\SiteCredentialType', $credential);
53 $form->handleRequest($request);
54
55 if ($form->isSubmitted() && $form->isValid()) {
56 $credential->setUsername($this->get('wallabag_core.helper.crypto_proxy')->crypt($credential->getUsername()));
57 $credential->setPassword($this->get('wallabag_core.helper.crypto_proxy')->crypt($credential->getPassword()));
58
59 $em = $this->getDoctrine()->getManager();
60 $em->persist($credential);
61 $em->flush();
62
63 $this->get('session')->getFlashBag()->add(
64 'notice',
65 $this->get('translator')->trans('flashes.site_credential.notice.added', ['%host%' => $credential->getHost()])
66 );
67
68 return $this->redirectToRoute('site_credentials_index');
69 }
70
71 return $this->render('WallabagCoreBundle:SiteCredential:new.html.twig', [
72 'credential' => $credential,
73 'form' => $form->createView(),
74 ]);
75 }
76
77 /**
78 * Displays a form to edit an existing site credential entity.
79 *
80 * @Route("/{id}/edit", name="site_credentials_edit")
81 * @Method({"GET", "POST"})
82 *
83 * @param Request $request
84 * @param SiteCredential $siteCredential
85 *
86 * @return \Symfony\Component\HttpFoundation\Response
87 */
88 public function editAction(Request $request, SiteCredential $siteCredential)
89 {
90 $this->isSiteCredentialsEnabled();
91
92 $this->checkUserAction($siteCredential);
93
94 $deleteForm = $this->createDeleteForm($siteCredential);
95 $editForm = $this->createForm('Wallabag\CoreBundle\Form\Type\SiteCredentialType', $siteCredential);
96 $editForm->handleRequest($request);
97
98 if ($editForm->isSubmitted() && $editForm->isValid()) {
99 $siteCredential->setUsername($this->get('wallabag_core.helper.crypto_proxy')->crypt($siteCredential->getUsername()));
100 $siteCredential->setPassword($this->get('wallabag_core.helper.crypto_proxy')->crypt($siteCredential->getPassword()));
101
102 $em = $this->getDoctrine()->getManager();
103 $em->persist($siteCredential);
104 $em->flush();
105
106 $this->get('session')->getFlashBag()->add(
107 'notice',
108 $this->get('translator')->trans('flashes.site_credential.notice.updated', ['%host%' => $siteCredential->getHost()])
109 );
110
111 return $this->redirectToRoute('site_credentials_index');
112 }
113
114 return $this->render('WallabagCoreBundle:SiteCredential:edit.html.twig', [
115 'credential' => $siteCredential,
116 'edit_form' => $editForm->createView(),
117 'delete_form' => $deleteForm->createView(),
118 ]);
119 }
120
121 /**
122 * Deletes a site credential entity.
123 *
124 * @Route("/{id}", name="site_credentials_delete")
125 * @Method("DELETE")
126 *
127 * @param Request $request
128 * @param SiteCredential $siteCredential
129 *
130 * @return \Symfony\Component\HttpFoundation\RedirectResponse
131 */
132 public function deleteAction(Request $request, SiteCredential $siteCredential)
133 {
134 $this->isSiteCredentialsEnabled();
135
136 $this->checkUserAction($siteCredential);
137
138 $form = $this->createDeleteForm($siteCredential);
139 $form->handleRequest($request);
140
141 if ($form->isSubmitted() && $form->isValid()) {
142 $this->get('session')->getFlashBag()->add(
143 'notice',
144 $this->get('translator')->trans('flashes.site_credential.notice.deleted', ['%host%' => $siteCredential->getHost()])
145 );
146
147 $em = $this->getDoctrine()->getManager();
148 $em->remove($siteCredential);
149 $em->flush();
150 }
151
152 return $this->redirectToRoute('site_credentials_index');
153 }
154
155 /**
156 * Throw a 404 if the feature is disabled.
157 */
158 private function isSiteCredentialsEnabled()
159 {
160 if (!$this->get('craue_config')->get('restricted_access')) {
161 throw $this->createNotFoundException('Feature "restricted_access" is disabled, controllers too.');
162 }
163 }
164
165 /**
166 * Creates a form to delete a site credential entity.
167 *
168 * @param SiteCredential $siteCredential The site credential entity
169 *
170 * @return \Symfony\Component\Form\Form The form
171 */
172 private function createDeleteForm(SiteCredential $siteCredential)
173 {
174 return $this->createFormBuilder()
175 ->setAction($this->generateUrl('site_credentials_delete', ['id' => $siteCredential->getId()]))
176 ->setMethod('DELETE')
177 ->getForm()
178 ;
179 }
180
181 /**
182 * Check if the logged user can manage the given site credential.
183 *
184 * @param SiteCredential $siteCredential The site credential entity
185 */
186 private function checkUserAction(SiteCredential $siteCredential)
187 {
188 if (null === $this->getUser() || $this->getUser()->getId() !== $siteCredential->getUser()->getId()) {
189 throw $this->createAccessDeniedException('You can not access this site credential.');
190 }
191 }
192 }