Skip to content

Commit ffe3694

Browse files
authored
Merge pull request #3456 from mhsdesign/task/allow-to-return-from-withoutAuthorizationChecks
TASK: Allow to return closure value from `withoutAuthorizationChecks()`
2 parents 7761f0c + 3c78306 commit ffe3694

File tree

4 files changed

+16
-21
lines changed

4 files changed

+16
-21
lines changed

Neos.Flow/Classes/Security/Authentication/Provider/PersistedUsernamePasswordProvider.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*/
1313

1414
use Neos\Flow\Annotations as Flow;
15-
use Neos\Flow\Security\Account;
1615
use Neos\Flow\Security\AccountRepository;
1716
use Neos\Flow\Security\Authentication\Token\UsernamePasswordTokenInterface;
1817
use Neos\Flow\Security\Authentication\TokenInterface;
@@ -86,9 +85,6 @@ public function authenticate(TokenInterface $authenticationToken)
8685
throw new UnsupportedAuthenticationTokenException(sprintf('This provider cannot authenticate the given token. The token must implement %s', UsernamePasswordTokenInterface::class), 1217339840);
8786
}
8887

89-
/** @var Account|null $account */
90-
$account = null;
91-
9288
if ($authenticationToken->getAuthenticationStatus() !== TokenInterface::AUTHENTICATION_SUCCESSFUL) {
9389
$authenticationToken->setAuthenticationStatus(TokenInterface::NO_CREDENTIALS_GIVEN);
9490
}
@@ -101,8 +97,8 @@ public function authenticate(TokenInterface $authenticationToken)
10197
}
10298

10399
$providerName = $this->options['lookupProviderName'] ?? $this->name;
104-
$this->securityContext->withoutAuthorizationChecks(function () use ($username, &$account, $providerName) {
105-
$account = $this->accountRepository->findActiveByAccountIdentifierAndAuthenticationProviderName($username, $providerName);
100+
$account = $this->securityContext->withoutAuthorizationChecks(function () use ($username, $providerName) {
101+
return $this->accountRepository->findActiveByAccountIdentifierAndAuthenticationProviderName($username, $providerName);
106102
});
107103

108104
$authenticationToken->setAuthenticationStatus(TokenInterface::WRONG_CREDENTIALS);

Neos.Flow/Classes/Security/Authorization/Privilege/Entity/Doctrine/PropertyConditionGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,8 +553,8 @@ public function getValueForOperand($expression)
553553
$objectAccess = explode('.', $expression, 3);
554554
$globalObjectsRegisteredClassName = $this->globalObjects[$objectAccess[1]];
555555
$globalObject = $this->objectManager->get($globalObjectsRegisteredClassName);
556-
$this->securityContext->withoutAuthorizationChecks(function () use ($globalObject, $objectAccess, &$globalObjectValue) {
557-
$globalObjectValue = $this->getObjectValueByPath($globalObject, $objectAccess[2]);
556+
$globalObjectValue = $this->securityContext->withoutAuthorizationChecks(function () use ($globalObject, $objectAccess) {
557+
return $this->getObjectValueByPath($globalObject, $objectAccess[2]);
558558
});
559559

560560
return $globalObjectValue;

Neos.Flow/Classes/Security/Context.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -202,22 +202,22 @@ class Context
202202
* Lets you switch off authorization checks (CSRF token, policies, content security, ...) for the runtime of $callback
203203
*
204204
* Usage:
205-
* $this->securityContext->withoutAuthorizationChecks(function () use ($accountRepository, $username, $providerName, &$account) {
206-
* // this will disable the PersistenceQueryRewritingAspect for this one call
207-
* $account = $accountRepository->findActiveByAccountIdentifierAndAuthenticationProviderName($username, $providerName)
208-
* });
209205
*
210-
* @param \Closure $callback
211-
* @return void
212-
* @throws \Exception
206+
* $account = $this->securityContext->withoutAuthorizationChecks(function () use ($accountRepository, $username, $providerName) {
207+
* // this will disable the PersistenceQueryRewritingAspect for this one call
208+
* return $accountRepository->findActiveByAccountIdentifierAndAuthenticationProviderName($username, $providerName)
209+
* });
210+
*
211+
* @template T
212+
* @param \Closure(): T $callback
213+
* @return T the return value of $callback
213214
*/
214-
public function withoutAuthorizationChecks(\Closure $callback)
215+
public function withoutAuthorizationChecks(\Closure $callback): mixed
215216
{
216217
$authorizationChecksAreAlreadyDisabled = $this->authorizationChecksDisabled;
217218
$this->authorizationChecksDisabled = true;
218219
try {
219-
/** @noinspection PhpUndefinedMethodInspection */
220-
$callback->__invoke();
220+
return $callback();
221221
} finally {
222222
$this->authorizationChecksDisabled = $authorizationChecksAreAlreadyDisabled;
223223
}

Neos.Flow/Tests/Functional/Command/BehatHelperCommandController.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ public function callBehatStepCommand($testHelperObjectName, $methodName, $withou
6767
$mappedArguments[] = $this->propertyMapper->convert($rawMethodArguments[$i+1], $rawMethodArguments[$i]);
6868
}
6969

70-
$result = null;
7170
try {
7271
if ($withoutSecurityChecks === true) {
73-
$this->securityContext->withoutAuthorizationChecks(function () use ($testHelper, $methodName, $mappedArguments, &$result) {
74-
$result = call_user_func_array([$testHelper, $methodName], $mappedArguments);
72+
$result = $this->securityContext->withoutAuthorizationChecks(function () use ($testHelper, $methodName, $mappedArguments) {
73+
return call_user_func_array([$testHelper, $methodName], $mappedArguments);
7574
});
7675
} else {
7776
$result = call_user_func_array([$testHelper, $methodName], $mappedArguments);

0 commit comments

Comments
 (0)