Skip to content

Commit

Permalink
Merge pull request #1 from netcarver/master
Browse files Browse the repository at this point in the history
Add support for checking permission of page's parents.
  • Loading branch information
ryancramerdesign committed Jan 24, 2013
2 parents 730a433 + e7a72a7 commit 800a0f7
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions PageEditPerUser.module
Expand Up @@ -3,7 +3,7 @@
/**
* Page Edit Per User
*
* Assign edit access to users on a per-page basis.
* Assign edit access to users on a per-page or per-branch basis.
*
* The user must already have page-edit permission on one of their roles in order to get
* edit access to assigned pages. Otherwise, they will only gain view access.
Expand All @@ -16,13 +16,13 @@
*
*/

class PageEditPerUser extends WireData implements Module {
class PageEditPerUser extends WireData implements Module, ConfigurableModule {

public static function getModuleInfo() {
return array(
'title' => 'Page Edit Per User',
'version' => 1,
'summary' => 'Assign edit access to users on a per-page basis.',
'version' => 2,
'summary' => 'Assign edit access to users on a per-page or per-branch basis.',
'singular' => true,
'autoload' => true,
);
Expand All @@ -37,13 +37,36 @@ class PageEditPerUser extends WireData implements Module {
$this->addHookAfter('Page::viewable', $this, 'hookPageEditable');
}

/**
* Check if this page, or any ancestor pages, are editable
*
*/
public function onMyBranch($page)
{
$page_on_my_branch = $this->user->editable_pages->has($page);

if ($this->scan_ancestors && !$page_on_my_branch) {
$parents = $page->parents();
while (!$page_on_my_branch && count($parents)) {
$p = $parents->pop();
$page_on_my_branch = $this->user->editable_pages->has($p);
}
}
return $page_on_my_branch;
}

/**
* Page::editable hook
*
*/
public function hookPageEditable($event) {
if($event->return) return;
$event->return = $this->user->hasPermission('page-edit') && $this->user->editable_pages->has($event->object);
if($event->return) return;

if ($this->user->hasPermission('page-edit')) {
$event->return = $this->onMyBranch($event->object);
} else {
$event->return = false;
}
}

/**
Expand All @@ -52,7 +75,7 @@ class PageEditPerUser extends WireData implements Module {
*/
public function hookPageViewable($event) {
if($event->return) return;
$event->return = $this->user->editable_pages->has($event->object);
$event->return = $this->onMyBranch($event->object);
}

/**
Expand Down Expand Up @@ -91,7 +114,33 @@ class PageEditPerUser extends WireData implements Module {
}
$this->fields->delete($field);
$this->message("Removed field: editable_pages");
}
}

/**
* Default settings used by this module
*/
static protected $defaultSettings = array(
'scan_ancestors' => false,
);

/**
* Build a form allowing configuration of this Module
*/
static public function getModuleConfigInputfields(array $data) {

$fields = new InputfieldWrapper();
$data = array_merge(self::$defaultSettings, $data);

// Scan ancestor nodes for edit permission?
$f = wire('modules')->get('InputfieldRadios');
$f->attr('name', 'scan_ancestors');
$f->label = __('Consider permissions further up the branch too?', __FILE__);
$f->addOption(false, __('No', __FILE__));
$f->addOption(true, __('Yes', __FILE__));
$f->attr('value', $data['scan_ancestors']);
$fields->add($f);

return $fields;
}
}

0 comments on commit 800a0f7

Please sign in to comment.