2016-12-04 12:51:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\CoreBundle\Controller;
|
|
|
|
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2016-12-04 12:51:58 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\SiteCredential;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2016-12-04 12:51:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* SiteCredential controller.
|
2016-12-09 15:47:50 +00:00
|
|
|
*
|
|
|
|
* @Route("/site-credentials")
|
2016-12-04 12:51:58 +00:00
|
|
|
*/
|
|
|
|
class SiteCredentialController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Lists all User entities.
|
|
|
|
*
|
2016-12-09 15:47:50 +00:00
|
|
|
* @Route("/", name="site_credentials_index")
|
2016-12-04 12:51:58 +00:00
|
|
|
* @Method("GET")
|
|
|
|
*/
|
|
|
|
public function indexAction()
|
|
|
|
{
|
2017-05-03 08:23:49 +00:00
|
|
|
$credentials = $this->get('wallabag_core.site_credential_repository')->findByUser($this->getUser());
|
2016-12-04 12:51:58 +00:00
|
|
|
|
2017-06-14 13:02:34 +00:00
|
|
|
return $this->render('WallabagCoreBundle:SiteCredential:index.html.twig', [
|
2016-12-04 12:51:58 +00:00
|
|
|
'credentials' => $credentials,
|
2017-06-14 13:02:34 +00:00
|
|
|
]);
|
2016-12-04 12:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a new site credential entity.
|
|
|
|
*
|
2016-12-09 15:47:50 +00:00
|
|
|
* @Route("/new", name="site_credentials_new")
|
2016-12-04 12:51:58 +00:00
|
|
|
* @Method({"GET", "POST"})
|
2017-06-14 13:02:34 +00:00
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
2016-12-04 12:51:58 +00:00
|
|
|
*/
|
|
|
|
public function newAction(Request $request)
|
|
|
|
{
|
|
|
|
$credential = new SiteCredential($this->getUser());
|
|
|
|
|
|
|
|
$form = $this->createForm('Wallabag\CoreBundle\Form\Type\SiteCredentialType', $credential);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2017-06-14 13:02:34 +00:00
|
|
|
$credential->setUsername($this->get('wallabag_core.helper.crypto_proxy')->crypt($credential->getUsername()));
|
2017-06-11 21:05:19 +00:00
|
|
|
$credential->setPassword($this->get('wallabag_core.helper.crypto_proxy')->crypt($credential->getPassword()));
|
|
|
|
|
2016-12-04 12:51:58 +00:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->persist($credential);
|
2017-06-14 13:02:34 +00:00
|
|
|
$em->flush();
|
2016-12-04 12:51:58 +00:00
|
|
|
|
|
|
|
$this->get('session')->getFlashBag()->add(
|
|
|
|
'notice',
|
|
|
|
$this->get('translator')->trans('flashes.site_credential.notice.added', ['%host%' => $credential->getHost()])
|
|
|
|
);
|
|
|
|
|
2017-06-14 13:02:34 +00:00
|
|
|
return $this->redirectToRoute('site_credentials_index');
|
2016-12-04 12:51:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 13:02:34 +00:00
|
|
|
return $this->render('WallabagCoreBundle:SiteCredential:new.html.twig', [
|
2016-12-04 12:51:58 +00:00
|
|
|
'credential' => $credential,
|
|
|
|
'form' => $form->createView(),
|
2017-06-14 13:02:34 +00:00
|
|
|
]);
|
2016-12-04 12:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays a form to edit an existing site credential entity.
|
|
|
|
*
|
2016-12-09 15:47:50 +00:00
|
|
|
* @Route("/{id}/edit", name="site_credentials_edit")
|
2016-12-04 12:51:58 +00:00
|
|
|
* @Method({"GET", "POST"})
|
2017-06-14 13:02:34 +00:00
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param SiteCredential $siteCredential
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
2016-12-04 12:51:58 +00:00
|
|
|
*/
|
|
|
|
public function editAction(Request $request, SiteCredential $siteCredential)
|
|
|
|
{
|
2016-12-09 15:47:50 +00:00
|
|
|
$this->checkUserAction($siteCredential);
|
|
|
|
|
2016-12-04 12:51:58 +00:00
|
|
|
$deleteForm = $this->createDeleteForm($siteCredential);
|
|
|
|
$editForm = $this->createForm('Wallabag\CoreBundle\Form\Type\SiteCredentialType', $siteCredential);
|
|
|
|
$editForm->handleRequest($request);
|
|
|
|
|
|
|
|
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
2017-06-14 13:02:34 +00:00
|
|
|
$siteCredential->setUsername($this->get('wallabag_core.helper.crypto_proxy')->crypt($siteCredential->getUsername()));
|
|
|
|
$siteCredential->setPassword($this->get('wallabag_core.helper.crypto_proxy')->crypt($siteCredential->getPassword()));
|
|
|
|
|
2016-12-04 12:51:58 +00:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->persist($siteCredential);
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
$this->get('session')->getFlashBag()->add(
|
|
|
|
'notice',
|
|
|
|
$this->get('translator')->trans('flashes.site_credential.notice.updated', ['%host%' => $siteCredential->getHost()])
|
|
|
|
);
|
|
|
|
|
2017-06-14 13:02:34 +00:00
|
|
|
return $this->redirectToRoute('site_credentials_index');
|
2016-12-04 12:51:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 13:02:34 +00:00
|
|
|
return $this->render('WallabagCoreBundle:SiteCredential:edit.html.twig', [
|
2016-12-04 12:51:58 +00:00
|
|
|
'credential' => $siteCredential,
|
|
|
|
'edit_form' => $editForm->createView(),
|
|
|
|
'delete_form' => $deleteForm->createView(),
|
2017-06-14 13:02:34 +00:00
|
|
|
]);
|
2016-12-04 12:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a site credential entity.
|
|
|
|
*
|
2016-12-09 15:47:50 +00:00
|
|
|
* @Route("/{id}", name="site_credentials_delete")
|
2016-12-04 12:51:58 +00:00
|
|
|
* @Method("DELETE")
|
2017-06-14 13:02:34 +00:00
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param SiteCredential $siteCredential
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse
|
2016-12-04 12:51:58 +00:00
|
|
|
*/
|
|
|
|
public function deleteAction(Request $request, SiteCredential $siteCredential)
|
|
|
|
{
|
2016-12-09 15:47:50 +00:00
|
|
|
$this->checkUserAction($siteCredential);
|
|
|
|
|
2016-12-04 12:51:58 +00:00
|
|
|
$form = $this->createDeleteForm($siteCredential);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$this->get('session')->getFlashBag()->add(
|
|
|
|
'notice',
|
|
|
|
$this->get('translator')->trans('flashes.site_credential.notice.deleted', ['%host%' => $siteCredential->getHost()])
|
|
|
|
);
|
|
|
|
|
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$em->remove($siteCredential);
|
|
|
|
$em->flush();
|
|
|
|
}
|
|
|
|
|
2016-12-09 15:47:50 +00:00
|
|
|
return $this->redirectToRoute('site_credentials_index');
|
2016-12-04 12:51:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a form to delete a site credential entity.
|
|
|
|
*
|
|
|
|
* @param SiteCredential $siteCredential The site credential entity
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\Form\Form The form
|
|
|
|
*/
|
|
|
|
private function createDeleteForm(SiteCredential $siteCredential)
|
|
|
|
{
|
|
|
|
return $this->createFormBuilder()
|
2017-06-14 13:02:34 +00:00
|
|
|
->setAction($this->generateUrl('site_credentials_delete', ['id' => $siteCredential->getId()]))
|
2016-12-04 12:51:58 +00:00
|
|
|
->setMethod('DELETE')
|
|
|
|
->getForm()
|
|
|
|
;
|
|
|
|
}
|
2016-12-09 15:47:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the logged user can manage the given site credential.
|
|
|
|
*
|
|
|
|
* @param SiteCredential $siteCredential The site credential entity
|
|
|
|
*/
|
|
|
|
private function checkUserAction(SiteCredential $siteCredential)
|
|
|
|
{
|
2017-07-01 07:52:38 +00:00
|
|
|
if (null === $this->getUser() || $this->getUser()->getId() !== $siteCredential->getUser()->getId()) {
|
2016-12-09 15:47:50 +00:00
|
|
|
throw $this->createAccessDeniedException('You can not access this site credential.');
|
|
|
|
}
|
|
|
|
}
|
2016-12-04 12:51:58 +00:00
|
|
|
}
|