// Enable initialization with an instance of ApplicationUser: public SelectUserRolesViewModel(MacheteUser userBeingModified, IEnumerable <MacheteRole> allRoles, UserManager <MacheteUser> userManager) { Roles = new List <SelectRoleEditorViewModel>(); UserName = userBeingModified.UserName; string[] firstLast = userBeingModified.UserName.Split('.'); FirstName = firstLast[0]; LastName = firstLast.Length > 1 ? firstLast[1] : ""; UserId = userBeingModified.Id; // ReSharper disable once SuggestVarOrType_Elsewhere // Add all available roles to the list of EditorViewModels: foreach (var role in allRoles) { // An EditorViewModel will be used by Editor Template: var selectableRole = new SelectRoleEditorViewModel(role); var isInRole = userManager.IsInRoleAsync(userBeingModified, selectableRole.RoleName); isInRole.Wait(); // I'm sorry if (isInRole.Result) { selectableRole.Selected = true; } Roles.Add(selectableRole); } }
// Enable initialization with an instance of ApplicationUser: public SelectUserRolesViewModel(MacheteUser user, MacheteContext dbFactory) : this() { UserName = user.UserName; string[] firstLast = user.UserName.Split('.'); FirstName = firstLast[0]; if (firstLast.Length > 1) { LastName = firstLast[1]; } else { LastName = ""; } UserId = user.Id; // ReSharper disable once SuggestVarOrType_Elsewhere // Add all available roles to the list of EditorViewModels: DbSet <IdentityRole> allRoles = dbFactory.Roles; foreach (var role in allRoles) { // An EditorViewModel will be used by Editor Template: var rvm = new SelectRoleEditorViewModel(role); Roles.Add(rvm); } // Set the Selected property to true for those roles for // which the current user is a member: foreach (var userRole in user.Roles) { SelectRoleEditorViewModel checkUserRole = Roles.Find(r => r.RoleId == userRole.Id); checkUserRole.Selected = true; } }
// Enable initialization with an instance of ApplicationUser: public SelectUserRolesViewModel(MacheteUser user, IDatabaseFactory dbFactory) : this() { this.UserName = user.UserName; string[] firstLast = user.UserName.Split('.'); this.FirstName = firstLast[0]; if (firstLast.Length > 1) { this.LastName = firstLast[1]; } else { this.LastName = ""; } this.UserId = user.Id; // Add all available roles to the list of EditorViewModels: IDbSet <IdentityRole> allRoles = dbFactory.Get().Roles; foreach (var role in allRoles) { // An EditorViewModel will be used by Editor Template: var rvm = new SelectRoleEditorViewModel(role); this.Roles.Add(rvm); } // Set the Selected property to true for those roles for // which the current user is a member: foreach (IdentityUserRole userRole in user.Roles) { SelectRoleEditorViewModel checkUserRole = this.Roles.Find(r => r.RoleId == userRole.RoleId); checkUserRole.Selected = true; } }