From 00eacd659f27df9c13246c510057c4f42c8866a2 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Thu, 15 Oct 2009 14:37:57 -0700 Subject: Start simplifying the interface by moving the static methods from user.php and group.php. Tried creating a identity helper, but the helper identity.php was confused with the library Identity.php. So got around this by making the methods on Identity static and calling the instance within the static methods. Also temporarily moved the user.php and group.php back into the user module. --- modules/gallery/helpers/access.php | 2 +- modules/gallery/helpers/gallery.php | 2 +- modules/gallery/helpers/gallery_event.php | 6 +- modules/gallery/helpers/gallery_theme.php | 2 +- modules/gallery/helpers/group.php | 79 ----------- modules/gallery/helpers/item.php | 4 +- modules/gallery/helpers/locales.php | 2 +- modules/gallery/helpers/log.php | 2 +- modules/gallery/helpers/movie.php | 2 +- modules/gallery/helpers/photo.php | 2 +- modules/gallery/helpers/site_status.php | 2 +- modules/gallery/helpers/task.php | 2 +- modules/gallery/helpers/user.php | 223 ------------------------------ 13 files changed, 14 insertions(+), 316 deletions(-) delete mode 100644 modules/gallery/helpers/group.php delete mode 100644 modules/gallery/helpers/user.php (limited to 'modules/gallery/helpers') diff --git a/modules/gallery/helpers/access.php b/modules/gallery/helpers/access.php index 949aea84..0e0e749e 100644 --- a/modules/gallery/helpers/access.php +++ b/modules/gallery/helpers/access.php @@ -79,7 +79,7 @@ class access_Core { * @return boolean */ static function can($perm_name, $item) { - return self::user_can(user::active(), $perm_name, $item); + return self::user_can(Identity::active(), $perm_name, $item); } /** diff --git a/modules/gallery/helpers/gallery.php b/modules/gallery/helpers/gallery.php index 50e2c43b..e1fa2a7c 100644 --- a/modules/gallery/helpers/gallery.php +++ b/modules/gallery/helpers/gallery.php @@ -27,7 +27,7 @@ class gallery_Core { static function maintenance_mode() { $maintenance_mode = Kohana::config("core.maintenance_mode", false, false); - if (Router::$controller != "login" && !empty($maintenance_mode) && !user::active()->admin) { + if (Router::$controller != "login" && !empty($maintenance_mode) && !Identity::active()->admin) { Router::$controller = "maintenance"; Router::$controller_path = MODPATH . "gallery/controllers/maintenance.php"; Router::$method = "index"; diff --git a/modules/gallery/helpers/gallery_event.php b/modules/gallery/helpers/gallery_event.php index 4a6b14b6..abead9e3 100644 --- a/modules/gallery/helpers/gallery_event.php +++ b/modules/gallery/helpers/gallery_event.php @@ -24,10 +24,10 @@ class gallery_event_Core { */ static function gallery_ready() { // Call Identity::instance() now to force the load of the user interface classes. - // user::load_user will attempt to load the active user from the session and needs + // Identity::load_user will attempt to load the active user from the session and needs // the user definition class, which can't be reached by Kohana's heiracrchical lookup. Identity::instance(); - user::load_user(); + Identity::load_user(); locales::set_request_locale(); } @@ -139,7 +139,7 @@ class gallery_event_Core { } } - if (user::active()->admin) { + if (Identity::active()->admin) { $menu->append($admin_menu = Menu::factory("submenu") ->id("admin_menu") ->label(t("Admin"))); diff --git a/modules/gallery/helpers/gallery_theme.php b/modules/gallery/helpers/gallery_theme.php index a342b4bd..cc46a88a 100644 --- a/modules/gallery/helpers/gallery_theme.php +++ b/modules/gallery/helpers/gallery_theme.php @@ -54,7 +54,7 @@ class gallery_theme_Core { static function header_top($theme) { if ($theme->page_type != "login") { $view = new View("login.html"); - $view->user = user::active(); + $view->user = Identity::active(); return $view->render(); } } diff --git a/modules/gallery/helpers/group.php b/modules/gallery/helpers/group.php deleted file mode 100644 index 295e5f50..00000000 --- a/modules/gallery/helpers/group.php +++ /dev/null @@ -1,79 +0,0 @@ -create_group($name); - } - - /** - * @see Identity_Driver::everbody. - */ - static function everybody() { - return Identity::instance()->everybody(); - } - - /** - * @see Identity_Driver::registered_users. - */ - static function registered_users() { - return Identity::instance()->everybody(); - } - - /** - * Look up a group by id. - * @param integer $id the user id - * @return Group_Definition the group object, or null if the id was invalid. - */ - static function lookup($id) { - return Identity::instance()->lookup_group_by_field("id", $id); - } - - /** - * Look up a group by name. - * @param integer $id the group name - * @return Group_Definition the group object, or null if the name was invalid. - */ - static function lookup_by_name($name) { - return Identity::instance()->lookup_group_by_field("name", $name); - } - - /** - * @see Identity_Driver::get_group_list. - */ - static function get_group_list($filter=array()) { - return Identity::instance()->get_group_list($filter); - } - - /** - * @see Identity_Driver::get_edit_rules. - */ - static function get_edit_rules() { - return Identity::instance()->get_edit_rules("group"); - } -} diff --git a/modules/gallery/helpers/item.php b/modules/gallery/helpers/item.php index 084bbc15..bce83bb3 100644 --- a/modules/gallery/helpers/item.php +++ b/modules/gallery/helpers/item.php @@ -158,8 +158,8 @@ class item_Core { */ static function viewable($model) { $view_restrictions = array(); - if (!user::active()->admin) { - foreach (user::group_ids() as $id) { + if (!Identity::active()->admin) { + foreach (Identity::group_ids_for_active_user() as $id) { // Separate the first restriction from the rest to make it easier for us to formulate // our where clause below if (empty($view_restrictions)) { diff --git a/modules/gallery/helpers/locales.php b/modules/gallery/helpers/locales.php index 2cd8b0c2..2dfc7f21 100644 --- a/modules/gallery/helpers/locales.php +++ b/modules/gallery/helpers/locales.php @@ -141,7 +141,7 @@ class locales_Core { $locale = self::cookie_locale(); // 2. Check the user's preference if (!$locale) { - $locale = user::active()->locale; + $locale = Identity::active()->locale; } // 3. Check the browser's / OS' preference if (!$locale) { diff --git a/modules/gallery/helpers/log.php b/modules/gallery/helpers/log.php index c8e94b45..512723dd 100644 --- a/modules/gallery/helpers/log.php +++ b/modules/gallery/helpers/log.php @@ -80,7 +80,7 @@ class log_Core { $log->url = substr(url::abs_current(true), 0, 255); $log->referer = request::referrer(null); $log->timestamp = time(); - $log->user_id = user::active()->id; + $log->user_id = Identity::active()->id; $log->save(); } diff --git a/modules/gallery/helpers/movie.php b/modules/gallery/helpers/movie.php index 9ca28fe6..32a27646 100644 --- a/modules/gallery/helpers/movie.php +++ b/modules/gallery/helpers/movie.php @@ -77,7 +77,7 @@ class movie_Core { $movie->title = $title; $movie->description = $description; $movie->name = $name; - $movie->owner_id = $owner_id ? $owner_id : user::active(); + $movie->owner_id = $owner_id ? $owner_id : Identity::active(); $movie->width = $movie_info[0]; $movie->height = $movie_info[1]; $movie->mime_type = strtolower($pi["extension"]) == "mp4" ? "video/mp4" : "video/x-flv"; diff --git a/modules/gallery/helpers/photo.php b/modules/gallery/helpers/photo.php index 6677ddc9..cf316819 100644 --- a/modules/gallery/helpers/photo.php +++ b/modules/gallery/helpers/photo.php @@ -76,7 +76,7 @@ class photo_Core { $photo->title = $title; $photo->description = $description; $photo->name = $name; - $photo->owner_id = $owner_id ? $owner_id : user::active(); + $photo->owner_id = $owner_id ? $owner_id : Identity::active(); $photo->width = $image_info[0]; $photo->height = $image_info[1]; $photo->mime_type = empty($image_info['mime']) ? "application/unknown" : $image_info['mime']; diff --git a/modules/gallery/helpers/site_status.php b/modules/gallery/helpers/site_status.php index b7c6de9a..3f7ff19d 100644 --- a/modules/gallery/helpers/site_status.php +++ b/modules/gallery/helpers/site_status.php @@ -95,7 +95,7 @@ class site_status_Core { * @return html text */ static function get() { - if (!user::active()->admin) { + if (!Identity::active()->admin) { return; } $buf = array(); diff --git a/modules/gallery/helpers/task.php b/modules/gallery/helpers/task.php index 9fa04305..4735c36c 100644 --- a/modules/gallery/helpers/task.php +++ b/modules/gallery/helpers/task.php @@ -42,7 +42,7 @@ class task_Core { $task->percent_complete = 0; $task->status = ""; $task->state = "started"; - $task->owner_id = user::active()->id; + $task->owner_id = Identity::active()->id; $task->context = serialize($context); $task->save(); diff --git a/modules/gallery/helpers/user.php b/modules/gallery/helpers/user.php deleted file mode 100644 index c51a4a9c..00000000 --- a/modules/gallery/helpers/user.php +++ /dev/null @@ -1,223 +0,0 @@ - "g-login-form")); - $form->set_attr('class', "g-narrow"); - $group = $form->group("login")->label(t("Login")); - $group->input("name")->label(t("Username"))->id("g-username")->class(null); - $group->password("password")->label(t("Password"))->id("g-password")->class(null); - $group->inputs["name"]->error_messages("invalid_login", t("Invalid name or password")); - $group->submit("")->value(t("Login")); - return $form; - } - - /** - * Return the active user. If there's no active user, return the guest user. - * - * @return User_Model - */ - static function active() { - // @todo (maybe) cache this object so we're not always doing session lookups. - $user = Session::instance()->get("user", null); - if (!isset($user)) { - // Don't do this as a fallback in the Session::get() call because it can trigger unnecessary - // work. - $user = self::guest(); - } - return $user; - } - - /** - * Change the active user. - * - * @return User_Model - */ - static function set_active($user) { - $session = Session::instance(); - $session->set("user", $user); - $session->delete("group_ids"); - self::load_user(); - } - - /** - * Return the array of group ids this user belongs to - * - * @return array - */ - static function group_ids() { - return Session::instance()->get("group_ids", array(1)); - } - - /** - * Make sure that we have a session and group_ids cached in the session. This is one - * of the first calls to reference the user so call the Identity::instance to load the - * driver classes. - */ - static function load_user() { - $session = Session::instance(); - if (!($user = $session->get("user"))) { - $session->set("user", $user = self::guest()); - } - - // The installer cannot set a user into the session, so it just sets an id which we should - // upconvert into a user. - // @todo what is user id===2 - if ($user === 2) { - $user = model_cache::get("user", 2); - self::login($user); - $session->set("user", $user); - } - - if (!$session->get("group_ids")) { - $ids = array(); - foreach ($user->groups as $group) { - $ids[] = $group->id; - } - $session->set("group_ids", $ids); - } - } - - /** - * Log in as a given user. - * @param object $user the user object. - */ - static function login($user) { - // @todo make this an interface call - $user->login_count += 1; - $user->last_login = time(); - $user->save(); - - self::set_active($user); - module::event("user_login", $user); - } - - /** - * Log out the active user and destroy the session. - * @param object $user the user object. - */ - static function logout() { - $user = self::active(); - if (!$user->guest) { - try { - Session::instance()->destroy(); - } catch (Exception $e) { - Kohana::log("error", $e); - } - module::event("user_logout", $user); - } - } - - /** - * @see Identity_Core::is_writable. - */ - static function is_writable() { - return Identity::instance()->is_writable(); - } - - /** - * @see Identity_Driver::guest. - */ - static function guest() { - return Identity::instance()->guest(); - } - - /** - * @see Identity_Driver::create_user. - */ - static function create($name, $full_name, $password) { - return Identity::instance()->create_user($name, $full_name, $password); - } - - /** - * @see Identity_Driver::is_correct_password. - */ - static function is_correct_password($user, $password) { - return Identity::instance()->is_correct_password($user, $password); - } - - /** - * @see Identity_Driver::hash_password. - */ - static function hash_password($password) { - return Identity::instance()->hash_password($password); - } - - /** - * Look up a user by id. - * @param integer $id the user id - * @return User_Definition the user object, or null if the id was invalid. - */ - static function lookup($id) { - return Identity::instance()->lookup_user_by_field("id", $id); - } - - /** - * Look up a user by name. - * @param integer $name the user name - * @return User_Definition the user object, or null if the name was invalid. - */ - static function lookup_by_name($name) { - return Identity::instance()->lookup_user_by_field("name", $name); - } - - /** - * Look up a user by hash. - * @param string $name the user name - * @return User_Definition the user object, or null if the name was invalid. - */ - static function lookup_by_hash($hash) { - return Identity::instance()->lookup_user_by_field("hash", $hash); - } - - /** - * @see Identity_Driver::get_user_list. - */ - static function get_user_list($filter=array()) { - return Identity::instance()->get_user_list($filter); - } - - /** - * @see Identity_Driver::get_edit_rules. - */ - static function get_edit_rules() { - return Identity::instance()->get_edit_rules("user"); - } - - private static function _lookup_user_by_field($field_name, $value) { - try { - $user = model_cache::get("user", $value, $field_name); - if ($user->loaded) { - return $user; - } - } catch (Exception $e) { - if (strpos($e->getMessage(), "MISSING_MODEL") === false) { - throw $e; - } - } - return null; - } -} \ No newline at end of file -- cgit v1.2.3