Merge branch 'MDL-70271-master' of git://github.com/peterRd/moodle

This commit is contained in:
Ilya Tregubov 2021-07-30 12:24:35 +02:00
commit 04a2e1e827
9 changed files with 106 additions and 67 deletions

View File

@ -25,9 +25,8 @@
namespace repository_dropbox;
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir . '/oauthlib.php');
use core\oauth2\client;
use core\oauth2\issuer;
/**
* Dropbox V2 API.
@ -36,7 +35,7 @@ require_once($CFG->libdir . '/oauthlib.php');
* @copyright Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class dropbox extends \oauth2_client {
class dropbox extends client {
/**
* @var array Custom continue endpoints that differ from the standard.
@ -48,12 +47,23 @@ class dropbox extends \oauth2_client {
/**
* Create the DropBox API Client.
*
* @param string $key The API key
* @param string $secret The API secret
* @param issuer $issuer The dropbox issuer
* @param string $callback The callback URL
*/
public function __construct($key, $secret, $callback) {
parent::__construct($key, $secret, $callback, '');
public function __construct(issuer $issuer, $callback) {
parent::__construct($issuer, $callback, '', false, true);
}
/**
* Override - Return an empty string to override parent function.
*
* Dropbox does not require scopes to be provided and can function without them.
* Additional information MDL-70268
*
* @return string
*/
protected function get_login_scopes() {
return '';
}
/**

View File

@ -21,7 +21,7 @@ defined('MOODLE_INTERNAL') || die();
* @return bool result
*/
function xmldb_repository_dropbox_upgrade($oldversion) {
global $CFG;
global $CFG, $DB;
// Automatically generated Moodle v3.6.0 release upgrade line.
// Put any upgrade step following this.
@ -35,5 +35,48 @@ function xmldb_repository_dropbox_upgrade($oldversion) {
// Automatically generated Moodle v3.9.0 release upgrade line.
// Put any upgrade step following this.
if ($oldversion < 2021052501) {
$key = get_config('dropbox', 'dropbox_key');
$secret = get_config('dropbox', 'dropbox_secret');
if ($key && $secret) {
$params = [
'name' => 'Dropbox',
'clientid' => $key,
'clientsecret' => $secret,
'loginparamsoffline' => 'token_access_type=offline',
'image' => '',
'showonloginpage' => 0, // Internal services only.
];
$record = $DB->get_record('oauth2_issuer', ['name' => 'Dropbox'], 'id');
if (!$record) {
$params = array_merge($params, [
'timecreated' => time(),
'timemodified' => time(),
'usermodified' => time(),
'baseurl' => 0,
'sortorder' => '',
'loginparams' => '',
'requireconfirmation' => 1,
'alloweddomains' => '',
'loginscopes' => 'openid profile email',
'loginscopesoffline' => 'openid profile email',
]);
$id = $DB->insert_record('oauth2_issuer', $params);
} else {
$id = $record->id;
$params['id'] = $id;
$DB->update_record('oauth2_issuer', $params);
}
set_config('dropbox_issuerid', $id, 'dropbox');
unset_config('dropbox_key', 'dropbox');
unset_config('dropbox_secret', 'dropbox');
}
// Dropbox savepoint reached.
upgrade_plugin_savepoint(true, 2021052501, 'repository', 'dropbox');
}
return true;
}

View File

@ -0,0 +1,3 @@
apikey,repository_dropbox
secret,repository_dropbox
instruction,repository_dropbox

View File

@ -27,10 +27,9 @@ $string['crontask'] = 'Background processing for Dropbox repository';
$string['notitle'] = 'notitle';
$string['remember'] = 'Remember me';
$string['pluginname'] = 'Dropbox';
$string['apikey'] = 'Dropbox API key';
$string['dropbox'] = 'Dropbox';
$string['secret'] = 'Dropbox secret';
$string['instruction'] = 'You can get your API Key and secret from <a href="https://www.dropbox.com/developers/apps">Dropbox developers</a>. When setting up your key please select "Full Dropbox" as the "Access level".';
$string['issuer'] = 'OAuth2 service';
$string['issuer_help'] = 'Select the OAuth2 service that is configured to talk to the Dropbox API. If the service does not exist yet, you will need to create it.';
$string['cachelimit'] = 'Cache limit';
$string['cachelimit_info'] = 'Enter the maximum size of files (in bytes) to be cached on server for Dropbox aliases/shortcuts. Cached files will be served when the source is no longer available. Empty value or zero mean caching of all files regardless of size.';
$string['dropbox:view'] = 'View a Dropbox folder';
@ -38,3 +37,8 @@ $string['logoutdesc'] = '(Logout when you finish using Dropbox)';
$string['oauth2redirecturi'] = 'OAuth 2 Redirect URI';
$string['privacy:metadata:repository_dropbox'] = 'The Dropbox repository plugin does not store any personal data, but does transmit user data from Moodle to the remote system.';
$string['privacy:metadata:repository_dropbox:query'] = 'The Dropbox repository user search text query.';
// Deprecated since Moodle 4.0.
$string['apikey'] = 'Dropbox API key';
$string['secret'] = 'Dropbox secret';
$string['instruction'] = 'You can get your API Key and secret from <a href="https://www.dropbox.com/developers/apps">Dropbox developers</a>. When setting up your key please select "Full Dropbox" as the "Access level".';

View File

@ -59,13 +59,8 @@ class repository_dropbox extends repository {
]);
// Create the dropbox API instance.
$key = get_config('dropbox', 'dropbox_key');
$secret = get_config('dropbox', 'dropbox_secret');
$this->dropbox = new repository_dropbox\dropbox(
$key,
$secret,
$returnurl
);
$issuer = \core\oauth2\api::get_issuer(get_config('dropbox', 'dropbox_issuerid'));
$this->dropbox = new repository_dropbox\dropbox($issuer, $returnurl);
}
/**
@ -436,7 +431,7 @@ class repository_dropbox extends repository {
}
/**
* Check if moodle has got access token and secret.
* Check if the dropbox is logged in via the oauth process.
*
* @inheritDocs
*/
@ -507,29 +502,15 @@ class repository_dropbox extends repository {
*/
public static function type_config_form($mform, $classname = 'repository') {
parent::type_config_form($mform);
$key = get_config('dropbox', 'dropbox_key');
$secret = get_config('dropbox', 'dropbox_secret');
if (empty($key)) {
$key = '';
$options = [];
$issuers = \core\oauth2\api::get_all_issuers();
foreach ($issuers as $issuer) {
$options[$issuer->get('id')] = s($issuer->get('name'));
}
if (empty($secret)) {
$secret = '';
}
$mform->addElement('text', 'dropbox_key', get_string('apikey', 'repository_dropbox'), array('value'=>$key,'size' => '40'));
$mform->setType('dropbox_key', PARAM_RAW_TRIMMED);
$mform->addElement('text', 'dropbox_secret', get_string('secret', 'repository_dropbox'), array('value'=>$secret,'size' => '40'));
$mform->addRule('dropbox_key', get_string('required'), 'required', null, 'client');
$mform->addRule('dropbox_secret', get_string('required'), 'required', null, 'client');
$mform->setType('dropbox_secret', PARAM_RAW_TRIMMED);
$mform->addElement('static', null, '', get_string('instruction', 'repository_dropbox'));
$mform->addElement('static', null,
get_string('oauth2redirecturi', 'repository_dropbox'),
self::get_oauth2callbackurl()->out()
);
$strrequired = get_string('required');
$mform->addElement('select', 'dropbox_issuerid', get_string('issuer', 'repository_dropbox'), $options);
$mform->addHelpButton('dropbox_issuerid', 'issuer', 'repository_dropbox');
$mform->addRule('dropbox_issuerid', $strrequired, 'required', null, 'client');
$mform->addElement('text', 'dropbox_cachelimit', get_string('cachelimit', 'repository_dropbox'), array('size' => '40'));
$mform->addRule('dropbox_cachelimit', null, 'numeric', null, 'client');
$mform->setType('dropbox_cachelimit', PARAM_INT);
@ -544,13 +525,9 @@ class repository_dropbox extends repository {
* @return mixed
*/
public function set_option($options = []) {
if (!empty($options['dropbox_key'])) {
set_config('dropbox_key', trim($options['dropbox_key']), 'dropbox');
unset($options['dropbox_key']);
}
if (!empty($options['dropbox_secret'])) {
set_config('dropbox_secret', trim($options['dropbox_secret']), 'dropbox');
unset($options['dropbox_secret']);
if (!empty($options['dropbox_issuerid'])) {
set_config('dropbox_issuerid', trim($options['dropbox_issuerid']), 'dropbox');
unset($options['dropbox_issuerid']);
}
if (!empty($options['dropbox_cachelimit'])) {
$this->cachelimit = (int) trim($options['dropbox_cachelimit']);
@ -567,16 +544,13 @@ class repository_dropbox extends repository {
* @return mixed
*/
public function get_option($config = '') {
if ($config === 'dropbox_key') {
return trim(get_config('dropbox', 'dropbox_key'));
} else if ($config === 'dropbox_secret') {
return trim(get_config('dropbox', 'dropbox_secret'));
if ($config === 'dropbox_issuerid') {
return trim(get_config('dropbox', 'dropbox_issuerid'));
} else if ($config === 'dropbox_cachelimit') {
return $this->max_cache_bytes();
} else {
$options = parent::get_option();
$options['dropbox_key'] = trim(get_config('dropbox', 'dropbox_key'));
$options['dropbox_secret'] = trim(get_config('dropbox', 'dropbox_secret'));
$options['dropbox_issuerid'] = trim(get_config('dropbox', 'dropbox_issuerid'));
$options['dropbox_cachelimit'] = $this->max_cache_bytes();
}
@ -601,8 +575,7 @@ class repository_dropbox extends repository {
*/
public static function get_type_option_names() {
return [
'dropbox_key',
'dropbox_secret',
'dropbox_issuerid',
'pluginname',
'dropbox_cachelimit',
];

View File

@ -41,11 +41,8 @@ class repository_dropbox_generator extends testing_repository_generator {
*/
protected function prepare_type_record(array $record) {
$record = parent::prepare_type_record($record);
if (!isset($record['dropbox_key'])) {
$record['dropbox_key'] = 'key';
}
if (!isset($record['dropbox_secret'])) {
$record['dropbox_secret'] = 'secret';
if (!isset($record['dropbox_issuerid'])) {
$record['dropbox_issuerid'] = 0;
}
if (!isset($record['dropbox_cachelimit'])) {
$record['dropbox_cachelimit'] = 0;

View File

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

View File

@ -100,10 +100,20 @@ class core_repository_generator_testcase extends advanced_testcase {
$this->assertEquals('Custom Flickr',
$DB->get_field('repository_instances', 'name', array('typeid' => $flickr->id), MUST_EXIST));
// Create a dropbox oauth issuer.
$this->setAdminUser();
$params = [
'name' => 'Dropbox',
'clientid' => 'key',
'clientsecret' => 'secret',
'loginparamsoffline' => 'token_access_type=offline',
'image' => '',
'showonloginpage' => 1,
];
$issuer = \core\oauth2\api::create_issuer((object)$params);
$record = new stdClass();
$record->pluginname = 'Custom Dropbox';
$record->dropbox_key = '12345';
$record->dropbox_secret = '67890';
$record->dropbox_issuerid = $issuer->get('id');
$record->dropbox_cachelimit = '123';
$dropbox = $this->getDataGenerator()->create_repository_type('dropbox', $record);

View File

@ -480,8 +480,7 @@ class core_repositorylib_testcase extends advanced_testcase {
// Check that a user can view SOME repositories when logged in as someone else.
$params = new stdClass();
$params->name = 'Dropbox';
$params->dropbox_key = 'key';
$params->dropbox_secret = 'secret';
$params->dropbox_issuerid = '2';
$privaterepoid = $this->getDataGenerator()->create_repository('dropbox')->id;
$notprivaterepoid = $this->getDataGenerator()->create_repository('upload')->id;