wallabag/src/Wallabag/CoreBundle/Controller/SiteCredentialController.php

175 lines
5.9 KiB
PHP
Raw Normal View History

2016-12-04 12:51:58 +00:00
<?php
namespace Wallabag\CoreBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Wallabag\UserBundle\Entity\User;
use Wallabag\CoreBundle\Entity\SiteCredential;
/**
* 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()
{
$credentials = $this->get('wallabag_core.site_credential_repository')->findByUser($this->getUser());
2016-12-04 12:51:58 +00:00
return $this->render('WallabagCoreBundle:SiteCredential:index.html.twig', [
2016-12-04 12:51:58 +00:00
'credentials' => $credentials,
]);
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"})
*
* @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()) {
$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);
$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()])
);
return $this->redirectToRoute('site_credentials_index');
2016-12-04 12:51:58 +00:00
}
return $this->render('WallabagCoreBundle:SiteCredential:new.html.twig', [
2016-12-04 12:51:58 +00:00
'credential' => $credential,
'form' => $form->createView(),
]);
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"})
*
* @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()) {
$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()])
);
return $this->redirectToRoute('site_credentials_index');
2016-12-04 12:51:58 +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(),
]);
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")
*
* @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()
->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)
{
if (null === $this->getUser() || $this->getUser()->getId() != $siteCredential->getUser()->getId()) {
throw $this->createAccessDeniedException('You can not access this site credential.');
}
}
2016-12-04 12:51:58 +00:00
}