Base solution for your next web application
Open Closed

Unable to retrieve User Roles after upgrade to 5.3.0 #4968


User avatar
0
ivanosw1 created

Hi, I've just update to 5.3.0 Core/Angular. Previously, to obtain User Roles I did:

var userProfile = await _userManager.GetUserByIdAsync(id); await _userManager.AbpStore.UserRepository.EnsureCollectionLoadedAsync(userProfile, u => u.Roles); var roleId = userProfile.Roles.FirstOrDefault()?.RoleId;

Now AbpStore isn't accessibile due to protection level. How Can I get the RoleId, starting from a User ?

Thanks


8 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    That was rather verbose anyway. Go with encapsulation:

    public class UserManager : AbpUserManager<Role, User>
    {
        // ...
    
        public Task EnsureRolesLoadedAsync(User user)
        {
            return AbpStore.UserRepository.EnsureCollectionLoadedAsync(user, u => u.Roles);
        }
    }
    

    Usage:

    var userProfile = await _userManager.GetUserByIdAsync(id);
    
    await _userManager.EnsureRolesLoadedAsync(userProfile);
    
    var roleId = userProfile.Roles.FirstOrDefault()?.RoleId;
    
  • User Avatar
    0
    ivanosw1 created

    Ok , but this requires a change to your code that I prefer don't touch if possible. We are developing new feature through plugins.

    No other way?

  • User Avatar
    0
    aaron created
    Support Team

    "a change to your code"?

    Either way you have to modify UserManager, which is in your .Core project. You can re-expose AbpStore and keep your code, but this is pretty backward:

    public class UserManager : AbpUserManager<Role, User>
    {
        public new AbpUserStore<Role, User> AbpStore => base.AbpStore;
    
        // ...
    }
    
  • User Avatar
    0
    ivanosw1 created

    Yes you are right but What I mean is that when you release a new version I need only replace the donwloaded template code with the new one (as I've done succesfully many times). If I begin to modify the template downloaded, the process is more dangerous because I can lost my changes.

    I have to care about only to my plugins and adeguate those to the new improovements. In this case instead there is a breaking change.

    I hope now is clear the purpose of my question.

    Thank you

  • User Avatar
    0
    aaron created
    Support Team

    You can make such changes safely. How to migrate existing solution: https://github.com/aspnetzero/aspnet-zero/issues/96#issuecomment-268093697

    If you really don't want to modify UserManager, you can inject and use IRepository<UserRole, long>:

    var roleId = await userRoleRepository.GetAll()
        .Where(ur => ur.UserId == id)
        .Select(ur => ur.RoleId)
        .FirstOrDefaultAsync();
    

    If you only need the first RoleId, this has the performance benefit of not loading the entire Roles collection.

  • User Avatar
    0
    ivanosw1 created

    Good. Now I have a range of possibilities.

    Ok Thank you @aaron.

  • User Avatar
    0
    alper created
    Support Team

    thank you @aaron ;)

  • User Avatar
    0
    aaron created
    Support Team

    That's great :)