Skip to content

Commit

Permalink
MDL-51645 tool_lp: Rename the plan capabilities
Browse files Browse the repository at this point in the history
  • Loading branch information
gauts authored and Frederic Massart committed Apr 18, 2016
1 parent 3a4c71d commit 53084ab
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 45 deletions.
34 changes: 17 additions & 17 deletions admin/tool/lp/classes/api.php
Expand Up @@ -1207,13 +1207,13 @@ public static function list_user_plans($userid) {

// We can allow guest user to pass they will not have LP.
if ($USER->id != $userid) {
require_capability('tool/lp:planviewall', $context);
require_capability('tool/lp:planview', $context);
} else {
require_capability('tool/lp:planviewown', $context);
}

// Users that can manage plans can only see active and completed plans.
if (!has_any_capability(array('tool/lp:planmanageall', 'tool/lp:planmanageown', 'tool/lp:plancreatedraft'), $context)) {
if (!has_any_capability(array('tool/lp:planmanage', 'tool/lp:planmanageown', 'tool/lp:plancreateown'), $context)) {
$select = ' AND status != :statusdraft';
$params['statusdraft'] = plan::STATUS_DRAFT;
}
Expand All @@ -1232,16 +1232,16 @@ public static function create_plan(stdClass $record) {

$context = context_user::instance($record->userid);

$manageplans = has_capability('tool/lp:planmanageall', $context);
$createdraft = has_capability('tool/lp:plancreatedraft', $context);
$manageplans = has_capability('tool/lp:planmanage', $context);
$createdraft = has_capability('tool/lp:plancreateown', $context);
$manageownplan = has_capability('tool/lp:planmanageown', $context);

// Any of them is enough.
if ($USER->id == $record->userid && !$manageplans && !$createdraft && !$manageownplan) {
// Exception about plancreatedraft as it is the one that is closer to basic users.
throw new required_capability_exception($context, 'tool/lp:plancreatedraft', 'nopermissions', '');
// Exception about plancreateown as it is the one that is closer to basic users.
throw new required_capability_exception($context, 'tool/lp:plancreateown', 'nopermissions', '');
} else if ($USER->id != $record->userid && !$manageplans) {
throw new required_capability_exception($context, 'tool/lp:planmanageall', 'nopermissions', '');
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
}

if (!isset($record->status)) {
Expand All @@ -1268,15 +1268,15 @@ public static function update_plan(stdClass $record) {

$context = context_user::instance($record->userid);

$manageplans = has_capability('tool/lp:planmanageall', $context);
$createdraft = has_capability('tool/lp:plancreatedraft', $context);
$manageplans = has_capability('tool/lp:planmanage', $context);
$createdraft = has_capability('tool/lp:plancreateown', $context);
$manageownplan = has_capability('tool/lp:planmanageown', $context);

// Any of them is enough.
if ($USER->id == $record->userid && !$manageplans && !$createdraft && !$manageownplan) {
throw new required_capability_exception($context, 'tool/lp:planmanageown', 'nopermissions', '');
} else if (!$manageplans) {
throw new required_capability_exception($context, 'tool/lp:planmanageall', 'nopermissions', '');
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
}

$plan = new plan($record->id);
Expand All @@ -1286,7 +1286,7 @@ public static function update_plan(stdClass $record) {
if (!$manageplans && !$manageownplan && $USER->id != $plan->get_usermodified()) {
throw new \moodle_exception('erroreditingmodifiedplan', 'tool_lp');
} else if (!$manageplans && $USER->id != $plan->get_userid()) {
throw new required_capability_exception($context, 'tool/lp:planmanageall', 'nopermissions', '');
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
}

// If the user can only create drafts we don't allow them to set other status.
Expand All @@ -1313,14 +1313,14 @@ public static function read_plan($id) {
if ($USER->id == $plan->get_userid()) {
require_capability('tool/lp:planviewown', $context);
} else {
require_capability('tool/lp:planviewall', $context);
require_capability('tool/lp:planview', $context);
}

// We require any of these capabilities to retrieve draft plans.
if ($plan->get_status() == plan::STATUS_DRAFT &&
!has_any_capability(array('tool/lp:planmanageown', 'tool/lp:planmanageall', 'tool/lp:plancreatedraft'), $context)) {
// Exception about plancreatedraft as it is the one that is closer to basic users.
throw new required_capability_exception($context, 'tool/lp:plancreatedraft', 'nopermissions', '');
!has_any_capability(array('tool/lp:planmanageown', 'tool/lp:planmanage', 'tool/lp:plancreateown'), $context)) {
// Exception about plancreateown as it is the one that is closer to basic users.
throw new required_capability_exception($context, 'tool/lp:plancreateown', 'nopermissions', '');
}
return $plan;
}
Expand All @@ -1338,7 +1338,7 @@ public static function delete_plan($id) {

$context = context_user::instance($plan->get_userid());

$manageplans = has_capability('tool/lp:planmanageall', $context);
$manageplans = has_capability('tool/lp:planmanage', $context);
$manageownplan = has_capability('tool/lp:planmanageown', $context);

if ($USER->id == $plan->get_userid() && $USER->id != $plan->get_usermodified() &&
Expand All @@ -1347,7 +1347,7 @@ public static function delete_plan($id) {
throw new required_capability_exception($context, 'tool/lp:planmanageown', 'nopermissions', '');
} else if ($USER->id != $plan->get_userid() && !$manageplans) {
// Other users needs to have tool/lp:planmanage.
throw new required_capability_exception($context, 'tool/lp:planmanageall', 'nopermissions', '');
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
}

return $plan->delete();
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/lp/classes/plan.php
Expand Up @@ -101,7 +101,7 @@ public function can_update() {
$context = context_user::instance($userid);

// Not all users can edit all plans, the template should know about it.
if (has_capability('tool/lp:planmanageall', $context) ||
if (has_capability('tool/lp:planmanage', $context) ||
has_capability('tool/lp:planmanageown', $context)) {
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions admin/tool/lp/db/access.php
Expand Up @@ -60,14 +60,14 @@
),
'clonepermissionsfrom' => 'moodle/block:view'
),
'tool/lp:plancreatedraft' => array(
'tool/lp:plancreateown' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'contextlevel' => CONTEXT_USER,
'archetypes' => array(
),
'clonepermissionsfrom' => 'moodle/site:config'
),
'tool/lp:planmanageall' => array(
'tool/lp:planmanage' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
Expand All @@ -81,7 +81,7 @@
),
'clonepermissionsfrom' => 'moodle/site:config'
),
'tool/lp:planviewall' => array(
'tool/lp:planview' => array(
'captype' => 'read',
'contextlevel' => CONTEXT_USER,
'archetypes' => array(
Expand Down
6 changes: 3 additions & 3 deletions admin/tool/lp/db/services.php
Expand Up @@ -346,15 +346,15 @@
'classpath' => '',
'description' => 'Creates a learning plan.',
'type' => 'write',
'capabilities' => 'tool/lp:planmanageall',
'capabilities' => 'tool/lp:planmanage',
),
'tool_lp_update_plan' => array(
'classname' => 'tool_lp\external',
'methodname' => 'update_plan',
'classpath' => '',
'description' => 'Updates a learning plan.',
'type' => 'write',
'capabilities' => 'tool/lp:planmanageall',
'capabilities' => 'tool/lp:planmanage',
),
'tool_lp_read_plan' => array(
'classname' => 'tool_lp\external',
Expand All @@ -370,7 +370,7 @@
'classpath' => '',
'description' => 'Delete a learning plan.',
'type' => 'write',
'capabilities' => 'tool/lp:planmanageall',
'capabilities' => 'tool/lp:planmanage',
),
'tool_lp_data_for_plans_page' => array(
'classname' => 'tool_lp\external',
Expand Down
6 changes: 3 additions & 3 deletions admin/tool/lp/editplan.php
Expand Up @@ -55,12 +55,12 @@
$PAGE->set_heading($pagetitle);
$output = $PAGE->get_renderer('tool_lp');

$manageplans = has_capability('tool/lp:planmanageall', $context);
$owncapabilities = array('tool/lp:plancreatedraft', 'tool/lp:planmanageown');
$manageplans = has_capability('tool/lp:planmanage', $context);
$owncapabilities = array('tool/lp:plancreateown', 'tool/lp:planmanageown');
if ($USER->id === $userid && !has_any_capability($owncapabilities, $context) && !$manageplans) {
throw new required_capability_exception($context, 'tool/lp:planmanageown', 'nopermissions', '');
} else if (!$manageplans) {
throw new required_capability_exception($context, 'tool/lp:planmanageall', 'nopermissions', '');
throw new required_capability_exception($context, 'tool/lp:planmanage', 'nopermissions', '');
}

// Passing the templates list to the form.
Expand Down
6 changes: 3 additions & 3 deletions admin/tool/lp/lang/en/tool_lp.php
Expand Up @@ -84,10 +84,10 @@
$string['lp:competencyread'] = 'View competency frameworks';
$string['lp:coursecompetencymanage'] = 'Manage course competencies';
$string['lp:coursecompetencyread'] = 'View course competencies';
$string['lp:plancreatedraft'] = 'Create draft learning plans';
$string['lp:planmanageall'] = 'Manage learning plans';
$string['lp:plancreateown'] = 'Create own learning plans';
$string['lp:planmanage'] = 'Manage learning plans';
$string['lp:planmanageown'] = 'Manage own learning plans';
$string['lp:planviewall'] = 'View all learning plans';
$string['lp:planview'] = 'View all learning plans';
$string['lp:planviewown'] = 'View own learning plans';
$string['lp:templatemanage'] = 'Manage templates';
$string['lp:templateread'] = 'View template';
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/lp/lib.php
Expand Up @@ -60,7 +60,7 @@ function tool_lp_myprofile_navigation(core_user\output\myprofile\tree $tree, $us
global $USER;

$context = context_user::instance($USER->id);
if (!$iscurrentuser && !has_capability('tool/lp:planviewall', $context)) {
if (!$iscurrentuser && !has_capability('tool/lp:planview', $context)) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions admin/tool/lp/plans.php
Expand Up @@ -43,11 +43,11 @@
throw new moodle_exception('invaliduser', 'error');
}

if (!has_capability('tool/lp:planviewall', $context)) {
if (!has_capability('tool/lp:planview', $context)) {
if ($iscurrentuser) {
require_capability('tool/lp:planviewown', $context);
} else {
throw new required_capability_exception($context, 'tool/lp:planviewall', 'nopermissions', '');
throw new required_capability_exception($context, 'tool/lp:planview', 'nopermissions', '');
}
}

Expand Down
20 changes: 10 additions & 10 deletions admin/tool/lp/tests/externallib_test.php
Expand Up @@ -99,9 +99,9 @@ protected function setUp() {
// Reset all default authenticated users permissions.
unassign_capability('tool/lp:competencymanage', $authrole->id);
unassign_capability('tool/lp:competencyread', $authrole->id);
unassign_capability('tool/lp:planmanageall', $authrole->id);
unassign_capability('tool/lp:planmanage', $authrole->id);
unassign_capability('tool/lp:planmanageown', $authrole->id);
unassign_capability('tool/lp:planviewall', $authrole->id);
unassign_capability('tool/lp:planview', $authrole->id);
unassign_capability('tool/lp:templatemanage', $authrole->id);
unassign_capability('tool/lp:templateread', $authrole->id);

Expand All @@ -111,9 +111,9 @@ protected function setUp() {

assign_capability('tool/lp:competencymanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
assign_capability('tool/lp:competencyread', CAP_ALLOW, $this->userrole, $syscontext->id);
assign_capability('tool/lp:planmanageall', CAP_ALLOW, $this->creatorrole, $syscontext->id);
assign_capability('tool/lp:planmanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
assign_capability('tool/lp:planmanageown', CAP_ALLOW, $this->creatorrole, $syscontext->id);
assign_capability('tool/lp:planviewall', CAP_ALLOW, $this->creatorrole, $syscontext->id);
assign_capability('tool/lp:planview', CAP_ALLOW, $this->creatorrole, $syscontext->id);
assign_capability('tool/lp:templatemanage', CAP_ALLOW, $this->creatorrole, $syscontext->id);
assign_capability('tool/lp:templateread', CAP_ALLOW, $this->userrole, $syscontext->id);

Expand Down Expand Up @@ -1052,7 +1052,7 @@ public function test_create_and_update_plans() {
$this->assertEquals('nopermissions', $e->errorcode);
}

assign_capability('tool/lp:plancreatedraft', CAP_ALLOW, $this->userrole, $syscontext->id);
assign_capability('tool/lp:plancreateown', CAP_ALLOW, $this->userrole, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();

$this->setUser($this->user);
Expand Down Expand Up @@ -1095,7 +1095,7 @@ public function test_create_and_update_plans() {
}

unassign_capability('tool/lp:planmanageown', $this->userrole, $syscontext->id);
unassign_capability('tool/lp:plancreatedraft', $this->userrole, $syscontext->id);
unassign_capability('tool/lp:plancreateown', $this->userrole, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();

try {
Expand Down Expand Up @@ -1133,7 +1133,7 @@ public function test_read_plans() {
$plan2['usercanupdate'] = false;
$plan3['usercanupdate'] = false;

// You need planmanage, planmanageown or plancreatedraft to see draft plans.
// You need planmanage, planmanageown or plancreateown to see draft plans.
try {
external::read_plan($plan1['id']);
$this->fail('Exception expected due to not permissions to read draft plan');
Expand All @@ -1143,13 +1143,13 @@ public function test_read_plans() {
$this->assertEquals((Array)$plan2, external::read_plan($plan2['id']));
$this->assertEquals((Array)$plan3, external::read_plan($plan3['id']));

assign_capability('tool/lp:plancreatedraft', CAP_ALLOW, $this->userrole, $syscontext->id);
assign_capability('tool/lp:plancreateown', CAP_ALLOW, $this->userrole, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();

$this->assertEquals((Array)$plan1, external::read_plan($plan1['id']));

assign_capability('tool/lp:planviewown', CAP_PROHIBIT, $this->userrole, $syscontext->id);
unassign_capability('tool/lp:plancreatedraft', $this->userrole, $syscontext->id);
unassign_capability('tool/lp:plancreateown', $this->userrole, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();

try {
Expand All @@ -1171,7 +1171,7 @@ public function test_delete_plans() {

$this->assertTrue(external::delete_plan($plan1['id']));

unassign_capability('tool/lp:planmanageall', $this->creatorrole, $syscontext->id);
unassign_capability('tool/lp:planmanage', $this->creatorrole, $syscontext->id);
accesslib_clear_all_caches_for_unit_testing();

try {
Expand Down
2 changes: 1 addition & 1 deletion admin/tool/lp/version.php
Expand Up @@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();


$plugin->version = 2015052416; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2015052418; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2014110400; // Requires this Moodle version.
$plugin->component = 'tool_lp'; // Full name of the plugin (used for diagnostics).

0 comments on commit 53084ab

Please sign in to comment.