forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'MDL-61958-master' of git://github.com/sarjona/moodle
- Loading branch information
Showing
5 changed files
with
245 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?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/>. | ||
|
||
/** | ||
* RSS language file. | ||
* | ||
* @package core_rss | ||
* @copyright 2018 Sara Arjona <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
$string['privacy:metadata:user_private_key'] = 'Information about the user\'s access keys used in cookieless scripts, such as RSS.'; | ||
$string['privacy:metadata:user_private_key:timecreated'] = 'The timestamp indicating when the key was created'; | ||
$string['privacy:metadata:user_private_key:userid'] = 'The ID of the user which is associated to this key'; | ||
$string['privacy:metadata:user_private_key:validuntil'] = 'The timestamp indicating when the key will expire'; | ||
$string['privacy:metadata:user_private_key:value'] = 'The token used for getting the ID of the user'; | ||
$string['rss'] = 'RSS'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?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_rss | ||
* @copyright 2018 Sara Arjona <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
namespace core_rss\privacy; | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
use \core_privacy\local\metadata\collection; | ||
use \core_privacy\local\request\contextlist; | ||
use \core_privacy\local\request\approved_contextlist; | ||
use \core_privacy\local\request\transform; | ||
use \core_privacy\local\request\writer; | ||
|
||
/** | ||
* Privacy class for requesting user data. | ||
* | ||
* @copyright 2018 Sara Arjona <[email protected]> | ||
* @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\plugin\provider { | ||
|
||
/** | ||
* Return the fields which contain personal data. | ||
* | ||
* @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', [ | ||
'value' => 'privacy:metadata:user_private_key:value', | ||
'userid' => 'privacy:metadata:user_private_key:userid', | ||
'validuntil' => 'privacy:metadata:user_private_key:validuntil', | ||
'timecreated' => 'privacy:metadata:user_private_key:timecreated' | ||
], 'privacy:metadata:user_private_key'); | ||
return $collection; | ||
} | ||
|
||
/** | ||
* Get the list of contexts that contain user information for the specified user. | ||
* | ||
* @param int $userid The user to search. | ||
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin. | ||
*/ | ||
public static function get_contexts_for_userid(int $userid) : contextlist { | ||
$sql = "SELECT ctx.id | ||
FROM {user_private_key} k | ||
JOIN {user} u ON k.userid = u.id | ||
JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel | ||
WHERE k.userid = :userid AND k.script = 'rss'"; | ||
|
||
$params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER]; | ||
|
||
$contextlist = new contextlist(); | ||
$contextlist->add_from_sql($sql, $params); | ||
return $contextlist; | ||
} | ||
|
||
/** | ||
* Export all user data for the specified user, in the specified contexts. | ||
* | ||
* @param approved_contextlist $contextlist The approved contexts to export information for. | ||
*/ | ||
public static function export_user_data(approved_contextlist $contextlist) { | ||
$results = static::get_records($contextlist->get_user()->id); | ||
$context = $contextlist->current(); | ||
if ($context->contextlevel == CONTEXT_USER) { | ||
foreach ($results as $result) { | ||
$context = \context_user::instance($result->userid); | ||
$subcontext = [ | ||
get_string('rss', 'rss'), | ||
transform::user($result->userid) | ||
]; | ||
$name = 'user_private_key-' . $result->id; | ||
$data = (object)[ | ||
'value' => $result->value, | ||
'iprestriction' => $result->iprestriction, | ||
'validuntil' => $result->validuntil, | ||
'timecreated' => transform::datetime($result->timecreated), | ||
]; | ||
writer::with_context($context)->export_related_data($subcontext, $name, $data); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Delete all use data which matches the specified deletion_criteria. | ||
* | ||
* @param context $context A user context. | ||
*/ | ||
public static function delete_data_for_all_users_in_context(\context $context) { | ||
// The information in user_private_key table is removed automaticaly when a user is deteled. | ||
} | ||
|
||
/** | ||
* Delete all user data for the specified user, in the specified contexts. | ||
* | ||
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for. | ||
*/ | ||
public static function delete_data_for_user(approved_contextlist $contextlist) { | ||
// The information in user_private_key table is removed automaticaly when a user is deteled. | ||
} | ||
|
||
/** | ||
* Get records related to this plugin and user. | ||
* | ||
* @param int $userid The user ID | ||
* @return array An array of records. | ||
*/ | ||
protected static function get_records(int $userid) : array { | ||
global $DB; | ||
|
||
return $DB->get_records('user_private_key', ['userid' => $userid, 'script' => 'rss']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?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/>. | ||
/** | ||
* Base class for unit tests for core_rss. | ||
* | ||
* @package core_rss | ||
* @copyright 2018 Sara Arjona <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
|
||
defined('MOODLE_INTERNAL') || die(); | ||
|
||
use \core_privacy\tests\provider_testcase; | ||
use \core_rss\privacy\provider; | ||
use \core_privacy\local\request\writer; | ||
|
||
/** | ||
* Unit tests for rss\classes\privacy\provider.php | ||
* | ||
* @copyright 2018 Sara Arjona <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class core_rss_testcase extends provider_testcase { | ||
|
||
/** | ||
* Basic setup for these tests. | ||
*/ | ||
public function setUp() { | ||
$this->resetAfterTest(true); | ||
} | ||
|
||
/** | ||
* Test getting the context for the user ID related to this plugin. | ||
*/ | ||
public function test_get_contexts_for_userid() { | ||
$user = $this->getDataGenerator()->create_user(); | ||
$context = \context_user::instance($user->id); | ||
$key = get_user_key('rss', $user->id); | ||
|
||
$contextlist = provider::get_contexts_for_userid($user->id); | ||
$this->assertEquals($context->id, $contextlist->current()->id); | ||
} | ||
|
||
/** | ||
* Test that data is exported correctly for this plugin. | ||
*/ | ||
public function test_export_user_data() { | ||
global $DB; | ||
|
||
$user = $this->getDataGenerator()->create_user(); | ||
$context = \context_user::instance($user->id); | ||
$keyvalue = get_user_key('rss', $user->id); | ||
$key = $DB->get_record('user_private_key', ['value' => $keyvalue]); | ||
|
||
$writer = writer::with_context($context); | ||
$this->assertFalse($writer->has_any_data()); | ||
$this->export_context_data_for_user($user->id, $context, 'core_rss'); | ||
$data = $writer->get_related_data([get_string('rss', 'rss'), $user->id], 'user_private_key-' . $key->id); | ||
$this->assertEquals($key->value, $data->value); | ||
} | ||
} |