Skip to content

[Security] improve VoteObject adding extraData for give more possibilities to AccessDecicsionStrategy #60085

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

eltharin
Copy link
Contributor

@eltharin eltharin commented Mar 30, 2025

Q A
Branch? 7.3
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 */

class ScoreData
{
    public $score = 0;
}

/** MyVoter.php */

    public function vote(TokenInterface $token, mixed $subject, array $attributes, ?Vote $vote = null) : int
    {
        $vote->result = 1;
        $vote->reasons[] = 'is Admin';
        $vote->extraData = new ScoreData();
        $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($vote->extraData instanceof ScoreData) {
                $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
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.

4 participants