From dd534af9d046ea71e3ffee5e0be188c39f516c31 Mon Sep 17 00:00:00 2001 From: "Michael J. Rubinsky" Date: Mon, 17 Jan 2011 16:53:34 -0500 Subject: [PATCH] reduce code duplication - move perms check into Ansel_Storage::getGalleries() and ::getGalleriesBySlugs() also, improve getting by slugs by having Horde_Share perform the filtering directly. --- ansel/lib/Api.php | 15 ++++----------- ansel/lib/Storage.php | 45 +++++++++++++++++++++++++-------------------- ansel/lib/View/List.php | 8 +------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/ansel/lib/Api.php b/ansel/lib/Api.php index d499d37a8..e1f1dab9a 100644 --- a/ansel/lib/Api.php +++ b/ansel/lib/Api.php @@ -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; diff --git a/ansel/lib/Storage.php b/ansel/lib/Storage.php index 8483602f7..f31136b31 100644 --- a/ansel/lib/Storage.php +++ b/ansel/lib/Storage.php @@ -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; } /** diff --git a/ansel/lib/View/List.php b/ansel/lib/View/List.php index ca2e46599..e2d967744 100644 --- a/ansel/lib/View/List.php +++ b/ansel/lib/View/List.php @@ -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(); } -- 2.11.0