3 namespace Wallabag\CoreBundle\Controller
;
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
;
13 * SiteCredential controller.
15 * @Route("/site-credentials")
17 class SiteCredentialController
extends Controller
20 * Lists all User entities.
22 * @Route("/", name="site_credentials_index")
25 public function indexAction()
27 $this->isSiteCredentialsEnabled();
29 $credentials = $this->get('wallabag_core.site_credential_repository')->findByUser($this->getUser());
31 return $this->render('WallabagCoreBundle:SiteCredential:index.html.twig', [
32 'credentials' => $credentials,
37 * Creates a new site credential entity.
39 * @Route("/new", name="site_credentials_new")
40 * @Method({"GET", "POST"})
42 * @param Request $request
44 * @return \Symfony\Component\HttpFoundation\Response
46 public function newAction(Request
$request)
48 $this->isSiteCredentialsEnabled();
50 $credential = new SiteCredential($this->getUser());
52 $form = $this->createForm('Wallabag\CoreBundle\Form\Type\SiteCredentialType', $credential);
53 $form->handleRequest($request);
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()));
59 $em = $this->getDoctrine()->getManager();
60 $em->persist($credential);
63 $this->get('session')->getFlashBag()->add(
65 $this->get('translator')->trans('flashes.site_credential.notice.added', ['%host%' => $credential->getHost()])
68 return $this->redirectToRoute('site_credentials_index');
71 return $this->render('WallabagCoreBundle:SiteCredential:new.html.twig', [
72 'credential' => $credential,
73 'form' => $form->createView(),
78 * Displays a form to edit an existing site credential entity.
80 * @Route("/{id}/edit", name="site_credentials_edit")
81 * @Method({"GET", "POST"})
83 * @param Request $request
84 * @param SiteCredential $siteCredential
86 * @return \Symfony\Component\HttpFoundation\Response
88 public function editAction(Request
$request, SiteCredential
$siteCredential)
90 $this->isSiteCredentialsEnabled();
92 $this->checkUserAction($siteCredential);
94 $deleteForm = $this->createDeleteForm($siteCredential);
95 $editForm = $this->createForm('Wallabag\CoreBundle\Form\Type\SiteCredentialType', $siteCredential);
96 $editForm->handleRequest($request);
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()));
102 $em = $this->getDoctrine()->getManager();
103 $em->persist($siteCredential);
106 $this->get('session')->getFlashBag()->add(
108 $this->get('translator')->trans('flashes.site_credential.notice.updated', ['%host%' => $siteCredential->getHost()])
111 return $this->redirectToRoute('site_credentials_index');
114 return $this->render('WallabagCoreBundle:SiteCredential:edit.html.twig', [
115 'credential' => $siteCredential,
116 'edit_form' => $editForm->createView(),
117 'delete_form' => $deleteForm->createView(),
122 * Deletes a site credential entity.
124 * @Route("/{id}", name="site_credentials_delete")
127 * @param Request $request
128 * @param SiteCredential $siteCredential
130 * @return \Symfony\Component\HttpFoundation\RedirectResponse
132 public function deleteAction(Request
$request, SiteCredential
$siteCredential)
134 $this->isSiteCredentialsEnabled();
136 $this->checkUserAction($siteCredential);
138 $form = $this->createDeleteForm($siteCredential);
139 $form->handleRequest($request);
141 if ($form->isSubmitted() && $form->isValid()) {
142 $this->get('session')->getFlashBag()->add(
144 $this->get('translator')->trans('flashes.site_credential.notice.deleted', ['%host%' => $siteCredential->getHost()])
147 $em = $this->getDoctrine()->getManager();
148 $em->remove($siteCredential);
152 return $this->redirectToRoute('site_credentials_index');
156 * Throw a 404 if the feature is disabled.
158 private function isSiteCredentialsEnabled()
160 if (!$this->get('craue_config')->get('restricted_access')) {
161 throw $this->createNotFoundException('Feature "restricted_access" is disabled, controllers too.');
166 * Creates a form to delete a site credential entity.
168 * @param SiteCredential $siteCredential The site credential entity
170 * @return \Symfony\Component\Form\Form The form
172 private function createDeleteForm(SiteCredential
$siteCredential)
174 return $this->createFormBuilder()
175 ->setAction($this->generateUrl('site_credentials_delete', ['id' => $siteCredential->getId()]))
176 ->setMethod('DELETE')
182 * Check if the logged user can manage the given site credential.
184 * @param SiteCredential $siteCredential The site credential entity
186 private function checkUserAction(SiteCredential
$siteCredential)
188 if (null === $this->getUser() || $this->getUser()->getId() !== $siteCredential->getUser()->getId()) {
189 throw $this->createAccessDeniedException('You can not access this site credential.');