Skip to content

Commit 447ee17

Browse files
committed
fix: Remove code duplication by using the new method
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent bd343a6 commit 447ee17

3 files changed

Lines changed: 7 additions & 47 deletions

File tree

lib/private/AppFramework/Middleware/FlowV2EphemeralSessionsMiddleware.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use OCP\ISession;
1919
use OCP\IUserSession;
2020
use Psr\Log\LoggerInterface;
21-
use ReflectionMethod;
2221

2322
// Will close the session if the user session is ephemeral.
2423
// Happens when the user logs in via the login flow v2.
@@ -61,12 +60,7 @@ public function beforeController(Controller $controller, string $methodName) {
6160
return;
6261
}
6362

64-
$reflectionMethod = new ReflectionMethod($controller, $methodName);
65-
if (!empty($reflectionMethod->getAttributes(PublicPage::class))) {
66-
return;
67-
}
68-
69-
if ($this->reflector->hasAnnotation('PublicPage')) {
63+
if ($this->reflector->hasAnnotationOrAttribute('PublicPage', PublicPage::class)) {
7064
return;
7165
}
7266

lib/private/AppFramework/Middleware/Security/PasswordConfirmationMiddleware.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ public function __construct(
4646
* @throws NotConfirmedException
4747
*/
4848
public function beforeController(Controller $controller, string $methodName) {
49-
$reflectionMethod = new ReflectionMethod($controller, $methodName);
50-
51-
if (!$this->needsPasswordConfirmation($reflectionMethod)) {
49+
if (!$this->needsPasswordConfirmation()) {
5250
return;
5351
}
5452

@@ -79,6 +77,7 @@ public function beforeController(Controller $controller, string $methodName) {
7977
return;
8078
}
8179

80+
$reflectionMethod = new ReflectionMethod($controller, $methodName);
8281
if ($this->isPasswordConfirmationStrict($reflectionMethod)) {
8382
$authHeader = $this->request->getHeader('Authorization');
8483
if (!str_starts_with(strtolower($authHeader), 'basic ')) {
@@ -101,18 +100,8 @@ public function beforeController(Controller $controller, string $methodName) {
101100
}
102101
}
103102

104-
private function needsPasswordConfirmation(ReflectionMethod $reflectionMethod): bool {
105-
$attributes = $reflectionMethod->getAttributes(PasswordConfirmationRequired::class);
106-
if (!empty($attributes)) {
107-
return true;
108-
}
109-
110-
if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) {
111-
$this->logger->debug($reflectionMethod->getDeclaringClass()->getName() . '::' . $reflectionMethod->getName() . ' uses the @' . 'PasswordConfirmationRequired' . ' annotation and should use the #[PasswordConfirmationRequired] attribute instead');
112-
return true;
113-
}
114-
115-
return false;
103+
private function needsPasswordConfirmation(): bool {
104+
return $this->reflector->hasAnnotationOrAttribute('PasswordConfirmationRequired', PasswordConfirmationRequired::class);
116105
}
117106

118107
private function isPasswordConfirmationStrict(ReflectionMethod $reflectionMethod): bool {

lib/private/AppFramework/Middleware/SessionMiddleware.php

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use OCP\AppFramework\Http\Response;
1515
use OCP\AppFramework\Middleware;
1616
use OCP\ISession;
17-
use ReflectionMethod;
1817

1918
class SessionMiddleware extends Middleware {
2019
public function __construct(
@@ -28,18 +27,7 @@ public function __construct(
2827
* @param string $methodName
2928
*/
3029
public function beforeController($controller, $methodName) {
31-
/**
32-
* Annotation deprecated with Nextcloud 26
33-
*/
34-
$hasAnnotation = $this->reflector->hasAnnotation('UseSession');
35-
if ($hasAnnotation) {
36-
$this->session->reopen();
37-
return;
38-
}
39-
40-
$reflectionMethod = new ReflectionMethod($controller, $methodName);
41-
$hasAttribute = !empty($reflectionMethod->getAttributes(UseSession::class));
42-
if ($hasAttribute) {
30+
if ($this->reflector->hasAnnotationOrAttribute('UseSession', UseSession::class)) {
4331
$this->session->reopen();
4432
}
4533
}
@@ -51,18 +39,7 @@ public function beforeController($controller, $methodName) {
5139
* @return Response
5240
*/
5341
public function afterController($controller, $methodName, Response $response) {
54-
/**
55-
* Annotation deprecated with Nextcloud 26
56-
*/
57-
$hasAnnotation = $this->reflector->hasAnnotation('UseSession');
58-
if ($hasAnnotation) {
59-
$this->session->close();
60-
return $response;
61-
}
62-
63-
$reflectionMethod = new ReflectionMethod($controller, $methodName);
64-
$hasAttribute = !empty($reflectionMethod->getAttributes(UseSession::class));
65-
if ($hasAttribute) {
42+
if ($this->reflector->hasAnnotationOrAttribute('UseSession', UseSession::class)) {
6643
$this->session->close();
6744
}
6845

0 commit comments

Comments
 (0)