reduce code duplication - move perms check into Ansel_Storage::getGalleries() and...
authorMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 17 Jan 2011 21:53:34 +0000 (16:53 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Mon, 17 Jan 2011 21:53:34 +0000 (16:53 -0500)
also, improve getting by slugs by having Horde_Share perform the filtering directly.

ansel/lib/Api.php
ansel/lib/Storage.php
ansel/lib/View/List.php

index d499d37..e1f1dab 100644 (file)
@@ -80,14 +80,9 @@ class Ansel_Api extends Horde_Registry_Api
                 // This request if for a certain gallery, list all sub-galleries
                 // and images.
                 $gallery_id = end($parts);
-                $galleries = $GLOBALS['injector']->getInstance('Ansel_Storage')->getGalleries(array($gallery_id));
-                if (!isset($galleries[$gallery_id]) ||
-                    !$galleries[$gallery_id]->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::READ)) {
-
-                    throw new Horde_Exception_NotFound(_("Invalid gallery specified."));
-                }
                 $galleries = $storage->listGalleries(array('parent' => $gallery_id,
-                                                           'all_levels' => false));
+                                                           'all_levels' => false,
+                                                           'perm' => Horde_Perms::SHOW));
                 $images = $this->listImages(null, $gallery_id, Horde_Perms::SHOW, 'mini');
 
             } elseif (count($parts) > 2 &&
@@ -720,12 +715,10 @@ class Ansel_Api extends Horde_Registry_Api
         }
 
         // We can't just return the results of the getGalleries call - we need
-        // to ensure the caller has at least Horde_Perms::READ on the galleries.
+        // to build the non-object return structure.
         $galleries = array();
         foreach ($results as $gallery) {
-            if ($gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::READ)) {
-                $galleries[$gallery->id] = array_merge($gallery->data, array('crumbs' => $gallery->getGalleryCrumbData()));
-            }
+            $galleries[$gallery->id] = array_merge($gallery->data, array('crumbs' => $gallery->getGalleryCrumbData()));
         }
 
         return $galleries;
index 8483602..f31136b 100644 (file)
@@ -293,36 +293,41 @@ class Ansel_Storage
      * @return array of Ansel_Gallery objects
      * @throws Ansel_Exception
      */
-    public function getGalleriesBySlugs($slugs)
+    public function getGalleriesBySlugs(array $slugs, $perms = Horde_Perms::SHOW)
     {
-        $sql = 'SELECT share_id FROM ' . $this->_shares->getTable()
-            . ' WHERE attribute_slug IN (' . str_repeat('?, ', count($slugs) - 1) . '?)';
-
-        $stmt = $this->_shares->getReadDb()->prepare($sql);
-        if ($stmt instanceof PEAR_Error) {
-            throw new Horde_Exception($stmt->getMessage());
-        }
-        $result = $stmt->execute($slugs);
-        if ($result instanceof PEAR_Error) {
-            throw new Ansel_Exception($result);
+        try {
+            return $this->_shares->listShares(
+                $GLOBALS['registry']->getAuth(),
+                array('perm' => $perms, 'attribtues' => array('slugs' => $slugs)));
+        } catch (Horde_Share_Exception $e) {
+            throw new Ansel_Exception($e);
         }
-        $ids = array_values($result->fetchCol());
-        $shares = $this->_shares->getShares($ids);
-
-        $stmt->free();
-        $result->free();
-
-        return $shares;
     }
 
     /**
      * Retrieve an array of Ansel_Gallery objects for the requested ids
      *
+     * @param array $ids      Gallery ids to fetch
+     * @param integer $perms  Horde_Perms constant for the perms required.
+     *
      * @return array of Ansel_Gallery objects
+     * @throws Ansel_Exception
      */
-    public function getGalleries($ids)
+    public function getGalleries(array $ids, $perms = Horde_Perms::SHOW)
     {
-        return $this->_shares->getShares($ids);
+        try {
+            $shares = $this->_shares->getShares($ids);
+        } catch (Horde_Share_Exception $e) {
+            throw new Ansel_Exception($e);
+        }
+        $galleries = array();
+        foreach ($shares as $id => $gallery) {
+            if ($gallery->hasPermission($GLOBALS['registry']->getAuth(), $perms)) {
+                $galleries[$id] = $gallery;
+            }
+        }
+
+        return $galleries;
     }
 
     /**
index ca2e465..e2d9677 100644 (file)
@@ -97,13 +97,7 @@ class Ansel_View_List extends Ansel_View_Base
             $this->_numGalleries = count($this->_params['gallery_ids']);
             if ($this->_numGalleries > $this->_start) {
                 $getThese = array_slice($this->_params['gallery_ids'], $this->_start, $this->_g_perPage);
-                $try = $ansel_storage->getGalleries($getThese);
-                $this->_galleryList = array();
-                foreach ($try as $id => $gallery) {
-                    if ($gallery->hasPermission($GLOBALS['registry']->getAuth(), Horde_Perms::SHOW)) {
-                        $this->_galleryList[$id] = $gallery;
-                    }
-                }
+                $this->_galleryList = $ansel_storage->getGalleries($getThese);
             } else {
                 $this->_galleryList = array();
             }