public IActionResult ChangeRole(string id, string roleName) { if (String.IsNullOrEmpty(id)) { return(NotFound().WithError(LOCALIZATION_ERROR_NOT_FOUND)); } string parsedRole = Enum.Parse(typeof(UserRole), roleName.ToString()).ToString(); var role = _roleManager.FindByNameAsync(parsedRole) .GetAwaiter().GetResult(); if (String.IsNullOrEmpty(role.Name)) { return(NotFound().WithError(LOCALIZATION_ERROR_NOT_FOUND)); } var user = _userManager.GetUserAsync(User).GetAwaiter().GetResult(); if (user.Id == id) { return(RedirectToAction(nameof(Index)).WithWarning(LOCALIZATION_WARNING_SELF_CHANGEROLE)); } var account = _userManager.FindByIdAsync(id).GetAwaiter().GetResult(); if (account == null) { return(NotFound().WithError(LOCALIZATION_ERROR_NOT_FOUND)); } var roles = _userManager.GetRolesAsync(account).GetAwaiter().GetResult(); if (roles.Count > 0) { _userManager.RemoveFromRolesAsync(account, roles.ToArray()) .GetAwaiter().GetResult(); _userRoleCountryService.Delete(account.Id, CountryId); } var addingresult = _userManager.AddToRoleAsync(account, parsedRole).GetAwaiter().GetResult(); _userRoleCountryService.Add(new UserRoleCountryCreateModel { RoleId = role.Id, CountryId = CountryId, ApplicationUserId = account.Id }); if (!addingresult.Succeeded) { return(RedirectToAction(nameof(Index)).WithError(LOCALIZATION_ERROR_DEFAULT)); } return(RedirectToAction(nameof(Index)).WithSuccess(LOCALIZATION_SUCCESS_DEFAULT)); }
public void AddTest() { var count = _memoryDbContext.UserRoleCountries.Count(); var urcCreateModel = Builder <UserRoleCountryCreateModel> .CreateNew().Build(); Assert.IsTrue(_urcService.Add(urcCreateModel)); Assert.IsTrue(_memoryDbContext.UserRoleCountries.Count() == count + 1); }
private void AddToUserRole(ApplicationUser user, Guid countryId) { var role = _roleManager.FindByNameAsync(Enum.GetName(typeof(UserRole), UserRole.User)) .GetAwaiter().GetResult(); _userRoleCountryService.Add( new UserRoleCountryCreateModel { ApplicationUserId = user.Id, CountryId = countryId, RoleId = role.Id } ); }