Jump to content

Hook for disabling specific pages from guest users without using access rights


MSP01
 Share

Recommended Posts

I have a template that is used on many pages, but some of those pages need to be disabled from guest users. 

An addHookBefore('Page::render'...) works fine for throwing a 404 if someone actually gets on the page. But is there a way to disable those pages in site navigation, and any other place where they might be visible in one fell swoop?

I thought that addHookAfter('Page::viewable'...) could work, but I haven't been able to make it do anything at all.

Basically I'd love to have a way to disable those pages without going through every single $pages->find() and similar piece of code where those pages might crop up.

Using access rights would be the easy way, but I cannot change the template and most of those pages are public.

Link to comment
Share on other sites

8 hours ago, MSP01 said:

but I cannot change the template

Why is that? Why not create a copy of the template (Setup > Templates > Add New > Copy fields from another template) that has access control enabled and then edit the pages to use that template? And the template file can just consist of:

include './your_other_template_file.php';

 

  • Like 1
Link to comment
Share on other sites

@horst This could work, but I'd rather keep things very literal. A check box that says "don't show this page to certain people" is more clear than setting them hidden.

@Robin S I could of course do that, but I'd really rather not. There are a lot of these pages, and there's a lot going on under the hood. I'd have to change code all over the place and it's what I'm trying to avoid in the first place.

Sorry if it seems I'm trying to find the most difficult way to do a simple thing. If there's no easy fix via hook, I'll just do it manually in the code. ?

It's also possible there will be need to hide pages from more than one group while showing it to other groups in the future and Horst's suggestion wouldn't work in that case. Neither would the template. Or at least I wouldn't want to create a new template every single time something like that came up. It seems like a very heavy way of doing something that should be handled within a page itself.

Link to comment
Share on other sites

On 4/22/2022 at 11:47 PM, Robin S said:

Why is that? Why not create a copy of the template (Setup > Templates > Add New > Copy fields from another template) that has access control enabled and then edit the pages to use that template?

Hi Robin, I think one can do that a bit simpler, at least I always use it this way: 

1) image.png.99df992db3ef7bfd33d74973cb122bc8.png
I'm not hundred percent sure, but maybe there is difference between cloning the whole template compared against only duplicate the fields. (Cloning definitely includes all settings under family, files, advanced, etc) 
 

2) image.png.57e9701a92837410bd4ad00bca451829.png

And this saves the creation of an extra template file.

All in all very useful when I want to use something like a single controller file, that calls different view files upon the template names. ?

 

  • Like 1
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...