diff --git a/enrol/lti/db/install.php b/enrol/lti/db/install.php new file mode 100644 index 00000000000..73db17a3963 --- /dev/null +++ b/enrol/lti/db/install.php @@ -0,0 +1,40 @@ +. + +/** + * Post installation code for enrol_lti. + * + * @package enrol_lti + * @copyright 2022 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Stub for database installation. + */ +function xmldb_enrol_lti_install() { + global $CFG, $OUTPUT; + + // LTI 1.3: Set a private key for this site (which is acting as a tool in LTI 1.3). + require_once($CFG->dirroot . '/enrol/lti/upgradelib.php'); + + $warning = enrol_lti_verify_private_key(); + if (!empty($warning)) { + echo $OUTPUT->notification($warning, 'notifyproblem'); + } +} diff --git a/enrol/lti/db/install.xml b/enrol/lti/db/install.xml index c724a4be149..8c3383f038e 100644 --- a/enrol/lti/db/install.xml +++ b/enrol/lti/db/install.xml @@ -9,6 +9,7 @@ + @@ -23,6 +24,9 @@ + + + @@ -30,6 +34,7 @@ + @@ -39,6 +44,7 @@ + @@ -183,5 +189,112 @@
+ + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + + + + + +
diff --git a/enrol/lti/db/upgrade.php b/enrol/lti/db/upgrade.php index 70288977bf7..b054928320f 100644 --- a/enrol/lti/db/upgrade.php +++ b/enrol/lti/db/upgrade.php @@ -37,7 +37,8 @@ * @return boolean */ function xmldb_enrol_lti_upgrade($oldversion) { - global $CFG; + global $CFG, $OUTPUT, $DB; + $dbman = $DB->get_manager(); // Automatically generated Moodle v3.6.0 release upgrade line. // Put any upgrade step following this. @@ -51,5 +52,271 @@ function xmldb_enrol_lti_upgrade($oldversion) { // Automatically generated Moodle v3.9.0 release upgrade line. // Put any upgrade step following this. + if ($oldversion < 2021052501) { + // LTI 1.3: Set a private key for this site (which is acting as a tool in LTI 1.3). + require_once($CFG->dirroot . '/enrol/lti/upgradelib.php'); + + $warning = enrol_lti_verify_private_key(); + if (!empty($warning)) { + echo $OUTPUT->notification($warning, 'notifyproblem'); + } + + // Lti savepoint reached. + upgrade_plugin_savepoint(true, 2021052501, 'enrol', 'lti'); + } + + if ($oldversion < 2021052502) { + // Define table enrol_lti_app_registration to be created. + $table = new xmldb_table('enrol_lti_app_registration'); + + // Adding fields to table enrol_lti_app_registration. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('platformid', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field('clientid', XMLDB_TYPE_CHAR, '1333', null, XMLDB_NOTNULL, null, null); + $table->add_field('platformclienthash', XMLDB_TYPE_CHAR, '64', null, XMLDB_NOTNULL, null, null); + $table->add_field('authenticationrequesturl', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field('jwksurl', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field('accesstokenurl', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + + // Adding keys to table enrol_lti_app_registration. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + + // Add unique index on platformclienthash. + $table->add_index('platformclienthash', XMLDB_INDEX_UNIQUE, ['platformclienthash']); + + // Conditionally launch create table for enrol_lti_app_registration. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Lti savepoint reached. + upgrade_plugin_savepoint(true, 2021052502, 'enrol', 'lti'); + } + + if ($oldversion < 2021052503) { + // Add a new column 'ltiversion' to the enrol_lti_tools table. + $table = new xmldb_table('enrol_lti_tools'); + + // Define field ltiversion to be added to enrol_lti_tools. + $field = new xmldb_field('ltiversion', XMLDB_TYPE_CHAR, 15, null, XMLDB_NOTNULL, null, "LTI-1p3", 'contextid'); + + // Conditionally launch add field ltiversion, setting it to the legacy value for all published content. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + $DB->set_field('enrol_lti_tools', 'ltiversion', 'LTI-1p0/LTI-2p0'); + } + + // Define field uuid to be added to enrol_lti_tools. + $field = new xmldb_field('uuid', XMLDB_TYPE_CHAR, 36, null, null, null, null, 'ltiversion'); + + // Conditionally launch add field uuid, setting it to null for existing rows. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + $key = new xmldb_key('uuid', XMLDB_KEY_UNIQUE, ['uuid']); + $dbman->add_key($table, $key); + } + + // Lti savepoint reached. + upgrade_plugin_savepoint(true, 2021052503, 'enrol', 'lti'); + } + + if ($oldversion < 2021052504) { + // Define table enrol_lti_deployment to be created. + $table = new xmldb_table('enrol_lti_deployment'); + + // Adding fields to table enrol_lti_deployment. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('name', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('deploymentid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('platformid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + + // Adding keys to table enrol_lti_deployment. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key('platformid', XMLDB_KEY_FOREIGN, ['platformid'], 'enrol_lti_app_registration', ['id']); + + // Add unique index on platformid (issuer), deploymentid. + $table->add_index('platformid-deploymentid', XMLDB_INDEX_UNIQUE, ['platformid', 'deploymentid']); + + // Conditionally launch create table for enrol_lti_deployment. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Lti savepoint reached. + upgrade_plugin_savepoint(true, 2021052504, 'enrol', 'lti'); + } + + if ($oldversion < 2021052505) { + // Add a new column 'ltideploymentid' to the enrol_lti_users table. + $table = new xmldb_table('enrol_lti_users'); + + // Define field ltideploymentid to be added to enrol_lti_users. + $field = new xmldb_field('ltideploymentid', XMLDB_TYPE_INTEGER, '10', null, null, null, null, 'sourceid'); + + // Conditionally launch add field deploymentid. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Launch add key ltideploymentid. + $key = new xmldb_key('ltideploymentid', XMLDB_KEY_FOREIGN, ['ltideploymentid'], 'enrol_lti_deployment', ['id']); + $dbman->add_key($table, $key); + + // Lti savepoint reached. + upgrade_plugin_savepoint(true, 2021052505, 'enrol', 'lti'); + } + + if ($oldversion < 2021052506) { + // Define table enrol_lti_resource_link to be created. + $table = new xmldb_table('enrol_lti_resource_link'); + + // Adding fields to table enrol_lti_resource_link. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('resourcelinkid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('resourceid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('ltideploymentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('lticontextid', XMLDB_TYPE_INTEGER, '10', null, null, null, null); + $table->add_field('lineitemsservice', XMLDB_TYPE_CHAR, '1333', null, null, null, null); + $table->add_field('lineitemservice', XMLDB_TYPE_CHAR, '1333', null, null, null, null); + $table->add_field('lineitemscope', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('resultscope', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('scorescope', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('contextmembershipsurl', XMLDB_TYPE_CHAR, '1333', null, null, null, null); + $table->add_field('nrpsserviceversions', XMLDB_TYPE_CHAR, '255', null, null, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + + // Adding keys to table enrol_lti_resource_link. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key('ltideploymentid', XMLDB_KEY_FOREIGN, ['ltideploymentid'], 'enrol_lti_deployment', ['id']); + $table->add_key('lticontextid', XMLDB_KEY_FOREIGN, ['lticontextid'], 'enrol_lti_context', ['id']); + + // Add unique index on resourcelinkid, ltideploymentid. + $table->add_index('resourcelinkdid-ltideploymentid', XMLDB_INDEX_UNIQUE, ['resourcelinkid', 'ltideploymentid']); + + // Conditionally launch create table for enrol_lti_resource_link. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + // Lti savepoint reached. + upgrade_plugin_savepoint(true, 2021052506, 'enrol', 'lti'); + } + + if ($oldversion < 2021052507) { + // Define table enrol_lti_context to be created. + $table = new xmldb_table('enrol_lti_context'); + + // Adding fields to table enrol_lti_context. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('contextid', XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null); + $table->add_field('ltideploymentid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('type', XMLDB_TYPE_TEXT, null, null, null, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('timemodified', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + + // Adding keys to table enrol_lti_context. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key('ltideploymentid', XMLDB_KEY_FOREIGN, ['ltideploymentid'], 'enrol_lti_deployment', ['id']); + + // Add unique index on ltideploymentid, contextid. + $table->add_index('ltideploymentid-contextid', XMLDB_INDEX_UNIQUE, ['ltideploymentid', 'contextid']); + + // Conditionally launch create table for enrol_lti_context. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + upgrade_plugin_savepoint(true, 2021052507, 'enrol', 'lti'); + } + + if ($oldversion < 2021052508) { + // Define table enrol_lti_user_resource_link to be created. + $table = new xmldb_table('enrol_lti_user_resource_link'); + + // Adding fields to table enrol_lti_user_resource_link. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('ltiuserid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('resourcelinkid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + + // Adding keys to table enrol_lti_user_resource_link. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + $table->add_key('ltiuserid', XMLDB_KEY_FOREIGN, ['ltiuserid'], 'enrol_lti_users', ['id']); + $table->add_key('resourcelinkid', XMLDB_KEY_FOREIGN, ['resourcelinkid'], 'enrol_lti_resource_link', ['id']); + + // Add unique index on userid, resourcelinkid. + $table->add_index('ltiuserid-resourcelinkid', XMLDB_INDEX_UNIQUE, ['ltiuserid', 'resourcelinkid']); + + // Conditionally launch create table for enrol_lti_user_resource_link. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + upgrade_plugin_savepoint(true, 2021052508, 'enrol', 'lti'); + } + + if ($oldversion < 2021052512) { + // Define field legacyconsumerkey to be added to enrol_lti_deployment. + $table = new xmldb_table('enrol_lti_deployment'); + $field = new xmldb_field('legacyconsumerkey', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'platformid'); + + // Conditionally launch add field legacyconsumerkey. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + upgrade_plugin_savepoint(true, 2021052512, 'enrol', 'lti'); + } + + if ($oldversion < 2021052513) { + // Define table enrol_lti_reg_token to be created. + $table = new xmldb_table('enrol_lti_reg_token'); + + // Adding fields to table enrol_lti_reg_token. + $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); + $table->add_field('token', XMLDB_TYPE_CHAR, '60', null, XMLDB_NOTNULL, null, null); + $table->add_field('expirytime', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + $table->add_field('timecreated', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, null); + + // Adding keys to table enrol_lti_reg_token. + $table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']); + + // Conditionally launch create table for enrol_lti_reg_token. + if (!$dbman->table_exists($table)) { + $dbman->create_table($table); + } + + upgrade_plugin_savepoint(true, 2021052513, 'enrol', 'lti'); + } + + if ($oldversion < 2021052514) { + // Add a new column 'provisioningmodelearner' to the enrol_lti_tools table. + $table = new xmldb_table('enrol_lti_tools'); + + // Define field provisioningmodelearner to be added to enrol_lti_tools. + $field = new xmldb_field('provisioningmodelearner', XMLDB_TYPE_INTEGER, 2, null, null, null, null, 'uuid'); + + // Conditionally launch add field provisioningmodelearner. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Define field provisioningmodeinstructor to be added to enrol_lti_tools. + $field = new xmldb_field('provisioningmodeinstructor', XMLDB_TYPE_INTEGER, 2, null, null, null, null, + 'provisioningmodelearner'); + + // Conditionally launch add field provisioningmodeinstructor. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Lti savepoint reached. + upgrade_plugin_savepoint(true, 2021052514, 'enrol', 'lti'); + } + return true; } diff --git a/enrol/lti/upgradelib.php b/enrol/lti/upgradelib.php new file mode 100644 index 00000000000..db0e419989c --- /dev/null +++ b/enrol/lti/upgradelib.php @@ -0,0 +1,62 @@ +. +/** + * This file contains functions used by upgrade and install. + * + * Because this is used during install it should not include additional files. + * + * @package enrol_lti + * @copyright 2021 Jake Dallimore + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * This function checks if a private key has been generated for this enrolment instance. + * + * If the key does not exist it generates a new one. If the openssl + * extension is not installed or configured properly it returns a warning message. + * + * @return string A warning message if a private key does not exist and cannot be generated. + */ +function enrol_lti_verify_private_key() { + + $name = 'lti_13_kid'; + $key = get_config('enrol_lti', $name); + + // If we already generated a valid key, no need to check. + if (empty($key)) { + // Create the private key. + $kid = bin2hex(openssl_random_pseudo_bytes(10)); + set_config($name, $kid, 'enrol_lti'); + $config = array( + "digest_alg" => "sha256", + "private_key_bits" => 2048, + "private_key_type" => OPENSSL_KEYTYPE_RSA, + ); + $res = openssl_pkey_new($config); + openssl_pkey_export($res, $privatekey); + + if (!empty($privatekey)) { + set_config('lti_13_privatekey', $privatekey, 'enrol_lti'); + } else { + return get_string('opensslconfiginvalid', 'enrol_lti'); + } + } + + return ''; +} diff --git a/enrol/lti/version.php b/enrol/lti/version.php index 70dfc34e91b..3b633f35ef8 100644 --- a/enrol/lti/version.php +++ b/enrol/lti/version.php @@ -24,6 +24,9 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2021052500; // The current plugin version (Date: YYYYMMDDXX). +$plugin->version = 2021052514; // The current plugin version (Date: YYYYMMDDXX). $plugin->requires = 2021052500; // Requires this Moodle version. $plugin->component = 'enrol_lti'; // Full name of the plugin (used for diagnostics). +$plugin->dependencies = [ + 'auth_lti' => 2021100500, // The auth_lti authentication plugin version 2021100500 or higher must be present. +];