Skip to content

Commit

Permalink
Merge pull request #157 from catalyst/issue147
Browse files Browse the repository at this point in the history
Issue #147: Added privacy API implementation
  • Loading branch information
Peterburnett authored Feb 14, 2020
2 parents f2cde8e + 544ed17 commit 9c8f280
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@ script:
- moodle-plugin-ci savepoints
- moodle-plugin-ci mustache
- moodle-plugin-ci grunt
- moodle-plugin-ci phpunit
- moodle-plugin-ci phpunit -vvv
- moodle-plugin-ci behat
# Privacy Specific tests
- /home/travis/build/moodle/vendor/bin/phpunit "provider_testcase" /home/travis/build/moodle/privacy/tests/provider_test.php
- /home/travis/build/moodle/vendor/bin/phpunit "tool_dataprivacy_expired_contexts_testcase" /home/travis/build/moodle/admin/tool/dataprivacy/tests/expired_contexts_test.php
122 changes: 122 additions & 0 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
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\approved_userlist;
use core_privacy\local\request\writer;
use core_privacy\local\request\userlist;

/**
* Class provider
Expand Down Expand Up @@ -61,4 +66,121 @@ public static function get_metadata(collection $collection) : collection {

return $collection;
}

/**
* Get the list of contexts that contain user information for the given user.
*
* @param int $userid the userid to search.
* @return contextlist the contexts in which data is contained.
*/
public static function get_contexts_for_userid(int $userid) : contextlist {
$contextlist = new \core_privacy\local\request\contextlist();
$contextlist->add_user_context($userid);
$contextlist->add_system_context();
return $contextlist;
}

/**
* Gets the list of users who have data with a context.
*
* @param userlist $userlist the userlist containing users who have data in this context.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
// If current context is system, all users are contained within, get all users.
if ($context->contextlevel == CONTEXT_SYSTEM) {
$sql = "
SELECT *
FROM {tool_mfa}";
$userlist->add_from_sql('userid', $sql, array());
}
}

/**
* Exports all data stored in provided contexts for user.
*
* @param approved_contextlist $contextlist the list of contexts to export for.
*/
public static function export_user_data(approved_contextlist $contextlist) {
global $DB;
$userid = $contextlist->get_user()->id;
foreach ($contextlist as $context) {

// If not in system context, exit loop.
if ($context->contextlevel == CONTEXT_SYSTEM) {

$parentclass = array();

// Get records for user ID.
$rows = $DB->get_records('tool_mfa', array('userid' => $userid));

if (count($rows) > 0) {
$i = 0;
foreach ($rows as $row) {
$parentclass[$i]['userid'] = $row->userid;
$timecreated = \core_privacy\local\request\transform::datetime($row->timecreated);
$parentclass[$i]['factor'] = $row->factor;
$parentclass[$i]['timecreated'] = $timecreated;
$parentclass[$i]['createdfromip'] = $row->createdfromip;
$timemodified = \core_privacy\local\request\transform::datetime($row->timemodified);
$parentclass[$i]['timemodified'] = $timemodified;
$lastverified = \core_privacy\local\request\transform::datetime($row->lastverified);
$parentclass[$i]['lastverified'] = $lastverified;
$parentclass[$i]['revoked'] = $row->revoked;
$i++;
}
}

writer::with_context($context)->export_data(
[get_string('privacy:metadata:tool_mfa', 'tool_mfa')],
(object) $parentclass);
}
}
}

/**
* Deletes data for all users in context.
*
* @param context $context The context to delete for.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
global $DB;
// All data contained in system context.
if ($context->contextlevel == CONTEXT_SYSTEM) {
$sql = "
DELETE
FROM {tool_mfa}";
$DB->execute($sql);
}
}

/**
* Deletes all data in all provided contexts for user.
*
* @param approved_contextlist $contextlist the list of contexts to delete for.
*/
public static function delete_data_for_user(approved_contextlist $contextlist) {
global $DB;
$userid = $contextlist->get_user()->id;
foreach ($contextlist as $context) {
// If not in system context, skip context.
if ($context->contextlevel == CONTEXT_SYSTEM) {
$sql = "DELETE
FROM {tool_mfa} mfa
WHERE mfa.userid = :userid";

$DB->execute($sql, array('userid' => $userid));
}
}
}

public static function delete_data_for_users(approved_userlist $userlist) {
$users = $userlist->get_users();
foreach ($users as $user) {
// Create contextlist.
$contextlist = new approved_contextlist($user, 'tool_mfa', array(CONTEXT_SYSTEM));
// Call delete data.
self::delete_data_for_user($contextlist);
}
}
}

0 comments on commit 9c8f280

Please sign in to comment.