Project

General

Profile

How could I get the user_id from authentication class of Wt?

Added by yyyy yyyy almost 8 years ago

Based on the example of auth2, I write down something like this

mapClass<db_user>("user");
mapClass<DBAuthInfo>("auth_info");
mapClass<DBAuthInfo::AuthIdentityType>("auth_identity");
mapClass<DBAuthInfo::AuthTokenType>("auth_token");

What I want to do is, find out all of the identities(in table auth_indentity)

and associate user_id? I want to use the user_id to filter the data.

example :

identity "abcd" associate with user_id "1"

identity "efgh" associate with user_id "2"

Thanks


Replies (3)

RE: How could I get the user_id from authentication class of Wt? - Added by Koen Deforche almost 8 years ago

Hey,

I guess you can do it directly at the database level, using a dbo query?

Something along these lines:

typedef boost::tuple<dbo::ptr<db_user>, dbo::ptr<DBAuthInfo::AuthIdentityType>> UserIdentity;

auto q = session.query<UserIdentity>("select u, aid from user u join auth_info ai on ai.user_id = u.id join auth_identity aid on aid.auth_info_id = ai.id");
auto list = q.resultList();

for (auto& i : list) {
   dbo::ptr<db_user> user;
   dbo::ptr<DBAuthInfo::AuthIdentityType> identity;
   boost::tie(user, identity) = i;

   ...
}

RE: How could I get the user_id from authentication class of Wt? - Added by yyyy yyyy almost 8 years ago

Hi, Koen

Sorry for my late reply, there are compile time error on this line

for (auto& i : list) {

*wt\win\vc2012\64\include\Wt\Dbo\SqlTraits_impl.h:50: error: C3861: 'read': identifier not found

wt\win\vc2012\64\include\Wt\Dbo\SqlTraits_impl.h:50: error: C2039: 'read' : is not a member of 'Wt::Dbo::sql_value_traits'

with

[

V=UserIdentity

]*

Although far from perfect, I figure out how to build the table of user_id<--->user identity

boost::bimap<Wt::Dbo::dbo_default_traits::IdType, Wt::WString>
build_user_map(Wt::Dbo::Session &session)
{
    using namespace Wt;

    typedef Dbo::collection<Dbo::ptr<DBAuthInfo::AuthIdentityType>> AITCollect;

    Dbo::Transaction transaction(session);
    AITCollect aitc = session.find<DBAuthInfo::AuthIdentityType>();

    //first user_id, second user name
    typedef boost::bimap<Wt::Dbo::dbo_default_traits::IdType, WString> BMType;
    BMType user_id_identity;
    for(auto const &it : aitc){
        auto const &identity = it->identity();
        auto const &auth_id = it->authInfo().id();
        Dbo::ptr<DBAuthInfo> info = session.find<DBAuthInfo>().where("id=?").bind(auth_id);
        auto const user_id = info->user().id();
        user_id_identity.insert(BMType::value_type(user_id, identity));
    }

    return user_id_identity;
}

Could I do batch find?Find the id value with a batch values rather than a value?I want to reduce the frequency of db manipulation

    (1-3/3)