Skip to content

Commit 9fc261e

Browse files
committed
Filter out personal material lists
1 parent 5eda290 commit 9fc261e

File tree

3 files changed

+56
-13
lines changed

3 files changed

+56
-13
lines changed

api/src/Entity/MaterialItem.php

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Doctrine\Common\Collections\Collection;
2525
use Doctrine\ORM\Mapping as ORM;
2626
use Symfony\Component\Serializer\Annotation\Groups;
27+
use Symfony\Component\Serializer\Attribute\SerializedName;
2728
use Symfony\Component\Validator\Constraints as Assert;
2829

2930
/**
@@ -71,14 +72,6 @@ class MaterialItem extends BaseEntity implements BelongsToCampInterface, CopyFro
7172
#[ORM\JoinColumn(nullable: false, onDelete: 'cascade')]
7273
public ?Camp $camp = null;
7374

74-
/**
75-
* The list to which this item belongs. Lists are used to keep track of who is
76-
* responsible to prepare and bring the item to the camp.
77-
*/
78-
#[Assert\NotNull]
79-
#[AssertBelongsToSameCamp]
80-
#[ApiProperty(example: '/material_lists/1a2b3c4d')]
81-
#[Groups(['read', 'write'])]
8275
#[ORM\ManyToOne(targetEntity: MaterialList::class, inversedBy: 'materialItems')]
8376
#[ORM\JoinColumn(nullable: true, onDelete: 'cascade')]
8477
public ?MaterialList $materialList = null;
@@ -149,6 +142,36 @@ public function __construct() {
149142
$this->periodMaterialItems = new ArrayCollection();
150143
}
151144

145+
/**
146+
* The list to which this item belongs. Lists are used to keep track of who is
147+
* responsible to prepare and bring the item to the camp.
148+
*/
149+
#[Assert\NotNull]
150+
#[AssertBelongsToSameCamp]
151+
#[ApiProperty(example: '/material_lists/1a2b3c4d', security: 'is_granted("CAMP_COLLABORATOR", object)')]
152+
#[Groups(['read', 'write'])]
153+
public function getMaterialList(): ?MaterialList {
154+
return $this->materialList;
155+
}
156+
157+
/**
158+
* The list to which this item belongs. Lists are used to keep track of who is
159+
* responsible to prepare and bring the item to the camp.
160+
*/
161+
#[Assert\NotNull]
162+
#[AssertBelongsToSameCamp]
163+
#[ApiProperty(example: '/material_lists/1a2b3c4d', security: '!is_granted("CAMP_COLLABORATOR", object)')]
164+
#[Groups(['read', 'write'])]
165+
#[SerializedName('materialList')]
166+
public function getPublicMaterialList(): ?MaterialList {
167+
// When accessing a shared or prototype camp, hide personal material lists
168+
if (null !== $this->materialList->campCollaboration) {
169+
return null;
170+
}
171+
172+
return $this->materialList;
173+
}
174+
152175
public function getCamp(): ?Camp {
153176
return $this->camp ?? $this->period->camp ?? $this->materialNode?->getCamp();
154177
}

api/src/Entity/MaterialList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
operations: [
3030
new Get(
3131
security: 'is_granted("CAMP_COLLABORATOR", object) or
32-
is_granted("CAMP_IS_SHARED", object) or
33-
is_granted("CAMP_IS_PROTOTYPE", object)'
32+
(object.campCollaboration === null and
33+
(is_granted("CAMP_IS_SHARED", object) or is_granted("CAMP_IS_PROTOTYPE", object)))'
3434
),
3535
new Patch(
3636
security: 'is_granted("CAMP_MEMBER", object) or is_granted("CAMP_MANAGER", object)'

api/src/Repository/MaterialListRepository.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace App\Repository;
44

55
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
6+
use App\Entity\Camp;
67
use App\Entity\MaterialList;
78
use App\Entity\User;
9+
use App\Entity\UserCamp;
810
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
911
use Doctrine\ORM\QueryBuilder;
1012
use Doctrine\Persistence\ManagerRegistry;
@@ -16,14 +18,32 @@
1618
* @method MaterialList[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
1719
*/
1820
class MaterialListRepository extends ServiceEntityRepository implements CanFilterByUserInterface {
19-
use FiltersByCampCollaboration;
20-
2121
public function __construct(ManagerRegistry $registry) {
2222
parent::__construct($registry, MaterialList::class);
2323
}
2424

2525
public function filterByUser(QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, User $user): void {
2626
$rootAlias = $queryBuilder->getRootAliases()[0];
27-
$this->filterByCampCollaborationOrPublic($queryBuilder, $user, "{$rootAlias}.camp");
27+
28+
$campsQry = $queryBuilder->getEntityManager()->createQueryBuilder();
29+
$campsQry->select('identity(uc.camp)');
30+
$campsQry->from(UserCamp::class, 'uc');
31+
$campsQry->where('uc.user = :current_user');
32+
33+
$publicCampsQry = $queryBuilder->getEntityManager()->createQueryBuilder();
34+
$publicCampsQry->select('c');
35+
$publicCampsQry->from(Camp::class, 'c');
36+
$publicCampsQry->where($queryBuilder->expr()->orX('c.isPrototype = true', 'c.isShared = true'));
37+
38+
$queryBuilder->andWhere(
39+
$queryBuilder->expr()->orX(
40+
$queryBuilder->expr()->andX(
41+
"{$rootAlias}.campCollaboration IS NULL",
42+
$queryBuilder->expr()->in("{$rootAlias}.camp", $publicCampsQry->getDQL())
43+
),
44+
$queryBuilder->expr()->in("{$rootAlias}.camp", $campsQry->getDQL())
45+
)
46+
);
47+
$queryBuilder->setParameter('current_user', $user);
2848
}
2949
}

0 commit comments

Comments
 (0)