improve permission checks for timesheets with activated lockdown (#2271)
This commit is contained in:
@@ -9,13 +9,17 @@
|
||||
|
||||
namespace App\Tests\Voter;
|
||||
|
||||
use App\Configuration\ConfigLoaderInterface;
|
||||
use App\Configuration\SystemConfiguration;
|
||||
use App\Entity\Activity;
|
||||
use App\Entity\Customer;
|
||||
use App\Entity\Project;
|
||||
use App\Entity\Timesheet;
|
||||
use App\Entity\User;
|
||||
use App\Timesheet\LockdownService;
|
||||
use App\Voter\TimesheetVoter;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
|
||||
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
|
||||
|
||||
/**
|
||||
@@ -23,10 +27,15 @@ use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
|
||||
*/
|
||||
class TimesheetVoterTest extends AbstractVoterTest
|
||||
{
|
||||
protected function getVoter(string $voterClass): Voter
|
||||
{
|
||||
return $this->getLockdownVoter();
|
||||
}
|
||||
|
||||
protected function assertVote(User $user, $subject, $attribute, $result)
|
||||
{
|
||||
$token = new UsernamePasswordToken($user, 'foo', 'bar', $user->getRoles());
|
||||
$sut = $this->getVoter(TimesheetVoter::class, $user);
|
||||
$sut = $this->getVoter(TimesheetVoter::class);
|
||||
|
||||
$this->assertEquals($result, $sut->vote($token, $subject, [$attribute]));
|
||||
}
|
||||
@@ -87,6 +96,36 @@ class TimesheetVoterTest extends AbstractVoterTest
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider getLockDownTestData
|
||||
*/
|
||||
public function testWithLockdown(string $permission, int $expected, string $beginModifier, string $lockdownBegin, string $lockdownEnd, ?string $lockdownGrace)
|
||||
{
|
||||
$user = $this->getUser(1, User::ROLE_USER);
|
||||
|
||||
$begin = new \DateTime('now');
|
||||
$begin->modify($beginModifier);
|
||||
|
||||
$timesheet = new Timesheet();
|
||||
$timesheet->setBegin($begin);
|
||||
$timesheet->setUser($user);
|
||||
|
||||
$token = new UsernamePasswordToken($user, 'foo', 'bar', $user->getRoles());
|
||||
$sut = $this->getLockdownVoter($lockdownBegin, $lockdownEnd, $lockdownGrace);
|
||||
|
||||
self::assertEquals($expected, $sut->vote($token, $timesheet, [$permission]));
|
||||
}
|
||||
|
||||
public function getLockDownTestData()
|
||||
{
|
||||
yield ['view', VoterInterface::ACCESS_GRANTED, '+1 days', 'first day of this month', 'last day of this month', '+10 days'];
|
||||
yield ['duplicate', VoterInterface::ACCESS_GRANTED, '+1 days', 'first day of this month', 'last day of this month', '+10 days'];
|
||||
yield ['delete', VoterInterface::ACCESS_GRANTED, '+1 days', 'first day of this month', 'last day of this month', '+10 days'];
|
||||
yield ['edit', VoterInterface::ACCESS_DENIED, '-50 days', 'first day of last month', 'last day of last month', '+1 days'];
|
||||
yield ['duplicate', VoterInterface::ACCESS_DENIED, '-50 days', 'first day of last month', 'last day of last month', '+1 days'];
|
||||
yield ['delete', VoterInterface::ACCESS_DENIED, '-50 days', 'first day of last month', 'last day of last month', '+1 days'];
|
||||
}
|
||||
|
||||
public function testSpecialCases()
|
||||
{
|
||||
$user1 = $this->getUser(1, User::ROLE_USER);
|
||||
@@ -154,10 +193,30 @@ class TimesheetVoterTest extends AbstractVoterTest
|
||||
*/
|
||||
protected function getUser($id, $role)
|
||||
{
|
||||
$user = $this->getMockBuilder(User::class)->getMock();
|
||||
$user = $this->createMock(User::class);
|
||||
$user->method('getId')->willReturn($id);
|
||||
$user->method('getRoles')->willReturn([$role]);
|
||||
$user->method('getTimezone')->willReturn(date_default_timezone_get());
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
protected function getLockdownVoter(?string $lockdownBegin = null, ?string $lockdownEnd = null, ?string $lockdownGrace = null): Voter
|
||||
{
|
||||
$loader = $this->createMock(ConfigLoaderInterface::class);
|
||||
$config = new SystemConfiguration($loader, [
|
||||
'timesheet' => [
|
||||
'rules' => [
|
||||
'lockdown_period_start' => $lockdownBegin,
|
||||
'lockdown_period_end' => $lockdownEnd,
|
||||
'lockdown_grace_period' => $lockdownGrace,
|
||||
],
|
||||
]
|
||||
]);
|
||||
|
||||
$voter = new TimesheetVoter($this->getRolePermissionManager(), new LockdownService($config));
|
||||
self::assertInstanceOf(Voter::class, $voter);
|
||||
|
||||
return $voter;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user