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

Sales improvements #33

Merged
merged 2 commits into from
Aug 12, 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
6 changes: 5 additions & 1 deletion src/Entity/Sale.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use ApiPlatform\Metadata\Delete;
use ApiPlatform\Metadata\Get;
use ApiPlatform\Metadata\GetCollection;
use ApiPlatform\Metadata\Patch;
use ApiPlatform\Metadata\Post;
use App\Entity\Interface\TimestampEntityInterface;
use App\Entity\Trait\TimestampTrait;
Expand All @@ -29,6 +30,9 @@
new GetCollection(),
new Get(),
new Post(),
new Patch(
security: "is_granted('ROLE_ADMIN') || is_granted('SALE_UPDATE', object)",
),
new Delete(
security: "is_granted('ROLE_ADMIN') || is_granted('SALE_DELETE', object)",
),
Expand All @@ -37,7 +41,7 @@
'groups' => ['sale', 'sale-read', 'timestamp']
],
denormalizationContext: [
'groups' => ['sale', 'sale-write']
'groups' => ['sale', 'sale-write', 'timestamp-write-create']
],
order: ['createdAt' => 'DESC']
)]
Expand Down
4 changes: 2 additions & 2 deletions src/Entity/Trait/TimestampTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

trait TimestampTrait {
#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
#[Groups(['timestamp'])]
#[Groups(['timestamp', 'timestamp-write-create'])]
private ?\DateTimeImmutable $createdAt = null;

#[ORM\Column(type: Types::DATETIME_IMMUTABLE, nullable: true)]
#[Groups(['timestamp'])]
#[Groups(['timestamp', 'timestamp-write-update'])]
private ?\DateTimeImmutable $updatedAt = null;

public function getCreatedAt(): ?\DateTimeImmutable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\User\UserInterface;

class SaleDeleteVoter extends Voter {
class SaleVoter extends Voter {
public const UPDATE = 'SALE_UPDATE';
public const DELETE = 'SALE_DELETE';

public function __construct(
Expand All @@ -19,7 +20,7 @@ public function __construct(
}

protected function supports(string $attribute, mixed $subject): bool {
return $attribute == self::DELETE && $subject instanceof Sale;
return $subject instanceof Sale && in_array($attribute, [self::UPDATE, self::DELETE]);
}

protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool {
Expand Down