Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checklist Prototypes #5930

Merged
merged 7 commits into from
Sep 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions api/fixtures/checklists.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,7 @@ App\Entity\Checklist:
checklist1campPrototype:
camp: '@campPrototype'
name: 'J+S Ausbildungsziele'
checklistPrototype:
camp: null
isPrototype: true
name: 'J+S Ausbildungsziele'
30 changes: 30 additions & 0 deletions api/migrations/schema/Version20240912183023.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240912183023 extends AbstractMigration {
public function getDescription(): string {
return 'Checklist.IsPrototype';
}

public function up(Schema $schema): void {
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE checklist ADD isPrototype BOOLEAN NOT NULL');
$this->addSql('ALTER TABLE checklist ALTER campid DROP NOT NULL');
}

public function down(Schema $schema): void {
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE checklist DROP isPrototype');
$this->addSql('ALTER TABLE checklist ALTER campId SET NOT NULL');
}
}
2 changes: 1 addition & 1 deletion api/src/Entity/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
validationContext: ['groups' => ['Default', 'create']],
denormalizationContext: ['groups' => ['write', 'create']],
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.category === null'
),
],
denormalizationContext: ['groups' => ['write']],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ActivityProgressLabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
validationContext: ['groups' => ['Default', 'create']],
denormalizationContext: ['groups' => ['write', 'create']],
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
securityPostDenormalize: 'is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MANAGER", object) or object.camp === null'
),
],
denormalizationContext: ['groups' => ['write']],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ActivityResponsible.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
security: 'is_authenticated()'
),
new Post(
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.activity === null'
), ],
denormalizationContext: ['groups' => ['write']],
normalizationContext: ['groups' => ['read']]
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/CampCollaboration.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
denormalizationContext: ['groups' => ['write', 'create']],
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
openapi: new OpenApiOperation(description: 'Also sends an invitation email to the inviteEmail address, if specified.'),
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.camp === null'
),
],
denormalizationContext: ['groups' => ['write']],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
processor: CategoryCreateProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.camp === null'
),
new GetCollection(
uriTemplate: self::CAMP_SUBRESOURCE_URI_TEMPLATE,
Expand Down
33 changes: 27 additions & 6 deletions api/src/Entity/Checklist.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,37 @@
#[ApiResource(
operations: [
new Get(
security: 'is_granted("CAMP_COLLABORATOR", object) or is_granted("CAMP_IS_PROTOTYPE", object)'
security: 'is_granted("CHECKLIST_IS_PROTOTYPE", object) or
is_granted("CAMP_IS_PROTOTYPE", object) or
is_granted("CAMP_COLLABORATOR", object)
'
),
new Patch(
security: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
security: '(is_granted("CHECKLIST_IS_PROTOTYPE", object) and is_granted("ROLE_ADMIN")) or
(is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object))
'
),
new Delete(
security: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
security: '(is_granted("CHECKLIST_IS_PROTOTYPE", object) and is_granted("ROLE_ADMIN")) or
(is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object))
'
),
new GetCollection(
security: 'is_authenticated()'
),
new Post(
processor: ChecklistCreateProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: '(is_granted("CHECKLIST_IS_PROTOTYPE", object) and is_granted("ROLE_ADMIN")) or
(!is_granted("CHECKLIST_IS_PROTOTYPE", object) and (is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.camp === null))
'
),
new GetCollection(
uriTemplate: self::CAMP_SUBRESOURCE_URI_TEMPLATE,
uriVariables: [
'campId' => new Link(
fromClass: Camp::class,
toProperty: 'camp',
fromClass: Camp::class,
security: 'is_granted("CAMP_COLLABORATOR", camp) or is_granted("CAMP_IS_PROTOTYPE", camp)'
),
],
Expand All @@ -70,8 +79,10 @@ class Checklist extends BaseEntity implements BelongsToCampInterface, CopyFromPr
*/
#[ApiProperty(example: '/camps/1a2b3c4d')]
#[Groups(['read', 'create'])]
#[Assert\Expression('!(this.isPrototype == true and this.camp != null)', 'This value should be null.')]
#[Assert\Expression('!(this.isPrototype == false and this.camp == null)', 'This value should not be null.')]
#[ORM\ManyToOne(targetEntity: Camp::class, inversedBy: 'checklists')]
#[ORM\JoinColumn(nullable: false, onDelete: 'cascade')]
#[ORM\JoinColumn(onDelete: 'cascade')]
public ?Camp $camp = null;

/**
Expand Down Expand Up @@ -101,6 +112,16 @@ class Checklist extends BaseEntity implements BelongsToCampInterface, CopyFromPr
#[ORM\Column(type: 'text')]
public string $name;

/**
* Whether this checklist is a template.
*/
#[Assert\Type('bool')]
#[Assert\DisableAutoMapping]
#[ApiProperty(example: true, writable: true)]
#[Groups(['read', 'create'])]
#[ORM\Column(type: 'boolean')]
public bool $isPrototype = false;

public function __construct() {
parent::__construct();
$this->checklistItems = new ArrayCollection();
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ChecklistItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
),
new Post(
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.checklist === null'
),
new GetCollection(
uriTemplate: self::CHECKLIST_SUBRESOURCE_URI_TEMPLATE,
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ContentNode/ChecklistNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
new Post(
processor: ChecklistNodePersistProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.parent === null',
validationContext: ['groups' => ['Default', 'create']],
),
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ContentNode/ColumnLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
new Post(
processor: ContentNodePersistProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.parent === null',
validationContext: ['groups' => ['Default', 'create']]
),
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ContentNode/MaterialNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
new Post(
processor: ContentNodePersistProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.parent === null',
validationContext: ['groups' => ['Default', 'create']]
),
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ContentNode/MultiSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
new Post(
processor: MultiSelectCreateProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.parent === null',
validationContext: ['groups' => ['Default', 'create']]
),
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ContentNode/ResponsiveLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
new Post(
processor: ContentNodePersistProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.parent === null',
validationContext: ['groups' => ['Default', 'create']]
),
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ContentNode/SingleText.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
new Post(
processor: SingleTextPersistProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.parent === null',
validationContext: ['groups' => ['Default', 'create']]
),
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ContentNode/Storyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
new Post(
processor: StoryboardPersistProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.parent === null',
validationContext: ['groups' => ['Default', 'create']]
),
],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/DayResponsible.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
],
),
new Post(
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.day === null'
),
],
denormalizationContext: ['groups' => ['write']],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/MaterialItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
security: 'is_authenticated()'
),
new Post(
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.materialList === null'
),
],
denormalizationContext: ['groups' => ['write']],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/MaterialList.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
),
new Post(
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.camp === null'
),
],
denormalizationContext: ['groups' => ['write']],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
new Post(
processor: PeriodPersistProcessor::class,
denormalizationContext: ['groups' => ['write', 'create']],
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.camp === null'
),
],
denormalizationContext: ['groups' => ['write']],
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/ScheduleEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
new Post(
denormalizationContext: ['groups' => ['write', 'create']],
normalizationContext: self::ITEM_NORMALIZATION_CONTEXT,
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)',
securityPostDenormalize: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object) or object.activity === null',
validationContext: ['groups' => ScheduleEntryPostGroupSequence::class]
),
],
Expand Down
48 changes: 35 additions & 13 deletions api/src/HttpCache/PurgeHttpCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\IriConverterInterface;
use ApiPlatform\Metadata\Link;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\ResourceClassResolverInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\Metadata\Util\ClassInfoTrait;
use App\Entity\BaseEntity;
use App\Entity\HasId;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\OnFlushEventArgs;
Expand Down Expand Up @@ -192,22 +194,22 @@ private function gatherResourceTagsForClass(string $resourceClass, object $entit
* (e.g. for updating period on a ScheduleEntry and the IRI changes from /periods/1/schedule_entries to /periods/2/schedule_entries)
*/
private function invalidateCollection(GetCollection $operation, object $entity, ?object $oldEntity = null): void {
$iri = $this->iriConverter->getIriFromResource($entity, UrlGeneratorInterface::ABS_PATH, $operation);
$iri = '';
$oldIri = '';

if (!$iri) {
return;
if ($this->canGenerateIri($operation, $entity)) {
$iri = $this->iriConverter->getIriFromResource($entity, UrlGeneratorInterface::ABS_PATH, $operation);
}

if (!$oldEntity) {
$this->cacheManager->invalidateTags([$iri]);

return;
if ($oldEntity && $this->canGenerateIri($operation, $oldEntity)) {
$oldIri = $this->iriConverter->getIriFromResource($oldEntity, UrlGeneratorInterface::ABS_PATH, $operation);
}

$oldIri = $this->iriConverter->getIriFromResource($oldEntity, UrlGeneratorInterface::ABS_PATH, $operation);
if ($oldIri && $iri !== $oldIri) {
$this->cacheManager->invalidateTags([$iri]);
$this->cacheManager->invalidateTags([$oldIri]);
if ($iri !== $oldIri) {
if ($iri) {
$this->cacheManager->invalidateTags([$iri]);
}
if ($oldIri) {
$this->cacheManager->invalidateTags([$oldIri]);
}
}
}

Expand Down Expand Up @@ -292,4 +294,24 @@ private function addTagForItem(mixed $value, ?string $property = null): void {
} catch (InvalidArgumentException|RuntimeException) {
}
}

private function canGenerateIri(GetCollection $operation, object $entity) {
if (is_iterable($operation->getUriVariables())) {
// UriVariable is Link, toProperty is set, fromClass is a entity
foreach ($operation->getUriVariables() as $uriVariable) {
if (is_a($uriVariable, Link::class)
&& is_string($uriVariable->getToProperty())
&& is_a($uriVariable->getFromClass(), HasId::class, true)
) {
// value of toProperty is NULL; Read of its ID will throw Exception
// -> invalid
if (null == $entity->{$uriVariable->getToProperty()}) {
return false;
}
}
}
}
Comment on lines +299 to +313
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@usu:
Please also direct your focus here


return true;
}
}
2 changes: 1 addition & 1 deletion api/src/Repository/ChecklistItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInter
$checklistQry = $this->getEntityManager()->createQueryBuilder();
$checklistQry->select('c');
$checklistQry->from(Checklist::class, 'c');
$checklistQry->join(UserCamp::class, 'uc', Join::WITH, 'c.camp = uc.camp');
$checklistQry->join(UserCamp::class, 'uc', Join::WITH, 'c.camp = uc.camp OR c.isPrototype = true');
$checklistQry->where('uc.user = :current_user');

$rootAlias = $queryBuilder->getRootAliases()[0];
Expand Down
15 changes: 14 additions & 1 deletion api/src/Repository/ChecklistRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
use App\Entity\Checklist;
use App\Entity\User;
use App\Entity\UserCamp;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ManagerRegistry;
Expand All @@ -24,6 +25,18 @@ public function __construct(ManagerRegistry $registry) {

public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
$rootAlias = $queryBuilder->getRootAliases()[0];
$this->filterByCampCollaboration($queryBuilder, $user, "{$rootAlias}.camp");

$campsQry = $queryBuilder->getEntityManager()->createQueryBuilder();
$campsQry->select('identity(uc.camp)');
$campsQry->from(UserCamp::class, 'uc');
$campsQry->where('uc.user = :current_user');

$queryBuilder->andWhere(
$queryBuilder->expr()->orX(
"{$rootAlias}.isPrototype = true",
$queryBuilder->expr()->in("{$rootAlias}.camp", $campsQry->getDQL())
)
);
$queryBuilder->setParameter('current_user', $user);
}
}
Loading
Loading