Allow deleting snips (including git repo)

This commit is contained in:
Tim 2023-04-06 20:46:30 +02:00
parent 004044022d
commit df708aa931
5 changed files with 69 additions and 1 deletions

View File

@ -3,6 +3,7 @@
namespace App\Controller; namespace App\Controller;
use App\Entity\Snip; use App\Entity\Snip;
use App\Form\ConfirmationType;
use App\Form\SnipType; use App\Form\SnipType;
use App\Repository\SnipRepository; use App\Repository\SnipRepository;
use App\Security\Voter\SnipVoter; use App\Security\Voter\SnipVoter;
@ -96,7 +97,7 @@ class SnipController extends AbstractController
$this->repository->save($snip); $this->repository->save($snip);
$this->snipServiceFactory->create($snip)->update($form->get('content')->getData()); $this->snipServiceFactory->create($snip)->update($form->get('content')->getData());
$this->addFlash('success', sprintf('Snip "%s" saved successfully', $snip)); $this->addFlash('success', sprintf('Snip "%s" saved', $snip));
return $this->redirectToRoute('snip_single', [ return $this->redirectToRoute('snip_single', [
'snip' => $snip->getId(), 'snip' => $snip->getId(),
@ -118,4 +119,24 @@ class SnipController extends AbstractController
return $this->edit($snip, $request); return $this->edit($snip, $request);
} }
#[Route('/delete/{snip}', name: '_delete')]
public function delete(Snip $snip, Request $request): Response
{
$this->denyAccessUnlessGranted(SnipVoter::EDIT, $snip);
$form = $this->createForm(ConfirmationType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->snipServiceFactory->create($snip)->deleteRepo();
$this->repository->remove($snip);
$this->addFlash('success', sprintf('Snip "%s" deleted', $snip));
return $this->redirectToRoute('snip_index');
}
return $this->render('form.html.twig', [
'message' => sprintf('Do you really want to delete "%s"?', $snip),
'form' => $form->createView(),
]);
}
} }

View File

@ -0,0 +1,33 @@
<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\IsTrue;
class ConfirmationType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('Check', CheckboxType::class, [
'constraints' => [
new IsTrue([
'message' => $options['message_wrong'],
]),
],
])
->add('confirm', SubmitType::class);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'message_wrong' => 'This should be checked.',
]);
}
}

View File

@ -51,4 +51,9 @@ class SnipService
{ {
return $this->repo; return $this->repo;
} }
public function deleteRepo(): void
{
system("rm -rf ".escapeshellarg($this->repo->getRepositoryPath()));
}
} }

6
templates/form.html.twig Normal file
View File

@ -0,0 +1,6 @@
{% extends 'base/base.html.twig' %}
{% block body %}
<h3>{{ message }}</h3>
{{ form(form) }}
{% endblock %}

View File

@ -13,6 +13,9 @@
<a class="btn btn-info" href="{{ path('history_index', {snip: snip.id}) }}"> <a class="btn btn-info" href="{{ path('history_index', {snip: snip.id}) }}">
<i class="fa fa-history" aria-hidden="true"></i> History <i class="fa fa-history" aria-hidden="true"></i> History
</a> </a>
<a href="{{ path('snip_delete', {snip: snip.id}) }}" class="btn btn-danger">
<i class="fa fa-trash"></i> Delete
</a>
{% endif %} {% endif %}
<a href="{{ path('snip_raw', {snip: snip.id}) }}" class="btn btn-danger"> <a href="{{ path('snip_raw', {snip: snip.id}) }}" class="btn btn-danger">
<i class="fa fa-eye"></i> Raw <i class="fa fa-eye"></i> Raw