Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.