MDL-68936 core_contentbank: Checking manage permissions to allow editing

This commit is contained in:
Amaia Anabitarte
2020-06-08 16:40:10 +02:00
parent bbb7dfc62e
commit 0633e60a91
4 changed files with 17 additions and 8 deletions
+11 -3
View File
@@ -330,10 +330,13 @@ abstract class contenttype {
/**
* Returns whether or not the user has permission to use the editor.
* This function will be called with the content to be edited as parameter,
* or null when is checking permission to create a new content using the editor.
*
* @param content $content The content to be edited or null when creating a new content.
* @return bool True if the user can edit content. False otherwise.
*/
final public function can_edit(): bool {
final public function can_edit(?content $content = null): bool {
if (!$this->is_feature_supported(self::CAN_EDIT)) {
return false;
}
@@ -342,19 +345,24 @@ abstract class contenttype {
return false;
}
if (!is_null($content) && !$this->can_manage($content)) {
return false;
}
$classname = 'contenttype/'.$this->get_plugin_name();
$editioncap = $classname.':useeditor';
$hascapabilities = has_all_capabilities(['moodle/contentbank:useeditor', $editioncap], $this->context);
return $hascapabilities && $this->is_edit_allowed();
return $hascapabilities && $this->is_edit_allowed($content);
}
/**
* Returns plugin allows edition.
*
* @param content $content The content to be edited.
* @return bool True if plugin allows edition. False otherwise.
*/
protected function is_edit_allowed(): bool {
protected function is_edit_allowed(?content $content): bool {
// Plugins can overwrite this function to add any check they need.
return true;
}
+1 -1
View File
@@ -75,7 +75,7 @@ class viewcontent implements renderable, templatable {
$data->contenthtml = $contenthtml;
// Check if the user can edit this content type.
if ($this->contenttype->can_edit()) {
if ($this->contenttype->can_edit($this->content)) {
$data->usercanedit = true;
$urlparams = [
'contextid' => $this->content->get_contextid(),
+4 -3
View File
@@ -45,6 +45,7 @@ if (!empty($id)) {
} else {
$contenttypename = "contenttype_$pluginname";
$heading = get_string('addinganew', 'moodle', get_string('description', $contenttypename));
$content = null;
}
// Check plugin is enabled.
@@ -61,9 +62,9 @@ if (class_exists($contenttypeclass)) {
print_error('unsupported', 'core_contentbank', $returnurl);
}
// Checks the user can edit this content type.
if (!$contenttype->can_edit()) {
print_error('contenttypenoedit', 'core_contentbank', $returnurl, $contenttype->get_plugin_name());
// Checks the user can edit this content and content type.
if (!$contenttype->can_edit($content)) {
print_error('contenttypenoedit', 'core_contentbank', $returnurl);
}
$values = [
+1 -1
View File
@@ -32,7 +32,7 @@ $string['contentnotrenamed'] = 'An error was encountered while trying to rename
$string['contentrenamed'] = 'The content has been renamed.';
$string['contentsmoved'] = 'Content bank contents moved to {$a}.';
$string['contenttypenoaccess'] = 'You cannot view this {$a} instance.';
$string['contenttypenoedit'] = 'You cannot edit contents of the {$a} content type.';
$string['contenttypenoedit'] = 'You can not edit this content';
$string['eventcontentcreated'] = 'Content created';
$string['eventcontentdeleted'] = 'Content deleted';
$string['eventcontentupdated'] = 'Content updated';