Skip to content

Commit 544ed17

Browse files
committed
Added Privacy API implementation
1 parent f2cde8e commit 544ed17

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

.travis.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,8 @@ script:
4949
- moodle-plugin-ci savepoints
5050
- moodle-plugin-ci mustache
5151
- moodle-plugin-ci grunt
52-
- moodle-plugin-ci phpunit
52+
- moodle-plugin-ci phpunit -vvv
5353
- moodle-plugin-ci behat
54+
# Privacy Specific tests
55+
- /home/travis/build/moodle/vendor/bin/phpunit "provider_testcase" /home/travis/build/moodle/privacy/tests/provider_test.php
56+
- /home/travis/build/moodle/vendor/bin/phpunit "tool_dataprivacy_expired_contexts_testcase" /home/travis/build/moodle/admin/tool/dataprivacy/tests/expired_contexts_test.php

classes/privacy/provider.php

+122
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
defined('MOODLE_INTERNAL') || die;
2828

2929
use core_privacy\local\metadata\collection;
30+
use core_privacy\local\request\contextlist;
31+
use core_privacy\local\request\approved_contextlist;
32+
use core_privacy\local\request\approved_userlist;
33+
use core_privacy\local\request\writer;
34+
use core_privacy\local\request\userlist;
3035

3136
/**
3237
* Class provider
@@ -61,4 +66,121 @@ public static function get_metadata(collection $collection) : collection {
6166

6267
return $collection;
6368
}
69+
70+
/**
71+
* Get the list of contexts that contain user information for the given user.
72+
*
73+
* @param int $userid the userid to search.
74+
* @return contextlist the contexts in which data is contained.
75+
*/
76+
public static function get_contexts_for_userid(int $userid) : contextlist {
77+
$contextlist = new \core_privacy\local\request\contextlist();
78+
$contextlist->add_user_context($userid);
79+
$contextlist->add_system_context();
80+
return $contextlist;
81+
}
82+
83+
/**
84+
* Gets the list of users who have data with a context.
85+
*
86+
* @param userlist $userlist the userlist containing users who have data in this context.
87+
*/
88+
public static function get_users_in_context(userlist $userlist) {
89+
$context = $userlist->get_context();
90+
// If current context is system, all users are contained within, get all users.
91+
if ($context->contextlevel == CONTEXT_SYSTEM) {
92+
$sql = "
93+
SELECT *
94+
FROM {tool_mfa}";
95+
$userlist->add_from_sql('userid', $sql, array());
96+
}
97+
}
98+
99+
/**
100+
* Exports all data stored in provided contexts for user.
101+
*
102+
* @param approved_contextlist $contextlist the list of contexts to export for.
103+
*/
104+
public static function export_user_data(approved_contextlist $contextlist) {
105+
global $DB;
106+
$userid = $contextlist->get_user()->id;
107+
foreach ($contextlist as $context) {
108+
109+
// If not in system context, exit loop.
110+
if ($context->contextlevel == CONTEXT_SYSTEM) {
111+
112+
$parentclass = array();
113+
114+
// Get records for user ID.
115+
$rows = $DB->get_records('tool_mfa', array('userid' => $userid));
116+
117+
if (count($rows) > 0) {
118+
$i = 0;
119+
foreach ($rows as $row) {
120+
$parentclass[$i]['userid'] = $row->userid;
121+
$timecreated = \core_privacy\local\request\transform::datetime($row->timecreated);
122+
$parentclass[$i]['factor'] = $row->factor;
123+
$parentclass[$i]['timecreated'] = $timecreated;
124+
$parentclass[$i]['createdfromip'] = $row->createdfromip;
125+
$timemodified = \core_privacy\local\request\transform::datetime($row->timemodified);
126+
$parentclass[$i]['timemodified'] = $timemodified;
127+
$lastverified = \core_privacy\local\request\transform::datetime($row->lastverified);
128+
$parentclass[$i]['lastverified'] = $lastverified;
129+
$parentclass[$i]['revoked'] = $row->revoked;
130+
$i++;
131+
}
132+
}
133+
134+
writer::with_context($context)->export_data(
135+
[get_string('privacy:metadata:tool_mfa', 'tool_mfa')],
136+
(object) $parentclass);
137+
}
138+
}
139+
}
140+
141+
/**
142+
* Deletes data for all users in context.
143+
*
144+
* @param context $context The context to delete for.
145+
*/
146+
public static function delete_data_for_all_users_in_context(\context $context) {
147+
global $DB;
148+
// All data contained in system context.
149+
if ($context->contextlevel == CONTEXT_SYSTEM) {
150+
$sql = "
151+
DELETE
152+
FROM {tool_mfa}";
153+
$DB->execute($sql);
154+
}
155+
}
156+
157+
/**
158+
* Deletes all data in all provided contexts for user.
159+
*
160+
* @param approved_contextlist $contextlist the list of contexts to delete for.
161+
*/
162+
public static function delete_data_for_user(approved_contextlist $contextlist) {
163+
global $DB;
164+
$userid = $contextlist->get_user()->id;
165+
foreach ($contextlist as $context) {
166+
// If not in system context, skip context.
167+
if ($context->contextlevel == CONTEXT_SYSTEM) {
168+
$sql = "DELETE
169+
FROM {tool_mfa} mfa
170+
WHERE mfa.userid = :userid";
171+
172+
$DB->execute($sql, array('userid' => $userid));
173+
}
174+
}
175+
}
176+
177+
public static function delete_data_for_users(approved_userlist $userlist) {
178+
$users = $userlist->get_users();
179+
foreach ($users as $user) {
180+
// Create contextlist.
181+
$contextlist = new approved_contextlist($user, 'tool_mfa', array(CONTEXT_SYSTEM));
182+
// Call delete data.
183+
self::delete_data_for_user($contextlist);
184+
}
185+
}
64186
}

0 commit comments

Comments
 (0)