Skip to content

Instantly share code, notes, and snippets.

@adrianbj
Last active June 18, 2018 13:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianbj/e391e2e343c5620d0720 to your computer and use it in GitHub Desktop.
Save adrianbj/e391e2e343c5620d0720 to your computer and use it in GitHub Desktop.
Module to hide pages that are not editable by the logged in user.
<?php
/**
*
* Module to hide pages that are not editable by the logged in user.
*
* ProcessWire 2.x
* Copyright (C) 2010 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
* http://www.ryancramer.com
*
*/
class HideUneditablePages extends WireData implements Module {
public static function getModuleInfo() {
return array(
'title' => 'HideUneditablePages',
'version' => 1,
'summary' => 'Module to hide pages that are not editable by the logged in user.',
'href' => '',
'singular' => true,
'autoload' => true
);
}
public function init() {
// only add hook only if the render parameter is set
// (as used by ProcessPageList)
// or if superuser, also exit
if(!isset($_GET['render']) || $this->user->isSuperuser()) return;
$this->addHookAfter('ProcessPageList::execute', $this, 'pageListHiddenPages');
}
public function pageListHiddenPages(HookEvent $event){
// make sure it's an ajax request
if($this->config->ajax){
// manipulate the json returned and remove any pages found from array
$json = json_decode($event->return, true);
foreach($json['children'] as $key => $child){
$c = $this->pages->get($child['id']);
$pagetemplate = $c->template;
$uroles = $this->user->roles->implode(',', 'id');
$uroles = explode(',', $uroles);
if(is_array($pagetemplate->editRoles) || is_array($pagetemplate->addRoles)) {
if(!array_intersect($uroles, $pagetemplate->editRoles) && !array_intersect($uroles, $pagetemplate->addRoles)) unset($json['children'][$key]);
}
}
$json['children'] = array_values($json['children']);
$event->return = json_encode($json);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment