Skip to content

[Security][TwigBridge] Add access_decision() and access_decision_for_user() #61379

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 4 commits into
base: 7.4
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/SecurityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public function isGranted(mixed $role, mixed $object = null, ?string $field = nu
}
}

public function accessDecision(mixed $role, mixed $object = null, ?string $field = null, ?AccessDecision $accessDecision = null): AccessDecision
{
$accessDecision = $accessDecision ?? new AccessDecision();
Comment on lines +58 to +60
Copy link
Member

Choose a reason for hiding this comment

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

not there's a point for templates to pass a previous access decision:

Suggested change
public function accessDecision(mixed $role, mixed $object = null, ?string $field = null, ?AccessDecision $accessDecision = null): AccessDecision
{
$accessDecision = $accessDecision ?? new AccessDecision();
public function accessDecision(mixed $role, mixed $object = null, ?string $field = null): AccessDecision
{
$accessDecision = new AccessDecision();


$this->isGranted($role, $object, $field, $accessDecision);

return $accessDecision;
}


public function isGrantedForUser(UserInterface $user, mixed $attribute, mixed $subject = null, ?string $field = null, ?AccessDecision $accessDecision = null): bool
{
if (null === $this->securityChecker) {
Expand All @@ -80,6 +90,15 @@ public function isGrantedForUser(UserInterface $user, mixed $attribute, mixed $s
}
}

public function accessDecisionForUser(UserInterface $user, mixed $attribute, mixed $subject = null, ?string $field = null, ?AccessDecision $accessDecision = null): AccessDecision
{
$accessDecision = $accessDecision ?? new AccessDecision();
Comment on lines +93 to +95
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
public function accessDecisionForUser(UserInterface $user, mixed $attribute, mixed $subject = null, ?string $field = null, ?AccessDecision $accessDecision = null): AccessDecision
{
$accessDecision = $accessDecision ?? new AccessDecision();
public function accessDecisionForUser(UserInterface $user, mixed $attribute, mixed $subject = null, ?string $field = null): AccessDecision
{
$accessDecision = new AccessDecision();


$this->isGrantedForUser($user, $attribute, $subject, $field, $accessDecision);

return $accessDecision;
}

public function getImpersonateExitUrl(?string $exitTo = null): string
{
if (null === $this->impersonateUrlGenerator) {
Expand Down Expand Up @@ -120,6 +139,7 @@ public function getFunctions(): array
{
$functions = [
new TwigFunction('is_granted', $this->isGranted(...)),
new TwigFunction('access_decision', $this->accessDecision(...)),
new TwigFunction('impersonation_exit_url', $this->getImpersonateExitUrl(...)),
new TwigFunction('impersonation_exit_path', $this->getImpersonateExitPath(...)),
new TwigFunction('impersonation_url', $this->getImpersonateUrl(...)),
Expand All @@ -128,6 +148,7 @@ public function getFunctions(): array

if ($this->securityChecker instanceof UserAuthorizationCheckerInterface) {
$functions[] = new TwigFunction('is_granted_for_user', $this->isGrantedForUser(...));
$functions[] = new TwigFunction('access_decision_for_user', $this->accessDecisionForUser(...));
}

return $functions;
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Bundle/SecurityBundle/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public function isGranted(mixed $attributes, mixed $subject = null, ?AccessDecis
->isGranted($attributes, $subject, $accessDecision);
}

public function accessDecision(mixed $attributes, mixed $subject = null): AccessDecision
{
$accessDecision = new AccessDecision();
$this->isGranted($attributes, $subject, $accessDecision);

return $accessDecision;
}

/**
* Checks if the attribute is granted against the user and optionally supplied subject.
*
Expand All @@ -76,6 +84,15 @@ public function isGrantedForUser(UserInterface $user, mixed $attribute, mixed $s
->isGrantedForUser($user, $attribute, $subject, $accessDecision);
}

public function accessDecisionForUser(UserInterface $user, mixed $attributes, mixed $subject = null): AccessDecision
{
$accessDecision = new AccessDecision();
$this->isGrantedForUser($user, $attributes, $subject, $accessDecision);

return $accessDecision;
}


public function getToken(): ?TokenInterface
{
return $this->container->get('security.token_storage')->getToken();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function testSendForIdnDomains()

$body = json_decode($options['body'], true);
// to
$this->assertSame('kältetechnik@xn--kltetechnik-xyz-0kb.de', $body['to'][0]);
$this->assertSame('Kältetechnik Xyz <kältetechnik@xn--kltetechnik-xyz-0kb.de>', $body['to'][0]);
// sender
$this->assertStringContainsString('info@xn--kltetechnik-xyz-0kb.de', $body['from']);
$this->assertStringContainsString('Kältetechnik Xyz', $body['from']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private function formatAddresses(array $addresses): array
{
$formattedAddresses = [];
foreach ($addresses as $address) {
$formattedAddresses[] = $address->getEncodedAddress();
$formattedAddresses[] = $this->formatAddress($address);
}

if (\count($formattedAddresses) > 50) {
Expand All @@ -99,8 +99,8 @@ private function getPayload(Email $email, Envelope $envelope): array
'to' => $this->formatAddresses($this->getRecipients($email, $envelope)),
'subject' => $email->getSubject(),
];
if ($attachements = $this->prepareAttachments($email)) {
$payload['attachments'] = $attachements;
if ($attachments = $this->prepareAttachments($email)) {
$payload['attachments'] = $attachments;
Copy link
Member

Choose a reason for hiding this comment

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

looks like a rebase is needed :)

}
if ($emails = $email->getReplyTo()) {
$payload['reply_to'] = current($this->formatAddresses($emails));
Expand Down
Loading