Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add privacy provider #101 #115

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 177 additions & 6 deletions classes/privacy/provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,193 @@

namespace tool_advancedreplace\privacy;

use core_privacy\local\metadata\collection;
use core_privacy\local\request\approved_contextlist;
use core_privacy\local\request\approved_userlist;
use core_privacy\local\request\contextlist;
use core_privacy\local\request\userlist;
use core_privacy\local\request\writer;
use tool_advancedreplace\db_search;
use tool_advancedreplace\files;

/**
* Privacy Subsystem implementation for tool_advancedreplace.
*
* @package tool_advancedreplace
* @copyright 2024 Catalyst IT Australia Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
class provider implements
\core_privacy\local\metadata\provider,
\core_privacy\local\request\plugin\provider,
\core_privacy\local\request\core_userlist_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(
db_search::TABLE,
[
'userid' => 'privacy:metadata:tool_advancedreplace_search:userid',
'usermodified' => 'privacy:metadata:tool_advancedreplace_search:usermodified',
],
'privacy:metadata:tool_advancedreplace_search'
);

$collection->add_database_table(
files::TABLE,
[
'userid' => 'privacy:metadata:tool_advancedreplace_files:userid',
'usermodified' => 'privacy:metadata:tool_advancedreplace_files:usermodified',
],
'privacy:metadata:tool_advancedreplace_search'
);
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 {
$contextlist = new contextlist();
$contextlist->add_system_context();
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) {
global $DB;

$userid = $contextlist->get_user()->id;
foreach ($contextlist as $context) {
if ($context->contextlevel == CONTEXT_SYSTEM) {
$dblist = [];
// DB searches with a matching userid.
$rows = $DB->get_records(db_search::TABLE, ['userid' => $userid]);
foreach ($rows as $row) {
$dblist[] = [
'userid' => $userid,
'timecreated' => $row->timecreated,
'search' => $row->search,
];
}
// DB searches with a matching usermodified.
$rows = $DB->get_records(db_search::TABLE, ['usermodified' => $userid]);
foreach ($rows as $row) {
$dblist[] = [
'usermodified' => $userid,
'timemodified' => $row->timemodified,
'search' => $row->search,
];
}
writer::with_context($context)->export_data(
[get_string('privacy:metadata:tool_advancedreplace_search', 'tool_advancedreplace')],
(object) $dblist
);

$filelist = [];
// File searches with a matching userid.
$rows = $DB->get_records(files::TABLE, ['userid' => $userid]);
foreach ($rows as $row) {
$filelist[] = [
'userid' => $userid,
'timecreated' => $row->timecreated,
'search' => $row->pattern,
];
}
// File searches with a matching usermodified.
$rows = $DB->get_records(files::TABLE, ['usermodified' => $userid]);
foreach ($rows as $row) {
$filelist[] = [
'usermodified' => $userid,
'timemodified' => $row->timemodified,
'search' => $row->pattern,
];
}
writer::with_context($context)->export_data(
[get_string('privacy:metadata:tool_advancedreplace_files', 'tool_advancedreplace')],
(object) $filelist
);
}
}
}

/**
* Delete all data for all users in the specified context.
*
* @param \context $context The specific context to delete data for.
*/
public static function delete_data_for_all_users_in_context(\context $context) {
global $DB;
if ($context->contextlevel == CONTEXT_SYSTEM) {
$DB->delete_records(db_search::TABLE, []);
$DB->delete_records(files::TABLE, []);
}
}

/**
* 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) {
global $DB;
$userid = $contextlist->get_user()->id;
foreach ($contextlist as $context) {
if ($context->contextlevel == CONTEXT_SYSTEM) {
$DB->delete_records(db_search::TABLE, ['userid' => $userid]);
$DB->delete_records(db_search::TABLE, ['usermodified' => $userid]);
$DB->delete_records(files::TABLE, ['userid' => $userid]);
$DB->delete_records(files::TABLE, ['usermodified' => $userid]);
}
}
}

/**
* Get the list of users who have data within a context.
*
* @param userlist $userlist The userlist containing the list of users who have data in this context/plugin combination.
*/
public static function get_users_in_context(userlist $userlist) {
$context = $userlist->get_context();
if ($context->contextlevel == CONTEXT_SYSTEM) {
$sql = "SELECT * FROM {tool_advancedreplace_search}";
$userlist->add_from_sql('userid', $sql, []);
$userlist->add_from_sql('usermodified', $sql, []);

$sql = "SELECT * FROM {tool_advancedreplace_files}";
$userlist->add_from_sql('userid', $sql, []);
$userlist->add_from_sql('usermodified', $sql, []);
}
}

/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
* Delete multiple users within a single context.
*
* @return string
* @param approved_userlist $userlist The approved context and user information to delete information for.
*/
public static function get_reason(): string {
return 'privacy:metadata';
public static function delete_data_for_users(approved_userlist $userlist) {
global $DB;
$context = $userlist->get_context();
if ($context->contextlevel == CONTEXT_SYSTEM) {
$users = $userlist->get_users();
foreach ($users as $user) {
$DB->delete_records(db_search::TABLE, ['userid' => $user->id]);
$DB->delete_records(db_search::TABLE, ['usermodified' => $user->id]);
$DB->delete_records(files::TABLE, ['userid' => $user->id]);
$DB->delete_records(files::TABLE, ['usermodified' => $user->id]);
}
}
}
}
7 changes: 6 additions & 1 deletion lang/en/tool_advancedreplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,9 @@
$string['settings:includetables'] = 'Include tables';
$string['settings:includetables_help'] = 'When set all searches will be restricted to only the specified tables and those defined in search options. Each table should be on a new line, and can include a column by formatting it as <code>tablename:columnname</code>.';
$string['pluginname'] = 'Advanced Search and Replace';
$string['privacy:metadata'] = 'The plugin does not store any personal data.';
$string['privacy:metadata:tool_advancedreplace_search'] = 'Advanced db search';
$string['privacy:metadata:tool_advancedreplace_search:userid'] = 'User who created the db search';
$string['privacy:metadata:tool_advancedreplace_search:usermodified'] = 'User who modified the db search';
$string['privacy:metadata:tool_advancedreplace_files'] = 'Advanced files search';
$string['privacy:metadata:tool_advancedreplace_files:userid'] = 'User who created the files search';
$string['privacy:metadata:tool_advancedreplace_files:usermodified'] = 'User who modified the files search';
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
defined('MOODLE_INTERNAL') || die();


$plugin->version = 2025012200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2025012200;
$plugin->version = 2025013000; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2025013000;
$plugin->requires = 2020110900; // Requires this Moodle version.
$plugin->component = 'tool_advancedreplace';
$plugin->maturity = MATURITY_STABLE;
Expand Down
Loading