added team permissions for activities (#1872)

This commit is contained in:
Kevin Papst
2020-08-08 18:50:04 +02:00
committed by GitHub
parent dc162bf385
commit 0914ebf737
84 changed files with 4300 additions and 3279 deletions

View File

@@ -11,11 +11,13 @@ declare(strict_types=1);
namespace App\API;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
use App\Entity\Team;
use App\Entity\User;
use App\Form\API\TeamApiEditForm;
use App\Repository\ActivityRepository;
use App\Repository\CustomerRepository;
use App\Repository\ProjectRepository;
use App\Repository\TeamRepository;
@@ -39,8 +41,8 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
*/
final class TeamController extends BaseApiController
{
public const GROUPS_ENTITY = ['Default', 'Entity', 'Team', 'Team_Entity'];
public const GROUPS_FORM = ['Default', 'Entity', 'Team', 'Team_Entity'];
public const GROUPS_ENTITY = ['Default', 'Entity', 'Team', 'Team_Entity', 'Not_Expanded'];
public const GROUPS_FORM = ['Default', 'Entity', 'Team', 'Team_Entity', 'Not_Expanded'];
public const GROUPS_COLLECTION = ['Default', 'Collection', 'Team'];
/**
@@ -266,7 +268,7 @@ final class TeamController extends BaseApiController
* @SWG\Post(
* @SWG\Response(
* response=200,
* description="Adds a new user to a team. The user must not be deactivated.",
* description="Adds a new user to a team.",
* @SWG\Schema(ref="#/definitions/TeamEntity")
* )
* )
@@ -388,7 +390,7 @@ final class TeamController extends BaseApiController
* @SWG\Post(
* @SWG\Response(
* response=200,
* description="Adds a new customer to a team. The customer must not be invisible.",
* description="Adds a new customer to a team.",
* @SWG\Schema(ref="#/definitions/TeamEntity")
* )
* )
@@ -427,10 +429,6 @@ final class TeamController extends BaseApiController
throw new NotFoundException('Customer not found');
}
if (!$customer->isVisible()) {
throw new BadRequestHttpException('Cannot grant access to an invisible customer');
}
if ($team->hasCustomer($customer)) {
throw new BadRequestHttpException('Team has already access to customer');
}
@@ -510,7 +508,7 @@ final class TeamController extends BaseApiController
* @SWG\Post(
* @SWG\Response(
* response=200,
* description="Adds a new project to a team. The project must not be invisible.",
* description="Adds a new project to a team.",
* @SWG\Schema(ref="#/definitions/TeamEntity")
* )
* )
@@ -549,10 +547,6 @@ final class TeamController extends BaseApiController
throw new NotFoundException('Project not found');
}
if (!$project->isVisible()) {
throw new BadRequestHttpException('Cannot grant access to an invisible project');
}
if ($team->hasProject($project)) {
throw new BadRequestHttpException('Team has already access to project');
}
@@ -625,4 +619,122 @@ final class TeamController extends BaseApiController
return $this->viewHandler->handle($view);
}
/**
* Grant the team access to an activity
*
* @SWG\Post(
* @SWG\Response(
* response=200,
* description="Adds a new activity to a team.",
* @SWG\Schema(ref="#/definitions/TeamEntity")
* )
* )
* @SWG\Parameter(
* name="id",
* in="path",
* type="integer",
* description="The team that is granted access",
* required=true,
* )
* @SWG\Parameter(
* name="activityId",
* in="path",
* type="integer",
* description="The activity to grant acecess to (Activity ID)",
* required=true,
* )
*
* @Security("is_granted('edit_team')")
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/
public function postActivityAction(int $id, int $activityId, ActivityRepository $repository): Response
{
$team = $this->repository->find($id);
if (null === $team) {
throw new NotFoundException('Team not found');
}
/** @var Activity|null $activity */
$activity = $repository->find($activityId);
if (null === $activity) {
throw new NotFoundException('Activity not found');
}
if ($team->hasActivity($activity)) {
throw new BadRequestHttpException('Team has already access to activity');
}
$team->addActivity($activity);
$this->repository->saveTeam($team);
$view = new View($team, Response::HTTP_OK);
$view->getContext()->setGroups(self::GROUPS_ENTITY);
return $this->viewHandler->handle($view);
}
/**
* Revokes access for an activity from a team
*
* @SWG\Delete(
* @SWG\Response(
* response=200,
* description="Removes a activity from the team.",
* @SWG\Schema(ref="#/definitions/TeamEntity")
* )
* )
* @SWG\Parameter(
* name="id",
* in="path",
* type="integer",
* description="The team whose permission will be revoked",
* required=true,
* )
* @SWG\Parameter(
* name="activityId",
* in="path",
* type="integer",
* description="The activity to remove (Activity ID)",
* required=true,
* )
*
* @Security("is_granted('edit_team')")
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*/
public function deleteActivityAction(int $id, int $activityId, ActivityRepository $repository): Response
{
$team = $this->repository->find($id);
if (null === $team) {
throw new NotFoundException('Team not found');
}
/** @var Activity|null $activity */
$activity = $repository->find($activityId);
if (null === $activity) {
throw new NotFoundException('Activity not found');
}
if (!$team->hasActivity($activity)) {
throw new BadRequestHttpException('Activity is not assigned to the team');
}
$team->removeActivity($activity);
$this->repository->saveTeam($team);
$view = new View($team, Response::HTTP_OK);
$view->getContext()->setGroups(self::GROUPS_ENTITY);
return $this->viewHandler->handle($view);
}
}