mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
fix: Remove code duplication by using the new method
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
This commit is contained in:
parent
bd343a6e9e
commit
447ee17759
3 changed files with 7 additions and 47 deletions
|
|
@ -18,7 +18,6 @@ use OCP\Authentication\TwoFactorAuth\ALoginSetupController;
|
|||
use OCP\ISession;
|
||||
use OCP\IUserSession;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ReflectionMethod;
|
||||
|
||||
// Will close the session if the user session is ephemeral.
|
||||
// Happens when the user logs in via the login flow v2.
|
||||
|
|
@ -61,12 +60,7 @@ class FlowV2EphemeralSessionsMiddleware extends Middleware {
|
|||
return;
|
||||
}
|
||||
|
||||
$reflectionMethod = new ReflectionMethod($controller, $methodName);
|
||||
if (!empty($reflectionMethod->getAttributes(PublicPage::class))) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->reflector->hasAnnotation('PublicPage')) {
|
||||
if ($this->reflector->hasAnnotationOrAttribute('PublicPage', PublicPage::class)) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -46,9 +46,7 @@ class PasswordConfirmationMiddleware extends Middleware {
|
|||
* @throws NotConfirmedException
|
||||
*/
|
||||
public function beforeController(Controller $controller, string $methodName) {
|
||||
$reflectionMethod = new ReflectionMethod($controller, $methodName);
|
||||
|
||||
if (!$this->needsPasswordConfirmation($reflectionMethod)) {
|
||||
if (!$this->needsPasswordConfirmation()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -79,6 +77,7 @@ class PasswordConfirmationMiddleware extends Middleware {
|
|||
return;
|
||||
}
|
||||
|
||||
$reflectionMethod = new ReflectionMethod($controller, $methodName);
|
||||
if ($this->isPasswordConfirmationStrict($reflectionMethod)) {
|
||||
$authHeader = $this->request->getHeader('Authorization');
|
||||
if (!str_starts_with(strtolower($authHeader), 'basic ')) {
|
||||
|
|
@ -101,18 +100,8 @@ class PasswordConfirmationMiddleware extends Middleware {
|
|||
}
|
||||
}
|
||||
|
||||
private function needsPasswordConfirmation(ReflectionMethod $reflectionMethod): bool {
|
||||
$attributes = $reflectionMethod->getAttributes(PasswordConfirmationRequired::class);
|
||||
if (!empty($attributes)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) {
|
||||
$this->logger->debug($reflectionMethod->getDeclaringClass()->getName() . '::' . $reflectionMethod->getName() . ' uses the @' . 'PasswordConfirmationRequired' . ' annotation and should use the #[PasswordConfirmationRequired] attribute instead');
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
private function needsPasswordConfirmation(): bool {
|
||||
return $this->reflector->hasAnnotationOrAttribute('PasswordConfirmationRequired', PasswordConfirmationRequired::class);
|
||||
}
|
||||
|
||||
private function isPasswordConfirmationStrict(ReflectionMethod $reflectionMethod): bool {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,6 @@ use OCP\AppFramework\Http\Attribute\UseSession;
|
|||
use OCP\AppFramework\Http\Response;
|
||||
use OCP\AppFramework\Middleware;
|
||||
use OCP\ISession;
|
||||
use ReflectionMethod;
|
||||
|
||||
class SessionMiddleware extends Middleware {
|
||||
public function __construct(
|
||||
|
|
@ -28,18 +27,7 @@ class SessionMiddleware extends Middleware {
|
|||
* @param string $methodName
|
||||
*/
|
||||
public function beforeController($controller, $methodName) {
|
||||
/**
|
||||
* Annotation deprecated with Nextcloud 26
|
||||
*/
|
||||
$hasAnnotation = $this->reflector->hasAnnotation('UseSession');
|
||||
if ($hasAnnotation) {
|
||||
$this->session->reopen();
|
||||
return;
|
||||
}
|
||||
|
||||
$reflectionMethod = new ReflectionMethod($controller, $methodName);
|
||||
$hasAttribute = !empty($reflectionMethod->getAttributes(UseSession::class));
|
||||
if ($hasAttribute) {
|
||||
if ($this->reflector->hasAnnotationOrAttribute('UseSession', UseSession::class)) {
|
||||
$this->session->reopen();
|
||||
}
|
||||
}
|
||||
|
|
@ -51,18 +39,7 @@ class SessionMiddleware extends Middleware {
|
|||
* @return Response
|
||||
*/
|
||||
public function afterController($controller, $methodName, Response $response) {
|
||||
/**
|
||||
* Annotation deprecated with Nextcloud 26
|
||||
*/
|
||||
$hasAnnotation = $this->reflector->hasAnnotation('UseSession');
|
||||
if ($hasAnnotation) {
|
||||
$this->session->close();
|
||||
return $response;
|
||||
}
|
||||
|
||||
$reflectionMethod = new ReflectionMethod($controller, $methodName);
|
||||
$hasAttribute = !empty($reflectionMethod->getAttributes(UseSession::class));
|
||||
if ($hasAttribute) {
|
||||
if ($this->reflector->hasAnnotationOrAttribute('UseSession', UseSession::class)) {
|
||||
$this->session->close();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue