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)
-
0
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;
-
0
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?
-
0
"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; // ... }
-
0
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
-
0
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.
-
0
Good. Now I have a range of possibilities.
Ok Thank you @aaron.
-
0
thank you @aaron ;)
-
0
That's great :)