MDL-35964 blocks: prevent undeletable blocks being deleted!

This commit simplifies the logic, so that $CFG->undeletableblocks
applies to all blocks, not just ones on certain page types in the
system context.

It also makes the setting apply to adding blocks, so you cannot
accidentally add a block that you cannot delete.
This commit is contained in:
Tim Hunt
2012-10-12 20:36:02 +01:00
parent ccd90e765e
commit 1c76fc96d1
2 changed files with 35 additions and 21 deletions
+31 -19
View File
@@ -224,12 +224,13 @@ class block_manager {
return $this->addableblocks;
}
$unaddableblocks = self::get_undeletable_block_types();
$pageformat = $this->page->pagetype;
foreach($allblocks as $block) {
if (!$bi = block_instance($block->name)) {
continue;
}
if ($block->visible &&
if ($block->visible && !in_array($block->name, $unaddableblocks) &&
($bi->instance_allow_multiple() || !$this->is_block_present($block->name)) &&
blocks_name_allowed_in_format($block->name, $pageformat) &&
$bi->user_can_addto($this->page)) {
@@ -373,6 +374,19 @@ class block_manager {
return $this->allblocks;
}
/**
* @return array names of block types that cannot be added or deleted. E.g. array('navigation','settings').
*/
public static function get_undeletable_block_types() {
if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) {
return array('navigation','settings');
} else if (is_string($CFG->undeletableblocktypes)) {
return explode(',', $CFG->undeletableblocktypes);
} else {
return $CFG->undeletableblocktypes;
}
}
/// Setter methods =============================================================
/**
@@ -798,7 +812,7 @@ class block_manager {
* Find a given block by its instance id
*
* @param integer $instanceid
* @return object
* @return block_base
*/
public function find_instance($instanceid) {
foreach ($this->regions as $region => $notused) {
@@ -1007,14 +1021,6 @@ class block_manager {
public function edit_controls($block) {
global $CFG;
if (!isset($CFG->undeletableblocktypes) || (!is_array($CFG->undeletableblocktypes) && !is_string($CFG->undeletableblocktypes))) {
$undeletableblocktypes = array('navigation','settings');
} else if (is_string($CFG->undeletableblocktypes)) {
$undeletableblocktypes = explode(',', $CFG->undeletableblocktypes);
} else {
$undeletableblocktypes = $CFG->undeletableblocktypes;
}
$controls = array();
$actionurl = $this->page->url->out(false, array('sesskey'=> sesskey()));
@@ -1030,14 +1036,10 @@ class block_manager {
'icon' => 't/edit', 'caption' => get_string('configuration'), 'class' => 'editing_edit');
}
if ($this->page->user_can_edit_blocks() && $block->user_can_edit() && $block->user_can_addto($this->page)) {
if (!in_array($block->instance->blockname, $undeletableblocktypes)
|| !in_array($block->instance->pagetypepattern, array('*', 'site-index'))
|| $block->instance->parentcontextid != SITEID) {
// Delete icon.
$controls[] = array('url' => $actionurl . '&bui_deleteid=' . $block->instance->id,
'icon' => 't/delete', 'caption' => get_string('delete'), 'class' => 'editing_delete');
}
if ($this->user_can_delete_block($block)) {
// Delete icon.
$controls[] = array('url' => $actionurl . '&bui_deleteid=' . $block->instance->id,
'icon' => 't/delete', 'caption' => get_string('delete'), 'class' => 'editing_delete');
}
if ($this->page->user_can_edit_blocks() && $block->instance_can_be_hidden()) {
@@ -1067,6 +1069,16 @@ class block_manager {
return $controls;
}
/**
* @param block_base $block a block that appears on this page.
* @return boolean boolean whether the currently logged in user is allowed to delete this block.
*/
protected function user_can_delete_block($block) {
return $this->page->user_can_edit_blocks() && $block->user_can_edit() &&
$block->user_can_addto($this->page) &&
!in_array($block->instance->blockname, self::get_undeletable_block_types());
}
/**
* Process any block actions that were specified in the URL.
*
@@ -1125,7 +1137,7 @@ class block_manager {
require_sesskey();
$block = $this->page->blocks->find_instance($blockid);
if (!$block->user_can_edit() || !$this->page->user_can_edit_blocks() || !$block->user_can_addto($this->page)) {
if (!$this->user_can_delete_block($block)) {
throw new moodle_exception('nopermissions', '', $this->page->url->out(), get_string('deleteablock'));
}