Execute middleware method on every request


#1

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


#2

As far as I know, yes it is expected . You can only register middleware for your controllers. I suggest you to look at the hooks, maybe you can find something for your needs.
https://doc.owncloud.org/server/10.0/developer_manual/app/fundamentals/hooks.html


#3

OK thanks, then documentation is kind of unclear on this one.

I already looked into hooks but the existing hooks don't fit my needs, e.g. executing some action on file download via web interface. Could you give some hint on how to achieve that?