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

182 lines
6.1 KiB
PHP
Raw Normal View History

2016-12-04 12:51:58 +00:00
<?php
namespace Wallabag\CoreBundle\Controller;
2017-07-01 07:52:38 +00:00
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
2016-12-04 12:51:58 +00:00
use Wallabag\CoreBundle\Entity\SiteCredential;
use Wallabag\CoreBundle\Helper\CryptoProxy;
use Wallabag\CoreBundle\Repository\SiteCredentialRepository;
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.
*
* @Route("/", name="site_credentials_index", methods={"GET"})
2016-12-04 12:51:58 +00:00
*/
public function indexAction()
{
$this->isSiteCredentialsEnabled();
$credentials = $this->get(SiteCredentialRepository::class)->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.
*
* @Route("/new", name="site_credentials_new", methods={"GET", "POST"})
*
* @return \Symfony\Component\HttpFoundation\Response
2016-12-04 12:51:58 +00:00
*/
public function newAction(Request $request)
{
$this->isSiteCredentialsEnabled();
2016-12-04 12:51:58 +00:00
$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(CryptoProxy::class)->crypt($credential->getUsername()));
$credential->setPassword($this->get(CryptoProxy::class)->crypt($credential->getPassword()));
2017-06-11 21:05:19 +00:00
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.
*
* @Route("/{id}/edit", name="site_credentials_edit", methods={"GET", "POST"})
*
* @return \Symfony\Component\HttpFoundation\Response
2016-12-04 12:51:58 +00:00
*/
public function editAction(Request $request, SiteCredential $siteCredential)
{
$this->isSiteCredentialsEnabled();
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(CryptoProxy::class)->crypt($siteCredential->getUsername()));
$siteCredential->setPassword($this->get(CryptoProxy::class)->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.
*
* @Route("/{id}", name="site_credentials_delete", methods={"DELETE"})
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
2016-12-04 12:51:58 +00:00
*/
public function deleteAction(Request $request, SiteCredential $siteCredential)
{
$this->isSiteCredentialsEnabled();
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
}
/**
* Throw a 404 if the feature is disabled.
*/
private function isSiteCredentialsEnabled()
{
if (!$this->get('craue_config')->get('restricted_access')) {
throw $this->createNotFoundException('Feature "restricted_access" is disabled, controllers too.');
}
}
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)
{
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
}