Skip to content

Commit

Permalink
Add $page->listable hook
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Schendel committed Mar 11, 2019
1 parent a94a242 commit b0ba7d7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
19 changes: 17 additions & 2 deletions PageAccessReleasetime.module.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static function getModuleInfo() {
return array(
'title' => __('Page Access Releasetime'),
'author' => 'Sebastian Schendel',
'version' => '1.0.3',
'version' => '1.0.4',
'summary' => __('Enables you to set a start- and end-time for the release of pages. Prevents unreleased pages from being displayed.'),
'singular' => true,
'autoload' => true,
Expand Down Expand Up @@ -134,13 +134,14 @@ public static function getModuleConfigInputfields(array $data) {
foreach(wire('templates') as $template) {
// Exclude system templates:
if($template->flags & Template::flagSystem) continue;
$f->addOption($template->id, $template->getLabel());
$f->addOption($template->id, $template->getLabel() . ' (' . $template->name . ')');

// Check, if the template already has all releasetime-fields:
$allFieldsExistFlag = true;
foreach(self::fieldnames as $fieldname){
if(!$template->hasField($fieldname)){
$allFieldsExistFlag = false;
break;
}
}

Expand All @@ -166,6 +167,9 @@ public function init() {
// Prevent unreleased pagse from being viewed
$this->addHook('Page::viewable', $this, 'hookPageViewable');

// Unreleased pages cannot be listed:
$this->addHook('Page::listable', $this, 'hookPageListable');

// Manage access to files ($config->pagefileSecure has to be true)
$this->addHookAfter('Page::isPublic', $this, 'hookPageIsPublic');
$this->addHookBefore('ProcessPageView::sendFile', $this, 'hookProcessPageViewSendFile');
Expand Down Expand Up @@ -196,6 +200,17 @@ public function hookPageViewable(HookEvent $event) {
$event->return = $viewable;
}

public function hookPageListable(HookEvent $event) {
$page = $event->object;
$listable = $event->return;

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

/**
* if Page::isPublic() returns false a prefix (-) will be added to the name of the assets directory
* the directory is not accessible directly anymore
Expand Down
29 changes: 22 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# PageAccessReleasetime
Enables you to set a start- and end-time for the release of pages. Prevents unreleased pages from being displayed.

ProcessWire-Module: [http://modules.processwire.com/modules/page-access-releasetime/](http://modules.processwire.com/modules/page-access-releasetime/)
Support-Forum: [https://processwire.com/talk/topic/20852-module-page-access-releasetime/](https://processwire.com/talk/topic/20852-module-page-access-releasetime/)
Github-Repo: [https://github.com/Sebiworld/PageAccessReleasetime](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](http://modules.processwire.com/install-uninstall/)

Expand All @@ -11,7 +15,7 @@ Check e.g. the checkbox "Activate Releasetime from?" and fill in a date in the f
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:
This module hooks into `Page::viewable` and `Page::listable` to prevent users to access unreleased pages:

```php
public function hookPageViewable($event) {
Expand All @@ -24,12 +28,23 @@ public function hookPageViewable($event) {
}
$event->return = $viewable;
}


public function hookPageListable($event) {
$page = $event->object;
$listable = $event->return;

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

```

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

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 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.

```php
public function hookPageIsPublic($e) {
Expand All @@ -49,23 +64,23 @@ public function hookProcessPageViewSendFile($e) {
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.
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()` or `$page->listable()` (for lists) wether the page can be accessed. `$page->viewable()` returns false, if the page is not released yet.

To filter unreleased pages, add the `PageAccessReleasetime::selector` to your selector:

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

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!
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!

## Versioning
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/Sebiworld/PageAccessReleasetime/tags).
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/Sebiworld/PageAccessReleasetime/tags).

## License
This project is licensed under the Mozilla Public License Version 2.0 - see the [LICENSE.md](LICENSE.md) file for details.

0 comments on commit b0ba7d7

Please sign in to comment.