-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSessionHandle.hpp
68 lines (61 loc) · 1.93 KB
/
SessionHandle.hpp
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
/*
* =====================================================================================
*
* Filename: SessionHandle.hpp
*
* Description: Hooks together witty-sessions- and the mongo UserManager
*
* Version: 1.0
* Created: 06/25/2011 08:58:24 AM
* Revision: none
* Compiler: gcc
*
* Author: Matthew Sherborne (), [email protected]
* Company:
*
* =====================================================================================
*/
#ifndef SESSION_HANDLE_HPP
#define SESSION_HANDLE_HPP
#include <string>
#include <Wt/WObject>
#include <Wt/WRandom>
#include <Wt/WTimer>
#include "SessionStore.hpp"
#include "UserManager.hpp"
using std::string;
using Wt::WObject;
using Wt::WRandom;
using Wt::WTimer;
namespace vidanueva {
/**
* @brief A handle to the session of the currently logged in user
*/
class SessionHandle : public WObject {
public:
static const string cookieName;
private:
UserManager userManager;
SessionStore& sessions;
string cookieCache;
string getCookie();
WTimer* touchSessionsTimer; // Used to stop sessions timing out without touching them too much
void onTouchSessionsActivate();
public:
SessionHandle(WObject* parent) : WObject(parent), sessions(SessionStore::getInstance()) {
// Check if we're already logged in
string username = sessions.username(getCookie(), true); // Touch the session as new app/view is openning for it
if (!username.empty())
userManager.forceLogin(username);
}
void configureMongo(const string& hostname, const string& dbName, const string& tableName="users") {
userManager.configure(hostname, dbName, tableName);
}
bool tryLogin(const string& username, const string& password);
void logout();
void touchSession();
string username();
bool isLoggedIn() { return !username().empty(); }
};
} // namespace vidanueva
#endif // SESSION_HANDLE_HPP