]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16343 New PARAM_ type, PARAM_CAPABILITY.
authortjhunt <tjhunt>
Mon, 8 Sep 2008 07:00:49 +0000 (07:00 +0000)
committertjhunt <tjhunt>
Mon, 8 Sep 2008 07:00:49 +0000 (07:00 +0000)
lib/accesslib.php
lib/moodlelib.php

index 7047e4fadd0e4ca07c7268bf465b32f4c39b5ae4..5afc5391aead83ce4b2af841e93ac25e49b20a25 100755 (executable)
@@ -356,20 +356,8 @@ function has_capability($capability, $context, $userid=NULL, $doanything=true) {
 
 /// Some sanity checks
     if (debugging('',DEBUG_DEVELOPER)) {
-        static $capsnames = null; // one request per page only
-
-        if (is_null($capsnames)) {
-            if ($caps = $DB->get_records('capabilities', null, '', 'id, name')) {
-                $capsnames = array();
-                foreach ($caps as $cap) {
-                    $capsnames[$cap->name] = true;
-                }
-            }
-        }
-        if ($capsnames) { // ignore if can not fetch caps
-            if (!isset($capsnames[$capability])) {
-                debugging('Capability "'.$capability.'" was not found! This should be fixed in code.');
-            }
+        if (!is_valid_capability($capability)) {
+            debugging('Capability "'.$capability.'" was not found! This should be fixed in code.');
         }
         if (!is_bool($doanything)) {
             debugging('Capability parameter "doanything" is wierd ("'.$doanything.'"). This should be fixed in code.');
@@ -3665,6 +3653,17 @@ function get_related_contexts_string($context) {
     }
 }
 
+function is_valid_capability($capabilityname) {
+    static $capsnames = null; // one request per page only
+
+    if (is_null($capsnames)) {
+        global $DB;
+        $capsnames = $DB->get_records_menu('capabilities', null, '', 'name, 1');
+    }
+
+    return array_key_exists($capabilityname, $capsnames);
+}
+
 /**
  * Returns the human-readable, translated version of the capability.
  * Basically a big switch statement.
index 97839e77ef6290bee45eb57ae5810051e706f748..78c9fb490228623f8715047515adfb33a9262fc9 100644 (file)
@@ -240,6 +240,11 @@ define('PARAM_PEM',      0x10000);
  */
 define('PARAM_BASE64',   0x20000);
 
+/**
+ * PARAM_CAPABILITY - A capability name, like 'moodle/role:manage'. Actually
+ * checked against the list of capabilties in the database.
+ */
+define('PARAM_CAPABILITY',   0x40000);
 
 /// Page types ///
 /**
@@ -600,6 +605,13 @@ function clean_param($param, $type) {
                 return '';
             }
 
+        case PARAM_CAPABILITY:
+            if (is_valid_capability($param)) {
+                return $param;
+            } else {
+                return '';
+            }
+
         default:                 // throw error, switched parameters in optional_param or another serious problem
             print_error("unknowparamtype", '', '', $type);
     }