PWaddict

[SOLVED] How to restrict view access of unpublished pages for non-superusers?

Recommended Posts

PW by default allows non-superusers to view unpublished pages on frontend. Is there an option somewhere to prevent that?

Share this post


Link to post
Share on other sites
1 hour ago, PWaddict said:

PW by default allows non-superusers to view unpublished pages on frontend. Is there an option somewhere to prevent that?

I never knew that, I wonder why guests have permissions to view unpublished pages? 

Anyway, I guess put this at the very top of your template file:

if($page->isUnpublished() && !$user->isSuperuser()) throw new Wire404Exception();

 

Share this post


Link to post
Share on other sites
6 minutes ago, Tom. said:

I never knew that, I wonder why guests have permissions to view unpublished pages? 

 

Guests can't view them. I was talking about the site editors (non-superusers).

 

7 minutes ago, Tom. said:

if($page->isUnpublished() && !$user->isSuperuser()) throw new Wire404Exception();

 

I've already tried that but something isn't right. The site editor can see a 404 page (which is what I want) but lot of pw related content is hidden like titles, other fields content etc.

Share this post


Link to post
Share on other sites

I've also tried this but isn't working. I'm getting a blank white page.

if($page->isUnpublished() && !$user->isSuperuser()) {
  $error404 = $pages->get("template=404, include=hidden");
  $session->redirect($error404->url, false);
}

 

Share this post


Link to post
Share on other sites

I had to create an alternate template for 404 page and now it works:

if($page->isUnpublished() && !$user->isSuperuser()) {
  $error404 = $pages->get("template=404-alt, include=hidden");
  $session->redirect($error404->url, false);
}

If you know a better way let me know. Thanks.

Share this post


Link to post
Share on other sites

No need for alt 404 template.

if($page->isUnpublished() && !$user->isSuperuser()) {
  $error404 = $pages->get(27); // ID of 404 page
  $session->redirect($error404->url, false); // 302 temporary redirect
}

 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.