Skip to content

Commit e6cfd21

Browse files
authored
Merge pull request #11357 from touhidurabir/i11356_main
#11356 Apply default API controller middleware to routes from plugin
2 parents 1985958 + 5bc3745 commit e6cfd21

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

classes/handler/APIHandler.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
use Illuminate\Http\Response;
1818
use Illuminate\Routing\Pipeline;
1919
use PKP\core\PKPBaseController;
20-
use PKP\core\PKPContainer;
2120
use PKP\core\PKPRoutingProvider;
21+
use PKP\middleware\HasRoles;
2222
use PKP\plugins\Hook;
2323
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
2424
use Throwable;
@@ -190,14 +190,31 @@ protected function registerRoute(): void
190190
$router = app('router'); /** @var \Illuminate\Routing\Router $router */
191191

192192
foreach ($this->routesFromHook as $routeParams) {
193-
$router
194-
->addRoute(
195-
$routeParams['method'],
196-
$this->getEndpointPattern() . '/' . $routeParams['uri'],
197-
$routeParams['callback']
198-
)
193+
$route = $router->addRoute(
194+
$routeParams['method'],
195+
$this->getEndpointPattern() . '/' . $routeParams['uri'],
196+
$routeParams['callback']
197+
);
198+
199+
$route
199200
->name($routeParams['name'])
200201
->middleware($this->apiController->roleAuthorizer($routeParams['roles']));
202+
203+
foreach ($this->apiController->getRouteGroupMiddleware() as $middleware) {
204+
205+
// As roles are already supplied for routes directly injecting in the router
206+
// we do not want to add any other roles middleware form controller if defined
207+
if (strstr($middleware, 'has.roles') !== false || strstr($middleware, HasRoles::class) !== false) {
208+
continue;
209+
}
210+
211+
// We don't want to set any middleware to the route which is already set
212+
if (in_array($middleware, $route->middleware())) {
213+
continue;
214+
}
215+
216+
$route->middleware($middleware);
217+
}
201218
}
202219
}
203220
}

0 commit comments

Comments
 (0)