Developer forum

Forum » Development » AccessUserAddress call for every user you can impersonate causes slow login

AccessUserAddress call for every user you can impersonate causes slow login

Mikkel Hammer
Mikkel Hammer
Reply

Hi,

We have a solution where when we log in as a person who can impersonate, there is a db call being made which is made for every user you can impersonate:


In this scenario, the logged in user can impersonate 7678 users.
Which results in a slow login because the call is being made for each user.

The list of users the person can impersonate is a query if that changes anything.


i've attached the debug=true result as a txt file, which you can change to .html if you want to have a look.

Is this call something that is necessary, and if not, something that can be toggled off?

Best regards,
Mikkel Hammer

 


Replies

 
Anders Ebdrup
Anders Ebdrup
Reply

It seems to be caused by a check like this:

if (Pageview.User.SecondaryUsers.Count > 0)

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Sent to QA for verification.

 
Stanislav Smetanin Dynamicweb Employee
Stanislav Smetanin
Reply
This post has been marked as an answer

Hi Mikkel,
Do you probably use the Rapido templates?

In Rapido templates (..\Files\Templates\Designs\Rapido\MasterBlocks\ImpersonationBar.cshtml) we do next check to verify that User has secondary users (impersonation check):

    if (Model.CurrentUser.ID > 0 && Model.SecondaryUsers.Count > 0)

Model.SecondaryUsers fills the collection of user models, so it could take a lot of time.

We can use Model.HasSecondaryUsers instead of it. It should work fast. So, instead of code above, we can do something like this

    if (Model.CurrentUser.ID > 0 && Model.HasSecondaryUsers )

I have added to this message the attachment with fixed ImpersonationBar.cshtml template for the case if you use the Rapido.

Kind regards.

Votes for this answer: 1
 
Mikkel Hammer
Mikkel Hammer
Reply

Hi Stanislav,

Yes it is indeed a rapido template, and HasSecondaryUsers seems to work as intended :)
Thanks a lot for the quick response!

Best regards,
Mikkel

 
Dan Gheorghe
Reply

Hello,

I have a similar problem in Swift, a user who can impersonate many users, when logging in it takes about 30 seconds..

this is the current version of the template 

I updated it with a newer version:

and it still takes about 30 seconds. 

Is there any fix for this?

 

Best regards,

Dan

 

 

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Which version are you seeing this?

 
Dan Gheorghe
Reply

Hi Nicolai,

the first screenshot is taken from Swift version 1.10.0 and the second screenshot is taken from the current version on GitHub 1.24.0. The template is Swift_ImpersonationBar.cshtml

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

The Dw version is 9.14.14.

Adrian

 
Dan Gheorghe
Reply

The Dw version is 9.14.10

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

Hi Dan,

 

If you're willing to customize, I can offer a solution that can make it be much faster.

 

You can use the API to get the Users's index (which is used anyways in Swift) and from it get a count of user you can impersonate. That way it's not initializing a entire list of users.

 

Applying it in the end could be as simple as this

bool canImpersonate = Dna.AutoImpersonate.Helpers.UsersICanSetAsSecondary.Any();

 

And here's (most of) the custom code for it

        internal static List<string> UsersICanSetAsSecondary()
        {
            var usersICanSetAsSecondary = new List<string>();
            
            var queryService = Dynamicweb.Extensibility.ServiceLocator.Current?.GetInstance<IQueryService>();
            if (queryService == null)
            {
                Logging.Log("Non existent Query Service.", LogLevel.Warning);
                return UsersICanSetAsSecondaryFromApi();
            }
 
            try
            {
                var iQuery = queryService.LoadQuery("Secondary Users", "Users.query");
                if (iQuery == null)
                {
                    Logging.Log($"The repository \"Secondary Users"\ or the query \"Users.query"\ could not be loaded.", LogLevel.Warning);
                    return UsersICanSetAsSecondaryFromApi();
                }
                
                var users = queryService.Query(iQuery, new QuerySettings());
                
                if(users.TotalCount == 0) {
                    return UsersICanSetAsSecondaryFromApi();
                }
                
                foreach (Dictionary<stringobject> queryResultItem in users.QueryResult)
                {
                    usersICanSetAsSecondary.Add(queryResultItem["UserID"].ToString());
                }
            }
            catch (Exception e)
            {
                Logging.Log($"The repository \"Secondary Users"\ or the query \"Users.query"\ does not exist.\n{e.Message}", LogLevel.Error);
                return UsersICanSetAsSecondaryFromApi();
            }
            
            return usersICanSetAsSecondary;
        }
 
        private static List<string> UsersICanSetAsSecondaryFromApi()
        {
            var usersICanSetAsSecondary = new List<string>();
            var user = User.GetCurrentFrontendUser();
 
            if (user == null)
            {
                return usersICanSetAsSecondary;
            }
            
            usersICanSetAsSecondary.AddRange(user.GetUsersICanSetAsSecondary().Select(u => u.ID.ToString()));
            
            return usersICanSetAsSecondary;
        }

 

Hope that helps.

Nuno Aguiar

 
Dan Gheorghe
Reply

Hi Nuno,

Thank you very much for the solution! Now it loads very fast ;)

Best,
Dan Gheorghe

 
Nuno Aguiar Dynamicweb Employee
Nuno Aguiar
Reply

You are welcome Dan.

 

You must be logged in to post in the forum