Skip to content

Commit

Permalink
fix Bug 60163
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelbannov committed Nov 27, 2023
1 parent ffbc7cd commit ed445bd
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 62 deletions.
4 changes: 2 additions & 2 deletions web/ASC.Web.Api/Api/PortalController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ public async Task DeletePortalImmediatelyAsync()

if (!_coreBaseSettings.Standalone)
{
await _apiSystemHelper.RemoveTenantFromCacheAsync(tenant.Alias);
await _apiSystemHelper.RemoveTenantFromCacheAsync(tenant.GetTenantDomain(_coreSettings));
}

try
Expand Down Expand Up @@ -725,7 +725,7 @@ public async Task<object> DeletePortalAsync()

if (!_coreBaseSettings.Standalone)
{
await _apiSystemHelper.RemoveTenantFromCacheAsync(tenant.Alias);
await _apiSystemHelper.RemoveTenantFromCacheAsync(tenant.GetTenantDomain(_coreSettings));
}

var owner = await _userManager.GetUsersAsync(tenant.OwnerId);
Expand Down
112 changes: 57 additions & 55 deletions web/ASC.Web.Core/Helpers/ApiSystemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public class ApiSystemHelper
private readonly TenantDomainValidator _tenantDomainValidator;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly IConfiguration _configuration;
private const string TenantRegionKey = "tenant_region";
private const string TenantDomainKey = "tenant_domain";
private const string RegionTableName = "docspace-tenants_region";

public ApiSystemHelper(IConfiguration configuration,
CoreBaseSettings coreBaseSettings,
Expand All @@ -66,17 +69,6 @@ public ApiSystemHelper(IConfiguration configuration,
{
ApiCacheEnable = true;
}


}

private AmazonDynamoDBClient GetDynamoDBClient()
{
var awsAccessKeyId = _configuration["aws:dynamoDB:accessKeyId"];
var awsSecretAccessKey = _configuration["aws:dynamoDB:secretAccessKey"];
var region = _configuration["aws:dynamoDB:region"];

return new AmazonDynamoDBClient(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.GetBySystemName(region));
}

public string CreateAuthToken(string pkey)
Expand All @@ -98,13 +90,13 @@ public async Task ValidatePortalNameAsync(string domain, Guid userId)

var dataJson = System.Text.Json.JsonSerializer.Serialize(data);
var result = await SendToApiAsync(ApiSystemUrl, "portal/validateportalname", WebRequestMethods.Http.Post, userId, dataJson);
var resObj = JsonNode.Parse(result).AsObject();
if (resObj["error"] != null)
var resObj = JsonNode.Parse(result)?.AsObject();
if (resObj?["error"] != null)
{
if (resObj["error"].ToString() == "portalNameExist")
{
var varians = resObj["variants"].AsArray().Select(r => r.ToString()).ToList();
throw new TenantAlreadyExistsException("Address busy.", varians);
var variants = resObj["variants"].AsArray().Select(r => r.ToString()).ToList();
throw new TenantAlreadyExistsException("Address busy.", variants);
}

throw new Exception(resObj["error"].ToString());
Expand All @@ -123,81 +115,84 @@ public async Task AddTenantToCacheAsync(string tenantDomain, string tenantRegion
tenantRegion = "default";
}

using var _awsDynamoDBClient = GetDynamoDBClient();
using var awsDynamoDbClient = GetDynamoDBClient();

var putItemRequest = new PutItemRequest
{
TableName = "docspace-tenants_region",
Item = new Dictionary<string, AttributeValue>()
{
{ "tenant_domain", new AttributeValue {
S = tenantDomain
}},
{ "tenant_region", new AttributeValue {
S = tenantRegion
}}
TableName = RegionTableName,
Item = new Dictionary<string, AttributeValue>
{
{ TenantDomainKey, new AttributeValue
{
S = tenantDomain
}
},
{ TenantRegionKey, new AttributeValue
{
S = tenantRegion
}
}
}
};

await _awsDynamoDBClient.PutItemAsync(putItemRequest);
await awsDynamoDbClient.PutItemAsync(putItemRequest);
}

public async Task UpdateTenantToCacheAsync(string oldTenantDomain, string newTenantDomain)
{
using var _awsDynamoDBClient = GetDynamoDBClient();
using var awsDynamoDbClient = GetDynamoDBClient();

var getItemRequest = new GetItemRequest
{
TableName = "docspace-tenants_region",
Key = new Dictionary<string, AttributeValue>()
{
{ "tenant_domain", new AttributeValue { S = oldTenantDomain } }
},
ProjectionExpression = "tenant_region",
TableName = RegionTableName,
Key = new Dictionary<string, AttributeValue>
{
{ TenantDomainKey, new AttributeValue { S = oldTenantDomain } }
},
ProjectionExpression = TenantRegionKey,
ConsistentRead = true
};

var region = (await _awsDynamoDBClient.GetItemAsync(getItemRequest)).Item.Values.First().S;
var region = (await awsDynamoDbClient.GetItemAsync(getItemRequest)).Item.Values.First().S;

await AddTenantToCacheAsync(newTenantDomain, region);
await RemoveTenantFromCacheAsync(oldTenantDomain);
}

public async Task RemoveTenantFromCacheAsync(string tenantDomain)
{
using var _awsDynamoDBClient = GetDynamoDBClient();
using var awsDynamoDbClient = GetDynamoDBClient();

var request = new DeleteItemRequest
{
TableName = "docspace-tenants_region",
Key = new Dictionary<string, AttributeValue>()
{
{ "tenant_domain", new AttributeValue { S = tenantDomain }
}
TableName = RegionTableName,
Key = new Dictionary<string, AttributeValue>
{
{ TenantDomainKey, new AttributeValue { S = tenantDomain } }
},
};

await _awsDynamoDBClient.DeleteItemAsync(request);
await awsDynamoDbClient.DeleteItemAsync(request);
}

public async Task<IEnumerable<string>> FindTenantsInCacheAsync(string portalName)
{
using var _awsDynamoDBClient = GetDynamoDBClient();
using var awsDynamoDbClient = GetDynamoDBClient();

var tenantDomain = $"{portalName}.{_coreBaseSettings.Basedomain}";

var getItemRequest = new GetItemRequest
{
TableName = "docspace-tenants_region",
Key = new Dictionary<string, AttributeValue>()
{
{ "tenant_domain", new AttributeValue { S = tenantDomain } }
},
ProjectionExpression = "tenant_region",
TableName = RegionTableName,
Key = new Dictionary<string, AttributeValue>
{
{ TenantDomainKey, new AttributeValue { S = tenantDomain } }
},
ProjectionExpression = TenantRegionKey,
ConsistentRead = true
};

var getItemResponse = await _awsDynamoDBClient.GetItemAsync(getItemRequest);
var getItemResponse = await awsDynamoDbClient.GetItemAsync(getItemRequest);

if (getItemResponse.Item.Count == 0) return null;

Expand All @@ -216,22 +211,29 @@ public async Task<IEnumerable<string>> FindTenantsInCacheAsync(string portalName

var scanRequest = new ScanRequest
{
TableName = "docspace-tenants_region",
TableName = RegionTableName,
FilterExpression = "begins_with(tenant_domain, :v_tenant_domain)",
ExpressionAttributeValues = new Dictionary<string, AttributeValue> {
{":v_tenant_domain", new AttributeValue { S = portalName }} },
ProjectionExpression = "tenant_domain",
ProjectionExpression = TenantDomainKey,
ConsistentRead = true
};

var scanResponce = await _awsDynamoDBClient.ScanAsync(scanRequest);
var result = scanResponce.Items.Select(x => x.Values.First().S.Split('.')[0]);

var scanResponse = await awsDynamoDbClient.ScanAsync(scanRequest);
var result = scanResponse.Items.Select(x => x.Values.First().S.Split('.')[0]);
return result;
}

#endregion

private AmazonDynamoDBClient GetDynamoDBClient()
{
var awsAccessKeyId = _configuration["aws:dynamoDB:accessKeyId"];
var awsSecretAccessKey = _configuration["aws:dynamoDB:secretAccessKey"];
var region = _configuration["aws:dynamoDB:region"];

return new AmazonDynamoDBClient(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.GetBySystemName(region));
}

private async Task<string> SendToApiAsync(string absoluteApiUrl, string apiPath, string httpMethod, Guid userId, string data = null)
{
if (!Uri.TryCreate(absoluteApiUrl, UriKind.Absolute, out _))
Expand Down
10 changes: 5 additions & 5 deletions web/ASC.Web.Core/Notify/StudioPeriodicNotify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ public class StudioPeriodicNotify
private readonly StudioNotifyHelper _studioNotifyHelper;
private readonly ITariffService _tariffService;
private readonly TenantExtra _tenantExtra;
private readonly AuthContext _authContext;
private readonly CommonLinkUtility _commonLinkUtility;
private readonly ApiSystemHelper _apiSystemHelper;
private readonly SetupInfo _setupInfo;
Expand All @@ -44,6 +43,7 @@ public class StudioPeriodicNotify
private readonly DisplayUserSettingsHelper _displayUserSettingsHelper;
private readonly AuthManager _authManager;
private readonly SecurityContext _securityContext;
private readonly CoreSettings _coreSettings;
private readonly IServiceProvider _serviceProvider;
private readonly ILogger _log;

Expand All @@ -55,7 +55,6 @@ public StudioPeriodicNotify(
StudioNotifyHelper studioNotifyHelper,
ITariffService tariffService,
TenantExtra tenantExtra,
AuthContext authContext,
CommonLinkUtility commonLinkUtility,
ApiSystemHelper apiSystemHelper,
SetupInfo setupInfo,
Expand All @@ -64,6 +63,7 @@ public StudioPeriodicNotify(
DisplayUserSettingsHelper displayUserSettingsHelper,
AuthManager authManager,
SecurityContext securityContext,
CoreSettings coreSettings,
IServiceProvider serviceProvider)
{
_workContext = workContext;
Expand All @@ -72,7 +72,6 @@ public StudioPeriodicNotify(
_studioNotifyHelper = studioNotifyHelper;
_tariffService = tariffService;
_tenantExtra = tenantExtra;
_authContext = authContext;
_commonLinkUtility = commonLinkUtility;
_apiSystemHelper = apiSystemHelper;
_setupInfo = setupInfo;
Expand All @@ -81,6 +80,7 @@ public StudioPeriodicNotify(
_displayUserSettingsHelper = displayUserSettingsHelper;
_authManager = authManager;
_securityContext = securityContext;
_coreSettings = coreSettings;
_serviceProvider = serviceProvider;
_log = log.CreateLogger("ASC.Notify");
}
Expand Down Expand Up @@ -245,7 +245,7 @@ public async ValueTask SendSaasLettersAsync(string senderName, DateTime schedule

if (!_coreBaseSettings.Standalone && _apiSystemHelper.ApiCacheEnable)
{
await _apiSystemHelper.RemoveTenantFromCacheAsync(tenant.Alias);
await _apiSystemHelper.RemoveTenantFromCacheAsync(tenant.GetTenantDomain(_coreSettings));
}
}

Expand Down Expand Up @@ -334,7 +334,7 @@ public async ValueTask SendSaasLettersAsync(string senderName, DateTime schedule

if (!_coreBaseSettings.Standalone && _apiSystemHelper.ApiCacheEnable)
{
await _apiSystemHelper.RemoveTenantFromCacheAsync(tenant.Alias);
await _apiSystemHelper.RemoveTenantFromCacheAsync(tenant.GetTenantDomain(_coreSettings));
}
}

Expand Down

0 comments on commit ed445bd

Please sign in to comment.