Skip to content

Instantly share code, notes, and snippets.

@somatonic
Last active October 21, 2015 10:03
Show Gist options
  • Save somatonic/058fe23c247eac4bbc8b to your computer and use it in GitHub Desktop.
Save somatonic/058fe23c247eac4bbc8b to your computer and use it in GitHub Desktop.
Example page permission hooks. Lister selector hooks
<?php
/**
* Site Helpers
*
*
*/
class SiteHelpers extends WireData implements Module {
/**
* getModuleInfo is a method required by all modules to tell ProcessWire about them
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => 'Site Helpers',
'version' => 100,
'summary' => 'Sitewide Helper Functions and Setup, Configs',
'href' => '',
'author' => 'Philipp Urlich, update AG',
'singular' => true,
'autoload' => true,
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
// custom workspace to edit and add pages via page select on role page
// if(!$this->user->isSuperuser()) $this->addHookAfter("Page::viewable", $this, 'hookPermission');
if(!$this->user->isSuperuser()) $this->addHookAfter("Page::editable", $this, 'hookPermission');
if(!$this->user->isSuperuser()) $this->addHookAfter("Page::addable", $this, 'hookPermission');
if(!$this->user->isSuperuser()) $this->addHookAfter("ProcessPageLister::getSelector", $this, 'hookListerSelector');
}
public function hookListerSelector(HookEvent $event){
$selector = $event->return;
$lister = $event->object;
$listerPage = wire("page")->name;
$userCompany = wire("user")->select_company;
$event->return .= ",(select_company=$userCompany),(parent.select_company=$userCompany)";
wire("log")->save("selector", $event->return);
}
/**
* when API and page to render is fully ready
*/
public function ready() {
}
public function renderAdminShortcuts() {
$page = $this->wire('page');
// if($page->name != 'page' || $this->wire('input')->urlSegment1) return '';
$user = $this->wire('user');
if($this->wire('user')->isGuest() || !$user->hasPermission('page-edit')) return '';
$module = $this->wire('modules')->getModule('ProcessPageAdd', array('noInit' => true));
$data = $module->executeNavJSON(array('getArray' => true));
$items = array();
foreach($data['list'] as $item) {
$items[] = "<li><a href='$data[url]$item[url]'><i class='fa fa-fw fa-$item[icon]'></i>&nbsp;$item[label]</a></li>";
}
if(!count($items)) return '';
$out = implode('', $items);
$label = $this->getAddNewLabel();
$out =
"<div id='head_button_dashboard'>" .
"<button class='ui-button dropdown-toggle'><i class='fa fa-angle-down'></i> $label</button>" .
"<ul class='dropdown-menu shortcuts' data-at='right bottom+1'>$out</ul>" .
"</div>";
return $out;
}
public function getAddNewLabel() {
return $this->_('Add New');
}
public function getUniquePageName(Page $page, $langID, $pageName, $ext) {
$numChildren = $page->parent->numChildren();
$n = 0;
do {
$name = $pageName;
if($n > 0) $name .= "-" . ($numChildren+$n);
$nameWithExt = $name . $ext;
$child = $page->parent->child("id!={$page->id}, name$langID=$nameWithExt, include=all"); // see if another page already has the same name
$n++;
} while($child->id);
return $nameWithExt;
}
public function renderPagerInfos($entries) {
$total = $entries->getTotal();
$limit = $entries->getLimit();
$start = $limit * (wire("input")->pageNum - 1) + 1;
$end = $start + $limit - 1;
// if total is less than limit
if($total < $limit) $end = $total;
$out = sprintf($this->_('Einträge %1$d – %2$d von %3$d'), $start, $end, $total);
return $out;
}
protected function hookPermission(HookEvent $event){
// if already has no edit access return
if(!$event->return) return;
$hasPermission = false;
$page = $event->object;
// don't change acces for user-admins
// if(($page->template->name == "user" || $page->template->name == "admin") && wire("user")->hasPermission("user-admin")){
// // $event->return = true;
// return;
// }
if($page->template->name !== "event" && $page->template->name !== "cast") return;
// $parents = $page->parents;
if(wire("user")->select_company === $page->select_company || wire("user")->select_company === $page->parent->select_company){
$hasPermission = true;
}
// remove or add permission to hide pages
// if(!$hasPermission) $this->roles->get("editor")->removePermission("page-hidden");
// if($hasPermission) $this->roles->get("editor_vsjf")->addPermission("page-hidden");
// if($hasPermission) $this->roles->get("editor_vsjf")->addPermission("page-action-delete");
$event->return = $hasPermission;
}
}
<?php
/**
* Site Helpers
*
*
*/
class SiteHelpers extends WireData implements Module {
/**
* getModuleInfo is a method required by all modules to tell ProcessWire about them
* @return array
*/
public static function getModuleInfo() {
return array(
'title' => 'Site Helpers',
'version' => 1,
'summary' => 'Sitewide Helper Functions and Setup, Configs',
'href' => '',
'author' => 'Philipp Urlich, update AG',
'singular' => true,
'autoload' => true,
);
}
/**
* Initialize the module
*
* ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
* when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
*
*/
public function init() {
// custom workspace to edit and add pages via page select on role page
// if(!$this->user->isSuperuser()) $this->addHookAfter("Page::viewable", $this, 'hookPermission');
if(!$this->user->isSuperuser()) $this->addHookAfter("Page::editable", $this, 'hookPermission');
if(!$this->user->isSuperuser()) $this->addHookAfter("Page::addable", $this, 'hookPermission');
if(!$this->user->isSuperuser()) $this->addHookAfter("ProcessPageLister::getSelector", $this, 'hookListerSelector');
}
public function hookListerSelector(HookEvent $event){
// only for a certain role?
if(!wire("user")->hasRole("editor-events")) return;
$selector = $event->return;
$lister = $event->object;
$listerPage = wire("page")->name;
$userCompany = wire("user")->select_company;
// add or replace the selector of lister to find the results
$event->return .= ",(select_company=$userCompany),(parent.select_company=$userCompany)";
// debug
//wire("log")->save("selector", $event->return);
}
/**
* when API and page to render is fully ready
*/
public function ready() {
}
protected function hookPermission(HookEvent $event){
// if already has no edit access return
if(!$event->return) return;
// don't change access for an certain role or permission?
// if(wire("user")->hasPermission("editor")){
// return;
// }
// start with no access
$hasPermission = false;
$page = $event->object;
if($page->is("template!=event|cast")) return;
// $parents = $page->parents;
if(wire("user")->select_company === $page->select_company || wire("user")->select_company === $page->parent->select_company){
$hasPermission = true;
}
// remove or add permission to role, not sure if this is good practice but works so far
// if(!$hasPermission) wire("roles")->get("editor")->removePermission("page-hidden");
// if($hasPermission) wire("roles")->get("editor_vsjf")->addPermission("page-hidden");
// if($hasPermission) wire("roles")->get("editor_vsjf")->addPermission("page-action-delete");
$event->return = $hasPermission;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment