Merge branch 'MDL-79959-repo-equella-sso-identification' of https://github.com/gbarat87/moodle

This commit is contained in:
Ilya Tregubov 2024-04-02 11:04:49 +08:00 committed by Shamim Rezaie
commit a6047e0fbb
5 changed files with 288 additions and 7 deletions

View File

@ -29,6 +29,9 @@ $string['breadcrumb'] = 'EQUELLA';
$string['equellaurl'] = 'EQUELLA URL';
$string['equellaaction'] = 'EQUELLA action';
$string['equellausername'] = 'Username';
$string['equellauserfield'] = 'EQUELLA userfield';
$string['equellauserfield_help'] = 'Choose the user field to be used for matching equella user (Only the user fields of type short text are being listed)';
$string['equellaoptions'] = 'EQUELLA options';
$string['equella:view'] = 'View EQUELLA repository';
$string['sharedid'] = 'Shared secret ID';

View File

@ -263,12 +263,21 @@ class repository_equella extends repository {
* @param moodleform $mform
*/
public static function instance_config_form($mform) {
global $DB;
$mform->addElement('text', 'equella_url', get_string('equellaurl', 'repository_equella'));
$mform->setType('equella_url', PARAM_URL);
$strrequired = get_string('required');
$mform->addRule('equella_url', $strrequired, 'required', null, 'client');
$userfieldoptions = ['default' => get_string('equellausername', 'repository_equella')];
foreach ($DB->get_records('user_info_field', ['datatype' => 'text']) as $params) {
$userfieldoptions[$params->shortname] = $params->name;
}
$mform->addElement('select', 'equella_userfield', get_string('equellauserfield', 'repository_equella'), $userfieldoptions);
$mform->setDefault('equella_userfield', $userfieldoptions['default']);
$mform->addHelpButton('equella_userfield', 'equellauserfield', 'repository_equella');
$mform->addElement('text', 'equella_options', get_string('equellaoptions', 'repository_equella'));
$mform->setType('equella_options', PARAM_NOTAGS);
@ -307,7 +316,7 @@ class repository_equella extends repository {
*/
public static function get_instance_option_names() {
$rv = array('equella_url', 'equella_select_restriction', 'equella_options',
'equella_shareid', 'equella_sharedsecret'
'equella_shareid', 'equella_sharedsecret', 'equella_userfield',
);
foreach (self::get_all_editing_roles() as $role) {
@ -364,7 +373,7 @@ class repository_equella extends repository {
if (empty($USER->username)) {
return false;
}
$equellauserfield = $this->get_userfield_value();
if ($readwrite == 'write') {
foreach (self::get_all_editing_roles() as $role) {
@ -372,7 +381,7 @@ class repository_equella extends repository {
// See if the user has a role that is linked to an equella role.
$shareid = $this->get_option("equella_{$role->shortname}_shareid");
if (!empty($shareid)) {
return $this->getssotoken_raw($USER->username, $shareid,
return $this->getssotoken_raw($equellauserfield, $shareid,
$this->get_option("equella_{$role->shortname}_sharedsecret"));
}
}
@ -381,7 +390,7 @@ class repository_equella extends repository {
// If we are only reading, use the unadorned shareid and secret.
$shareid = $this->get_option('equella_shareid');
if (!empty($shareid)) {
return $this->getssotoken_raw($USER->username, $shareid, $this->get_option('equella_sharedsecret'));
return $this->getssotoken_raw($equellauserfield, $shareid, $this->get_option('equella_sharedsecret'));
}
}
@ -436,4 +445,19 @@ class repository_equella extends repository {
public function contains_private_data() {
return false;
}
/**
* Retrieve the userfield/username.
*
* @return string
*/
public function get_userfield_value(): string {
global $USER;
$userfield = $this->get_option('equella_userfield');
if ($userfield != 'default' && isset($USER->profile[$userfield])) {
return $USER->profile[$userfield];
} else {
return $USER->username;
}
}
}

View File

@ -44,6 +44,9 @@ class repository_equella_generator extends testing_repository_generator {
if (!isset($record['equella_url'])) {
$record['equella_url'] = 'http://dummy.url.com';
}
if (!isset($record['equella_userfield'])) {
$record['equella_userfield'] = 'default';
}
if (!isset($record['equella_select_restriction'])) {
$record['equella_select_restriction'] = 'none';
}
@ -53,8 +56,20 @@ class repository_equella_generator extends testing_repository_generator {
if (!isset($record['equella_shareid'])) {
$record['equella_shareid'] = 'id';
}
if (!isset($record['equella_sharesecret'])) {
$record['equella_url'] = 'secret';
if (!isset($record['equella_sharedsecret'])) {
$record['equella_sharedsecret'] = 'secret';
}
if (!isset($record['equella_manager_shareid'])) {
$record['equella_manager_shareid'] = '';
}
if (!isset($record['equella_manager_sharedsecret'])) {
$record['equella_manager_sharedsecret'] = '';
}
if (!isset($record['equella_editingteacher_shareid'])) {
$record['equella_editingteacher_shareid'] = '';
}
if (!isset($record['equella_editingteacher_sharedsecret'])) {
$record['equella_editingteacher_sharedsecret'] = '';
}
return $record;
}

View File

@ -0,0 +1,239 @@
<?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/>.
/**
* This file contains tests for the repository_equella class.
*
* @package repository_equella
*
* @author Guillaume BARAT <guillaumebarat@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace repository_equella;
use repository_equella;
use stdClass;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->dirroot . '/repository/lib.php');
require_once($CFG->libdir . '/webdavlib.php');
/**
* Class repository_equella_lib_testcase
*
* @group repository_equella
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class lib_test extends \advanced_testcase {
/** @var null|\repository_equella the repository_equella object, which the tests are run on. */
private $repo = null;
/**
* Create some data for repository.
*
* @return stdClass
*/
private function create_new_form_data(): stdClass {
$record = new stdClass();
$record->equella_url = 'http://dummy.url.com';
$record->equella_userfield = 'default';
$record->equella_select_restriction = 'none';
$record->equella_options = '';
$record->equella_shareid = 'id';
$record->equella_sharedsecret = 'secret';
$record->equella_manager_shareid = '';
$record->equella_manager_sharedsecret = '';
$record->equella_editingteacher_shareid = '';
$record->equella_editingteacher_sharedsecret = '';
return $record;
}
/**
* Create repository for testing.
*
* @return repository_equella
*/
private function create_repository(): repository_equella {
$record = new \stdClass();
$this->getDataGenerator()->create_repository_type('equella', $record);
$generator = $this->getDataGenerator()->get_plugin_generator('repository_equella');
$instance = $generator->create_instance();
$this->repo = new repository_equella($instance->id);
return $this->repo;
}
/**
* Test that environment is created.
* @covers \repository_equella::get_repository_by_id
* @return void
*/
public function test_repository_is_created(): void {
$this->initialise_repository();
$actual = repository_equella::get_repository_by_id($this->repo->id, $this->repo->context);
$this->assertEquals($this->repo->options['equella_url'], $actual->get_option('equella_url'));
$this->assertEquals($this->repo->options['equella_userfield'], $actual->get_option('equella_userfield'));
$this->assertEquals($this->repo->options['equella_select_restriction'],
$actual->get_option('equella_select_restriction'));
$this->assertEquals($this->repo->options['equella_options'], $actual->get_option('equella_options'));
$this->assertEquals($this->repo->options['equella_shareid'], $actual->get_option('equella_shareid'));
$this->assertEquals($this->repo->options['equella_sharedsecret'], $actual->get_option('equella_sharedsecret'));
$this->assertEquals($this->repo->options['equella_manager_shareid'], $actual->get_option('equella_manager_shareid'));
$this->assertEquals($this->repo->options['equella_manager_sharedsecret'],
$actual->get_option('equella_manager_sharedsecret'));
$this->assertEquals($this->repo->options['equella_editingteacher_shareid'],
$actual->get_option('equella_editingteacher_shareid'));
$this->assertEquals($this->repo->options['equella_editingteacher_sharedsecret'],
$actual->get_option('equella_editingteacher_sharedsecret'));
$this->resetAfterTest(true);
}
/**
* Data provider for get_userfield_value.
*
* @return array
* @covers ::get_userfield_value
*/
public static function get_userfield_value_provider(): array {
return [
[
'input' => [
'userfield' => 'nickname',
'value' => 'administrator',
],
'expected' => [
'username' => 'administrator',
],
], [
'input' => [
'userfield' => 'default',
'value' => 'default',
],
'expected' => [
'username' => 'admin',
],
], [
'input' => [
'userfield' => 'test',
'value' => 'test',
],
'expected' => [
'username' => 'test',
],
],
];
}
/**
* Test method get_userfield_value.
*
* @dataProvider get_userfield_value_provider
*
* @param array $input
* @param array $expected
* @covers ::get_userfield_value
*
* @return void
*/
public function test_get_userfield_value($input, $expected): void {
global $USER;
$this->initialise_repository();
$USER->profile[$input['userfield']] = $input['value'];
$this->repo->set_option(['equella_userfield' => $input['userfield']]);
$return = $this->repo->get_userfield_value();
$this->assertEquals($expected['username'], $return);
}
/**
* Data provider for get_listing.
*
* @return array
* @covers ::get_listing
*/
public static function get_listing_provider(): array {
return [
[
'input' => [
'url' => 'http://dummy.url.com',
'userfield' => 'nickname',
'value' => 'administrator',
],
'expected' => [
'username' => 'administrator',
],
], [
'input' => [
'url' => 'http://dummy.url.com',
'userfield' => 'default',
'value' => '',
],
'expected' => [
'username' => 'admin',
],
], [
'input' => [
'url' => 'http://dummy.url.com',
'userfield' => 'test',
'value' => 'test',
],
'expected' => [
'username' => 'test',
],
],
];
}
/**
* Test that the method get_listing return the correct array.
*
* @dataProvider get_listing_provider
*
* @param array $input
* @param array $expected
* @covers ::get_listing
*
* @return void
*/
public function test_get_listing($input, $expected): void {
global $USER;
$this->initialise_repository();
$USER->profile[$input['userfield']] = $input['value'];
$this->repo->set_option(['url' => $input['url'],
'equella_userfield' => $input['userfield']]);
$listing = $this->repo->get_listing();
$this->assertArrayHasKey('manage', $listing);
$this->assertStringContainsString($expected['username'], $listing['manage']);
}
/**
* Create and initialise the repository for test.
* @return void
*/
public function initialise_repository(): void {
$this->resetAfterTest(true);
// Admin is neccessary to create repository.
$this->setAdminUser();
$this->create_repository();
$this->create_new_form_data();
}
}

View File

@ -25,6 +25,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2023100900; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2024021500; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2023100400; // Requires this Moodle version.
$plugin->component = 'repository_equella'; // Full name of the plugin (used for diagnostics).