Fix invalidating the cache when updating a permission.
authorJan Schneider <jan@horde.org>
Mon, 3 May 2010 21:47:57 +0000 (23:47 +0200)
committerJan Schneider <jan@horde.org>
Mon, 3 May 2010 21:50:51 +0000 (23:50 +0200)
framework/Perms/lib/Horde/Perms/Datatree.php
framework/Perms/lib/Horde/Perms/Permission.php
framework/Perms/lib/Horde/Perms/Permission/DataTreeObject.php
framework/Perms/lib/Horde/Perms/Permission/SqlObject.php
framework/Perms/lib/Horde/Perms/Sql.php

index b1a6d10..a939164 100644 (file)
@@ -83,7 +83,7 @@ class Horde_Perms_Datatree extends Horde_Perms
             } catch (Horde_Perms_Exception $e) {}
         }
 
-        $perm = new Horde_Perms_Permission_DataTreeObject($name, $type, $params);
+        $perm = new Horde_Perms_Permission_DataTreeObject($name, $this->_cacheVersion, $type, $params);
         $perm->setDataTree($this->_datatree);
 
         return $perm;
@@ -106,6 +106,7 @@ class Horde_Perms_Datatree extends Horde_Perms
         $perm = $this->_cache->get('perm_' . $this->_cacheVersion . $name, $GLOBALS['conf']['cache']['default_lifetime']);
         if ($perm === false) {
             $perm = $this->_datatree->getObject($name, 'Horde_Perms_Permission_DataTreeObject');
+            $perm->setCacheVersion($this->_cacheVersion);
             $this->_cache->set('perm_' . $this->_cacheVersion . $name, serialize($perm), $GLOBALS['conf']['cache']['default_lifetime']);
             $this->_permsCache[$name] = $perm;
         } else {
@@ -123,9 +124,12 @@ class Horde_Perms_Datatree extends Horde_Perms
      */
     public function getPermissionById($cid)
     {
-        return ($cid == Horde_Perms::ROOT)
-            ? $this->newPermission(Horde_Perms::ROOT)
-            : $this->_datatree->getObjectById($cid, 'Horde_Perms_Permission_DataTreeObject');
+        if ($cid == Horde_Perms::ROOT) {
+            return $this->newPermission(Horde_Perms::ROOT);
+        }
+        $perm = $this->_datatree->getObjectById($cid, 'Horde_Perms_Permission_DataTreeObject');
+        $perm->setCacheVersion($this->_cacheVersion);
+        return $perm;
     }
 
     /**
index 5ceaed2..ed8dcce 100644 (file)
@@ -21,16 +21,25 @@ class Horde_Perms_Permission
     public $name;
 
     /**
+     * Incrementing version number if cached classes change.
+     *
+     * @var integer
+     */
+    private $_cacheVersion;
+
+    /**
      * Constructor.
      *
-     * @param string $name   The name of the perm.
-     * @param string $type   The permission type.
-     * @param array $params  A hash with any parameters that the permission
-     *                       type needs.
+     * @param string $name           The name of the perm.
+     * @param integer $cacheVersion  The revision number of the class.
+     * @param string $type           The permission type.
+     * @param array $params          A hash with any parameters that the
+     *                               permission type needs.
      */
-    public function __construct($name, $type = 'matrix', $params = null)
+    public function __construct($name, $cacheVersion = null, $type = 'matrix', $params = null)
     {
         $this->setName($name);
+        $this->setCacheVersion($cacheVersion);
         $this->data['type'] = $type;
         if (is_array($params)) {
             $this->data['params'] = $params;
@@ -38,6 +47,16 @@ class Horde_Perms_Permission
     }
 
     /**
+     * Sets the revision number of the class.
+     *
+     * @param integer $cacheVersion  The revision number of the class.
+     */
+    public function setCacheVersion($cacheVersion)
+    {
+        $this->_cacheVersion = $cacheVersion;
+    }
+
+    /**
      * Gets one of the attributes of the object, or null if it isn't defined.
      *
      * @param string $attribute  The attribute to get.
index 7517a03..6926929 100644 (file)
@@ -555,8 +555,8 @@ class Horde_Perms_Permission_DataTreeObject extends DataTreeObject
         parent::save();
 
         $cache = $GLOBALS['injector']->getInstance('Horde_Cache');
-        $cache->expire('perm_' . $name);
-        $cache->expire('perm_exists_' . $name);
+        $cache->expire('perm_' . $this->_cacheVersion . $name);
+        $cache->expire('perm_exists_' . $this->_cacheVersion . $name);
     }
 
 }
index 7fb6f04..6334210 100644 (file)
@@ -78,8 +78,8 @@ class Horde_Perms_Permission_SqlObject extends Horde_Perms_Permission
         }
 
         $cache = $GLOBALS['injector']->getInstance('Horde_Cache');
-        $cache->expire('perm_sql_' . $name);
-        $cache->expire('perm_sql_exists_' . $name);
+        $cache->expire('perm_sql_' . $this->_cacheVersion . $name);
+        $cache->expire('perm_sql_exists_' . $this->_cacheVersion . $name);
     }
 
 }
index 8dda934..8b10f65 100644 (file)
@@ -91,7 +91,7 @@ class Horde_Perms_Sql extends Horde_Perms
             } catch (Horde_Perms_Exception $e) {}
         }
 
-        return new Horde_Perms_Permission_SqlObject($name, $type, $params);
+        return new Horde_Perms_Permission_SqlObject($name, $this->_cacheVersion, $type, $params);
     }
 
     /**
@@ -122,7 +122,7 @@ class Horde_Perms_Sql extends Horde_Perms
                 throw new Horde_Perms_Exception('Does not exist');
             }
 
-            $object = new Horde_Perms_Permission_SqlObject($name);
+            $object = new Horde_Perms_Permission_SqlObject($name, $this->_cacheVersion);
             $object->setId($result['perm_id']);
             $object->setData(unserialize($result['perm_data']));
 
@@ -163,7 +163,7 @@ class Horde_Perms_Sql extends Horde_Perms
                 throw new Horde_Perms_Exception('Does not exist');
             }
 
-            $object = new Horde_Perms_Permission_SqlObject($result['perm_name']);
+            $object = new Horde_Perms_Permission_SqlObject($result['perm_name'], $this->_cacheVersion);
             $object->setId($id);
             $object->setData(unserialize($result['perm_data']));
             $object->setSQLOb($this->_write_db);