Skip to content

Commit

Permalink
MDL-62574 core_privacy: Check the contextlist for real contexts.
Browse files Browse the repository at this point in the history
There are situations when contexts will be deleted in the provider.
The context list is not updated and so we have introduced a try
catch.
  • Loading branch information
abgreeve committed Jun 12, 2018
1 parent 50e48a8 commit c84cb13
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions privacy/classes/local/request/contextlist_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,20 @@ public function get_contextids() : array {
* Get the complete list of context objects that relate to this
* request.
*
* @return \contect[]
* @return \context[]
*/
public function get_contexts() : array {
$contexts = [];
foreach ($this->contextids as $contextid) {
$contexts[] = \context::instance_by_id($contextid);
// It is possible that this context has been deleted and we now have subsequent calls being made with this
// contextlist. Exceptions here will stop the further processing of this component and that is why we are
// doing a try catch.
try {
$contexts[] = \context::instance_by_id($contextid);
} catch (\Exception $e) {
// Remove this context.
unset($this->contextids[$this->iteratorposition]);
}
}

return $contexts;
Expand Down Expand Up @@ -114,7 +122,25 @@ public function get_component() : string {
* @return \context
*/
public function current() {
return \context::instance_by_id($this->contextids[$this->iteratorposition]);
// It is possible that this context has been deleted and we now have subsequent calls being made with this
// contextlist. Exceptions here will stop the further processing of this component and that is why we are
// doing a try catch.
try {
$context = \context::instance_by_id($this->contextids[$this->iteratorposition]);
} catch (\Exception $e) {
// Remove this context.
unset($this->contextids[$this->iteratorposition]);
// Check to see if there are any more contexts left.
if ($this->count()) {
// Move the pointer to the next record and try again.
$this->next();
$context = $this->current();
} else {
// There are no more context ids left.
return;
}
}
return $context;
}

/**
Expand Down

0 comments on commit c84cb13

Please sign in to comment.