Skip to content

Commit

Permalink
fix: #51 & #52 flush entitymanager after remove operation & remove re…
Browse files Browse the repository at this point in the history
…dundant voter
  • Loading branch information
stlgaits committed Sep 18, 2022
1 parent c51f5aa commit 57afd09
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/DataPersister/CustomerDataPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function persist($data, array $context = [])
$data->setAccount($user->getAccount());
$this->entityManager->persist($data);
$this->entityManager->flush();
return $data;
}

/**
Expand All @@ -44,6 +45,7 @@ public function persist($data, array $context = [])
public function remove($data, array $context = [])
{
$this->entityManager->remove($data);
$this->entityManager->flush();
}
}

2 changes: 2 additions & 0 deletions src/DataPersister/UserDataPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function persist($data, $context = [])
$data->setAccount($user->getAccount());
$this->entityManager->persist($data);
$this->entityManager->flush();
return $data;
}

/**
Expand All @@ -55,5 +56,6 @@ public function persist($data, $context = [])
public function remove($data, array $context = [])
{
$this->entityManager->remove($data);
$this->entityManager->flush();
}
}
18 changes: 8 additions & 10 deletions src/Entity/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,16 @@
#[ORM\Entity(repositoryClass: CustomerRepository::class)]
#[ApiResource(
collectionOperations: [
"get" => [
"security" => "is_granted('VIEW_CUSTOMER', object)",
"security_message" => "So sorry, you can only access Customers linked to your own Account.",
],
"get",
"post"
],
itemOperations: [
"get" => [
'security' => 'is_granted("ROLE_ADMIN") or object.getAccount() == user.getAccount()',
'security_message' => 'Sorry, you can only access Customers linked to your own Account.',
],
"delete" => ["security" => "is_granted('ROLE_ADMIN') or object.getAccount() == user.getAccount()"],
],
"get",
"delete" => [
"security" => "is_granted('DELETE_CUSTOMER', object)",
'security_message' => 'Sorry, you can only delete Customers linked to your own Account.',
],
],
attributes: [
'pagination_items_per_page' => 10,
'formats' => ['json', 'jsonld'],
Expand All @@ -39,6 +36,7 @@ class Customer
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups(['customer:read'])]
private ?int $id;

#[ORM\Column(type: 'string', length: 255, unique: true)]
Expand Down
6 changes: 3 additions & 3 deletions src/Security/Voter/CustomerVoter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class CustomerVoter extends Voter
{
public const VIEW_CUSTOMER = 'VIEW_CUSTOMER';
public const DELETE_CUSTOMER = 'DELETE_CUSTOMER';

private Security $security;

Expand All @@ -22,7 +22,7 @@ public function __construct(Security $security)

protected function supports(string $attribute, $subject): bool
{
return $attribute == self::VIEW_CUSTOMER
return $attribute == self::DELETE_CUSTOMER
&& $subject instanceof Customer;
}

Expand All @@ -39,7 +39,7 @@ protected function voteOnAttribute(string $attribute, $subject, TokenInterface

/** @var Customer $subject */

if ($attribute == self::VIEW_CUSTOMER) {
if ($attribute == self::DELETE_CUSTOMER) {
if ($subject->getAccount() === $user->getAccount()) {
return true;
}
Expand Down

0 comments on commit 57afd09

Please sign in to comment.