From 8293c95b1b485475d31ec6102129e742097a7298 Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Thu, 14 Sep 2023 17:12:35 +0300 Subject: [PATCH] wip: load user account into cache for idp with no GetAccount support --- management/server/account.go | 39 ++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/management/server/account.go b/management/server/account.go index d08e25fd4..74b2bdfb3 100644 --- a/management/server/account.go +++ b/management/server/account.go @@ -951,9 +951,12 @@ func (am *DefaultAccountManager) warmupIDPCache() error { for _, user := range account.Users { // fetch user info with idp manager + // verify the validity of this user before adding to the cache userData, err := am.idpManager.GetUserDataByID(user.Id, idp.AppMetadata{WTAccountID: account.Id}) if err != nil { - return err + // delete/do not add user to the cache + log.Debugf("user with id: %s does not exist", user.Id) + continue } // update user metadata as are not stored in upstream idp @@ -1026,6 +1029,9 @@ func (am *DefaultAccountManager) addAccountIDToIDPAppMeta(userID string, account err = am.idpManager.UpdateUserAppMetadata(userID, idp.AppMetadata{WTAccountID: account.Id}) if err != nil { + // TODO: with self hosted with idp which do not implement this method + // store the attributes and tobe available in next cache refresh. + // FIX: unable to access dashboard after sending user invite. return status.Errorf(status.Internal, "updating user's app metadata failed with: %v", err) } // refresh cache to reflect the update @@ -1039,7 +1045,36 @@ func (am *DefaultAccountManager) addAccountIDToIDPAppMeta(userID string, account func (am *DefaultAccountManager) loadAccount(_ context.Context, accountID interface{}) ([]*idp.UserData, error) { log.Debugf("account %s not found in cache, reloading", accountID) - return am.idpManager.GetAccount(fmt.Sprintf("%v", accountID)) + account, err := am.idpManager.GetAccount(fmt.Sprintf("%v", accountID)) + if err != nil { + var idpErr *idp.Error + if errors.As(err, &idpErr) { + acc, err := am.Store.GetAccount(fmt.Sprintf("%v", accountID)) + if err != nil { + return nil, err + } + + data := make([]*idp.UserData, 0) + + for _, user := range acc.Users { + // fetch user info with idp manager + userData, err := am.idpManager.GetUserDataByID(user.Id, idp.AppMetadata{}) + if err != nil { + return nil, err + } + + // update user metadata as are not stored in upstream idp + // Note (self-hosted does not support pendingInvite and invitedBy status) + userData.AppMetadata.WTAccountID = fmt.Sprintf("%v", accountID) + data = append(data, userData) + } + + return account, nil + } + return nil, err + } + + return account, nil } func (am *DefaultAccountManager) lookupUserInCacheByEmail(email string, accountID string) (*idp.UserData, error) {