Skip to content

Commit

Permalink
MDL-68936 core_contentbank: Checking manage permissions to allow editing
Browse files Browse the repository at this point in the history
  • Loading branch information
Amaia Anabitarte committed Jun 8, 2020
1 parent bbb7dfc commit 0633e60
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
14 changes: 11 additions & 3 deletions contentbank/classes/contenttype.php
Expand Up @@ -330,10 +330,13 @@ protected function is_manage_allowed(content $content): bool {

/**
* 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;
}
Expand All @@ -342,19 +345,24 @@ final public function can_edit(): bool {
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;
}
Expand Down
2 changes: 1 addition & 1 deletion contentbank/classes/output/viewcontent.php
Expand Up @@ -75,7 +75,7 @@ public function export_for_template(renderer_base $output): stdClass {
$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(),
Expand Down
7 changes: 4 additions & 3 deletions contentbank/edit.php
Expand Up @@ -45,6 +45,7 @@
} else {
$contenttypename = "contenttype_$pluginname";
$heading = get_string('addinganew', 'moodle', get_string('description', $contenttypename));
$content = null;
}

// Check plugin is enabled.
Expand All @@ -61,9 +62,9 @@
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 = [
Expand Down
2 changes: 1 addition & 1 deletion lang/en/contentbank.php
Expand Up @@ -32,7 +32,7 @@
$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';
Expand Down

0 comments on commit 0633e60

Please sign in to comment.