public bool AddUserToRole(string userId, SelectRoleEditorViewModel roleName) { //var idResult = _userManager.AddToRole(userId, roleName.RoleName); //add new role var userRole = new SYSTEM_USER_ROLE { UserId = userId, RoleId = _roleManager.FindByName(roleName.RoleName).Id, DateFrom = roleName.DATE_FROM, DateTo = roleName.DATE_TO }; _db.SYSTEM_USER_ROLEs.Add(userRole); _db.SaveChanges(); return(true); }
// Enable initialization with an instance of SYSTEM_USER: public SelectUserRolesViewModel(SYSTEM_USER user) : this() { this.UserName = user.UserName; this.FirstName = user.FirstName; this.LastName = user.LastName; var Db = new ApplicationDbContext(); var um = new UserManager <SYSTEM_USER>( new UserStore <SYSTEM_USER>(new ApplicationDbContext())); var dbUser = um.FindById(user.Id); // Add all available roles to the list of EditorViewModels: var allRoles = Db.Roles; foreach (var role in allRoles) { 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: if (user.SYSTEM_USER_ROLE.Count > 0) { foreach (var userRole in user.SYSTEM_USER_ROLE) { var checkUserRole = this.Roles.Find(r => r.RoleName == userRole.SYSTEM_ROLE.Name); checkUserRole.Selected = true; } } }