diff options
Diffstat (limited to 'inc/user.inc.php')
-rw-r--r-- | inc/user.inc.php | 38 |
1 files changed, 34 insertions, 4 deletions
diff --git a/inc/user.inc.php b/inc/user.inc.php index f023ae7..496857e 100644 --- a/inc/user.inc.php +++ b/inc/user.inc.php @@ -7,6 +7,7 @@ class User private static $organization = NULL; private static $isShib = false; private static $isInDb = false; + private static $isAnonymous = false; public static function isLoggedIn() { @@ -28,11 +29,23 @@ class User return self::$user !== false && self::$isShib === false; } + public static function isAnonymous() + { + return self::$isAnonymous; + } + public static function getData() { return self::$user; } + public static function getId() + { + if (!isset(self::$user['userid'])) + return false; + return (int)self::$user['userid']; + } + public static function getName() { if (!self::isLoggedIn()) @@ -97,7 +110,7 @@ class User { if (self::isLoggedIn()) return true; - Session::load(); + $hasSession = Session::load(); if (empty($_SERVER['persistent-id'])) { if (Session::getUid() === false) return false; @@ -106,6 +119,11 @@ class User return self::$user !== false; } // Try bwIDM etc. + if (!$hasSession) { + Session::create(); + Session::set('token', md5(mt_rand() . $_SERVER['REMOTE_ADDR'] . microtime(true) . $_SERVER['persistent-id'] . mt_rand())); + Session::save(); + } self::$isShib = true; if (!isset($_SERVER['sn'])) $_SERVER['sn'] = ''; if (!isset($_SERVER['givenName'])) $_SERVER['givenName'] = ''; @@ -133,9 +151,14 @@ class User // No match in database, user is not signed up return true; } + if (Session::getUid() === false) { + Session::setUid($user['userid']); + Session::save(); + } // Already signed up, see if we can fetch missing fields from DB self::$user['login'] = $user['login']; self::$isInDb = true; + self::$isAnonymous = (empty($user['firstname']) && empty($user['lastname'])); foreach (array('firstname', 'lastname', 'email') as $key) { if (empty(self::$user[$key])) self::$user[$key] = $user[$key]; @@ -149,13 +172,15 @@ class User Util::traceError('NO SHIBID'); if ($anonymous) { Database::exec("INSERT INTO user (shibid, login, organizationid, firstname, lastname, email) " - . " VALUES (:shibid, :shibid, :org, '', '', '')", array( + . " VALUES (:shibid, :shibid, :org, '', '', '') " + . " ON DUPLICATE KEY UPDATE firstname = '', lastname = '', email = ''", array( 'shibid' => self::$user['shibid'], 'org' => self::getOrganizationId() )); } else { Database::exec("INSERT INTO user (shibid, login, organizationid, firstname, lastname, email) " - . " VALUES (:shibid, :shibid, :org, :firstname, :lastname, :email)", array( + . " VALUES (:shibid, :shibid, :org, :firstname, :lastname, :email) " + . " ON DUPLICATE KEY UPDATE firstname = VALUES(firstname), lastname = VALUES(lastname), email = VALUES(email)", array( 'shibid' => self::$user['shibid'], 'firstname' => self::$user['firstname'], 'lastname' => self::$user['lastname'], @@ -181,8 +206,13 @@ class User public static function logout() { + foreach ($_COOKIE as $name => $value) { + if (substr($name, 0, 5) !== '_shib') + continue; + @setcookie($name, '', time() - 8640000, null, null, !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off', true); + } Session::delete(); - Header('Location: ?do=Main&fromlogout'); + Header('Location: ?do=Logout&noredirect=yes'); exit(0); } |