processwire / processwire-requests

ProcessWire feature requests.
39 stars 0 forks source link

Add option of "let the template file handle it" when user requests page with no view access #501

Open Toutouwai opened 1 year ago

Toutouwai commented 1 year ago

Short description of the enhancement

There are currently three options available in the template settings for "What to do when user attempts to view a page and has no access?"

2023-08-31_182155

This request is for a fourth option: "Let the template file handle it"

The way that this would work is that for users with no view access, the page would be rendered but $page->viewable would be false. So the developer can perform any actions or output whatever they want via the template file.

if($page->viewable) {
    // Normal template output here for users who are allowed to view it
    // ...
} else {
    // Output whatever needs to be shown to users without view access
    // Might be a message or a custom login form or some redirect logic
    echo "You are not allowed to view this page";
}

This allows for everything to be self-contained within the template file rather than having to use a separate page ID if you want to render some markup for users with no view access.

Toutouwai commented 1 year ago

For the meantime this hook in /site/init.php does the job:

$wire->addHookAfter('PagesRequest::getLoginPageOrUrl', function(HookEvent $event) {
    $page = $event->arguments(0);
    if($page->template == 'my_template') {
        $event->return = $page;
    }
});

Then use $page->viewable logic in the "my_template" template file as per the first post.