]> git.mjollnir.org Git - moodle.git/commitdiff
accesslib: Introduce functions to deal with dirty contexts
authormartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:10:09 +0000 (07:10 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:10:09 +0000 (07:10 +0000)
The accessinfo held in $USER->access can easily get out of
sync with reality if and admin has removed our access,
or expanded it after we loaded our accessinfo.

To handle this, we'll use the config_plugins table with an
'accesslib/dirtycontexts' plugin signature to store the paths of
recently changed contexts. To handle those dirrrty entries, here
we introduce

  get_dirty_contexts() - for lib/setup
  mark_context_dirty()
  cleanup_dirty_contexts() - for cron

lib/accesslib.php

index 798338a943a30edf4e759b2bcc21859185169f9d..bced2ff91a7cca5b659d96f7dfd6ecf6b36e6a2d 100755 (executable)
@@ -4629,4 +4629,53 @@ function make_context_subobj($rec) {
     return $rec;
 }
 
+/*
+ * Fetch recent dirty contexts to know cheaply whether our $USER->access
+ * is stale and needs to be reloaded.
+ *
+ * Uses config_plugins.
+ *
+ */
+function get_dirty_contexts($time=NULL) {
+    global $CFG;
+
+    $timecond = '';
+    if (!is_null($time)) {
+        $timecond = " AND CAST(value to integer) > $time";
+    }
+    $sql = "SELECT name, value 
+            FROM {$CFG->prefix}config_plugins
+            WHERE plugin='accesslib/dirtycontexts'
+                  $timecond";
+    if ($ctx = get_records_sql($sql)) {
+        return $ctx;
+    }
+    return array();
+}
+
+/*
+ * Mark a context as dirty (with timestamp)
+ * so as to force reloading of the context.
+ *
+ */
+function mark_context_dirty($path) {
+    // Trivial (for now?)
+    set_config($path, time(), 'accesslib/dirtycontexts');
+}
+
+/*
+ * Cleanup all the old/stale dirty contexts.
+ * Any context exceeding our session
+ * timeout is stale. We only keep these for ongoing
+ * sessions.
+ *
+ */
+function cleanup_dirty_contexts() {
+    global $CFG;
+    
+    $sql = "plugin='accesslib/dirtycontexts' AND
+                  CAST(value to integer) < " . time() - $CFG->sessiontimeout;
+    delete_records_select('config_plugins', $sql);
+}
+
 ?>