Permalink
Browse files

Merge branch 'MDL-62047-33' of git://github.com/andrewnicols/moodle i…

…nto MOODLE_33_STABLE
  • Loading branch information...
dmonllao committed Apr 23, 2018
2 parents 59fc010 + 6480c40 commit e0de95472879fdd08b8e7b3865a33a509b65bed4
@@ -33,6 +33,14 @@
$string['keyvaliduntil_help'] = 'Select an optional date after which the key will no longer be valid (recommended for added security).';
$string['keyvalue'] = 'Key value';
$string['newuserkey'] = 'New user key';
$string['privacy:metadata:user_private_key:script'] = 'The script which is responsible for the user key.';
$string['privacy:metadata:user_private_key:value'] = 'The value of the key.';
$string['privacy:metadata:user_private_key:userid'] = 'The user associated with the key.';
$string['privacy:metadata:user_private_key:instance'] = 'The instance of the script.';
$string['privacy:metadata:user_private_key:iprestriction'] = 'The IP address range that this key can be used from.';
$string['privacy:metadata:user_private_key:validuntil'] = 'The date and time that the private key is valid until.';
$string['privacy:metadata:user_private_key:timecreated'] = 'The date and time that the key was created.';
$string['privacy:metadata:user_private_key'] = 'Private keys for the user.';
$string['userkey'] = 'User key';
$string['userkey_help'] = 'Select a saved key that will give users access to the data published by this export plugin, without having to log into Moodle. Select "Create a new user key" to generate a new key when submitting this form.';
$string['userkeys'] = 'User keys';
@@ -480,7 +480,7 @@ protected static function fetch_subsystems() {
'tag' => $CFG->dirroot.'/tag',
'timezones' => null,
'user' => $CFG->dirroot.'/user',
'userkey' => null,
'userkey' => $CFG->dirroot.'/lib/userkey',
'webservice' => $CFG->dirroot.'/webservice',
);
@@ -0,0 +1,132 @@
<?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/>.
/**
* Privacy class for requesting user data.
*
* @package core_userkey
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_userkey\privacy;
defined('MOODLE_INTERNAL') || die();
use \core_privacy\local\metadata\collection;
use \core_privacy\local\request\transform;
use \core_privacy\local\request\writer;
/**
* Privacy class for requesting user data.
*
* @package core_userkey
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\subsystem\plugin_provider {
/**
* Returns meta data about this system.
*
* @param collection $collection The initialised collection to add items to.
* @return collection A listing of user data stored through this system.
*/
public static function get_metadata(collection $collection) : collection {
$collection->add_database_table('user_private_key', [
'script' => 'privacy:metadata:user_private_key:script',
'value' => 'privacy:metadata:user_private_key:value',
'userid' => 'privacy:metadata:user_private_key:userid',
'instance' => 'privacy:metadata:user_private_key:instance',
'iprestriction' => 'privacy:metadata:user_private_key:iprestriction',
'validuntil' => 'privacy:metadata:user_private_key:validuntil',
'timecreated' => 'privacy:metadata:user_private_key:timecreated',
], 'privacy:metadata:user_private_key');
return $collection;
}
/**
* Exports the data relating to user keys for the specified scripts and instance, within the specified
* context/subcontext.
*
* @param \context $context Context owner of the data.
* @param array $subcontext Context owner of the data.
* @param string $script The owner of the data (usually a component name).
* @param int $instance The instance owner of the data.
*/
public static function export_userkeys(\context $context, array $subcontext, $script, $instance = null) {
global $DB, $USER;
$searchparams = [
'script' => $script,
'userid' => $USER->id,
];
if (null !== $instance) {
$searchparams['instance'] = $instance;
}
$keys = $DB->get_recordset('user_private_key', $searchparams);
$keydata = [];
foreach ($keys as $key) {
$keydata[] = (object) [
'script' => $key->script,
'instance' => $key->instance,
'iprestriction' => $key->iprestriction,
'validuntil' => transform::datetime($key->validuntil),
'timecreated' => transform::datetime($key->timecreated),
];
}
$keys->close();
if (!empty($keydata)) {
$data = (object) [
'keys' => $keydata,
];
writer::with_context($context)->export_related_data($subcontext, 'userkeys', $data);
}
}
/**
* Deletes all userkeys for a script.
*
* @param string $script The owner of the data (usually a component name).
* @param int $userid The owner of the data.
* @param int $instance The instance owner of the data.
*/
public static function delete_userkeys($script, $userid = null, $instance = null) {
global $DB;
$searchparams = [
'script' => $script,
];
if (null !== $userid) {
$searchparams['userid'] = $userid;
}
if (null !== $instance) {
$searchparams['instance'] = $instance;
}
$DB->delete_records('user_private_key', $searchparams);
}
}
Oops, something went wrong.

0 comments on commit e0de954

Please sign in to comment.