Skip to content

Conversation

@eltharin
Copy link
Contributor

@eltharin eltharin commented Mar 30, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
License MIT

In continuation of #58107 and #59771, add ExtraData in VoteObject and pass it to AccessDecicsionStrategy Object to allow the decision to be refined.

ExtraData can be an array or an object, and AccessDecicsionStrategy can get it to get the final decision.

In 7.2, voter can only respond abstain / allow or deny, but what if we want more choices, per example a ponderable vote ?

With this PR, it allow to put somme data in the new VoteObject as :

/** ScoreData.php */

/** MyVoter.php */

    public function vote(TokenInterface $token, mixed $subject, array $attributes, ?Vote $vote = null) : int
    {
        $vote->result = 1;
        $vote->reasons[] = 'is Admin';
        $vote->extraData['score'] = 10;

        return $vote->result;
    }

we need also a custom strategy to take this score into account :

/** MyStrategy.php */

public function decide(\Traversable $results, $accessDecision = null): bool
    {
        $score = 0;

        foreach ($results as $key => $result) {
            $vote = $accessDecision->votes[$key];  // <==

            if(array_key_exists('score', $vote->extraData)) {
                $score += $vote->extraData['score'];
            } else {
                $score += $vote->result;
            }
        }

        $accessDecision->result = $score;

        if ($score > 0) {
            return true;
        }

        if ($score< 0) {
            return false;
        }


        return $this->allowIfAllAbstainDecisions;
    }

AccessDecision contains Vote objects and we can read our score from it.

@eltharin eltharin requested a review from chalasr as a code owner March 30, 2025 09:25
@carsonbot carsonbot added this to the 7.3 milestone Mar 30, 2025
@carsonbot carsonbot changed the title improve VoteObject adding extraData for give more possibilities to AccessDecicsionStrategy [Security] improve VoteObject adding extraData for give more possibilities to AccessDecicsionStrategy Mar 30, 2025
@eltharin eltharin force-pushed the improveVoteObject branch from 290b65e to 8f4be1b Compare March 31, 2025 16:09
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a line about the new argument in the upgrade file

@eltharin eltharin force-pushed the improveVoteObject branch 3 times, most recently from 83eac24 to b1ff632 Compare September 23, 2025 13:56
@eltharin

This comment was marked as outdated.

@eltharin
Copy link
Contributor Author

thanks!!

return $accessDecision->isGranted = $this->strategy->decide(
$this->collectResults($token, $attributes, $object, $accessDecision)
$this->collectResults($token, $attributes, $object, $accessDecision),
$accessDecision
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$accessDecision
$accessDecision,

@fabpot
Copy link
Member

fabpot commented Sep 24, 2025

Thank you @eltharin.

@fabpot fabpot merged commit f1f6344 into symfony:7.4 Sep 24, 2025
10 of 12 checks passed
This was referenced Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants