summaryrefslogtreecommitdiff
path: root/modules/gallery/libraries/MY_Session.php
blob: 1a3ae8010546ba21a4ec96b584ae5667f01fbc8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?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() {
    try {
      $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);
      }
    } catch (Exception $e) {
      try {
        Session::instance()->destroy();
      } catch (Exception $e) {
        // We don't care if there was a problem destroying the session.
      }
      url::redirect(item::root()->abs_url());
    }
  }

  /**
   * 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();
  }
}