Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Security] Allow using expressions with the #[IsGranted] attribute #46978

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

Merged
merged 1 commit into from
Aug 2, 2022

Conversation

HypeMC
Copy link
Contributor

@HypeMC HypeMC commented Jul 18, 2022

Q A
Branch? 6.2
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #46912
License MIT
Doc PR -

Allows using the expression language with the #[IsGranted] attribute:

#[IsGranted(
    attribute: new Expression('"ROLE_ADMIN" in role_names or is_granted("POST_VIEW", subject)'),
    subject: 'post',
)]
public function index(Post $post)
{
}

#[IsGranted(
    attribute: new Expression('user === subject'),
    subject: new Expression('args["post"].getAuthor()'),
)]
public function index(Post $post)
{
}

#[IsGranted(
    attribute: new Expression('user === subject["author"] and subject["post"].isPublished()'),
    subject: [
        'author' => new Expression('args["post"].getAuthor()'),
        'post' => 'post',
    ],
)]
public function index(Post $post)
{
}

@HypeMC HypeMC force-pushed the improve-isgranted branch 2 times, most recently from bdd0c16 to 3957a98 Compare July 21, 2022 11:52
@HypeMC HypeMC force-pushed the improve-isgranted branch from 3957a98 to 8de7c8f Compare July 24, 2022 20:28
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.

$accessDeniedException->setAttributes($attribute->attributes);

Don't we want to provide the resolved values instead? That would solve the issue with accepting Expression into AccessDeniedException and might improve debugging?

@HypeMC HypeMC force-pushed the improve-isgranted branch 2 times, most recently from 7c38656 to 3d81940 Compare July 31, 2022 14:16
@HypeMC
Copy link
Contributor Author

HypeMC commented Jul 31, 2022

$accessDeniedException->setAttributes($attribute->attributes);

Don't we want to provide the resolved values instead? That would solve the issue with accepting Expression into AccessDeniedException and might improve debugging?

@nicolas-grekas The problem is that the attributes expression is evaluated by the ExpressionVoter and I don't see a way to get that info with the current implementation:

$result = VoterInterface::ACCESS_DENIED;
if ($this->expressionLanguage->evaluate($attribute, $variables)) {
return VoterInterface::ACCESS_GRANTED;
}
}
return $result;

Also, if I'm not mistaking, the result of the expression is always false (or falsy) if access is denied by the voter, so I'm not sure if that would really help with debugging.

@HypeMC
Copy link
Contributor Author

HypeMC commented Aug 2, 2022

@nicolas-grekas Was this closed by accident or did I miss something ?

@lyrixx
Copy link
Member

lyrixx commented Aug 2, 2022

Indeed, this is a mistake because of the other PR description

@lyrixx lyrixx reopened this Aug 2, 2022
@HypeMC HypeMC force-pushed the improve-isgranted branch from 0eb6bd8 to f5cee77 Compare August 2, 2022 08:38
@nicolas-grekas
Copy link
Member

Thank you @HypeMC.

@nicolas-grekas nicolas-grekas merged commit e7fbf28 into symfony:6.2 Aug 2, 2022
@HypeMC HypeMC deleted the improve-isgranted branch August 2, 2022 13:07
@fabpot fabpot mentioned this pull request Oct 24, 2022
fabpot added a commit to symfony/symfony-docs that referenced this pull request Mar 24, 2023
… (HypeMC)

This PR was merged into the 6.2 branch.

Discussion
----------

[Security] Use expression for `#[IsGranted()]` subject

symfony/symfony#46978
symfony/symfony#48080
symfony/symfony#48102

Commits
-------

9d4045f [Security] Use expression for #[IsGranted()] subject
weaverryan pushed a commit to symfony/symfony-docs that referenced this pull request Mar 28, 2023
… (HypeMC)

This PR was merged into the 6.2 branch.

Discussion
----------

[Security] Use expression for `#[IsGranted()]` subject

symfony/symfony#46978
symfony/symfony#48080
symfony/symfony#48102

Commits
-------

9d4045f [Security] Use expression for #[IsGranted()] subject
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.

Improve #[IsGranted]
7 participants