Skip to content

Commit e0de954

Browse files
author
David Monllao
committed
Merge branch 'MDL-62047-33' of git://github.com/andrewnicols/moodle into MOODLE_33_STABLE
2 parents 59fc010 + 6480c40 commit e0de954

File tree

5 files changed

+490
-1
lines changed

5 files changed

+490
-1
lines changed

lang/en/userkey.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@
3333
$string['keyvaliduntil_help'] = 'Select an optional date after which the key will no longer be valid (recommended for added security).';
3434
$string['keyvalue'] = 'Key value';
3535
$string['newuserkey'] = 'New user key';
36+
$string['privacy:metadata:user_private_key:script'] = 'The script which is responsible for the user key.';
37+
$string['privacy:metadata:user_private_key:value'] = 'The value of the key.';
38+
$string['privacy:metadata:user_private_key:userid'] = 'The user associated with the key.';
39+
$string['privacy:metadata:user_private_key:instance'] = 'The instance of the script.';
40+
$string['privacy:metadata:user_private_key:iprestriction'] = 'The IP address range that this key can be used from.';
41+
$string['privacy:metadata:user_private_key:validuntil'] = 'The date and time that the private key is valid until.';
42+
$string['privacy:metadata:user_private_key:timecreated'] = 'The date and time that the key was created.';
43+
$string['privacy:metadata:user_private_key'] = 'Private keys for the user.';
3644
$string['userkey'] = 'User key';
3745
$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.';
3846
$string['userkeys'] = 'User keys';

lib/classes/component.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ protected static function fetch_subsystems() {
480480
'tag' => $CFG->dirroot.'/tag',
481481
'timezones' => null,
482482
'user' => $CFG->dirroot.'/user',
483-
'userkey' => null,
483+
'userkey' => $CFG->dirroot.'/lib/userkey',
484484
'webservice' => $CFG->dirroot.'/webservice',
485485
);
486486

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* Privacy class for requesting user data.
19+
*
20+
* @package core_userkey
21+
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
namespace core_userkey\privacy;
26+
27+
defined('MOODLE_INTERNAL') || die();
28+
29+
use \core_privacy\local\metadata\collection;
30+
use \core_privacy\local\request\transform;
31+
use \core_privacy\local\request\writer;
32+
33+
/**
34+
* Privacy class for requesting user data.
35+
*
36+
* @package core_userkey
37+
* @copyright 2018 Andrew Nicols <andrew@nicols.co.uk>
38+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39+
*/
40+
class provider implements
41+
\core_privacy\local\metadata\provider,
42+
43+
\core_privacy\local\request\subsystem\plugin_provider {
44+
45+
/**
46+
* Returns meta data about this system.
47+
*
48+
* @param collection $collection The initialised collection to add items to.
49+
* @return collection A listing of user data stored through this system.
50+
*/
51+
public static function get_metadata(collection $collection) : collection {
52+
$collection->add_database_table('user_private_key', [
53+
'script' => 'privacy:metadata:user_private_key:script',
54+
'value' => 'privacy:metadata:user_private_key:value',
55+
'userid' => 'privacy:metadata:user_private_key:userid',
56+
'instance' => 'privacy:metadata:user_private_key:instance',
57+
'iprestriction' => 'privacy:metadata:user_private_key:iprestriction',
58+
'validuntil' => 'privacy:metadata:user_private_key:validuntil',
59+
'timecreated' => 'privacy:metadata:user_private_key:timecreated',
60+
], 'privacy:metadata:user_private_key');
61+
62+
return $collection;
63+
}
64+
65+
/**
66+
* Exports the data relating to user keys for the specified scripts and instance, within the specified
67+
* context/subcontext.
68+
*
69+
* @param \context $context Context owner of the data.
70+
* @param array $subcontext Context owner of the data.
71+
* @param string $script The owner of the data (usually a component name).
72+
* @param int $instance The instance owner of the data.
73+
*/
74+
public static function export_userkeys(\context $context, array $subcontext, $script, $instance = null) {
75+
global $DB, $USER;
76+
77+
$searchparams = [
78+
'script' => $script,
79+
'userid' => $USER->id,
80+
];
81+
82+
if (null !== $instance) {
83+
$searchparams['instance'] = $instance;
84+
}
85+
86+
$keys = $DB->get_recordset('user_private_key', $searchparams);
87+
$keydata = [];
88+
foreach ($keys as $key) {
89+
$keydata[] = (object) [
90+
'script' => $key->script,
91+
'instance' => $key->instance,
92+
'iprestriction' => $key->iprestriction,
93+
'validuntil' => transform::datetime($key->validuntil),
94+
'timecreated' => transform::datetime($key->timecreated),
95+
];
96+
}
97+
$keys->close();
98+
99+
if (!empty($keydata)) {
100+
$data = (object) [
101+
'keys' => $keydata,
102+
];
103+
104+
writer::with_context($context)->export_related_data($subcontext, 'userkeys', $data);
105+
}
106+
}
107+
108+
/**
109+
* Deletes all userkeys for a script.
110+
*
111+
* @param string $script The owner of the data (usually a component name).
112+
* @param int $userid The owner of the data.
113+
* @param int $instance The instance owner of the data.
114+
*/
115+
public static function delete_userkeys($script, $userid = null, $instance = null) {
116+
global $DB;
117+
118+
$searchparams = [
119+
'script' => $script,
120+
];
121+
122+
if (null !== $userid) {
123+
$searchparams['userid'] = $userid;
124+
}
125+
126+
if (null !== $instance) {
127+
$searchparams['instance'] = $instance;
128+
}
129+
130+
$DB->delete_records('user_private_key', $searchparams);
131+
}
132+
}

0 commit comments

Comments
 (0)