Skip to content

Commit 6118d53

Browse files
author
David Monllao
committed
Merge branch 'MDL-62560-34' into MOODLE_34_STABLE
2 parents c757afb + 674b262 commit 6118d53

File tree

129 files changed

+11931
-336
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+11931
-336
lines changed

admin/tool/dataprivacy/classes/api.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,7 @@ public static function set_expired_context_status(expired_context $expiredctx, $
988988
*/
989989
public static function add_request_contexts_with_status(contextlist_collection $clcollection, int $requestid, int $status) {
990990
$request = new data_request($requestid);
991+
$user = \core_user::get_user($request->get('userid'));
991992
foreach ($clcollection as $contextlist) {
992993
// Convert the \core_privacy\local\request\contextlist into a contextlist persistent and store it.
993994
$clp = \tool_dataprivacy\contextlist::from_contextlist($contextlist);
@@ -998,10 +999,14 @@ public static function add_request_contexts_with_status(contextlist_collection $
998999
foreach ($contextlist->get_contextids() as $contextid) {
9991000
if ($request->get('type') == static::DATAREQUEST_TYPE_DELETE) {
10001001
$context = \context::instance_by_id($contextid);
1001-
if (($purpose = static::get_effective_context_purpose($context)) && !empty($purpose->get('protected'))) {
1002+
$purpose = static::get_effective_context_purpose($context);
1003+
1004+
// Data can only be deleted from it if the context is either expired, or unprotected.
1005+
if (!expired_contexts_manager::is_context_expired_or_unprotected_for_user($context, $user)) {
10021006
continue;
10031007
}
10041008
}
1009+
10051010
$context = new contextlist_context();
10061011
$context->set('contextid', $contextid)
10071012
->set('contextlistid', $contextlistid)
@@ -1099,6 +1104,15 @@ public static function get_approved_contextlist_collection_for_request(data_requ
10991104
$contexts = [];
11001105
}
11011106

1107+
if ($request->get('type') == static::DATAREQUEST_TYPE_DELETE) {
1108+
$context = \context::instance_by_id($record->contextid);
1109+
$purpose = static::get_effective_context_purpose($context);
1110+
// Data can only be deleted from it if the context is either expired, or unprotected.
1111+
if (!expired_contexts_manager::is_context_expired_or_unprotected_for_user($context, $foruser)) {
1112+
continue;
1113+
}
1114+
}
1115+
11021116
$contexts[] = $record->contextid;
11031117
$lastcomponent = $record->component;
11041118
}
@@ -1196,4 +1210,25 @@ public static function set_context_defaults($contextlevel, $categoryid, $purpose
11961210

11971211
return true;
11981212
}
1213+
1214+
/**
1215+
* Format the supplied date interval as a retention period.
1216+
*
1217+
* @param \DateInterval $interval
1218+
* @return string
1219+
*/
1220+
public static function format_retention_period(\DateInterval $interval) : string {
1221+
// It is one or another.
1222+
if ($interval->y) {
1223+
$formattedtime = get_string('numyears', 'moodle', $interval->format('%y'));
1224+
} else if ($interval->m) {
1225+
$formattedtime = get_string('nummonths', 'moodle', $interval->format('%m'));
1226+
} else if ($interval->d) {
1227+
$formattedtime = get_string('numdays', 'moodle', $interval->format('%d'));
1228+
} else {
1229+
$formattedtime = get_string('retentionperiodzero', 'tool_dataprivacy');
1230+
}
1231+
1232+
return $formattedtime;
1233+
}
11991234
}

admin/tool/dataprivacy/classes/expired_context.php

Lines changed: 177 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,36 @@ class expired_context extends \core\persistent {
6060
* @return array
6161
*/
6262
protected static function define_properties() {
63-
return array(
64-
'contextid' => array(
63+
return [
64+
'contextid' => [
6565
'type' => PARAM_INT,
6666
'description' => 'The context id.',
67-
),
68-
'status' => array(
67+
],
68+
'defaultexpired' => [
69+
'type' => PARAM_INT,
70+
'description' => 'Whether to default retention period for the purpose has been reached',
71+
'default' => 1,
72+
],
73+
'expiredroles' => [
74+
'type' => PARAM_TEXT,
75+
'description' => 'This list of roles to include during deletion',
76+
'default' => '',
77+
],
78+
'unexpiredroles' => [
79+
'type' => PARAM_TEXT,
80+
'description' => 'This list of roles to exclude during deletion',
81+
'default' => '',
82+
],
83+
'status' => [
6984
'choices' => [
7085
self::STATUS_EXPIRED,
7186
self::STATUS_APPROVED,
7287
self::STATUS_CLEANED,
7388
],
7489
'type' => PARAM_INT,
7590
'description' => 'The deletion status of the context.',
76-
),
77-
);
91+
],
92+
];
7893
}
7994

8095
/**
@@ -160,21 +175,130 @@ public static function get_record_count_by_contextlevel($contextlevel = null, $s
160175
return $DB->count_records_sql($sql, $params);
161176
}
162177

178+
/**
179+
* Set the list of role IDs for either expiredroles, or unexpiredroles.
180+
*
181+
* @param string $field
182+
* @param int[] $roleids
183+
* @return expired_context
184+
*/
185+
protected function set_roleids_for(string $field, array $roleids) : expired_context {
186+
$roledata = json_encode($roleids);
187+
188+
$this->raw_set($field, $roledata);
189+
190+
return $this;
191+
}
192+
193+
/**
194+
* Get the list of role IDs for either expiredroles, or unexpiredroles.
195+
*
196+
* @param string $field
197+
* @return int[]
198+
*/
199+
protected function get_roleids_for(string $field) {
200+
$value = $this->raw_get($field);
201+
if (empty($value)) {
202+
return [];
203+
}
204+
205+
return json_decode($value);
206+
}
207+
208+
/**
209+
* Set the list of unexpired role IDs.
210+
*
211+
* @param int[] $roleids
212+
* @return expired_context
213+
*/
214+
protected function set_unexpiredroles(array $roleids) : expired_context {
215+
$this->set_roleids_for('unexpiredroles', $roleids);
216+
217+
return $this;
218+
}
219+
220+
/**
221+
* Add a set of role IDs to the list of expired role IDs.
222+
*
223+
* @param int[] $roleids
224+
* @return expired_context
225+
*/
226+
public function add_expiredroles(array $roleids) : expired_context {
227+
$existing = $this->get('expiredroles');
228+
$newvalue = array_merge($existing, $roleids);
229+
230+
$this->set('expiredroles', $newvalue);
231+
232+
return $this;
233+
}
234+
235+
/**
236+
* Add a set of role IDs to the list of unexpired role IDs.
237+
*
238+
* @param int[] $roleids
239+
* @return unexpired_context
240+
*/
241+
public function add_unexpiredroles(array $roleids) : expired_context {
242+
$existing = $this->get('unexpiredroles');
243+
$newvalue = array_merge($existing, $roleids);
244+
245+
$this->set('unexpiredroles', $newvalue);
246+
247+
return $this;
248+
}
249+
250+
/**
251+
* Set the list of expired role IDs.
252+
*
253+
* @param int[] $roleids
254+
* @return expired_context
255+
*/
256+
protected function set_expiredroles(array $roleids) : expired_context {
257+
$this->set_roleids_for('expiredroles', $roleids);
258+
259+
return $this;
260+
}
261+
262+
/**
263+
* Get the list of expired role IDs.
264+
*
265+
* @return int[]
266+
*/
267+
protected function get_expiredroles() {
268+
return $this->get_roleids_for('expiredroles');
269+
}
270+
271+
/**
272+
* Get the list of unexpired role IDs.
273+
*
274+
* @return int[]
275+
*/
276+
protected function get_unexpiredroles() {
277+
return $this->get_roleids_for('unexpiredroles');
278+
}
279+
163280
/**
164281
* Create a new expired_context based on the context, and expiry_info object.
165282
*
166283
* @param \context $context
167284
* @param expiry_info $info
285+
* @param boolean $save
168286
* @return expired_context
169287
*/
170-
public static function create_from_expiry_info(\context $context, expiry_info $info) : expired_context {
288+
public static function create_from_expiry_info(\context $context, expiry_info $info, bool $save = true) : expired_context {
171289
$record = (object) [
172290
'contextid' => $context->id,
173291
'status' => self::STATUS_EXPIRED,
292+
'defaultexpired' => (int) $info->is_default_expired(),
174293
];
175294

176295
$expiredcontext = new static(0, $record);
177-
$expiredcontext->save();
296+
$expiredcontext->set('expiredroles', $info->get_expired_roles());
297+
$expiredcontext->set('unexpiredroles', $info->get_unexpired_roles());
298+
299+
if ($save) {
300+
$expiredcontext->save();
301+
}
178302

179303
return $expiredcontext;
180304
}
@@ -186,7 +310,42 @@ public static function create_from_expiry_info(\context $context, expiry_info $i
186310
* @return $this
187311
*/
188312
public function update_from_expiry_info(expiry_info $info) : expired_context {
313+
$save = false;
314+
315+
// Compare the expiredroles.
316+
$thisexpired = $this->get('expiredroles');
317+
$infoexpired = $info->get_expired_roles();
318+
319+
sort($thisexpired);
320+
sort($infoexpired);
321+
if ($infoexpired != $thisexpired) {
322+
$this->set('expiredroles', $infoexpired);
323+
$save = true;
324+
}
325+
326+
// Compare the unexpiredroles.
327+
$thisunexpired = $this->get('unexpiredroles');
328+
$infounexpired = $info->get_unexpired_roles();
329+
330+
sort($thisunexpired);
331+
sort($infounexpired);
332+
if ($infounexpired != $thisunexpired) {
333+
$this->set('unexpiredroles', $infounexpired);
334+
$save = true;
335+
}
336+
337+
if (empty($this->get('defaultexpired')) == $info->is_default_expired()) {
338+
$this->set('defaultexpired', (int) $info->is_default_expired());
339+
$save = true;
340+
}
341+
342+
if ($save) {
343+
$this->set('status', self::STATUS_EXPIRED);
344+
$this->save();
345+
}
346+
189347
return $this;
348+
190349
}
191350

192351
/**
@@ -206,4 +365,14 @@ public function can_process_deletion() : bool {
206365
public function is_complete() : bool {
207366
return ($this->get('status') == self::STATUS_CLEANED);
208367
}
368+
369+
/**
370+
* Whether this context has 'fully' expired.
371+
* That is to say that the default retention period has been reached, and that there are no unexpired roles.
372+
*
373+
* @return bool
374+
*/
375+
public function is_fully_expired() : bool {
376+
return $this->get('defaultexpired') && empty($this->get('unexpiredroles'));
377+
}
209378
}

0 commit comments

Comments
 (0)