From 130e26983aedac1e4bb9f26d6a82c629248075e8 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Wed, 17 Dec 2008 17:40:45 +0000 Subject: Add initialization to the user module to put the user and group_ids into the session, for easy access. This cuts down the number of queries when we're loading images through file_proxy.php --- core/helpers/access.php | 4 ++-- core/helpers/module.php | 6 ++++-- modules/user/helpers/user.php | 38 ++++++++++++++++++++++++++++++++----- modules/user/helpers/user_event.php | 27 ++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 modules/user/helpers/user_event.php diff --git a/core/helpers/access.php b/core/helpers/access.php index 05799bf5..82d7b9b6 100644 --- a/core/helpers/access.php +++ b/core/helpers/access.php @@ -98,8 +98,8 @@ class access_Core { $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) { + foreach (user::group_ids() as $id) { + if ($resource->__get("{$perm_name}_$id") === self::ALLOW) { return true; } } diff --git a/core/helpers/module.php b/core/helpers/module.php index 02851528..5713901d 100644 --- a/core/helpers/module.php +++ b/core/helpers/module.php @@ -117,6 +117,8 @@ class module_Core { // // @todo get rid of this extra error checking when we have an installer. set_error_handler(array("module", "_dummy_error_handler")); + $modules = ORM::factory("module")->find_all(); + restore_error_handler(); // Reload module list from the config file since we'll do a refresh after calling install() $core = Kohana::config_load('core'); @@ -124,7 +126,7 @@ class module_Core { self::$module_names = array(); self::$modules = array(); try { - foreach (ORM::factory("module")->find_all() as $module) { + foreach ($modules as $module) { self::$module_names[] = $module->name; self::$modules[] = $module; $kohana_modules[] = MODPATH . $module->name; @@ -136,7 +138,7 @@ class module_Core { self::$modules = array(); } - restore_error_handler(); + self::event("gallery_ready"); } public function get_var($module_name, $name, $default_value=null) { diff --git a/modules/user/helpers/user.php b/modules/user/helpers/user.php index a04542d3..1667afd1 100644 --- a/modules/user/helpers/user.php +++ b/modules/user/helpers/user.php @@ -56,12 +56,39 @@ class user_Core { return $form; } + /** + * Make sure that we have a session and group_ids cached in the session. + */ + public static function load_user() { + $session = Session::instance(); + if (!($user = $session->get("user"))) { + $session->set("user", $user = user::guest()); + } + + if (!$session->get("group_ids")) { + $ids = array(); + foreach ($user->groups as $group) { + $ids[] = $group->id; + } + $session->set("group_ids", $ids); + } + } + + /** + * Return the array of group ids this user belongs to + * + * @return array + */ + public static function group_ids() { + return Session::instance()->get("group_ids", array(1)); + } + /** * Return the active user. If there's no active user, return the guest user. * * @return User_Model */ - static function active() { + public static function active() { return Session::instance()->get("user", self::guest()); } @@ -72,7 +99,7 @@ class user_Core { * * @return User_Model */ - static function guest() { + public static function guest() { return model_cache::get("user", 1); } @@ -81,8 +108,9 @@ class user_Core { * * @return User_Model */ - static function set_active($user) { - return Session::instance()->set("user", $user); + public static function set_active($user) { + Session::instance()->set("user", $user); + self::load_user(); } /** @@ -93,7 +121,7 @@ class user_Core { * @param string $password * @return User_Model */ - static function create($name, $display_name, $password) { + public static function create($name, $display_name, $password) { $user = ORM::factory("user")->where("name", $name)->find(); if ($user->loaded) { throw new Exception("@todo USER_ALREADY_EXISTS $name"); diff --git a/modules/user/helpers/user_event.php b/modules/user/helpers/user_event.php new file mode 100644 index 00000000..2d1ce171 --- /dev/null +++ b/modules/user/helpers/user_event.php @@ -0,0 +1,27 @@ +