Feat: Extend and apply voter for checking of uploaded documents
This commit is contained in:
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DoctrineMigrations;
|
||||||
|
|
||||||
|
use Doctrine\DBAL\Schema\Schema;
|
||||||
|
use Doctrine\Migrations\AbstractMigration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Auto-generated Migration: Please modify to your needs!
|
||||||
|
*/
|
||||||
|
final class Version20231014151112 extends AbstractMigration
|
||||||
|
{
|
||||||
|
public function getDescription(): string
|
||||||
|
{
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function up(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this up() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->addSql('ALTER TABLE upload ADD comment LONGTEXT DEFAULT NULL');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function down(Schema $schema): void
|
||||||
|
{
|
||||||
|
// this down() migration is auto-generated, please modify it to your needs
|
||||||
|
$this->addSql('ALTER TABLE upload DROP comment');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,6 +31,7 @@ class CheckController extends AbstractController
|
|||||||
|
|
||||||
#[Route('/admin/document/check/{uuid}', name: 'app_admin_document_check')]
|
#[Route('/admin/document/check/{uuid}', name: 'app_admin_document_check')]
|
||||||
#[IsGranted('ROLE_ADMINISTRATIVE')]
|
#[IsGranted('ROLE_ADMINISTRATIVE')]
|
||||||
|
#[IsGranted('CHECK', subject: 'document')]
|
||||||
public function index(Upload $document, Request $request): JsonResponse
|
public function index(Upload $document, Request $request): JsonResponse
|
||||||
{
|
{
|
||||||
$response = new AjaxModalResponseDto();
|
$response = new AjaxModalResponseDto();
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ namespace App\Security\Voter;
|
|||||||
|
|
||||||
use App\Entity\Upload;
|
use App\Entity\Upload;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
|
||||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||||
|
|
||||||
@@ -13,9 +12,7 @@ class UploadVoter extends Voter
|
|||||||
public const VIEW = 'VIEW';
|
public const VIEW = 'VIEW';
|
||||||
public const DELETE = 'DELETE';
|
public const DELETE = 'DELETE';
|
||||||
public const DOWNLOAD = 'DOWNLOAD';
|
public const DOWNLOAD = 'DOWNLOAD';
|
||||||
|
public const CHECK = 'CHECK';
|
||||||
public function __construct(private readonly Security $security)
|
|
||||||
{}
|
|
||||||
|
|
||||||
protected function supports(string $attribute, mixed $subject): bool
|
protected function supports(string $attribute, mixed $subject): bool
|
||||||
{
|
{
|
||||||
@@ -23,21 +20,32 @@ class UploadVoter extends Voter
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return in_array($attribute, [static::VIEW, static::DELETE, static::DOWNLOAD]);
|
return in_array($attribute, [static::VIEW, static::DELETE, static::DOWNLOAD, static::CHECK]);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
|
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
|
||||||
{
|
{
|
||||||
if (true === $this->security->isGranted('ROLE_ADMINISTRATIVE')) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @var Upload $upload */
|
/** @var Upload $upload */
|
||||||
$upload = $subject;
|
$upload = $subject;
|
||||||
|
|
||||||
|
// Administrative users have full access
|
||||||
|
if (true === in_array('ROLE_ADMINISTRATIVE', $token->getRoleNames())) {
|
||||||
|
// Only new documents may be checked
|
||||||
|
if (static::CHECK === $attribute) {
|
||||||
|
return Upload::STATUS_NEW === $upload->getStatus();
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Teamers may only access their own documents
|
||||||
|
if (true === in_array($attribute, [static::VIEW, static::DELETE, static::DOWNLOAD])) {
|
||||||
/** @var User $user */
|
/** @var User $user */
|
||||||
$user = $this->security->getUser();
|
$user = $token->getUser();
|
||||||
|
|
||||||
return $upload->getOwner() === $user;
|
return $upload->getOwner() === $user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -60,7 +60,8 @@
|
|||||||
{{ upload.status|upload_status_badge }}
|
{{ upload.status|upload_status_badge }}
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
<div class="flex items-center space-x-2">
|
<div class="flex items-center space-x-2 justify-end">
|
||||||
|
{% if is_granted('CHECK', upload) %}
|
||||||
<button type="button"
|
<button type="button"
|
||||||
{{ stimulus_controller('modal-button', [], [], {'ajax-modal': '#ajax-modal'}) }}
|
{{ stimulus_controller('modal-button', [], [], {'ajax-modal': '#ajax-modal'}) }}
|
||||||
{{ stimulus_action('modal-button', 'ajax', null, {
|
{{ stimulus_action('modal-button', 'ajax', null, {
|
||||||
@@ -69,6 +70,7 @@
|
|||||||
}) }}>
|
}) }}>
|
||||||
{{ icon('edit') }}
|
{{ icon('edit') }}
|
||||||
</button>
|
</button>
|
||||||
|
{% endif %}
|
||||||
<a href="{{ path('app_common_download', { 'uuid': upload.uuid }) }}"
|
<a href="{{ path('app_common_download', { 'uuid': upload.uuid }) }}"
|
||||||
target="_blank"
|
target="_blank"
|
||||||
title="Download">
|
title="Download">
|
||||||
|
|||||||
Reference in New Issue
Block a user