/// Returns a dbo::ptr to the currently logged in user * dbo::ptr<User> user() { if (_userCache) { if (time(NULL) - _cacheTime > _userSessionStore.getTimeout()) _userCache.reset(); else return _userCache; } // Check the session thing live .. this will possibly slow things down by locking if another thread is writing // but on the plus side it means if one thread los out .. all threads log out string cookie = getCookie(); if (!cookie.empty()) { unsigned int userId; try { userId = _userSessionStore.userId(cookie); } catch (invalid_argument e) { // If they have an old session cookie, but they're not logged in now .. delete the old cookie WApplication::instance()->setCookie(_cookieName, cookie, 0, "", "/", true); cookieCache = ""; return dbo::ptr<User>(); // Empty pointer } touchSession(); // Cache and return the result dbo::Transaction t(_dbSession); _userCache = _dbSession.find<User>().where("id = ?").bind((int)userId); _cacheTime = time(NULL); // (now) t.commit(); return _userCache; } else { return dbo::ptr<User>(); // Empty pointer } }
SessionHandle(WObject* parent, dbo::Session& dbSession, const string& cookieName) : BaseSessionHandle(parent, cookieName), _userSessionStore(SessionStore::getInstance()), _dbSession(dbSession), _cacheTime(time(NULL)) { // Check if we're already logged in const string& cookie = getCookie(); if (!cookie.empty()) { int userId; try { userId = _userSessionStore.userId(getCookie(), true); // Touch the session as new app/view is openning for it } catch (invalid_argument e) { // Nobody's logged in right now _userCache.reset(); return; } // Remember our user object dbo::Transaction transaction(_dbSession); _userCache = _dbSession.find<User>().where("id = ?").bind((int)userId); transaction.commit(); } }
/// Logs the user out of all _userSessionStore eventually; this session immediately. void logout() { _userCache.reset(); _userSessionStore.logout(getCookie()); }