4646use App \Entity \Parts \Part ;
4747use App \Entity \Parts \PartLot ;
4848use Symfony \Component \Security \Core \Authentication \Token \TokenInterface ;
49+ use Symfony \Component \Security \Core \Authorization \Voter \Vote ;
4950use Symfony \Component \Security \Core \Authorization \Voter \Voter ;
5051
5152/**
@@ -59,20 +60,24 @@ public function __construct(private readonly Security $security, private readonl
5960
6061 protected const ALLOWED_PERMS = ['read ' , 'edit ' , 'create ' , 'delete ' , 'show_history ' , 'revert_element ' , 'withdraw ' , 'add ' , 'move ' ];
6162
62- protected function voteOnAttribute (string $ attribute , $ subject , TokenInterface $ token ): bool
63+ protected function voteOnAttribute (string $ attribute , $ subject , TokenInterface $ token, ? Vote $ vote = null ): bool
6364 {
6465 $ user = $ this ->helper ->resolveUser ($ token );
6566
6667 if (in_array ($ attribute , ['withdraw ' , 'add ' , 'move ' ], true ))
6768 {
68- $ base_permission = $ this ->helper ->isGranted ($ token , 'parts_stock ' , $ attribute );
69+ $ base_permission = $ this ->helper ->isGranted ($ token , 'parts_stock ' , $ attribute, $ vote );
6970
7071 $ lot_permission = true ;
7172 //If the lot has an owner, we need to check if the user is the owner of the lot to be allowed to withdraw it.
7273 if ($ subject instanceof PartLot && $ subject ->getOwner ()) {
7374 $ lot_permission = $ subject ->getOwner () === $ user || $ subject ->getOwner ()->getID () === $ user ->getID ();
7475 }
7576
77+ if (!$ lot_permission ) {
78+ $ vote ->addReason ('User is not the owner of the lot. ' );
79+ }
80+
7681 return $ base_permission && $ lot_permission ;
7782 }
7883
@@ -86,7 +91,7 @@ protected function voteOnAttribute(string $attribute, $subject, TokenInterface $
8691
8792 //If we have no part associated use the generic part permission
8893 if (is_string ($ subject ) || !$ subject ->getPart () instanceof Part) {
89- return $ this ->helper ->isGranted ($ token , 'parts ' , $ operation );
94+ return $ this ->helper ->isGranted ($ token , 'parts ' , $ operation, $ vote );
9095 }
9196
9297 //Otherwise vote on the part
0 commit comments