summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/helpers/access.php48
-rw-r--r--core/helpers/model_cache.php35
-rw-r--r--core/libraries/ORM_MPTT.php3
-rw-r--r--core/models/item.php7
4 files changed, 56 insertions, 37 deletions
diff --git a/core/helpers/access.php b/core/helpers/access.php
index f40a5c7c..eda91d65 100644
--- a/core/helpers/access.php
+++ b/core/helpers/access.php
@@ -79,15 +79,8 @@ class access_Core {
* @return boolean
*/
public static function group_can($group, $perm_name, $item) {
- if ($perm_name == "view") {
- $resource = $item;
- } else {
- $resource = ORM::factory("access_cache")->where("item_id", $item->id)->find();
- if (!$resource) {
- throw new Exception("@todo MISSING_ACCESS for $item->id");
- }
- }
-
+ $resource = $perm_name == "view" ?
+ $item : model_cache::get("access_cache", $item->id, "item_id");
return $resource->__get("{$perm_name}_{$group->id}") === self::ALLOW;
}
@@ -103,15 +96,8 @@ class access_Core {
return false;
}
- if ($perm_name == "view") {
- $resource = $item;
- } else {
- $resource = ORM::factory("access_cache")->where("item_id", $item->id)->find();
- if (!$resource) {
- throw new Exception("@todo MISSING_ACCESS for $item->id");
- }
- }
-
+ $resource = $perm_name == "view" ?
+ $item : model_cache::get("access_cache", $item->id, "item_id");
foreach (user::active()->groups as $group) {
if ($resource->__get("{$perm_name}_{$group->id}") === self::ALLOW) {
return true;
@@ -135,11 +121,7 @@ class access_Core {
if ($album->type != "album") {
throw new Exception("@todo INVALID_ALBUM_TYPE not an album");
}
- $access = ORM::factory("access_intent")->where("item_id", $album->id)->find();
- if (!$access->loaded) {
- throw new Exception("@todo MISSING_ACCESS for $album->id");
- }
-
+ $access = model_cache::get("access_intent", $album->id, "item_id");
$access->__set("{$perm_name}_{$group->id}", $value);
$access->save();
@@ -268,17 +250,19 @@ class access_Core {
$access_intent->save();
// Create a new access cache entry and copy the parents values.
- $parent_access_cache =
- ORM::factory("access_cache")->where("item_id", $item->parent()->id)->find();
$access_cache = ORM::factory("access_cache");
$access_cache->item_id = $item->id;
- foreach (self::_get_all_groups() as $group) {
- foreach (ORM::factory("permission")->find_all() as $perm) {
- $field = "{$perm->name}_{$group->id}";
- if ($perm->name == "view") {
- $item->$field = $item->parent()->$field;
- } else {
- $access_cache->$field = $parent_access_cache->$field;
+ if ($item->id != 1) {
+ $parent_access_cache =
+ ORM::factory("access_cache")->where("item_id", $item->parent()->id)->find();
+ foreach (self::_get_all_groups() as $group) {
+ foreach (ORM::factory("permission")->find_all() as $perm) {
+ $field = "{$perm->name}_{$group->id}";
+ if ($perm->name == "view") {
+ $item->$field = $item->parent()->$field;
+ } else {
+ $access_cache->$field = $parent_access_cache->$field;
+ }
}
}
}
diff --git a/core/helpers/model_cache.php b/core/helpers/model_cache.php
new file mode 100644
index 00000000..7a01393b
--- /dev/null
+++ b/core/helpers/model_cache.php
@@ -0,0 +1,35 @@
+<?php defined("SYSPATH") or die("No direct script access.");
+/**
+ * Gallery - a web based photo album viewer and editor
+ * Copyright (C) 2000-2008 Bharat Mediratta
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+class model_cache_Core {
+ private static $cache;
+
+ function get($model_name, $id, $field_name="id") {
+ if (TEST_MODE || empty(self::$cache->$model_name->$field_name->$id)) {
+ $model = ORM::factory($model_name)->where($field_name, $id)->find();
+ if (!$model->loaded) {
+ throw new Exception("@todo MISSING_MODEL $model_name:$id");
+ }
+ self::$cache->$model_name->$field_name->$id = $model;
+ }
+
+
+ return self::$cache->$model_name->$field_name->$id;
+ }
+}
diff --git a/core/libraries/ORM_MPTT.php b/core/libraries/ORM_MPTT.php
index 411afe44..37789156 100644
--- a/core/libraries/ORM_MPTT.php
+++ b/core/libraries/ORM_MPTT.php
@@ -114,8 +114,7 @@ class ORM_MPTT_Core extends ORM {
*/
function parent() {
if (!isset($this->parent)) {
- $this->parent =
- ORM::factory($this->model_name)->where("id", $this->parent_id)->find();
+ $this->parent = model_cache::get($this->model_name, $this->parent_id);
}
return $this->parent;
}
diff --git a/core/models/item.php b/core/models/item.php
index 25b1d7f3..47fae0e7 100644
--- a/core/models/item.php
+++ b/core/models/item.php
@@ -19,7 +19,6 @@
*/
class Item_Model extends ORM_MPTT {
protected $children = 'items';
- protected $has_one = array("owner" => "user");
var $rules = array();
@@ -224,7 +223,9 @@ class Item_Model extends ORM_MPTT {
public function __get($column) {
if (substr($column, -5) == "_edit") {
$real_column = substr($column, 0, strlen($column) - 5);
- if (access::can("edit", $this)) {
+ $editable = $this->type == "album" ?
+ access::can("edit", $this) : access::can("edit", $this->parent());
+ if ($editable) {
return "<span class=\"gInPlaceEdit gEditField-{$this->id}-{$real_column}\">" .
"{$this->$real_column}</span>";
} else {
@@ -234,7 +235,7 @@ class Item_Model extends ORM_MPTT {
// This relationship depends on an outside module, which may not be present so handle
// failures gracefully.
try {
- return parent::__get($column);
+ return model_cache::get("user", $this->owner_id);
} catch (Exception $e) {
return null;
}