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;
|
2018-10-04 12:07:20 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2016-12-04 12:51:58 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\SiteCredential;
|
2022-04-24 15:48:59 +00:00
|
|
|
use Wallabag\CoreBundle\Helper\CryptoProxy;
|
2022-04-24 15:58:57 +00:00
|
|
|
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.
|
|
|
|
*
|
2018-10-04 12:07:20 +00:00
|
|
|
* @Route("/", name="site_credentials_index", methods={"GET"})
|
2016-12-04 12:51:58 +00:00
|
|
|
*/
|
|
|
|
public function indexAction()
|
|
|
|
{
|
2017-11-22 08:59:11 +00:00
|
|
|
$this->isSiteCredentialsEnabled();
|
|
|
|
|
2022-04-24 15:58:57 +00:00
|
|
|
$credentials = $this->get(SiteCredentialRepository::class)->findByUser($this->getUser());
|
2016-12-04 12:51:58 +00:00
|
|
|
|
2022-04-28 22:47:22 +00:00
|
|
|
return $this->render('@WallabagCore/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.
|
|
|
|
*
|
2018-10-04 12:07:20 +00:00
|
|
|
* @Route("/new", name="site_credentials_new", methods={"GET", "POST"})
|
2017-06-14 13:02:34 +00:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
2016-12-04 12:51:58 +00:00
|
|
|
*/
|
|
|
|
public function newAction(Request $request)
|
|
|
|
{
|
2017-11-22 08:59:11 +00:00
|
|
|
$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()) {
|
2022-04-24 15:48:59 +00:00
|
|
|
$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);
|
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
|
|
|
}
|
|
|
|
|
2022-04-28 22:47:22 +00:00
|
|
|
return $this->render('@WallabagCore/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.
|
|
|
|
*
|
2018-10-04 12:07:20 +00:00
|
|
|
* @Route("/{id}/edit", name="site_credentials_edit", methods={"GET", "POST"})
|
2017-06-14 13:02:34 +00:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\Response
|
2016-12-04 12:51:58 +00:00
|
|
|
*/
|
|
|
|
public function editAction(Request $request, SiteCredential $siteCredential)
|
|
|
|
{
|
2017-11-22 08:59:11 +00:00
|
|
|
$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()) {
|
2022-04-24 15:48:59 +00:00
|
|
|
$siteCredential->setUsername($this->get(CryptoProxy::class)->crypt($siteCredential->getUsername()));
|
|
|
|
$siteCredential->setPassword($this->get(CryptoProxy::class)->crypt($siteCredential->getPassword()));
|
2017-06-14 13:02:34 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-04-28 22:47:22 +00:00
|
|
|
return $this->render('@WallabagCore/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.
|
|
|
|
*
|
2018-10-04 12:07:20 +00:00
|
|
|
* @Route("/{id}", name="site_credentials_delete", methods={"DELETE"})
|
2017-06-14 13:02:34 +00:00
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse
|
2016-12-04 12:51:58 +00:00
|
|
|
*/
|
|
|
|
public function deleteAction(Request $request, SiteCredential $siteCredential)
|
|
|
|
{
|
2017-11-22 08:59:11 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-11-22 08:59:11 +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()
|
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
|
|
|
}
|