Jump to content

Restrict access to template's files but allow guest to view the page


DrQuincy
 Share

Recommended Posts

I have a template, product, that has a field, downloads. I want people to be logged in to access downloads but I also want role guest to view the page. When you choose the option to restrict access to a template's files it determines this, rightly, based on the options set in the Access tab.

How can I make ProcessWire restrict access to downloads while at the same time making the actual page viewable by guests?

So if you had /page/ that has a link to /site/assets/files/999/file.pdf, everyone could view the page but the file would 404 for guest - but be accessible for anyone logged in.

Thanks.

Link to comment
Share on other sites

AFAIK ProcessWire’s pagefileSecure feature only works on the page level. So when your page is viewable by guests, its files will be viewable as well.

However, when pagefileSecure is active, all files should be routed through the hookable method ProcessPageView::___sendFile(). It’s a short little method you could hook to add your own logic. For example:

$this->addHookBefore('ProcessPageView::sendFile(template=MY_SPECIAL_TEMPLATE)', function(HookEvent $event)
{
    /** @var Page $page */
    $page = $event->arguments(0);
    $filename = $event->arguments(1);

    if ($this->wire->user->isGuest())
        throw new Wire404Exception('File not found', Wire404Exception::codeFile);
});

 

Edited by Jan Romero
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...