82 lines
2.0 KiB
C++
82 lines
2.0 KiB
C++
#include <eedb/pg/PgUser.hpp>
|
|
|
|
#include <eedb/AuthInfoConst.hpp>
|
|
#include <eedb/pg/PgAuthIdentity.hpp>
|
|
#include <eedb/pg/PgAuthToken.hpp>
|
|
#include <eedb/pg/connection.hpp>
|
|
#include <eedb/pg/model/all.hpp>
|
|
|
|
#include <sqlpp11/sqlpp11.h>
|
|
|
|
namespace eedb {
|
|
|
|
struct PgUser::PgUserPriv {
|
|
PgUserPriv(db::PgConnection & db, int uid, PgUser * owner) : _db{db}, id{uid}, self{owner} {}
|
|
|
|
void initAuthTokens() {
|
|
_authTokens = std::make_unique< PgAuthTokens >(_db, self);
|
|
}
|
|
|
|
void initAuthInfo() {
|
|
auto info = _db(select(sqlpp::all_of(t_auth_info)) //
|
|
.from(t_auth_info) //
|
|
.where(t_auth_info.id == self->uid()));
|
|
|
|
assert(!info.empty());
|
|
|
|
auto & row = info.front();
|
|
|
|
Password pass{row.password_method, row.password_salt, row.password_hash};
|
|
Email email{row.email};
|
|
|
|
authInfo = std::make_unique< AuthInfoConst >(std::move(pass), std::move(email));
|
|
}
|
|
|
|
void initAuthIdentities() {
|
|
_authIdentities = std::make_unique< PgAuthIdentities >(_db, self);
|
|
}
|
|
|
|
db::PgConnection & _db;
|
|
int id;
|
|
PgUser * self;
|
|
UserConfig config;
|
|
|
|
std::unique_ptr< PgAuthTokens > _authTokens;
|
|
std::unique_ptr< AuthInfoConst > authInfo;
|
|
std::unique_ptr< PgAuthIdentities > _authIdentities;
|
|
};
|
|
|
|
PgUser::PgUser(db::PgConnection & db, int uid) : _priv{spimpl::make_unique_impl< PgUserPriv >(db, uid, this)} {}
|
|
|
|
int PgUser::uid() const {
|
|
return _priv->id;
|
|
}
|
|
|
|
const UserConfig & PgUser::config() const {
|
|
return _priv->config;
|
|
}
|
|
|
|
void PgUser::logout() {
|
|
assert(false);
|
|
}
|
|
|
|
AuthTokens & PgUser::authTokens() const {
|
|
if(!_priv->_authTokens)
|
|
_priv->initAuthTokens();
|
|
return *(_priv->_authTokens.get());
|
|
}
|
|
|
|
AuthIdentities & PgUser::authIdentities() const {
|
|
if(!_priv->_authIdentities)
|
|
_priv->initAuthIdentities();
|
|
return *(_priv->_authIdentities.get());
|
|
}
|
|
|
|
AuthInfo & PgUser::authInfo() const {
|
|
if(!_priv->authInfo)
|
|
_priv->initAuthInfo();
|
|
return *(_priv->authInfo.get());
|
|
}
|
|
|
|
} // namespace eedb
|