Wt examples: Session Class Reference

Wt examples 4.10.4

Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | List of all members
Session Class Reference

`#include <Session.h>`

## Public Member Functions
| Session ()
Wt::Auth::AbstractUserDatabase & | users ()
Wt::Auth::Login & | login ()
std::vector< User > | topUsers (int limit)
std::string | userName () const
int | findRanking ()
void | addToScore (int s)

## Static Public Member Functions
static void | configureAuth ()
static const Wt::Auth::AuthService & | auth ()
static const Wt::Auth::AbstractPasswordService & | passwordAuth ()
static std::vector< const Wt::Auth::OAuthService * > | oAuth ()

## Private Member Functions
Wt::Dbo::ptr< User > | user () const

## Private Attributes
Wt::Dbo::Session | session_
std::unique_ptr< UserDatabase > | users_
Wt::Auth::Login | login_

## Detailed Description

Definition at line 24 of file Session.h.

## Constructor & Destructor Documentation

## ◆ Session()

Session::Session | ( |  | ) |

Definition at line 88 of file Session.C.

89{
90 auto sqlite3 = std::make_unique<Dbo::backend::Sqlite3>(WApplication::instance()->appRoot() + "hangman.db");
91 sqlite3->setProperty("show-queries", "true");
92 session_.setConnection(std::move(sqlite3));
93
94 session_.mapClass<User>("user");
95 session_.mapClass<AuthInfo>("auth_info");
96 session_.mapClass<AuthInfo::AuthIdentityType>("auth_identity");
97 session_.mapClass<AuthInfo::AuthTokenType>("auth_token");
98
99 users_ = std::make_unique<UserDatabase>(session_);
100
101 dbo::Transaction transaction(session_);
102 try {
103 session_.createTables();
104
105 /*
106 * Add a default guest/guest account
107 */
108 Auth::User guestUser = users_->registerNew();
109 guestUser.addIdentity(Auth::Identity::LoginName, "guest");
110 myPasswordService.updatePassword(guestUser, "guest");
111
112 log("info") << "Database created";
113 } catch (...) {
114 log("info") << "Using existing database";
115 }
116
117 transaction.commit();
118}
AuthInfo
Wt::Auth::Dbo::AuthInfo< User > AuthInfo
Definition User.h:21
Session::users_
std::unique_ptr< UserDatabase > users_
Definition Session.h:49
Session::session_
Wt::Dbo::Session session_
Definition Session.h:48
User
Definition User.h:25

## Member Function Documentation

## ◆ addToScore()

void Session::addToScore | ( | int | s | ) |

Definition at line 144 of file Session.C.

145{
146 dbo::Transaction transaction(session_);
147
148 dbo::ptr<User> u = user();
149 if (u) {
150 u.modify()->score += s;
151 ++u.modify()->gamesPlayed;
152 u.modify()->lastGame = WDateTime::currentDateTime();
153 }
154
155 transaction.commit();
156}
Session::user
Wt::Dbo::ptr< User > user() const
Definition Session.C:120

## ◆ auth()

const Auth::AuthService & Session::auth | ( |  | ) |
| static

Definition at line 201 of file Session.C.

202{
203 return myAuthService;
204}

## ◆ configureAuth()

void Session::configureAuth | ( |  | ) |
| static

Definition at line 65 of file Session.C.

66{
67 myAuthService.setAuthTokensEnabled(true, "hangmancookie");
68 myAuthService.setEmailVerificationEnabled(true);
69
70 auto verifier = std::make_unique<Auth::PasswordVerifier>();
71 verifier->addHashFunction(std::make_unique<Auth::BCryptHashFunction>(7));
72
73#ifdef HAVE_CRYPT
74 // We want to still support users registered in the pre - Wt::Auth
75 // version of the hangman example
76 verifier->addHashFunction(std::make_unique<UnixCryptHashFunction>());
77#endif
78
79 myPasswordService.setVerifier(std::move(verifier));
80 myPasswordService.setStrengthValidator(std::make_unique<Auth::PasswordStrengthValidator>());
81 myPasswordService.setAttemptThrottlingEnabled(true);
82
83 if (Auth::GoogleService::configured()) {
84 myOAuthServices.push_back(std::make_unique<Auth::GoogleService>(myAuthService));
85 }
86}

## ◆ findRanking()

int Session::findRanking | ( |  | ) |

Definition at line 180 of file Session.C.

181{
182 dbo::Transaction transaction(session_);
183
184 dbo::ptr<User> u = user();
185 int ranking = -1;
186
187 if (u)
188 ranking = session_.query<int>("select distinct count(score) from user")
189 .where("score > ?").bind(u->score);
190
191 transaction.commit();
192
193 return ranking + 1;
194}

## ◆ login()

Wt::Auth::Login & Session::login | ( |  | ) |
| inline

Definition at line 32 of file Session.h.

32{ return login_; }
Session::login_
Wt::Auth::Login login_
Definition Session.h:50

## ◆ oAuth()

std::vector< const Auth::OAuthService * > Session::oAuth | ( |  | ) |
| static

Definition at line 211 of file Session.C.

212{
213 std::vector<const Auth::OAuthService *> result;
214 result.reserve(myOAuthServices.size());
215 for (const auto& service : myOAuthServices) {
216 result.push_back(service.get());
217 }
218 return result;
219}

## ◆ passwordAuth()

const Auth::AbstractPasswordService & Session::passwordAuth | ( |  | ) |
| static

Definition at line 206 of file Session.C.

207{
208 return myPasswordService;
209}

## ◆ topUsers()

std::vector< User > Session::topUsers | ( | int | limit | ) |

Definition at line 158 of file Session.C.

159{
160 dbo::Transaction transaction(session_);
161
162 Users top = session_.find<User>().orderBy("score desc").limit(limit);
163
164 std::vector<User> result;
165 for (Users::const_iterator i = top.begin(); i != top.end(); ++i) {
166 dbo::ptr<User> user = *i;
167 result.push_back(*user);
168
169 dbo::ptr<AuthInfo> auth = user->authInfo;
170 std::string name = auth->identity(Auth::Identity::LoginName).toUTF8();
171
172 result.back().name = name;
173 }
174
175 transaction.commit();
176
177 return result;
178}
Users
dbo::collection< dbo::ptr< User > > Users
Definition User.h:22
Session::auth
static const Wt::Auth::AuthService & auth()
Definition Session.C:201

## ◆ user()

dbo::ptr< User > Session::user | ( |  | ) | const
| private

Definition at line 120 of file Session.C.

121{
122 if (login_.loggedIn()) {
123 dbo::ptr<AuthInfo> authInfo = users_->find(login_.user());
124 dbo::ptr<User> user = authInfo->user();
125
126 if (!user) {
127 user = session_.add(std::make_unique<User>());
128 authInfo.modify()->setUser(user);
129 }
130
131 return user;
132 } else
133 return dbo::ptr<User>();
134}

## ◆ userName()

std::string Session::userName | ( |  | ) | const

Definition at line 136 of file Session.C.

137{
138 if (login_.loggedIn())
139 return login_.user().identity(Auth::Identity::LoginName).toUTF8();
140 else
141 return std::string();
142}

## ◆ users()

Auth::AbstractUserDatabase & Session::users | ( |  | ) |

Definition at line 196 of file Session.C.

197{
198 return *users_;
199}

## Member Data Documentation

## ◆ login_

Wt::Auth::Login Session::login_
| private

Definition at line 50 of file Session.h.

## ◆ session_

Wt::Dbo::Session Session::session_
| mutableprivate

Definition at line 48 of file Session.h.

## ◆ users_

std::unique_ptr<UserDatabase> Session::users_
| private

Definition at line 49 of file Session.h.

The documentation for this class was generated from the following files:

- hangman/Session.h
- hangman/Session.C