public async Task <TenantResponse> AddAsync([FromBody] TenantRequest request) { if (!await _UserValidators.ValidatePasswordAsync(new ApplicationUser(), request.password)) { return(GetResponse(false, _UserValidators.InvalidPasswordMessage)); } if (!await _UserValidators.ValidateUserName(request.tenant.AssociatedUsername)) { return(GetResponse(false, _UserValidators.InvalidUserNameMessage)); } var tenant = request.tenant; var creator = await _ApplicationUsersRepository.GetByUserNameAsync(User.Identity.Name); var result = await _TenantsRepository.AddAsync(tenant, creator); if (!result) { return(GetResponse(false)); } var addAssociatedUserResult = await AddAssociatedUserAsync(tenant, request.password); if (!addAssociatedUserResult) { await _TenantsRepository.DeleteAsync(tenant.TenantId); return(GetResponse(false)); } return(GetResponse(true)); }
public async Task <TenantResponse> UpdateAsync([FromBody] TenantRequest request) { if (request.password != null && !await _UserValidators.ValidatePasswordAsync(new ApplicationUser(), request.password)) { return(GetResponse(false, _UserValidators.InvalidPasswordMessage)); } var tenant = request.tenant; var actualTenant = await _TenantsRepository.GetAsync(tenant.TenantId); if (actualTenant.AssociatedUsername != tenant.AssociatedUsername && !await _UserValidators.ValidateUserName(request.tenant.AssociatedUsername)) { return(GetResponse(false, _UserValidators.InvalidUserNameMessage)); } var associatedUser = await _ApplicationUsersRepository.GetByUserNameAsync(actualTenant.AssociatedUsername); if (tenant.AssociatedUsername != actualTenant.AssociatedUsername) { associatedUser.UserName = tenant.AssociatedUsername; await _ApplicationUsersRepository.UpdateAsync(associatedUser); } if (request.password != null) { await _ApplicationUsersRepository.ChangePassword(associatedUser, request.password); } var result = await _TenantsRepository.UpdateAsync(tenant); return(GetResponse(result)); }