mirror of
https://github.com/processwire/processwire.git
synced 2025-08-13 02:04:35 +02:00
Fix issue processwire/processwire-issues#352 where deleted role could interfere with the "who can access this page" field in the page editor
This commit is contained in:
@@ -600,12 +600,11 @@ class PagesType extends Wire implements \IteratorAggregate, \Countable {
|
||||
public function ___deleteReady(Page $page) { }
|
||||
|
||||
/**
|
||||
* Hook called when a page and it's data have been deleted
|
||||
* Hook called when a page and its data have been deleted
|
||||
*
|
||||
* #pw-internal
|
||||
*
|
||||
* @param Page $page
|
||||
* @deprecated
|
||||
*
|
||||
*/
|
||||
public function ___deleted(Page $page) { }
|
||||
|
@@ -105,4 +105,23 @@ class Roles extends PagesType {
|
||||
$page->permissions->add($this->wire('permissions')->get("name=page-view"));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook called when a page and its data have been deleted
|
||||
*
|
||||
* #pw-internal
|
||||
*
|
||||
* @param Page $page
|
||||
*
|
||||
*/
|
||||
public function ___deleted(Page $page) {
|
||||
foreach($this->wire('templates') as $template) {
|
||||
/** @var Template $template */
|
||||
if(!$template->useRoles) continue;
|
||||
$template->removeRole($page, 'all');
|
||||
if($template->isChanged()) $template->save();
|
||||
}
|
||||
|
||||
parent::___deleted($page);
|
||||
}
|
||||
}
|
||||
|
@@ -308,14 +308,18 @@ class Template extends WireData implements Saveable, Exportable {
|
||||
|
||||
if(strpos($type, 'page-') === 0) $type = str_replace('page-', '', $type);
|
||||
|
||||
if($type != 'view') {
|
||||
$roles = $this->wire('pages')->newPageArray();
|
||||
if($type !== 'view') {
|
||||
$roleIDs = null;
|
||||
if($type == 'edit') $roleIDs = $this->editRoles;
|
||||
else if($type == 'create') $roleIDs = $this->createRoles;
|
||||
else if($type == 'add') $roleIDs = $this->addRoles;
|
||||
else throw new WireException("Unknown roles type: $type");
|
||||
if(empty($roleIDs)) return $roles;
|
||||
if($type === 'edit') {
|
||||
$roleIDs = $this->editRoles;
|
||||
} else if($type === 'create') {
|
||||
$roleIDs = $this->createRoles;
|
||||
} else if($type === 'add') {
|
||||
$roleIDs = $this->addRoles;
|
||||
} else {
|
||||
throw new WireException("Unknown roles type: $type");
|
||||
}
|
||||
if(empty($roleIDs)) return $this->wire('pages')->newPageArray();
|
||||
return $this->wire('pages')->getById($roleIDs);
|
||||
}
|
||||
|
||||
@@ -414,17 +418,22 @@ class Template extends WireData implements Saveable, Exportable {
|
||||
*
|
||||
*/
|
||||
public function setRoles($value, $type = 'view') {
|
||||
if($type == 'view' || $type == 'page-view') {
|
||||
if($type === 'view' || $type === 'page-view') {
|
||||
if(is_array($value) || $value instanceof PageArray) {
|
||||
$this->_roles = $value;
|
||||
}
|
||||
} else if(WireArray::iterable($value)) {
|
||||
$roleIDs = array();
|
||||
foreach($value as $v) {
|
||||
if(is_int($v)) $id = $v;
|
||||
else if(is_string($v) && ctype_digit($v)) $id = (int) $v;
|
||||
else if($v instanceof Page) $id = $v->id;
|
||||
else continue;
|
||||
if(is_int($v)) {
|
||||
$id = $v;
|
||||
} else if(is_string($v) && ctype_digit($v)) {
|
||||
$id = (int) $v;
|
||||
} else if($v instanceof Page) {
|
||||
$id = $v->id;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
$roleIDs[] = $id;
|
||||
}
|
||||
if($type == 'edit' || $type == 'page-edit') {
|
||||
@@ -464,18 +473,40 @@ class Template extends WireData implements Saveable, Exportable {
|
||||
*
|
||||
* @param Role|int|string $role Role instance, id or name
|
||||
* @param string $type Type of role being added, one of: view, edit, create, add. (default=view)
|
||||
* You may also specify “all” to remove the role entirely from all possible usages in the template.
|
||||
* @return $this
|
||||
* @throws WireException If given $role cannot be resolved
|
||||
*
|
||||
*/
|
||||
public function removeRole($role, $type = 'view') {
|
||||
if(is_int($role) || is_string($role)) $role = $this->wire('roles')->get($role);
|
||||
if(!$role instanceof Role) throw new WireException("removeRole requires Role instance, name or id");
|
||||
$roles = $this->getRoles($type);
|
||||
|
||||
if(is_int($role) || is_string($role)) {
|
||||
$role = $this->wire('roles')->get($role);
|
||||
}
|
||||
|
||||
if(!$role instanceof Role) {
|
||||
throw new WireException("removeRole requires Role instance, name or id");
|
||||
}
|
||||
|
||||
if($type == 'all') {
|
||||
$types = array('create', 'add', 'edit', 'view');
|
||||
$rolesPermissions = $this->rolesPermissions;
|
||||
if(isset($rolesPermissions["$role->id"])) {
|
||||
unset($rolesPermissions["$role->id"]);
|
||||
$this->rolesPermissions = $rolesPermissions;
|
||||
}
|
||||
} else {
|
||||
$types = array($type);
|
||||
}
|
||||
|
||||
foreach($types as $t) {
|
||||
$roles = $this->getRoles($t);
|
||||
if($roles->has($role)) {
|
||||
$roles->remove($role);
|
||||
$this->setRoles($roles, $type);
|
||||
$this->setRoles($roles, $t);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@@ -1347,8 +1347,9 @@ class ProcessPageEdit extends Process implements WirePageEditor, ConfigurableMod
|
||||
if(count($addRoles)) {
|
||||
foreach($addRoles as $roleID) {
|
||||
$role = $this->wire('roles')->get($roleID);
|
||||
if(!$role->id) continue;
|
||||
if(!$role->hasPermission("page-add", $this->page)) continue;
|
||||
if($role->id) $table->row(array($role->name, $addLabel));
|
||||
$table->row(array($role->name, $addLabel));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user