Refactor SavedExportVoter to improve export permission check

Revised the permission logic in `canUserGenerate` to enhance clarity and maintainability. Replaced nested condition with early return and updated the export permission check to use `isGrantedForElement`.
This commit is contained in:
Julien Fastré 2025-04-17 15:47:38 +02:00
parent a14ed78e25
commit fc8e3789e0
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 36 additions and 4 deletions

View File

@ -60,7 +60,12 @@ class SavedExportVoter extends Voter
private function canUserGenerate(User $user, SavedExport $savedExport): bool private function canUserGenerate(User $user, SavedExport $savedExport): bool
{ {
return ($savedExport->getUser() === $user || $savedExport->isSharedWithUser($user)) if (!($savedExport->getUser() === $user || $savedExport->isSharedWithUser($user))) {
&& $this->security->isGranted(ChillExportVoter::EXPORT, $this->exportManager->getExport($savedExport->getExportAlias())); return false;
}
$export = $this->exportManager->getExport($savedExport->getExportAlias());
return $this->exportManager->isGrantedForElement($export);
} }
} }

View File

@ -1,13 +1,26 @@
<?php <?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Tests\Entity\Workflow; namespace Chill\MainBundle\Tests\Entity\Workflow;
use Chill\MainBundle\Entity\SavedExport; use Chill\MainBundle\Entity\SavedExport;
use Chill\MainBundle\Entity\User; use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserGroup; use Chill\MainBundle\Entity\UserGroup;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/**
* @internal
*
* @coversNothing
*/
class SavedExportTest extends TestCase class SavedExportTest extends TestCase
{ {
public function testIsSharedWithUser(): void public function testIsSharedWithUser(): void

View File

@ -1,5 +1,14 @@
<?php <?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Tests\Security\Authorization; namespace Chill\MainBundle\Tests\Security\Authorization;
use Chill\MainBundle\Entity\SavedExport; use Chill\MainBundle\Entity\SavedExport;
@ -15,6 +24,11 @@ use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
use Symfony\Component\Security\Core\Security; use Symfony\Component\Security\Core\Security;
/**
* @internal
*
* @coversNothing
*/
class SavedExportVoterTest extends TestCase class SavedExportVoterTest extends TestCase
{ {
use ProphecyTrait; use ProphecyTrait;
@ -22,7 +36,7 @@ class SavedExportVoterTest extends TestCase
/** /**
* @dataProvider voteProvider * @dataProvider voteProvider
*/ */
public function testVote(string $attribute, SavedExport $savedExport, User $user, $expectedResult, bool|null $isGranted = null): void public function testVote(string $attribute, SavedExport $savedExport, User $user, $expectedResult, ?bool $isGranted = null): void
{ {
$security = $this->prophesize(Security::class); $security = $this->prophesize(Security::class);
if (null !== $isGranted) { if (null !== $isGranted) {