Created
August 26, 2015 14:59
-
-
Save Da-Fecto/1caab8226d764a7c3d06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Module from ryan.... | |
class CustomPageRoles extends WireData implements Module { | |
/** | |
* Provide information about this module to ProcessWire | |
* | |
*/ | |
public static function getModuleInfo() { | |
return array( | |
'title' => 'Custom Page Roles', | |
'summary' => 'Control viewable roles at the page level.', | |
'version' => 002, | |
'permanent' => false, | |
'autoload' => true, | |
'singular' => true, | |
); | |
} | |
/** | |
* Add the hook | |
* | |
*/ | |
public function init() { | |
$this->addHookAfter("Page::viewable", $this, 'viewable'); | |
} | |
/** | |
* Hook called after Page::viewable is called | |
* | |
*/ | |
public function viewable(HookEvent $event) { | |
// get the vars we need to check access | |
$page = $event->object; | |
$user = $this->user; | |
// no need to check anything if it's the superuser | |
if($user->isSuperuser()) return; | |
// don't allow this access control on system templates | |
if($page->template->flags & Template::flagSystem) return; | |
// get the roles we'll be comparing | |
$pageRoles = $page->page_roles; | |
$userRoles = $this->user->roles; | |
// if page doesn't have a 'page_roles' field, then return control to template | |
if(is_null($pageRoles) || (!$pageRoles instanceof PageArray)) return; | |
// if page has a 'page_roles' field, but none defined, then inherit 'viewable' state from template or parent | |
if(!count($pageRoles)) { | |
// if template disallows access give it control. | |
if(!$event->return) return; | |
// if there is no parent, give the template control. | |
if(!$page->parent->id) return; | |
// if the parent has no page_roles field, then give the template control. | |
if(is_null($page->parent->page_roles)) return; | |
// otherwise determine access from page's parent, recursively | |
$event->return = $page->parent->viewable(); | |
return; | |
} | |
// we assume it's not viewable until we can prove otherwise | |
$viewable = false; | |
// loop through the pageRoles and try to match up to user | |
// we don't need to check permissions in the role because | |
// all roles are assumed to have page-view permission. | |
foreach($pageRoles as $role) { | |
if($role->name == 'guest' || $user->hasRole($role)) { | |
// guest role was found or the user has the required role | |
$viewable = true; | |
break; | |
} | |
} | |
$event->return = $viewable; | |
} | |
/** | |
* Install the module | |
* | |
*/ | |
public function ___install() { | |
if($this->fields->get('page_roles')) { | |
$this->error("You already have a 'page_roles' field."); | |
return; | |
} | |
$field = new Field(); | |
$field->type = $this->modules->get("FieldtypePage"); | |
$field->name = 'page_roles'; | |
$field->label = 'Roles that can view this page'; | |
$field->description = | |
"Check the roles that may view this page. At least one role must be checked (like one of your roles or superuser), " . | |
"otherwise this page does not define access. When access is not defined here, it is inherited from the template " . | |
"or parent(s). If this page's template allows 'view' access to the user, then it will check the parent(s). " . | |
"Access will inherit from parents that also have a custom page roles field. "; | |
$field->derefAsPage = 0; | |
$field->parent_id = $this->config->rolesPageID; | |
$field->labelFieldName = 'name'; | |
$field->inputfield = 'InputfieldCheckboxes'; | |
$field->save(); | |
$this->message("Added field 'page_roles'. Add this field to any templates where you want to control view access."); | |
} | |
/** | |
* Uninstall the module | |
* | |
*/ | |
public function ___uninstall() { | |
$this->message("To complete uninstall, remove the 'page_roles' field from all templates and then delete the 'page_roles' field."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment