forked from biblioverse/biblioteca
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(kobo): KoboDevice admin is accessible to all users
- Loading branch information
Showing
10 changed files
with
185 additions
and
100 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace App\Controller\Kobo; | ||
|
||
use App\Entity\KoboDevice; | ||
use App\Entity\User; | ||
use App\Form\KoboType; | ||
use App\Repository\KoboDeviceRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
#[Route('/settings/kobo')] | ||
class KoboDeviceController extends AbstractController | ||
{ | ||
#[Route('/', name: 'app_kobo_setting_index', methods: ['GET'])] | ||
public function index(KoboDeviceRepository $koboDeviceRepository): Response | ||
{ | ||
if ($this->getUser() === null) { | ||
throw $this->createAccessDeniedException(); | ||
} | ||
|
||
return $this->render('kobo_admin/index.html.twig', [ | ||
'kobos' => $koboDeviceRepository->findAllByUser($this->getUser()), | ||
]); | ||
} | ||
|
||
#[Route('/new', name: 'app_kobo_setting_new', methods: ['GET', 'POST'])] | ||
public function new(Request $request, EntityManagerInterface $entityManager): Response | ||
{ | ||
$user = $this->getUser(); | ||
if (!$user instanceof User) { | ||
throw $this->createAccessDeniedException(); | ||
} | ||
$koboDevice = new KoboDevice(); | ||
$koboDevice->setUser($user); | ||
|
||
if (!$this->isGranted('CREATE', $koboDevice)) { | ||
throw $this->createAccessDeniedException(); | ||
} | ||
|
||
$form = $this->createForm(KoboType::class, $koboDevice); | ||
$form->handleRequest($request); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
$entityManager->persist($koboDevice); | ||
$entityManager->flush(); | ||
|
||
return $this->redirectToRoute('app_kobo_setting_index', [], Response::HTTP_SEE_OTHER); | ||
} | ||
|
||
return $this->render('kobo_admin/new.html.twig', [ | ||
'kobo' => $koboDevice, | ||
'form' => $form, | ||
]); | ||
} | ||
|
||
#[Route('/{id}', name: 'app_kobo_setting_show', methods: ['GET'])] | ||
public function show(KoboDevice $koboDevice): Response | ||
{ | ||
if (!$this->isGranted('VIEW', $koboDevice)) { | ||
throw $this->createAccessDeniedException(); | ||
} | ||
|
||
return $this->render('kobo_admin/show.html.twig', [ | ||
'kobo' => $koboDevice, | ||
]); | ||
} | ||
|
||
#[Route('/{id}/edit', name: 'app_kobo_setting_edit', methods: ['GET', 'POST'])] | ||
public function edit(Request $request, KoboDevice $koboDevice, EntityManagerInterface $entityManager): Response | ||
{ | ||
if (!$this->isGranted('EDIT', $koboDevice)) { | ||
throw $this->createAccessDeniedException('You don\'t have permission to edit this koboDevice'); | ||
} | ||
|
||
$form = $this->createForm(KoboType::class, $koboDevice); | ||
$form->handleRequest($request); | ||
|
||
if ($form->isSubmitted() && $form->isValid()) { | ||
$entityManager->flush(); | ||
|
||
return $this->redirectToRoute('app_kobo_setting_index', [], Response::HTTP_SEE_OTHER); | ||
} | ||
|
||
return $this->render('kobo_admin/edit.html.twig', [ | ||
'kobo' => $koboDevice, | ||
'form' => $form, | ||
]); | ||
} | ||
|
||
#[Route('/{id}', name: 'app_kobo_setting_delete', methods: ['POST'])] | ||
public function delete(Request $request, KoboDevice $koboDevice, EntityManagerInterface $entityManager): Response | ||
{ | ||
if (!$this->isGranted('DELETE', $koboDevice)) { | ||
throw $this->createAccessDeniedException(); | ||
} | ||
|
||
if ($this->isCsrfTokenValid('delete'.$koboDevice->getId(), (string) $request->request->get('_token'))) { | ||
$entityManager->remove($koboDevice); | ||
$entityManager->flush(); | ||
} | ||
|
||
return $this->redirectToRoute('app_kobo_setting_index', [], Response::HTTP_SEE_OTHER); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Security\Voter; | ||
|
||
use App\Entity\KoboDevice; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
class KoboDeviceVoter extends Voter | ||
{ | ||
public const EDIT = 'EDIT'; | ||
public const VIEW = 'VIEW'; | ||
public const CREATE = 'CREATE'; | ||
public const DELETE = 'DELETE'; | ||
|
||
protected function supports(string $attribute, mixed $subject): bool | ||
{ | ||
return in_array($attribute, [self::EDIT, self::VIEW, self::CREATE, self::DELETE], true) | ||
&& $subject instanceof KoboDevice; | ||
} | ||
|
||
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool | ||
{ | ||
$user = $token->getUser(); | ||
// if the user is anonymous, do not grant access | ||
if (!$user instanceof UserInterface) { | ||
return false; | ||
} | ||
|
||
if (!$subject instanceof KoboDevice) { | ||
return false; | ||
} | ||
|
||
if ($subject->getUser() === $user) { | ||
return true; | ||
} | ||
|
||
if (in_array('ROLE_ADMIN', $user->getRoles(), true)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<form method="post" action="{{ path('app_kobo_admin_delete', {'id': kobo.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');"> | ||
{% if is_granted("DELETE", kobo) %} | ||
<form method="post" action="{{ path('app_kobo_setting_delete', {'id': kobo.id}) }}" onsubmit="return confirm('Are you sure you want to delete this item?');"> | ||
<input type="hidden" name="_token" value="{{ csrf_token('delete' ~ kobo.id) }}"> | ||
<button class="btn btn-danger">Delete</button> | ||
</form> | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters