Hello OC community!
I'm developing an app and try to implement a custom middleware. I followed the documentation at https://doc.owncloud.org/server/latest/developer_manual/app/fundamentals/middleware.html but I seem to have misunderstood the sentence
"Middleware is logic that is run before and after each request …"
The middleware I implemented is only executed for the controllers I implemented myself, not for every other request, which is what I want.
Is that expected behaviour?
Here is how I implemented my middleware:
Application.php
$container->registerService('LoggerMiddleware', function (IAppContainer $c) {
/** @var \OC\Server $server */
$server = $c->query('ServerContainer');
return new LoggerMiddleware(
$server->getRequest(),
$c->query('ServerContainer')->getUserSession()->getUser(),
$c->query('LogService')
);
});
$container->registerMiddleWare('LoggerMiddleware');
LoggerMiddleware.php
class LoggerMiddleware extends Middleware {
private $request;
private $logService;
private $currentUser;
public function __construct($request, IUser $currentUser, LogService $logService) {
$this->request = $request;
$this->currentUser = $currentUser;
$this->logService = $logService;
}
public function beforeOutput($controller, $methodName, $output) {
$http_verb = $this->request->getMethod();
$this->logService->log($http_verb);
if ($http_verb == 'GET') {
if (true === $this->isDownloadRequest($this->request)) {
$this->logService->log(sprintf('Datei %s durch User %s heruntergeladen', $this->request->getRawPathInfo(),
$this->currentUser->getUID()),
['requestId' => $this->request->getId()]
);
}
}
}
// ...
Any help much appreciated!
Best regards,
Constantin