Project

General

Profile

Getting default implementation of UserDatabase to store proper UserId

Added by Aleksander Fular over 10 years ago

I am using defualt implementation of UserDatabase to hold information about authorization data etc.

However I cannot accomplish what you did in blog example - because the implementation of the UserDatabase there is custom.

In my DbSession Class i want to have method 'getUser' which will check whether user is logged in and if he is then just return the User class.

In example you have:

@

dbo::ptr BlogSession::user() const

{

if (login_.loggedIn())

return users*.find(login*.user());

else

return dbo::ptr();

}

@

I have to work around that, in my case it is:

@

Wt::Dbo::ptrModels::User FuxDbSession::getUser() {

if(login_.loggedIn()) {

return findModels::User().where("name = ?").bind(login_.user().identity(Wt::Auth::Identity::LoginName));

} else {

return Wt::Dbo::ptrModels::User();

}

}

@

Because in database no matter how i try to add the user auth_info table, column user_id is always 'null'

In the example you use findWithIdentity to register the admin user. thats how I register the admin user now.

@

void FuxDbSession::createAdminUser() {

Wt::Dbo::Transaction t(*this);

Wt::Dbo::ptrModels::User admin = add(new Models::User());

Models::User *a = admin.modify();

a->name = "UserName";

a->role = Models::User::Admin;

admin.flush();

Wt::Auth::User authUser = userDatabase_->registerNew();

authUser.addIdentity(Wt::Auth::Identity::LoginName, a->name);

authUser.setEmail("mail@mail.com");

auth_.getPasswordService().updatePassword(authUser, "Secret");

}

@

Can you tell me what I need to do differently so my getuser method can work like that:

@

Wt::Dbo::ptrModels::User FuxDbSession::getUser() {

if(login_.loggedIn()) {

return userDatabase_find(login_.user())>user();

} else {

return Wt::Dbo::ptrModels::User();

}

}

@


Replies (1)

RE: Getting default implementation of UserDatabase to store proper UserId - Added by Koen Deforche over 10 years ago

Hey,

See the feature/auth2 example of how to create a new 'User' dbo on the fly.

dbo::ptr<User> Session::user()
{
  if (login_.loggedIn())
    return user(login_.user());
  else
    return dbo::ptr<User>();
}

dbo::ptr<User> Session::user(const Wt::Auth::User& authUser)
{
  dbo::ptr<AuthInfo> authInfo = users_->find(authUser);

  dbo::ptr<User> user = authInfo->user();

  if (!user) {
    user = add(new User());
    authInfo.modify()->setUser(user);
  }

  return user;
}
    (1-1/1)