-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
203 additions
and
27 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/Domain/User/Specification/CanUserPublishRegulation.php
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Domain\User\Specification; | ||
|
||
use App\Domain\Regulation\RegulationOrderRecord; | ||
use App\Domain\User\Enum\OrganizationRolesEnum; | ||
use App\Infrastructure\Security\SymfonyUser; | ||
|
||
class CanUserPublishRegulation | ||
{ | ||
public function isSatisfiedBy(RegulationOrderRecord $regulationOrderRecord, SymfonyUser $user): bool | ||
{ | ||
$organization = $regulationOrderRecord->getOrganization(); | ||
|
||
foreach ($user->getOrganizationUsers() as $userOrganization) { | ||
if ($userOrganization->uuid !== $organization->getUuid()) { | ||
continue; | ||
} | ||
|
||
return \in_array(OrganizationRolesEnum::ROLE_ORGA_ADMIN->value, $userOrganization->roles) | ||
|| \in_array(OrganizationRolesEnum::ROLE_ORGA_PUBLISHER->value, $userOrganization->roles); | ||
} | ||
|
||
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
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
51 changes: 51 additions & 0 deletions
51
src/Infrastructure/Security/Voter/RegulationOrderRecordVoter.php
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,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Security\Voter; | ||
|
||
use App\Domain\Regulation\RegulationOrderRecord; | ||
use App\Domain\User\Specification\CanUserPublishRegulation; | ||
use App\Infrastructure\Security\SymfonyUser; | ||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | ||
use Symfony\Component\Security\Core\Authorization\Voter\Voter; | ||
|
||
final class RegulationOrderRecordVoter extends Voter | ||
{ | ||
public function __construct( | ||
private readonly CanUserPublishRegulation $canUserPublishRegulation, | ||
) { | ||
} | ||
|
||
public const PUBLISH = 'publish'; | ||
|
||
protected function supports(string $attribute, mixed $subject): bool | ||
{ | ||
if (!\in_array($attribute, [self::PUBLISH])) { | ||
return false; | ||
} | ||
|
||
if (!$subject instanceof RegulationOrderRecord) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool | ||
{ | ||
$user = $token->getUser(); | ||
|
||
if (!$user instanceof SymfonyUser) { | ||
return false; | ||
} | ||
|
||
/** @var RegulationOrderRecord $regulationOrderRecord */ | ||
$regulationOrderRecord = $subject; | ||
|
||
return match ($attribute) { | ||
self::PUBLISH => $this->canUserPublishRegulation->isSatisfiedBy($regulationOrderRecord, $user), | ||
default => throw new \LogicException('This code should not be reached!') | ||
}; | ||
} | ||
} |
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
26 changes: 14 additions & 12 deletions
26
templates/regulation/fragments/_publication_button.html.twig
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,14 +1,16 @@ | ||
{% if canPublish %} | ||
{% set publishCsrfToken = csrf_token('publish-regulation') %} | ||
<form method="post" action="{{ path('app_regulation_publish', { uuid }) }}" data-controller="form-submit" data-action="modal-trigger:submit->form-submit#submit"> | ||
<d-modal-trigger modal="regulation-publish-modal" submitValue="publish"> | ||
<button class="fr-btn fr-btn--secondary" aria-controls="regulation-publish-modal"> | ||
{{ 'common.publish'|trans }} | ||
</button> | ||
{% if is_granted(constant('App\\Infrastructure\\Security\\Voter\\RegulationOrderRecordVoter::PUBLISH'), regulationOrderRecord) %} | ||
{% if canPublish %} | ||
{% set publishCsrfToken = csrf_token('publish-regulation') %} | ||
<form method="post" action="{{ path('app_regulation_publish', { uuid }) }}" data-controller="form-submit" data-action="modal-trigger:submit->form-submit#submit"> | ||
<d-modal-trigger modal="regulation-publish-modal" submitValue="publish"> | ||
<button class="fr-btn fr-btn--secondary" aria-controls="regulation-publish-modal"> | ||
{{ 'common.publish'|trans }} | ||
</button> | ||
|
||
<input type="hidden" name="token" value="{{ publishCsrfToken }}"/> | ||
</d-modal-trigger> | ||
</form> | ||
{% else %} | ||
<button class="fr-btn fr-btn--secondary" disabled>{{ 'common.publish'|trans }}</button> | ||
<input type="hidden" name="token" value="{{ publishCsrfToken }}"/> | ||
</d-modal-trigger> | ||
</form> | ||
{% else %} | ||
<button class="fr-btn fr-btn--secondary" disabled>{{ 'common.publish'|trans }}</button> | ||
{% endif %} | ||
{% 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
68 changes: 68 additions & 0 deletions
68
tests/Unit/Domain/User/Specification/CanUserPublishRegulationTest.php
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,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Unit\Domain\User\Specification; | ||
|
||
use App\Application\User\View\OrganizationView; | ||
use App\Domain\Regulation\RegulationOrderRecord; | ||
use App\Domain\User\Enum\OrganizationRolesEnum; | ||
use App\Domain\User\Organization; | ||
use App\Domain\User\Specification\CanUserPublishRegulation; | ||
use App\Infrastructure\Security\SymfonyUser; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class CanUserPublishRegulationTest extends TestCase | ||
{ | ||
public function testCanPublish(): void | ||
{ | ||
$organization = $this->createMock(Organization::class); | ||
$organization | ||
->expects(self::once()) | ||
->method('getUuid') | ||
->willReturn('c1790745-b915-4fb5-96e7-79b104092a55'); | ||
|
||
$regulationOrderRecord = $this->createMock(RegulationOrderRecord::class); | ||
$regulationOrderRecord | ||
->expects(self::once()) | ||
->method('getOrganization') | ||
->willReturn($organization); | ||
|
||
$symfonyUser = $this->createMock(SymfonyUser::class); | ||
$symfonyUser | ||
->expects(self::once()) | ||
->method('getOrganizationUsers') | ||
->willReturn([ | ||
new OrganizationView('c1790745-b915-4fb5-96e7-79b104092a55', 'Dialog', [OrganizationRolesEnum::ROLE_ORGA_PUBLISHER->value]), | ||
]); | ||
|
||
$pattern = new CanUserPublishRegulation(); | ||
$this->assertTrue($pattern->isSatisfiedBy($regulationOrderRecord, $symfonyUser)); | ||
} | ||
|
||
public function testCantPublish(): void | ||
{ | ||
$organization = $this->createMock(Organization::class); | ||
$organization | ||
->expects(self::once()) | ||
->method('getUuid') | ||
->willReturn('c1790745-b915-4fb5-96e7-79b104092a55'); | ||
|
||
$regulationOrderRecord = $this->createMock(RegulationOrderRecord::class); | ||
$regulationOrderRecord | ||
->expects(self::once()) | ||
->method('getOrganization') | ||
->willReturn($organization); | ||
|
||
$symfonyUser = $this->createMock(SymfonyUser::class); | ||
$symfonyUser | ||
->expects(self::once()) | ||
->method('getOrganizationUsers') | ||
->willReturn([ | ||
new OrganizationView('c1790745-b915-4fb5-96e7-79b104092a55', 'Dialog', [OrganizationRolesEnum::ROLE_ORGA_CONTRIBUTOR->value]), | ||
]); | ||
|
||
$pattern = new CanUserPublishRegulation(); | ||
$this->assertFalse($pattern->isSatisfiedBy($regulationOrderRecord, $symfonyUser)); | ||
} | ||
} |