Skip to content

[Security] voters now are going to be obligated to return int values #39248

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Symfony/Component/Security/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
CHANGELOG
=========
5.3.0
-----

* Deprecation error in `AccessDecisionManager`. Cast when voter return value different than integer.

5.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ private function decideAffirmative(TokenInterface $token, array $attributes, $ob
$deny = 0;
foreach ($this->voters as $voter) {
$result = $voter->vote($token, $object, $attributes);
if (false === \is_int($result)) {
trigger_deprecation('symfony/security-core', '5.3', 'Returning a value of type "%s" in "%s::vote()" is deprecated, return an int instead.', get_debug_type($result), get_debug_type($voter));
}

if (VoterInterface::ACCESS_GRANTED === $result) {
return true;
Expand Down Expand Up @@ -119,6 +122,9 @@ private function decideConsensus(TokenInterface $token, array $attributes, $obje
$deny = 0;
foreach ($this->voters as $voter) {
$result = $voter->vote($token, $object, $attributes);
if (false === \is_int($result)) {
Copy link
Member

Choose a reason for hiding this comment

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

what's about adding a elseif ($result !== VoterInterface::ACCESS_ABSTAIN) below?
It will assert the return value is a valid constant!

The deprecation message would be `return either VoterInterface::ACCESS_GRANTED, VoterInterface::ACCESS_DENIED or VoterInterface::ACCESS_ABSTAIN).

In Symfony 6.0 we will be able to type hint and replace the deprecation by an exception.

trigger_deprecation('symfony/security-core', '5.3', 'Returning a value of type "%s" in "%s::vote()" is deprecated, return an int instead.', get_debug_type($result), get_debug_type($voter));
}

if (VoterInterface::ACCESS_GRANTED === $result) {
++$grant;
Expand Down Expand Up @@ -154,6 +160,9 @@ private function decideUnanimous(TokenInterface $token, array $attributes, $obje
foreach ($this->voters as $voter) {
foreach ($attributes as $attribute) {
$result = $voter->vote($token, $object, [$attribute]);
if (false === \is_int($result)) {
trigger_deprecation('symfony/security-core', '5.3', 'Returning a value of type "%s" in "%s::vote()" is deprecated, return an int instead.', get_debug_type($result), get_debug_type($voter));
}

if (VoterInterface::ACCESS_DENIED === $result) {
return false;
Expand Down Expand Up @@ -184,6 +193,9 @@ private function decidePriority(TokenInterface $token, array $attributes, $objec
{
foreach ($this->voters as $voter) {
$result = $voter->vote($token, $object, $attributes);
if (false === \is_int($result)) {
trigger_deprecation('symfony/security-core', '5.3', 'Returning a value of type "%s" in "%s::vote()" is deprecated, return an int instead.', get_debug_type($result), get_debug_type($voter));
}

if (VoterInterface::ACCESS_GRANTED === $result) {
return true;
Expand Down