diff options
Diffstat (limited to 'modules/gallery/libraries/MY_Session.php')
| -rw-r--r-- | modules/gallery/libraries/MY_Session.php | 84 | 
1 files changed, 84 insertions, 0 deletions
| diff --git a/modules/gallery/libraries/MY_Session.php b/modules/gallery/libraries/MY_Session.php new file mode 100644 index 00000000..6394c0fb --- /dev/null +++ b/modules/gallery/libraries/MY_Session.php @@ -0,0 +1,84 @@ +<?php defined("SYSPATH") or die("No direct script access."); +/** + * Gallery - a web based photo album viewer and editor + * Copyright (C) 2000-2009 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 Session extends Session_Core { +  /** +   * Make sure that we have a session and group_ids cached in the session. +   */ +  static function load_user() { +    $session = Session::instance(); +    if (!($user = $session->get("user"))) { +      $session->set("user", $user = Identity::guest()); +    } + +    // The installer cannot set a user into the session, so it just sets an id which we should +    // upconvert into a user. +    // @todo set the user name into the session instead of 2 and then use it to get the user object +    if ($user === 2) { +      $user = Instance::lookup_user_by_name("admin"); +      self::set_active_user($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); +    } +  } + +  /** +   * Return the array of group ids this user belongs to +   * +   * @return array +   */ +  static function group_ids_for_active_user() { +    return self::instance()->get("group_ids", array(1)); +  } + +  /** +   * Return the active user.  If there's no active user, return the guest user. +   * +   * @return User_Definition +   */ +  static function active_user() { +    // @todo (maybe) cache this object so we're not always doing session lookups. +    $user = self::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 = Identity::guest(); +    } +    return $user; +  } + +  /** +   * Change the active user. +   * @param User_Definition $user +   */ +  static function set_active_user($user) { +    $session = Session::instance(); +    $session->set("user", $user); +    $session->delete("group_ids"); +    self::load_user(); +  } +}
\ No newline at end of file | 
