1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-24 15:23:11 +02:00

Add new $page->restorable() method to accompany the existing $page->trashable() method. This method returns true/false as to whether or not the page can be restored from the trash to its original location by the current user. Also add a PagePermissions::trashListable() method for determining whether the trash is viewable by the current user.

This commit is contained in:
Ryan Cramer
2018-06-28 12:53:56 -04:00
parent 54fad20ffd
commit 2883d2adb5

View File

@@ -104,7 +104,8 @@ class PagePermissions extends WireData implements Module {
$this->addHook('Page::listable', $this, 'listable');
$this->addHook('Page::deleteable', $this, 'deleteable');
$this->addHook('Page::deletable', $this, 'deleteable');
$this->addHook('Page::trashable', $this, 'trashable');
$this->addHook('Page::trashable', $this, 'trashable');
$this->addHook('Page::restorable', $this, 'restorable');
$this->addHook('Page::addable', $this, 'addable');
$this->addHook('Page::moveable', $this, 'moveable');
$this->addHook('Page::sortable', $this, 'sortable');
@@ -590,13 +591,62 @@ class PagePermissions extends WireData implements Module {
else if($page instanceof User && $user->hasPermission('user-admin')) $listable = true;
else if($page->hasStatus(Page::statusUnpublished) && !$page->editable()) $listable = false;
else if($page->process && !$this->processViewable($page)) $listable = false;
else if($page->isTrash()) $listable = false;
else if($page->isTrash()) $listable = $this->trashListable($page);
else if(($accessTemplate = $page->getAccessTemplate()) && $accessTemplate->guestSearchable) $listable = true;
else if(!$user->hasPermission("page-view", $page)) $listable = false;
$event->return = $listable;
}
/**
* Return whether or not given page in Trash is listable
*
* @param null $page Page, or specify null for a general "trash is listable" request
* @return bool
*
*/
public function trashListable($page = null) {
/** @var User $user */
static $showTrashUsers = array();
$user = $this->wire('user');
if(isset($showTrashUsers[$user->id])) {
$showTrash = $showTrashUsers[$user->id];
} else {
$petc = 'page-edit-trash-created';
if(!$this->wire('permissions')->has($petc)) $petc = false;
if($user->isSuperuser()) {
// superuser
$showTrash = true;
} else if($user->hasPermission('page-delete')) {
// has page-delete globally
$showTrash = true;
} else if($petc && $user->hasPermission($petc)) {
// has page-edit-trash-created globally
$showTrash = true;
} else if($user->hasPermission('page-delete', true)) {
// has page-delete added specifically at a template
$showTrash = true;
} else if($petc && $user->hasPermission($petc, true)) {
// has page-edit-trash-created added specifically at a template
$showTrash = true;
} else {
$showTrash = false;
}
$showTrashUsers[$user->id] = $showTrash;
}
if(!$showTrash) return false;
if($page === null || !$page->id) return $showTrash;
$trashPageID = $this->wire('config')->trashPageID;
if($page->id == $trashPageID) return $showTrash;
$listable = $this->pageEditable($page);
return $listable;
}
/**
* Is the page deleteable by the current user?
*
@@ -663,6 +713,35 @@ class PagePermissions extends WireData implements Module {
}
}
/**
* Is page restorable from trash?
*
* @param HookEvent $event
*
*/
public function restorable($event) {
$event->return = true;
/** @var Page $page */
$page = $event->object;
/** @var User $user */
$user = $this->wire('user');
$event->return = false;
if($page->isLocked()) return;
if(!$page->isTrash()) return;
if(!$user->isSuperuser() && !$page->editable()) return;
$parts = explode('.', $page->name, 4);
if(count($parts) < 3) return;
list($pageID, $parentID, $rest) = explode('.', $page->name);
list($sort, $name) = explode('_', $rest, 2);
if($pageID || $sort) {} // ignore
$parent = $this->wire('pages')->get((int) $parentID);
// check if parent does not allow this user to add pages here
if(!$parent->addable($page)) return;
// check if parent already has a page with the same name
if($parent->numChildren("name=$name, include=all")) return;
$event->return = true;
}
/**
* Can the current user add child pages to this page?
*