Re-use code.
authorJan Schneider <jan@horde.org>
Thu, 6 Jan 2011 11:25:48 +0000 (12:25 +0100)
committerJan Schneider <jan@horde.org>
Thu, 6 Jan 2011 11:30:13 +0000 (12:30 +0100)
framework/Share/lib/Horde/Share/Sql.php
framework/Share/lib/Horde/Share/Sql/Hierarchical.php

index 3539ee2..8859f7d 100644 (file)
@@ -572,11 +572,8 @@ class Horde_Share_Sql extends Horde_Share_Base
     }
 
     /**
-     * Returns an array of criteria for querying shares.
-     * @access protected
+     * Returns a criteria statement for querying shares.
      *
-     * @TODO: Horde_SQL:: stuff should be refactored/removed when it's ported
-     *        to Horde_Db
      * @param string  $userid      The userid of the user to check access for.
      * @param integer $perm        The level of permissions required.
      * @param mixed   $attributes  Restrict the shares returned to those who
@@ -587,10 +584,49 @@ class Horde_Share_Sql extends Horde_Share_Base
     public function getShareCriteria($userid, $perm = Horde_Perms::SHOW,
                                      $attributes = null)
     {
-        $query = ' FROM ' . $this->_table . ' s ';
-        $where = '';
+        list($query, $where) = $this->_getUserAndGroupCriteria($userid, $perm);
+        $query = ' FROM ' . $this->_table . ' s ' . $query;
+
+        /* Convert to driver's keys */
+        $attributes = $this->_toDriverKeys($attributes);
+
+        /* ...and to driver charset */
+        $attributes = $this->toDriverCharset($attributes);
+
+        if (is_array($attributes)) {
+            // Build attribute/key filter.
+            $where = ' (' . $where . ') ';
+            foreach ($attributes as $key => $value) {
+                $where .= ' AND ' . $key . ' = ' . $this->_db->quote($value);
+            }
+        } elseif (!empty($attributes)) {
+            // Restrict to shares owned by the user specified in the
+            // $attributes string.
+            $where = ' (' . $where . ') AND s.share_owner = ' . $this->_db->quote($attributes);
+        }
+
+        return $query . ' WHERE ' . $where;
+    }
+
+    /**
+     * Returns criteria statement fragments for querying shares.
+     *
+     * @todo: Horde_SQL:: stuff should be refactored/removed when it's ported
+     *        to Horde_Db
+     *
+     * @param string  $userid  The userid of the user to check access for.
+     * @param integer $perm    The level of permissions required.
+     *
+     * @return array  An array with query and where string fragments.
+     */
+    protected function _getUserAndGroupCriteria($userid,
+                                                $perm = Horde_Perms::SHOW)
+    {
+        $query = $where = '';
 
-        if (!empty($userid)) {
+        if (empty($userid)) {
+            $where = '(' . Horde_SQL::buildClause($this->_db, 's.perm_guest', '&', $perm) . ')';
+        } else {
             // (owner == $userid)
             $where .= 's.share_owner = ' . $this->_db->quote($userid);
 
@@ -622,26 +658,9 @@ class Horde_Share_Sql extends Horde_Share_Base
             } catch (Horde_Group_Exception $e) {
                 $this->_logger->err($e);
             }
-        } else {
-            $where = '(' . Horde_SQL::buildClause($this->_db, 's.perm_guest', '&', $perm) . ')';
-        }
-
-        $attributes = $this->_toDriverKeys($attributes);
-        $attributes = $this->toDriverCharset($attributes);
-
-        if (is_array($attributes)) {
-            // Build attribute/key filter.
-            $where = ' (' . $where . ') ';
-            foreach ($attributes as $key => $value) {
-                $where .= ' AND ' . $key . ' = ' . $this->_db->quote($value);
-            }
-        } elseif (!empty($attributes)) {
-            // Restrict to shares owned by the user specified in the
-            // $attributes string.
-            $where = ' (' . $where . ') AND s.share_owner = ' . $this->_db->quote($attributes);
         }
 
-        return $query . ' WHERE ' . $where;
+        return array($query, $where);
     }
 
     /**
index 9ba3678..b83926b 100644 (file)
@@ -172,47 +172,11 @@ class Horde_Share_Sql_Hierarchical extends Horde_Share_Sql
             $parent_id = $parent;
         }
 
-        $query = ' FROM ' . $this->_table . ' s ';
-        $where = '';
-
+        $query = $where = '';
         if (!is_null($perm)) {
-            if (empty($userid)) {
-                $where = '(' . Horde_SQL::buildClause($this->_db, 's.perm_guest', '&', $perm) . ')';
-            } else {
-                // (owner == $userid)
-                $where = 's.share_owner = ' . $this->_db->quote($userid);
-
-                // (name == perm_creator and val & $perm)
-                $where .= ' OR (' . Horde_SQL::buildClause($this->_db, 's.perm_creator', '&', $perm) . ')';
-
-                // (name == perm_creator and val & $perm)
-                $where .= ' OR (' . Horde_SQL::buildClause($this->_db, 's.perm_default', '&', $perm) . ')';
-
-                // (name == perm_users and key == $userid and val & $perm)
-                $query .= ' LEFT JOIN ' . $this->_table . '_users u ON u.share_id = s.share_id';
-                $where .= ' OR ( u.user_uid = ' .  $this->_db->quote($userid)
-                . ' AND (' . Horde_SQL::buildClause($this->_db, 'u.perm', '&', $perm) . '))';
-
-                // If the user has any group memberships, check for those also.
-                // @TODO: Inject the group driver
-                try {
-                    $groups = $this->_groups->getGroupMemberships($userid, true);
-                    if ($groups) {
-                        // (name == perm_groups and key in ($groups) and val & $perm)
-                        $ids = array_keys($groups);
-                        $group_ids = array();
-                        foreach ($ids as $id) {
-                            $group_ids[] = $this->_db->quote((string)$id);
-                        }
-                        $query .= ' LEFT JOIN ' . $this->_table . '_groups g ON g.share_id = s.share_id';
-                        $where .= ' OR (g.group_uid IN (' . implode(',', $group_ids) . ')'
-                            . ' AND (' . Horde_SQL::buildClause($this->_db, 'g.perm', '&', $perm) . '))';
-                    }
-                } catch (Horde_Group_Exception $e) {
-                    $this->_logError($e, 'Horde_Share_Sql_Hierarchical::getShareCriteria()');
-                }
-            }
+            list($query, $where) = $this->_getUserAndGroupCriteria($userid, $perm);
         }
+        $query = ' FROM ' . $this->_table . ' s ' . $query;
 
         /* Convert to driver's keys */
         $attributes = $this->_toDriverKeys($attributes);