public async Task AddToRolesShouldSuccess() { _userManagerFacade.Setup(x => x.AddToRolesAsync(null, null)) .Returns(Task.FromResult(IdentityResult.Success)); var result = await _userManager.AddToRolesAsync(null, null); Assert.That(result.Succeeded, Is.EqualTo(true)); }
private async Task AddUserRoles(TUser user, IReadOnlyCollection <string> rolesToAdd) { if (!rolesToAdd.Any()) { return; } var result = await userManager.AddToRolesAsync(user, rolesToAdd); if (!result.Succeeded) { throw new UserManagementException($"Failed to add roles for the user. {result}"); } }
public async Task <IActionResult> Edit(int?userId, List <string> roles) { if (userId.HasValue) { var userRoles = await _userManager.GetRolesAsync(userId); var addedRoles = roles.Except(userRoles); var removedRoles = userRoles.Except(roles); await _userManager.AddToRolesAsync(userId.Value, addedRoles); await _userManager.RemoveFromRolesAsync(userId.Value, removedRoles); return(RedirectToAction("UserList")); } return(NotFound()); }
public async Task <IActionResult> RegisterCompany(RegisterCompanyViewModel data) { data.UserName = data.Email; //if (data.Email.Contains("@") && data.Email.IndexOf("@", StringComparison.Ordinal) > -1) //{ // .Substring(0, data.Email.IndexOf("@", StringComparison.Ordinal)); //} if (User.IsAuthenticated()) { return(Redirect($"{HttpContext.GetAppBaseUrl()}/home")); } if (!ModelState.IsValid) { data.CountrySelectListItems = await _organizationService.GetCountrySelectListAsync(); return(View(data)); } var userNameExist = await _userManager.UserManager.FindByNameAsync(data.UserName); var userEmailExist = await _userManager.UserManager.FindByEmailAsync(data.Email); if (userEmailExist != null) { data.CountrySelectListItems = await _organizationService.GetCountrySelectListAsync(); ModelState.AddModelError(string.Empty, "Email address is used!"); return(View(data)); } if (userNameExist != null) { data.CountrySelectListItems = await _organizationService.GetCountrySelectListAsync(); ModelState.AddModelError(string.Empty, "UserName is used!"); return(View(data)); } var newCompanyOwner = new GearUser { Email = data.Email, UserName = data.UserName, FirstName = data.FirstName, LastName = data.LastName, AuthenticationType = AuthenticationType.Local, EmailConfirmed = false, IsEditable = true }; //create new user var usrReq = await _userManager.UserManager.CreateAsync(newCompanyOwner, data.Password); if (!usrReq.Succeeded) { ModelState.AppendIdentityResult(usrReq); data.CountrySelectListItems = await _organizationService.GetCountrySelectListAsync(); return(View(data)); } var reqTenant = await _organizationService.CreateOrganizationAsync(data); if (reqTenant.IsSuccess) { var claim = new Claim(nameof(Tenant).ToLowerInvariant(), newCompanyOwner.TenantId.ToString()); newCompanyOwner.TenantId = reqTenant.Result.Id; await _userManager.UserManager.UpdateAsync(newCompanyOwner); await _userManager.UserManager.AddClaimAsync(newCompanyOwner, claim); var generateResult = await _service.GenerateTablesForTenantAsync(reqTenant.Result); if (!generateResult.IsSuccess) { ModelState.AppendResultModelErrors(generateResult.Errors); return(View(reqTenant.Result.Adapt <RegisterCompanyViewModel>())); } //Trigger event TenantEvents.Company.CompanyRegistered(new CompanyRegisterEventArgs { UserName = newCompanyOwner.UserName, UserId = newCompanyOwner.Id, CompanyName = reqTenant.Result.Name, UserEmail = newCompanyOwner.Email }); //send confirm email request await _organizationService.SendConfirmEmailRequestAsync(newCompanyOwner); var roleReq = await _userManager.AddToRolesAsync(newCompanyOwner, new List <string> { MultiTenantResources.Roles.COMPANY_ADMINISTRATOR }); if (!roleReq.IsSuccess) { ModelState.AppendResultModelErrors(roleReq.Errors); return(View(reqTenant.Result.Adapt <RegisterCompanyViewModel>())); } //sing in new created var signResult = await _signInManager.PasswordSignInAsync(data.UserName, data.Password, true, false); if (signResult.Succeeded) { return(RedirectToAction("Index", "Home")); } ModelState.AddModelError(string.Empty, "Fail to sign in"); return(View(reqTenant.Result.Adapt <RegisterCompanyViewModel>())); } await _userManager.UserManager.DeleteAsync(newCompanyOwner); ModelState.AppendResultModelErrors(reqTenant.Errors); return(View(reqTenant.Result.Adapt <RegisterCompanyViewModel>())); }