Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 * Privacy Subsystem implementation for tool_mobile.
18
 *
19
 * @package    tool_mobile
20
 * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
21
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 */
23
namespace tool_mobile\privacy;
24
defined('MOODLE_INTERNAL') || die();
25
use core_privacy\local\request\writer;
26
use core_privacy\local\metadata\collection;
27
use core_privacy\local\request\contextlist;
28
use core_privacy\local\request\approved_contextlist;
29
use core_privacy\local\request\approved_userlist;
30
use core_privacy\local\request\transform;
31
use core_privacy\local\request\userlist;
32
 
33
/**
34
 * Privacy provider for tool_mobile.
35
 *
36
 * @copyright  2018 Carlos Escobedo <carlos@moodle.com>
37
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38
 */
39
class provider implements
40
    \core_privacy\local\metadata\provider,
41
    \core_privacy\local\request\core_userlist_provider,
42
    \core_privacy\local\request\user_preference_provider,
43
    \core_privacy\local\request\plugin\provider {
44
    /**
45
     * Returns meta data about this system.
46
     *
47
     * @param   collection     $collection The initialised item collection to add items to.
48
     * @return  collection     A listing of user data stored through this system.
49
     */
50
    public static function get_metadata(collection $collection): collection {
51
        // There is a one user preference.
52
        $collection->add_user_preference('tool_mobile_autologin_request_last',
53
            'privacy:metadata:preference:tool_mobile_autologin_request_last');
54
        $collection->add_subsystem_link('core_userkey', [], 'privacy:metadata:core_userkey');
55
 
56
        return $collection;
57
    }
58
    /**
59
     * Get the list of contexts that contain user information for the specified user.
60
     *
61
     * @param int $userid The user to search.
62
     * @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
63
     */
64
    public static function get_contexts_for_userid(int $userid): contextlist {
65
        $sql = "SELECT ctx.id
66
                  FROM {user_private_key} k
67
                  JOIN {user} u ON k.userid = u.id
68
                  JOIN {context} ctx ON ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel
69
                 WHERE k.userid = :userid AND (k.script = 'tool_mobile' OR k.script = 'tool_mobile/qrlogin')";
70
        $params = ['userid' => $userid, 'contextlevel' => CONTEXT_USER];
71
        $contextlist = new contextlist();
72
        $contextlist->add_from_sql($sql, $params);
73
 
74
        return $contextlist;
75
    }
76
 
77
    /**
78
     * Get the list of users who have data within a context.
79
     *
80
     * @param   userlist    $userlist   The userlist containing the list of users who have data in this context/plugin combination.
81
     */
82
    public static function get_users_in_context(userlist $userlist) {
83
        $context = $userlist->get_context();
84
 
85
        if (!is_a($context, \context_user::class)) {
86
            return;
87
        }
88
 
89
        // Add users based on userkey.
90
        \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'tool_mobile');
91
        \core_userkey\privacy\provider::get_user_contexts_with_script($userlist, $context, 'tool_mobile/qrlogin');
92
    }
93
 
94
    /**
95
     * Export all user data for the specified user, in the specified contexts.
96
     *
97
     * @param approved_contextlist $contextlist The approved contexts to export information for.
98
     */
99
    public static function export_user_data(approved_contextlist $contextlist) {
100
        // If the user has data, then only the CONTEXT_USER should be present so get the first context.
101
        $contexts = $contextlist->get_contexts();
102
        if (count($contexts) == 0) {
103
            return;
104
        }
105
        $context = reset($contexts);
106
        // Sanity check that context is at the user context level, then get the userid.
107
        if ($context->contextlevel !== CONTEXT_USER) {
108
            return;
109
        }
110
        // Export associated userkeys.
111
        \core_userkey\privacy\provider::export_userkeys($context, [], 'tool_mobile');
112
        \core_userkey\privacy\provider::export_userkeys($context, [], 'tool_mobile/qrlogin');
113
    }
114
    /**
115
     * Export all user preferences for the plugin.
116
     *
117
     * @param   int         $userid The userid of the user whose data is to be exported.
118
     */
119
    public static function export_user_preferences(int $userid) {
120
        $autologinrequestlast = get_user_preferences('tool_mobile_autologin_request_last', null, $userid);
121
        if ($autologinrequestlast !== null) {
122
            $time = transform::datetime($autologinrequestlast);
123
            writer::export_user_preference('tool_mobile',
124
                'tool_mobile_autologin_request_last',
125
                $time,
126
                get_string('privacy:metadata:preference:tool_mobile_autologin_request_last', 'tool_mobile')
127
            );
128
        }
129
    }
130
    /**
131
     * Delete all use data which matches the specified deletion_criteria.
132
     *
133
     * @param context $context A user context.
134
     */
135
    public static function delete_data_for_all_users_in_context(\context $context) {
136
        // Sanity check that context is at the user context level, then get the userid.
137
        if ($context->contextlevel !== CONTEXT_USER) {
138
            return;
139
        }
140
        $userid = $context->instanceid;
141
        // Delete all the userkeys.
142
        \core_userkey\privacy\provider::delete_userkeys('tool_mobile', $userid);
143
        \core_userkey\privacy\provider::delete_userkeys('tool_mobile/qrlogin', $userid);
144
    }
145
    /**
146
     * Delete all user data for the specified user, in the specified contexts.
147
     *
148
     * @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
149
     */
150
    public static function delete_data_for_user(approved_contextlist $contextlist) {
151
        // If the user has data, then only the user context should be present so get the first context.
152
        $contexts = $contextlist->get_contexts();
153
        if (count($contexts) == 0) {
154
            return;
155
        }
156
        $context = reset($contexts);
157
        // Sanity check that context is at the user context level, then get the userid.
158
        if ($context->contextlevel !== CONTEXT_USER) {
159
            return;
160
        }
161
        $userid = $context->instanceid;
162
        // Delete all the userkeys.
163
        \core_userkey\privacy\provider::delete_userkeys('tool_mobile', $userid);
164
        \core_userkey\privacy\provider::delete_userkeys('tool_mobile/qrlogin', $userid);
165
    }
166
 
167
    /**
168
     * Delete multiple users within a single context.
169
     *
170
     * @param   approved_userlist       $userlist The approved context and user information to delete information for.
171
     */
172
    public static function delete_data_for_users(approved_userlist $userlist) {
173
        global $DB;
174
        $context = $userlist->get_context();
175
        $userids = $userlist->get_userids();
176
        $userid = reset($userids);
177
 
178
        // Only deleting data for the user ID in that user's user context should be valid.
179
        if ($context->contextlevel !== CONTEXT_USER || count($userids) != 1 || $userid != $context->instanceid) {
180
            return;
181
        }
182
 
183
        // Delete all the userkeys.
184
        \core_userkey\privacy\provider::delete_userkeys('tool_mobile', $userid);
185
        \core_userkey\privacy\provider::delete_userkeys('tool_mobile/qrlogin', $userid);
186
    }
187
}