Jump to content

Module: Page Access Releasetime


Sebi
 Share

Recommended Posts

I've created a small module which lets you define a timestamp after which a page should be accessible. In addition you can define a timestamp when the release should end and the page should not be accessable any more.

ProcessWire-Module: http://modules.processwire.com/modules/page-access-releasetime/
Github: https://github.com/Sebiworld/PageAccessReleasetime

Usage

PageAccessReleasetime can be installed like every other module in ProcessWire. Check the following guide for detailed information: How-To Install or Uninstall Modules

After that, you will find checkboxes for activating the releasetime-fields at the settings-tab of each page. You don't need to add the fields to your templates manually.

Check e.g. the checkbox "Activate Releasetime from?" and fill in a date in the future. The page will not be accessable for your users until the given date is reached.

If you have $config->pagefileSecure = true, the module will protect files of unreleased pages as well.

How it works

This module hooks into Page::viewable to prevent users to access unreleased pages:

public function hookPageViewable($event) {
	$page = $event->object;
	$viewable = $event->return;

	if($viewable){
		// If the page would be viewable, additionally check Releasetime and User-Permission
		$viewable = $this->canUserSee($page);
	}
	$event->return = $viewable;
}

To prevent access to the files of unreleased pages, we hook into Page::isPublic and ProcessPageView::sendFile.

public function hookPageIsPublic($e) {
	$page = $e->object;
	if($e->return && $this->isReleaseTimeSet($page)) {
		$e->return = false;
	}
}

The site/assets/files/ directory of pages, which isPublic() returns false, will get a '-' as prefix. This indicates ProcessWire (with activated $config->pagefileSecure) to check the file's permissions via PHP before delivering it to the client.

The check wether a not-public file should be accessable happens in ProcessPageView::sendFile. We throw an 404 Exception if the current user must not see the file.

public function hookProcessPageViewSendFile($e) {
	$page = $e->arguments[0];
	if(!$this->canUserSee($page)) {
		throw new Wire404Exception('File not found');
	}
}

Additionally we hook into ProcessPageEdit::buildForm to add the PageAccessReleasetime fields to each page and move them to the settings tab.

Limitations

In the current version, releasetime-protected pages will appear in wire('pages')->find() queries. If you want to display a list of pages, where pages could be releasetime-protected, you should double-check with $page->viewable() wether the page can be accessed. $page->viewable() returns false, if the page is not released yet.

If you have an idea how unreleased pages can be filtered out of ProcessWire selector queries, feel free to write an issue, comment or make a pull request!

  • Like 11
Link to comment
Share on other sites

Nice! First post and you already come with a new module ?

I'll take your module for a test-run soon.

edit: A quick glimpse at your code shows me you apparently didn't take into account translateable strings, i.e. multilanguage setups:

$field->label = 'Activate Releasetime from?';

https://processwire.com/docs/multi-language-support/code-i18n/#translatable-strings

 

Edited by dragan
multilang suggestion
  • Like 1
Link to comment
Share on other sites

Very nice, thanks for the module!

I have to say though, the fact that the fields get automatically added to all templates doesn't sit quite right with me. People probably only need to use time limits for specific templates and for the other templates the fields become a kind of clutter. Better I think to let people add the fields to only the templates they need to be on.

I haven't looked closely but perhaps you could reduce the number of needed fields to two - just the Datetime fields. Instead of the checkboxes you can have the Datetime fields set to collapse when empty. Don't have the fields default to "today" but just let people populate them when they want them to be active and leave them empty if they want them deactivated.

Another possibility if you want to make it easy for people to add/remove the fields from templates would be to have an AsmSelect field in the module config for choosing templates. Then you could hook Modules::saveConfig to get the selected templates and programmatically add/remove the fields from those templates.

 

4 hours ago, Sebi said:

If you have an idea how unreleased pages can be filtered out of ProcessWire selector queries

Clauses could be added to the selector to check...

1. Either releasetime_start is unpopulated or releasetime_start is less than "now"

...and... 

2. Either releasetime_end is unpopulated or releasetime_end is greater than "now"...

$pages->find("or1=(releasetime_start=''), or1=(releasetime_start<now), or2=(releasetime_end=''), or2=(releasetime_end>now)");

But rather than try and do this automatically in a hook to Pages::find (which would almost certainly be problematic) I suggest just explaining this in the documentation and letting people add it to their selectors as needed.

  • Like 3
Link to comment
Share on other sites

6 hours ago, dragan said:

Nice! First post and you already come with a new module ?

I'll take your module for a test-run soon.

edit: A quick glimpse at your code shows me you apparently didn't take into account translateable strings, i.e. multilanguage setups:


$field->label = 'Activate Releasetime from?';

https://processwire.com/docs/multi-language-support/code-i18n/#translatable-strings

 

Hey @dragan, thanks for this hint!

I did not have much contact with multilanguage setups in the past, so I hope I added translatable string support the right way in version 1.0.1 of the module. Additionally I removed the german date formats for the inputs. I think, that is something that german users have to configure on their own if needed.

  • Like 1
Link to comment
Share on other sites

Hi @Robin S, thanks for your help making the module even better and more useable.

On 2/11/2019 at 11:54 PM, Robin S said:

I have to say though, the fact that the fields get automatically added to all templates doesn't sit quite right with me. People probably only need to use time limits for specific templates and for the other templates the fields become a kind of clutter. Better I think to let people add the fields to only the templates they need to be on.

I think I found a good solution to fullfill both requirements. 

With version 1.0.2 you can choose in module's configuration wether the fields should be added globally or only to selected templates. You can choose the templates via ASMSelect, if the checkbox is unchecked. I do check wether the fields were added manually in the template as well, and check the template if all fields were found.

On 2/11/2019 at 11:54 PM, Robin S said:

I haven't looked closely but perhaps you could reduce the number of needed fields to two - just the Datetime fields. Instead of the checkboxes you can have the Datetime fields set to collapse when empty. Don't have the fields default to "today" but just let people populate them when they want them to be active and leave them empty if they want them deactivated.

I actually find my solution with checkboxes more understandable for normal users. If you uncheck the option "Activate Releasetime from?", it is very clear that the no releasetime is active. If I'd use only the datetime fields without checkbox, I would at least add a notice that gives a hint about the possibility to leave it blank. Of course it's a trifle, but I've worked with customers who would otherwise have had queries about it.

I've heeded your advice about explaining selectors in the documentation. For convenience, I added a constant with the selectorstring that is needed to filter unreleased pages. To filter unreleased pages, add the PageAccessReleasetime::selector to your selector:

$onlyReleasedPages = wire('pages')->find('template.name=news, ' . PageAccessReleasetime::selector);

I thought about manipulating the page's status to achieve this without adding a custom selector part to each query, but I haven't found anything useful yet. Extending the selector seems to be the best solution at the moment.

Thanks again for your support! ?

Bildschirmfoto 2019-02-13 um 01.39.45.png

  • Like 7
Link to comment
Share on other sites

We are online!
PageAccessReleasetime is now available in the ProcessWire modules directory! ?

I think about building some more access-modules for different use cases:

  • userrole- and user-fields - limits a page to be accessable only for given userroles or users
  • password protection - a password-field (+activate-field), password-validation-functionality, maybe a render function for the login-form in the frontend

What do you think about it?

  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

Hey @szabesz, thank you for this hint.

I did not know about @adrian's module. Finally I got time to install it and look into the code. I really like that it also cares for limiting view- and editing-rights in the backend, what PageAccessReleasetime currently doesn't. But it could be a useful feature for PageAccessReleasetime as well. In the current version every backend-user can see unreleased pages in the pagetree.

I don't like that the module deals with the HTML shown in the frontend if a user cannot access a page. PageAccessReleasetime instead throws a 404 exception, what can be acceptable for the releasetime context. If I think of a PageAccessUserrole-module, it would be better to throw a 401 exception that leads to a custom 401 page where a login-form is shown. But ProcessWire seems not to support other exception pages than 404 and there is no other core exception class than Wire404Exception. 

Let me know if you have a practical solution for it, where my module does not have to generate its own HTML for the error page. I think that is something that the template-developer should be aware of and not the module.

  • Like 1
Link to comment
Share on other sites

I have a (small) new version for you: v1.0.4 adds a $page->listable() hook. If you want to check wether a page should be accessible in list-views, you should use this call to double check if each page can be seen.

For detail-views or to check wether a list-item can be linked to a detail-view, you should continue to use $page->viewable().

  • Like 1
Link to comment
Share on other sites

  • 4 months later...

Hi @Juergen, you're right!

Generally speaking, the module does not work automatically in all functions where ProcessWire returns a list of pages ($pages->find(), $page->children(), $page->find(), $page->parents(), ...). The reason for this is, that the used Hooks to block access will not be triggered in the list functions. The only way to filter out all pages that are unreleased, is to add filtering for the PageAccessReleasetime-fields in your selector. For convenience, I added the selector as a static attribute to the module:

$onlyReleasedPages = $page->children(PageAccessReleasetime::selector);

I would be really thankful if anybody had a better way to implement automatic filtering for unrelease pages, like it already works for ProcessWire's page-status. But since then adding PageAccessReleasetime::selector  to selectors is a good workaround.

Link to comment
Share on other sites

  • 2 years later...

Hey Sebi,
first of all: thanks for the module you shared!!
I implemented it on my site and it worked well but I now face the problem that each day again the select area where you choose which templates should get the releasetime fields deselects itself?!? So the pages that should get deactivated show up each morning again … That happens either over night or when I get logged out of my admin area automatically after a while (couldn’t find that out yet). Have you had this problem before as well?

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

×
×
  • Create New...