Skip to content

Commit

Permalink
MDL-68093 availability: Add support for private rule sets
Browse files Browse the repository at this point in the history
This allows an availability plugin to indicate that a selected condition
should be considered "private", and therefore should never be shown to
users who don't satisfy the criteria. The availability_groups plugin
uses this to protect visibility of groups that are not visible to
non-members.
  • Loading branch information
marxjohnson committed Mar 14, 2023
1 parent 958da5b commit cab6e97
Show file tree
Hide file tree
Showing 13 changed files with 611 additions and 25 deletions.
2 changes: 1 addition & 1 deletion availability/classes/frontend.php
Expand Up @@ -151,7 +151,7 @@ public static function include_all_javascript($course, \cm_info $cm = null,
'show_verb', 'shown_individual', 'hidden_all', 'shown_all',
'condition_group', 'condition_group_info', 'and', 'or',
'label_multi', 'label_sign', 'setheading', 'itemheading',
'missingplugin'),
'missingplugin', 'disabled_verb'),
'availability');
}

Expand Down
7 changes: 5 additions & 2 deletions availability/condition/group/classes/frontend.php
Expand Up @@ -52,8 +52,11 @@ protected function get_javascript_init_params($course, \cm_info $cm = null,
$jsarray = array();
$context = \context_course::instance($course->id);
foreach ($groups as $rec) {
$jsarray[] = (object)array('id' => $rec->id, 'name' =>
format_string($rec->name, true, array('context' => $context)));
$jsarray[] = (object)array(
'id' => $rec->id,
'name' => format_string($rec->name, true, array('context' => $context)),
'visibility' => $rec->visibility
);
}
return array($jsarray);
}
Expand Down
Expand Up @@ -41,16 +41,41 @@ M.availability_group.form.getNode = function(json) {
for (var i = 0; i < this.groups.length; i++) {
var group = this.groups[i];
// String has already been escaped using format_string.
html += '<option value="' + group.id + '">' + group.name + '</option>';
html += '<option value="' + group.id + '" data-visibility="' + group.visibility + '">' + group.name + '</option>';
}
html += '</select></span></label>';
var node = Y.Node.create('<span class="form-inline">' + html + '</span>');

var select = node.one('select[name=id]');

select.on('change', function(e) {
var value = e.target.get('value');
// Find the visibility of the selected group.
var visibility = e.target.one('option[value=' + value + ']').get('dataset').visibility;

var event;
if (visibility > 0) {
event = 'availability:privateRuleSet';
} else {
event = 'availability:privateRuleUnset';
}
node.fire(event, {plugin: 'group'});
});

// Set initial values (leave default 'choose' if creating afresh).
if (json.creating === undefined) {
if (json.id !== undefined &&
node.one('select[name=id] > option[value=' + json.id + ']')) {
node.one('select[name=id]').set('value', '' + json.id);
if (json.id !== undefined) {
var option = select.one('option[value=' + json.id + ']');
if (option) {
select.set('value', '' + json.id);
var visibility = option.get('dataset').visibility;
if (visibility > 0) {
// Defer firing the event, to allow event bubbling to be set up in M.core_availability.form.
window.setTimeout(function() {
node.fire('availability:privateRuleSet', {plugin: 'group'});
}, 0);
}
}
} else if (json.id === undefined) {
node.one('select[name=id]').set('value', 'any');
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -41,16 +41,41 @@ M.availability_group.form.getNode = function(json) {
for (var i = 0; i < this.groups.length; i++) {
var group = this.groups[i];
// String has already been escaped using format_string.
html += '<option value="' + group.id + '">' + group.name + '</option>';
html += '<option value="' + group.id + '" data-visibility="' + group.visibility + '">' + group.name + '</option>';
}
html += '</select></span></label>';
var node = Y.Node.create('<span class="form-inline">' + html + '</span>');

var select = node.one('select[name=id]');

select.on('change', function(e) {
var value = e.target.get('value');
// Find the visibility of the selected group.
var visibility = e.target.one('option[value=' + value + ']').get('dataset').visibility;

var event;
if (visibility > 0) {
event = 'availability:privateRuleSet';
} else {
event = 'availability:privateRuleUnset';
}
node.fire(event, {plugin: 'group'});
});

// Set initial values (leave default 'choose' if creating afresh).
if (json.creating === undefined) {
if (json.id !== undefined &&
node.one('select[name=id] > option[value=' + json.id + ']')) {
node.one('select[name=id]').set('value', '' + json.id);
if (json.id !== undefined) {
var option = select.one('option[value=' + json.id + ']');
if (option) {
select.set('value', '' + json.id);
var visibility = option.get('dataset').visibility;
if (visibility > 0) {
// Defer firing the event, to allow event bubbling to be set up in M.core_availability.form.
window.setTimeout(function() {
node.fire('availability:privateRuleSet', {plugin: 'group'});
}, 0);
}
}
} else if (json.id === undefined) {
node.one('select[name=id]').set('value', 'any');
}
Expand Down
33 changes: 29 additions & 4 deletions availability/condition/group/yui/src/form/js/form.js
Expand Up @@ -39,16 +39,41 @@ M.availability_group.form.getNode = function(json) {
for (var i = 0; i < this.groups.length; i++) {
var group = this.groups[i];
// String has already been escaped using format_string.
html += '<option value="' + group.id + '">' + group.name + '</option>';
html += '<option value="' + group.id + '" data-visibility="' + group.visibility + '">' + group.name + '</option>';
}
html += '</select></span></label>';
var node = Y.Node.create('<span class="form-inline">' + html + '</span>');

var select = node.one('select[name=id]');

select.on('change', function(e) {
var value = e.target.get('value');
// Find the visibility of the selected group.
var visibility = e.target.one('option[value=' + value + ']').get('dataset').visibility;

var event;
if (visibility > 0) {
event = 'availability:privateRuleSet';
} else {
event = 'availability:privateRuleUnset';
}
node.fire(event, {plugin: 'group'});
});

// Set initial values (leave default 'choose' if creating afresh).
if (json.creating === undefined) {
if (json.id !== undefined &&
node.one('select[name=id] > option[value=' + json.id + ']')) {
node.one('select[name=id]').set('value', '' + json.id);
if (json.id !== undefined) {
var option = select.one('option[value=' + json.id + ']');
if (option) {
select.set('value', '' + json.id);
var visibility = option.get('dataset').visibility;
if (visibility > 0) {
// Defer firing the event, to allow event bubbling to be set up in M.core_availability.form.
window.setTimeout(function() {
node.fire('availability:privateRuleSet', {plugin: 'group'});
}, 0);
}
}
} else if (json.id === undefined) {
node.one('select[name=id]').set('value', 'any');
}
Expand Down
150 changes: 150 additions & 0 deletions availability/tests/behat/private_ruleset.feature
@@ -0,0 +1,150 @@
@core @core_availability @javascript
Feature: Private rule sets
In order to prevent private data being leaked in restriction sets
As a teacher
I want to have restrictions hidden when a private condition is selected

Background:
Given the following "courses" exist:
| fullname | shortname | format | enablecompletion | numsections |
| Course 1 | C1 | topics | 1 | 3 |
And the following "users" exist:
| username |
| teacher1 |
| student1 |
And the following "course enrolments" exist:
| user | course | role |
| teacher1 | C1 | editingteacher |
| student1 | C1 | student |
And the following "groups" exist:
| name | course | idnumber | visibility |
| Group A | C1 | GA | 0 |
| Group B | C1 | GB | 1 |
And I log in as "teacher1"
And I am on "Course 1" course homepage with editing mode on
And I add a "Page" to section "1"
And I expand all fieldsets

Scenario: Add restriction with visible condition (must match), display option should be active
When I click on "Add restriction..." "button"
And I click on "Date" "button" in the "Add restriction..." "dialogue"
Then ".availability-children .availability-eye" "css_element" should be visible
And ".availability-children .availability-eye-disabled" "css_element" should not be visible
And the "title" attribute of ".availability-eye" "css_element" should contain "Click to hide"

Scenario: Add restriction with private condition (must match), display option should be disabled
When I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
Then ".availability-children .availability-eye" "css_element" should not be visible
And ".availability-children .availability-eye-disabled" "css_element" should be visible
And the "title" attribute of ".availability-eye-disabled" "css_element" should contain "Cannot be changed as ruleset includes a rule containing private data."

Scenario: Add restrictions with a visible and a private condition (must match all), display option should be disabled
When I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
When I click on "Add restriction..." "button"
And I click on "Date" "button" in the "Add restriction..." "dialogue"
Then ".availability-children .availability-eye" "css_element" should not be visible
And ".availability-children .availability-eye-disabled" "css_element" should be visible

Scenario: Remove private condition (must match), display option should be active
When I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
And I click on "Add restriction..." "button"
And I click on "Date" "button" in the "Add restriction..." "dialogue"
Then ".availability-children .availability-eye" "css_element" should not be visible
And ".availability-children .availability-eye-disabled" "css_element" should be visible
# Should pick the first one (Group B)
When I click on ".availability-item .availability-delete img" "css_element"
Then ".availability-children .availability-eye" "css_element" should be visible
And ".availability-children .availability-eye-disabled" "css_element" should not be visible

Scenario: Set a private condition to a visible value (must match), display option should be active
When I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
Then ".availability-children .availability-eye" "css_element" should not be visible
And ".availability-children .availability-eye-disabled" "css_element" should be visible
# Should pick the first one (Group B)
When I set the field "Group" to "Group A"
Then ".availability-children .availability-eye" "css_element" should be visible
And ".availability-children .availability-eye-disabled" "css_element" should not be visible

Scenario: Add restrictions with a visible and a private condition (must match any), display option should be disabled
When I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
And I click on "Add restriction..." "button"
And I click on "Date" "button" in the "Add restriction..." "dialogue"
And I set the field "Required restrictions" to "any"
# "Hidden" icon should be shown in header.
And ".availability-children .availability-eye" "css_element" should not be visible
And ".availability-children .availability-eye-disabled" "css_element" should not be visible
And ".availability-header .availability-eye" "css_element" should not be visible
And ".availability-header .availability-eye-disabled" "css_element" should be visible

Scenario: Add restriction with private condition (must not match), display option should be disabled
When I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
And I set the field "Restriction type" to "must not"
# "Hidden" icon should be shown in header.
And ".availability-children .availability-eye" "css_element" should not be visible
And ".availability-children .availability-eye-disabled" "css_element" should not be visible
And ".availability-header .availability-eye" "css_element" should not be visible
And ".availability-header .availability-eye-disabled" "css_element" should be visible

Scenario: Add restrictions with a visible and a private condition (must not match all), display option should be disabled
When I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
And I click on "Add restriction..." "button"
And I click on "Date" "button" in the "Add restriction..." "dialogue"
And I set the field "Restriction type" to "must not"
# "Hidden" icon should be shown in header.
And ".availability-children .availability-eye" "css_element" should not be visible
And ".availability-children .availability-eye-disabled" "css_element" should not be visible
And ".availability-header .availability-eye" "css_element" should not be visible
And ".availability-header .availability-eye-disabled" "css_element" should be visible

Scenario: Add restrictions with a visible and a private condition (must not match any), display option should be disabled
When I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
And I click on "Add restriction..." "button"
And I click on "Date" "button" in the "Add restriction..." "dialogue"
And I set the field "Restriction type" to "must not"
And I set the field "Required restrictions" to "any"
# "Hidden" icon should be shown in conditions, but not in the header.
And ".availability-header .availability-eye" "css_element" should not be visible
And ".availability-header .availability-eye-disabled" "css_element" should not be visible
And ".availability-children .availability-eye" "css_element" should not be visible
And ".availability-children .availability-eye-disabled" "css_element" should be visible

Scenario: Private conditions should not show to unprivileged users
Given I set the field "Name" to "Test page"
And I set the field "Page content" to "test"
And I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
And I press "Save and return to course"
And I log out
And I log in as "student1"
When I am on "Course 1" course homepage
Then I should not see "Test page"
And I should not see "Not available unless: You belong to Group B"

Scenario: Loading a rule set containing private conditions should disable display option
Given I set the field "Name" to "Test page"
And I set the field "Page content" to "test"
And I click on "Add restriction..." "button"
And I click on "Group" "button" in the "Add restriction..." "dialogue"
And I set the field "Group" to "Group B"
And I press "Save and display"
When I follow "Settings"
And I expand all fieldsets
Then ".availability-children .availability-eye" "css_element" should not be visible
And ".availability-children .availability-eye-disabled" "css_element" should be visible

0 comments on commit cab6e97

Please sign in to comment.