added new permission to separate time and money budget (#3352)

This commit is contained in:
Kevin Papst
2022-06-11 12:23:02 +02:00
committed by GitHub
parent 3f827b3104
commit b46fdcefad
47 changed files with 853 additions and 346 deletions

View File

@@ -41,6 +41,7 @@ abstract class AbstractVoterTest extends TestCase
$user = new User();
$user->setRoles($roles);
$user->setUsername($id);
$reflection = new \ReflectionClass($user);
$property = $reflection->getProperty('id');
@@ -58,12 +59,12 @@ abstract class AbstractVoterTest extends TestCase
protected function getRolePermissionManager(array $permissions = [], bool $overwrite = false)
{
if (!$overwrite) {
$activities = ['view_activity', 'edit_activity', 'budget_activity', 'delete_activity', 'create_activity'];
$activitiesTeam = ['view_activity', 'create_activity', 'edit_teamlead_activity', 'budget_teamlead_activity'];
$projects = ['view_project', 'create_project', 'edit_project', 'budget_project', 'delete_project', 'permissions_project', 'comments_project', 'details_project'];
$projectsTeam = ['view_teamlead_project', 'edit_teamlead_project', 'budget_teamlead_project', 'permissions_teamlead_project', 'comments_teamlead_project', 'details_teamlead_project'];
$customers = ['view_customer', 'create_customer', 'edit_customer', 'budget_customer', 'delete_customer', 'permissions_customer', 'comments_customer', 'details_customer'];
$customersTeam = ['view_teamlead_customer', 'edit_teamlead_customer', 'budget_teamlead_customer', 'comments_teamlead_customer', 'details_teamlead_customer'];
$activities = ['view_activity', 'edit_activity', 'budget_activity', 'time_activity', 'delete_activity', 'create_activity'];
$activitiesTeam = ['view_activity', 'create_activity', 'edit_teamlead_activity', 'budget_teamlead_activity', 'time_teamlead_activity'];
$projects = ['view_project', 'create_project', 'edit_project', 'budget_project', 'time_project', 'delete_project', 'permissions_project', 'comments_project', 'details_project'];
$projectsTeam = ['view_teamlead_project', 'edit_teamlead_project', 'budget_teamlead_project', 'time_teamlead_project', 'permissions_teamlead_project', 'comments_teamlead_project', 'details_teamlead_project'];
$customers = ['view_customer', 'create_customer', 'edit_customer', 'budget_customer', 'time_customer', 'delete_customer', 'permissions_customer', 'comments_customer', 'details_customer'];
$customersTeam = ['view_teamlead_customer', 'edit_teamlead_customer', 'budget_teamlead_customer', 'time_teamlead_customer', 'comments_teamlead_customer', 'details_teamlead_customer'];
$invoice = ['view_invoice', 'create_invoice'];
$invoiceTemplate = ['manage_invoice_template'];
$timesheet = ['view_own_timesheet', 'start_own_timesheet', 'stop_own_timesheet', 'create_own_timesheet', 'edit_own_timesheet', 'export_own_timesheet', 'delete_own_timesheet'];

View File

@@ -0,0 +1,86 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests\Voter;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\User;
use App\Voter\EntityMultiRoleVoter;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
/**
* @covers \App\Voter\EntityMultiRoleVoter
*/
class EntityMultiRoleVoterTest extends AbstractVoterTest
{
/**
* @dataProvider getTestData
*/
public function testVote(User $user, $subject, $attribute, $result)
{
$token = new UsernamePasswordToken($user, 'foo', 'bar', $user->getRoles());
$sut = $this->getVoter(EntityMultiRoleVoter::class);
$this->assertEquals($result, $sut->vote($token, $subject, [$attribute]), 'Failed on permission "' . $attribute . '" for User ' . $user->getUsername());
}
public function getTestData()
{
$user0 = $this->getUser(0, null);
$user1 = $this->getUser(1, User::ROLE_USER);
$user2 = $this->getUser(2, User::ROLE_TEAMLEAD);
$user3 = $this->getUser(3, User::ROLE_ADMIN);
$user4 = $this->getUser(4, User::ROLE_SUPER_ADMIN);
$result = VoterInterface::ACCESS_GRANTED;
$allPermissions = ['budget_money', 'budget_time', 'budget_any', 'details'];
$allSubjects = ['project', 'customer', new Project(), new Customer()];
foreach ($allPermissions as $permission) {
foreach ($allSubjects as $subject) {
yield [$user3, $subject, $permission, $result];
yield [$user4, $subject, $permission, $result];
}
}
$result = VoterInterface::ACCESS_GRANTED;
$allPermissions = ['budget_money', 'budget_time', 'budget_any'];
$allSubjects = ['activity', new Activity()];
foreach ($allPermissions as $permission) {
foreach ($allSubjects as $subject) {
yield [$user3, $subject, $permission, $result];
yield [$user4, $subject, $permission, $result];
}
}
$result = VoterInterface::ACCESS_DENIED;
yield [$user4, 'activity', 'details', $result]; // there is no details permission for activity
$result = VoterInterface::ACCESS_ABSTAIN;
yield [$user0, 'team', 'view', $result];
yield [$user0, 'team', 'edit', $result];
yield [$user0, 'team', 'delete', $result];
yield [$user1, 'team', 'view', $result];
yield [$user1, 'team', 'edit', $result];
yield [$user1, 'team', 'delete', $result];
yield [$user2, 'team', 'view', $result];
yield [$user2, 'team', 'edit', $result];
yield [$user2, 'team', 'delete', $result];
yield [$user3, 'team', 'view', $result];
yield [$user3, 'team', 'edit', $result];
yield [$user3, 'team', 'delete', $result];
yield [$user4, 'team', 'view', $result];
yield [$user4, 'team', 'edit', $result];
yield [$user4, 'team', 'delete', $result];
}
}