added team permissions (#996)

This commit is contained in:
Kevin Papst
2019-08-16 01:22:33 +02:00
committed by GitHub
parent 9611646bc6
commit 561ec3b1e9
124 changed files with 4122 additions and 362 deletions

View File

@@ -10,6 +10,7 @@
namespace App\Voter;
use App\Entity\Activity;
use App\Entity\Team;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -65,6 +66,44 @@ class ActivityVoter extends AbstractVoter
return false;
}
return $this->hasRolePermission($user, $attribute . '_activity');
if ($this->hasRolePermission($user, $attribute . '_activity')) {
return true;
}
$project = $subject->getProject();
if (null === $project) {
return false;
}
$hasTeamleadPermission = $this->hasRolePermission($user, $attribute . '_teamlead_activity');
$hasTeamPermission = $this->hasRolePermission($user, $attribute . '_team_activity');
if (!$hasTeamleadPermission && !$hasTeamPermission) {
return false;
}
/** @var Team $team */
foreach ($project->getTeams() as $team) {
if ($hasTeamleadPermission && $user->isTeamleadOf($team)) {
return true;
}
if ($hasTeamPermission && $user->isInTeam($team)) {
return true;
}
}
/** @var Team $team */
foreach ($project->getCustomer()->getTeams() as $team) {
if ($hasTeamleadPermission && $user->isTeamleadOf($team)) {
return true;
}
if ($hasTeamPermission && $user->isInTeam($team)) {
return true;
}
}
return false;
}
}

View File

@@ -10,6 +10,7 @@
namespace App\Voter;
use App\Entity\Customer;
use App\Entity\Team;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -22,6 +23,7 @@ class CustomerVoter extends AbstractVoter
public const EDIT = 'edit';
public const BUDGET = 'budget';
public const DELETE = 'delete';
public const PERMISSIONS = 'permissions';
/**
* support rules based on the given $subject (here: Customer)
@@ -31,6 +33,7 @@ class CustomerVoter extends AbstractVoter
self::EDIT,
self::BUDGET,
self::DELETE,
self::PERMISSIONS,
];
/**
@@ -65,6 +68,28 @@ class CustomerVoter extends AbstractVoter
return false;
}
return $this->hasRolePermission($user, $attribute . '_customer');
if ($this->hasRolePermission($user, $attribute . '_customer')) {
return true;
}
$hasTeamleadPermission = $this->hasRolePermission($user, $attribute . '_teamlead_customer');
$hasTeamPermission = $this->hasRolePermission($user, $attribute . '_team_customer');
if (!$hasTeamleadPermission && !$hasTeamPermission) {
return false;
}
/** @var Team $team */
foreach ($subject->getTeams() as $team) {
if ($hasTeamleadPermission && $user->isTeamleadOf($team)) {
return true;
}
if ($hasTeamPermission && $user->isInTeam($team)) {
return true;
}
}
return false;
}
}

View File

@@ -10,6 +10,7 @@
namespace App\Voter;
use App\Entity\Project;
use App\Entity\Team;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
@@ -22,6 +23,7 @@ class ProjectVoter extends AbstractVoter
public const EDIT = 'edit';
public const BUDGET = 'budget';
public const DELETE = 'delete';
public const PERMISSIONS = 'permissions';
/**
* support rules based on the given $subject (here: Project)
@@ -31,6 +33,7 @@ class ProjectVoter extends AbstractVoter
self::EDIT,
self::BUDGET,
self::DELETE,
self::PERMISSIONS,
];
/**
@@ -65,6 +68,39 @@ class ProjectVoter extends AbstractVoter
return false;
}
return $this->hasRolePermission($user, $attribute . '_project');
if ($this->hasRolePermission($user, $attribute . '_project')) {
return true;
}
$hasTeamleadPermission = $this->hasRolePermission($user, $attribute . '_teamlead_project');
$hasTeamPermission = $this->hasRolePermission($user, $attribute . '_team_project');
if (!$hasTeamleadPermission && !$hasTeamPermission) {
return false;
}
/** @var Team $team */
foreach ($subject->getTeams() as $team) {
if ($hasTeamleadPermission && $user->isTeamleadOf($team)) {
return true;
}
if ($hasTeamPermission && $user->isInTeam($team)) {
return true;
}
}
/** @var Team $team */
foreach ($subject->getCustomer()->getTeams() as $team) {
if ($hasTeamleadPermission && $user->isTeamleadOf($team)) {
return true;
}
if ($hasTeamPermission && $user->isInTeam($team)) {
return true;
}
}
return false;
}
}

65
src/Voter/TeamVoter.php Normal file
View File

@@ -0,0 +1,65 @@
<?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\Voter;
use App\Entity\Team;
use App\Entity\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class TeamVoter extends AbstractVoter
{
public const VIEW = 'view';
public const EDIT = 'edit';
public const DELETE = 'delete';
/**
* support rules based on the given $subject (here: Team)
*/
public const ALLOWED_ATTRIBUTES = [
self::VIEW,
self::EDIT,
self::DELETE,
];
/**
* @param string $attribute
* @param Team $subject
* @return bool
*/
protected function supports($attribute, $subject)
{
if (!($subject instanceof Team)) {
return false;
}
if (!in_array($attribute, self::ALLOWED_ATTRIBUTES)) {
return false;
}
return true;
}
/**
* @param string $attribute
* @param Team $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
return $this->hasRolePermission($user, $attribute . '_team');
}
}

View File

@@ -22,6 +22,7 @@ class UserVoter extends AbstractVoter
public const DELETE = 'delete';
public const PASSWORD = 'password';
public const ROLES = 'roles';
public const TEAMS = 'teams';
public const PREFERENCES = 'preferences';
public const API_TOKEN = 'api-token';
public const HOURLY_RATE = 'hourly-rate';
@@ -30,6 +31,7 @@ class UserVoter extends AbstractVoter
self::VIEW,
self::EDIT,
self::ROLES,
self::TEAMS,
self::PASSWORD,
self::DELETE,
self::PREFERENCES,
@@ -93,6 +95,7 @@ class UserVoter extends AbstractVoter
case self::PASSWORD:
case self::API_TOKEN:
case self::ROLES:
case self::TEAMS:
case self::HOURLY_RATE:
$permission .= $attribute;
break;