Skip to content

Commit

Permalink
Merge pull request #33 from froozeify/sales-improvements
Browse files Browse the repository at this point in the history
Sales improvements
  • Loading branch information
froozeify authored Aug 12, 2024
2 parents c15d845 + 2170c64 commit 2be6687
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
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

0 comments on commit 2be6687

Please sign in to comment.