Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDL-79959 repository: Add custom field picker for SSO identification. #2

Open
wants to merge 1 commit into
base: MOODLE_403_STABLE
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions repository/equella/lang/en/repository_equella.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@

$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';
Expand Down
32 changes: 28 additions & 4 deletions repository/equella/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,21 @@ public function send_file($stored_file, $lifetime=null , $filter=0, $forcedownlo
* @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);

Expand Down Expand Up @@ -307,7 +316,7 @@ public static function instance_config_form($mform) {
*/
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) {
Expand Down Expand Up @@ -364,15 +373,15 @@ private function getssotoken($readwrite = 'read') {
if (empty($USER->username)) {
return false;
}

$equellauserfield = $this->get_userfield_value();
if ($readwrite == 'write') {

foreach (self::get_all_editing_roles() as $role) {
if (user_has_role_assignment($USER->id, $role->id, $this->context->id)) {
// 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"));
}
}
Expand All @@ -381,7 +390,7 @@ private function getssotoken($readwrite = 'read') {
// 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'));
}
}

Expand Down Expand Up @@ -436,4 +445,19 @@ public function get_reference_details($reference, $filestatus = 0) {
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;
}
}
}
19 changes: 17 additions & 2 deletions repository/equella/tests/generator/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ protected function prepare_record(array $record) {
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';
}
Expand All @@ -53,8 +56,20 @@ protected function prepare_record(array $record) {
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;
}
Expand Down
239 changes: 239 additions & 0 deletions repository/equella/tests/lib_test.php
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
* @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();
}
}
2 changes: 1 addition & 1 deletion repository/equella/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).