Skip to content

Commit

Permalink
MDL-83954 lti: Abstract contentitem cap checks into classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocolate-lightning committed Jan 13, 2025
1 parent ef81387 commit 5ee96e9
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ltix/amd/build/contentitem.min.js

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

2 changes: 1 addition & 1 deletion ltix/amd/build/contentitem.min.js.map

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions ltix/amd/src/contentitem.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export default class ContentItem {
/** @property {string|null} toolInstanceText The tool instance text. */
toolInstanceText = null;

/** @property {string|null} placement The placement. */
placement = null;

/** @property {Object|null} modal The modal object. */
modal = null;

Expand All @@ -62,10 +65,11 @@ export default class ContentItem {
* @param {int} contextID The context ID.
* @param {string|null} toolInstanceTitle The tool instance title.
* @param {string|null} toolInstanceText The tool instance text.
* @param {string|null} placement The placement.
* @returns {void}
*/
static async init(toolID, contextID, toolInstanceTitle = null, toolInstanceText = null) {
const contentItem = new this(toolID, contextID, toolInstanceTitle, toolInstanceText);
static async init(toolID, contextID, toolInstanceTitle = null, toolInstanceText = null, placement = null) {
const contentItem = new this(toolID, contextID, toolInstanceTitle, toolInstanceText, placement);
contentItem.registerEventListeners();
}

Expand All @@ -76,13 +80,15 @@ export default class ContentItem {
* @param {int} contextID The context ID.
* @param {string|null} toolInstanceTitle The tool instance title.
* @param {string|null} toolInstanceText The tool instance text.
* @param {string|null} placement The placement.
* @returns {void}
*/
constructor(toolID, contextID, toolInstanceTitle = null, toolInstanceText = null) {
constructor(toolID, contextID, toolInstanceTitle = null, toolInstanceText = null, placement = null) {
this.toolID = toolID;
this.contextID = contextID;
this.toolInstanceTitle = toolInstanceTitle;
this.toolInstanceText = toolInstanceText;
this.placement = placement;
}

/**
Expand Down Expand Up @@ -144,7 +150,8 @@ export default class ContentItem {
toolid: this.toolID,
contextid: this.contextID,
toolinstancetitle: this.toolInstanceTitle,
toolinstancetext: this.toolInstanceText
toolinstancetext: this.toolInstanceText,
placementstring: this.placement
}
};

Expand Down
37 changes: 37 additions & 0 deletions ltix/classes/placement/access.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core_ltix\placement;

use core\context;

/**
* Base class for access control within LTI placements.
*
* @package core_ltix
* @copyright 2024 Mathew May <mathew.solutions>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class access {

/**
* Check if the user has access to the placement location.
*
* @param context $context The context.
*/
abstract public function resource_link_setup_capabilities(context $context): void;

}
17 changes: 8 additions & 9 deletions ltix/contentitem.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
$contextid = required_param('contextid', PARAM_INT);
$title = optional_param('title', '', PARAM_TEXT);
$text = optional_param('text', '', PARAM_RAW);
$placementstring = optional_param('placementstring', 'ltix', PARAM_ALPHANUMEXT); // If there is no placement string, then we will do some basic checks instead.

$context = \context_helper::instance_by_id($contextid);

Expand All @@ -50,18 +51,16 @@

$course = $DB->get_record('course', ['id' => $context->get_course_context()->instanceid], '*', MUST_EXIST);

// Check access and capabilities.
if ($context instanceof context_course) {
require_login($course);
} else if ($context instanceof context_module) {
$cm = get_coursemodule_from_id('', $context->instanceid, 0, false, MUST_EXIST);
require_login(null, true, $cm, true, true);
//TODO: Maybe look at using plugin_supports().
$classname = $placementstring . '_access';
$class = "$placementstring\local\ltix\placement\\$classname";
if (class_exists($class) && is_subclass_of($class, core_ltix\placement\access::class)) {
$placement_access = new $class();
$placement_access->resource_link_setup_capabilities($context);
} else {
require_login();
// TODO: Add some default checks.
}

// TODO: Assess capability checks.

// Set the return URL. We send the launch container along to help us avoid frames-within-frames when the user returns.
$returnurlparams = [
'contextid' => $course->get_context()->id,
Expand Down
1 change: 1 addition & 0 deletions ltix/templates/contentitem.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<input type="hidden" name="contextid" value="{{postData.contextid}}" />
<input type="hidden" name="title" value="{{postData.toolinstancetitle}}" />
<input type="hidden" name="text" value="{{postData.toolinstancetext}}" />
<input type="hidden" name="placementstring" value="{{postData.placementstring}}" />
</form>
</div>
</div>
Loading

0 comments on commit 5ee96e9

Please sign in to comment.