Skip to content

Commit

Permalink
Merge pull request 'Add Tenant Middleware and update tenant managemen…
Browse files Browse the repository at this point in the history
…t logic' (#95) from feature/remove-async into develop
  • Loading branch information
pavelbannov committed Nov 28, 2024
2 parents 85d3865 + 8cbf73d commit c6259da
Show file tree
Hide file tree
Showing 172 changed files with 1,058 additions and 1,077 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -961,7 +961,7 @@ await SetProgress(Convert.ToInt32(percentage),
case LdapOperationType.Save:
case LdapOperationType.Sync:
removedUser.Sid = null;
if (!removedUser.IsOwner(await _tenantManager.GetCurrentTenantAsync()) && !(_currentUser != null && _currentUser.Id == removedUser.Id && await _userManager.IsDocSpaceAdminAsync(removedUser)))
if (!removedUser.IsOwner(_tenantManager.GetCurrentTenant()) && !(_currentUser != null && _currentUser.Id == removedUser.Id && await _userManager.IsDocSpaceAdminAsync(removedUser)))
{
removedUser.Status = EmployeeStatus.Terminated; // Disable user on portal
}
Expand Down
8 changes: 4 additions & 4 deletions common/ASC.ActiveDirectory/LdapUserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ private async Task<UserInfoAndLdapChangeCollectionWrapper> SyncLDAPUserAsync(Use
using var scope = serviceProvider.CreateScope();
var tenantManager = scope.ServiceProvider.GetRequiredService<TenantManager>();
var source = scope.ServiceProvider.GetRequiredService<LdapNotifySource>();
source.Init(await tenantManager.GetCurrentTenantAsync());
source.Init(tenantManager.GetCurrentTenant());
var workContext = scope.ServiceProvider.GetRequiredService<WorkContext>();
var client = workContext.RegisterClient(scope.ServiceProvider, source);
var urlShortener = scope.ServiceProvider.GetRequiredService<IUrlShortener>();

var confirmLink = await commonLinkUtility.GetConfirmationEmailUrlAsync(ldapUserInfo.Email, ConfirmType.EmailActivation);
var confirmLink = commonLinkUtility.GetConfirmationEmailUrl(ldapUserInfo.Email, ConfirmType.EmailActivation);

await client.SendNoticeToAsync(
NotifyConstants.ActionLdapActivation,
Expand Down Expand Up @@ -567,7 +567,7 @@ private async Task<bool> NeedUpdateUserAsync(UserInfo portalUser, UserInfo ldapU
userToUpdate.MobilePhone = updateInfo.MobilePhone;
}

if (!userToUpdate.IsOwner(await tenantManager.GetCurrentTenantAsync())) // Owner must never be terminated by LDAP!
if (!userToUpdate.IsOwner(tenantManager.GetCurrentTenant())) // Owner must never be terminated by LDAP!
{
userToUpdate.Status = updateInfo.Status;
}
Expand Down Expand Up @@ -638,7 +638,7 @@ public async Task<UserInfo> TryGetAndSyncLdapUserInfo(string login, string passw
{
logger.DebugTryCheckAndSyncToLdapUser(ldapUserInfo.Item1.UserName, ldapUserInfo.Item1.Email, ldapUserInfo.Item2.DistinguishedName);

var tenant = await tenantManager.GetCurrentTenantAsync();
var tenant = tenantManager.GetCurrentTenant();

_ = Task.Run(Action);

Expand Down
2 changes: 2 additions & 0 deletions common/ASC.Api.Core/Core/BaseStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ public virtual void Configure(IApplicationBuilder app, IWebHostEnvironment env)

app.UseSynchronizationContextMiddleware();

app.UseTenantMiddleware();

app.UseAuthentication();

// TODO: if some client requests very slow, this line will need to remove
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Api.Core/Cors/DynamicCorsPolicyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private async Task<IEnumerable<string>> GetOriginsFromOAuth2App()
var claimIdClaim = token.Claims.Single(c => string.Equals(c.Type, "cid", StringComparison.OrdinalIgnoreCase));
var clientId = Guid.Parse(claimIdClaim.Value);

var tenantId = await tenantManager.GetCurrentTenantIdAsync();
var tenantId = tenantManager.GetCurrentTenantId();
var cookieValue = await cookieStorage.EncryptCookieAsync(tenantId, userId, 0);
var cookieName = cookiesManager.GetAscCookiesName();

Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Api.Core/Middleware/CultureMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task Invoke(HttpContext context, UserManager userManager, TenantMan
}
}

culture ??= (await tenantManager.GetCurrentTenantAsync(false))?.GetCulture();
culture ??= (tenantManager.GetCurrentTenant(false))?.GetCulture();

if (culture != null)
{
Expand Down
2 changes: 1 addition & 1 deletion common/ASC.Api.Core/Middleware/TenantStatusFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class TenantStatusFilter(ILogger<TenantStatusFilter> logger, TenantManage

public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
{
var tenant = await tenantManager.GetCurrentTenantAsync(false);
var tenant = tenantManager.GetCurrentTenant(false);
if (tenant == null)
{
context.Result = new StatusCodeResult((int)HttpStatusCode.NotFound);
Expand Down
86 changes: 43 additions & 43 deletions common/ASC.Api.Core/Model/EmployeeFullDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,27 @@ namespace ASC.Web.Api.Models;

public class EmployeeFullDto : EmployeeDto
{
/// <summary>
/// First name
/// </summary>
/// <summary>
/// First name
/// </summary>
[SwaggerSchemaCustom(Example = "Mike")]
public string FirstName { get; set; }

/// <summary>
/// Last name
/// </summary>
/// <summary>
/// Last name
/// </summary>
[SwaggerSchemaCustom(Example = "Zanyatski")]
public string LastName { get; set; }

/// <summary>
/// Username
/// </summary>
/// <summary>
/// Username
/// </summary>
[SwaggerSchemaCustom(Example = "Mike.Zanyatski")]
public string UserName { get; set; }

/// <summary>
/// Email
/// </summary>
/// <summary>
/// Email
/// </summary>
[SwaggerSchemaCustom(Example = "[email protected]")]
[EmailAddress]
public string Email { get; set; }
Expand All @@ -65,9 +65,9 @@ public class EmployeeFullDto : EmployeeDto
/// </summary>
public ApiDateTime Birthday { get; set; }

/// <summary>
/// Sex
/// </summary>
/// <summary>
/// Sex
/// </summary>
[SwaggerSchemaCustom(Example = "male")]
public string Sex { get; set; }

Expand All @@ -86,9 +86,9 @@ public class EmployeeFullDto : EmployeeDto
/// </summary>
public ApiDateTime Terminated { get; set; }

/// <summary>
/// Department
/// </summary>
/// <summary>
/// Department
/// </summary>
[SwaggerSchemaCustom(Example = "Marketing")]
public string Department { get; set; }

Expand All @@ -102,21 +102,21 @@ public class EmployeeFullDto : EmployeeDto
/// </summary>
public List<GroupSummaryDto> Groups { get; set; }

/// <summary>
/// Location
/// </summary>
/// <summary>
/// Location
/// </summary>
[SwaggerSchemaCustom(Example = "Palo Alto")]
public string Location { get; set; }

/// <summary>
/// Notes
/// </summary>
/// <summary>
/// Notes
/// </summary>
[SwaggerSchemaCustom(Example = "Notes to worker")]
public string Notes { get; set; }

/// <summary>
/// Specifies if the user is an administrator or not
/// </summary>
/// <summary>
/// Specifies if the user is an administrator or not
/// </summary>
[SwaggerSchemaCustom(Example = false)]
public bool IsAdmin { get; set; }

Expand All @@ -125,15 +125,15 @@ public class EmployeeFullDto : EmployeeDto
/// </summary>
public bool IsRoomAdmin { get; set; }

/// <summary>
/// Specifies if the LDAP settings are enabled for the user or not
/// </summary>
/// <summary>
/// Specifies if the LDAP settings are enabled for the user or not
/// </summary>
[SwaggerSchemaCustom(Example = false)]
public bool IsLDAP { get; set; }

/// <summary>
/// List of administrator modules
/// </summary>
/// <summary>
/// List of administrator modules
/// </summary>
[SwaggerSchemaCustom(Example = "[\"projects\", \"crm\"]")]
public List<string> ListAdminModules { get; set; }

Expand All @@ -152,9 +152,9 @@ public class EmployeeFullDto : EmployeeDto
/// </summary>
public bool IsCollaborator { get; set; }

/// <summary>
/// Language
/// </summary>
/// <summary>
/// Language
/// </summary>
[SwaggerSchemaCustom(Example = "en-EN")]
public string CultureName { get; set; }

Expand All @@ -168,9 +168,9 @@ public class EmployeeFullDto : EmployeeDto
/// </summary>
public MobilePhoneActivationStatus MobilePhoneActivationStatus { get; set; }

/// <summary>
/// Specifies if the SSO settings are enabled for the user or not
/// </summary>
/// <summary>
/// Specifies if the SSO settings are enabled for the user or not
/// </summary>
[SwaggerSchemaCustom(Example = false)]
public bool IsSSO { get; set; }

Expand All @@ -184,9 +184,9 @@ public class EmployeeFullDto : EmployeeDto
/// </summary>
public long? QuotaLimit { get; set; }

/// <summary>
/// Portal used space
/// </summary>
/// <summary>
/// Portal used space
/// </summary>
[SwaggerSchemaCustom(Example = 12345)]
public double? UsedSpace { get; set; }

Expand Down Expand Up @@ -306,7 +306,7 @@ public async Task<EmployeeFullDto> GetSimpleWithEmail(UserInfo userInfo)
public async Task<EmployeeFullDto> GetFullAsync(UserInfo userInfo, bool? shared = null)
{
var currentType = await _userManager.GetUserTypeAsync(userInfo.Id);
var tenant = await tenantManager.GetCurrentTenantAsync();
var tenant = tenantManager.GetCurrentTenant();

var result = new EmployeeFullDto
{
Expand Down
32 changes: 16 additions & 16 deletions common/ASC.Api.Core/Security/EmailValidationKeyModelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ public async Task<ValidationResult> ValidateAsync(EmailValidationKeyModel inDto)
switch (type)
{
case ConfirmType.EmpInvite:
checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + (int)emplType, key, provider.ValidEmailKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + (int)emplType, key, provider.ValidEmailKeyInterval);
if (checkKeyResult == ValidationResult.Invalid)
{
checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + (int)emplType + "trust", key, provider.ValidEmailKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + (int)emplType + "trust", key, provider.ValidEmailKeyInterval);
if (checkKeyResult == ValidationResult.Ok)
{
var tenant = await tenantManager.GetCurrentTenantAsync();
var tenant = tenantManager.GetCurrentTenant();

if (tenant.TrustedDomainsType == TenantTrustedDomainsType.All)
{
Expand Down Expand Up @@ -142,15 +142,15 @@ public async Task<ValidationResult> ValidateAsync(EmailValidationKeyModel inDto)
checkKeyResult = ValidationResult.Invalid;
break;
}
checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + uiD.GetValueOrDefault(), key, provider.ValidEmailKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + uiD.GetValueOrDefault(), key, provider.ValidEmailKeyInterval);
break;

case ConfirmType.EmailChange:
var userId = uiD.GetValueOrDefault();
var emailChangeEvent = (await auditEventsRepository.GetByFilterAsync(action: MessageAction.UserSentEmailChangeInstructions, entry: EntryType.User, target: MessageTarget.Create(userId).ToString(), limit: 1)).FirstOrDefault();
var postfix = emailChangeEvent == null ? userId.ToString() : tenantUtil.DateTimeToUtc(emailChangeEvent.Date).ToString("s", CultureInfo.InvariantCulture);

checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + postfix, key, provider.ValidEmailKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + postfix, key, provider.ValidEmailKeyInterval);
break;
case ConfirmType.PasswordChange:
userInfo = await userManager.GetUserByEmailAsync(email);
Expand All @@ -175,7 +175,7 @@ public async Task<ValidationResult> ValidateAsync(EmailValidationKeyModel inDto)
hash = passwordStamp.ToString("s", CultureInfo.InvariantCulture);
}

checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + hash, key, provider.ValidEmailKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + hash, key, provider.ValidEmailKeyInterval);

if (checkKeyResult is ValidationResult.Ok && userInfo.ActivationStatus is not EmployeeActivationStatus.Activated)
{
Expand All @@ -187,7 +187,7 @@ public async Task<ValidationResult> ValidateAsync(EmailValidationKeyModel inDto)
break;

case ConfirmType.Activation:
checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + uiD, key, provider.ValidEmailKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + uiD, key, provider.ValidEmailKeyInterval);
break;

case ConfirmType.ProfileRemove:
Expand All @@ -198,25 +198,25 @@ public async Task<ValidationResult> ValidateAsync(EmailValidationKeyModel inDto)
return ValidationResult.Invalid;
}

checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + uiD, key, provider.ValidEmailKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + uiD, key, provider.ValidEmailKeyInterval);
break;

case ConfirmType.Wizard:
checkKeyResult = await provider.ValidateEmailKeyAsync("" + type, key, provider.ValidEmailKeyInterval);
checkKeyResult = provider.ValidateEmailKey("" + type, key, provider.ValidEmailKeyInterval);
break;

case ConfirmType.PhoneActivation:
case ConfirmType.PhoneAuth:
case ConfirmType.TfaActivation:
case ConfirmType.TfaAuth:
checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + first, key, provider.ValidAuthKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + first, key, provider.ValidAuthKeyInterval);
break;
case ConfirmType.Auth:
checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + first, key, provider.ValidAuthKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + first, key, provider.ValidAuthKeyInterval);
if (checkKeyResult == ValidationResult.Invalid)
{
userInfo = await userManager.GetUserByEmailAsync(email);
var portalRenameEvent = (await auditEventsRepository.GetByFilterAsync(action: MessageAction.PortalRenamed, target: MessageTarget.Create(await tenantManager.GetCurrentTenantIdAsync()).ToString(), limit: 1)).FirstOrDefault();
var portalRenameEvent = (await auditEventsRepository.GetByFilterAsync(action: MessageAction.PortalRenamed, target: MessageTarget.Create(tenantManager.GetCurrentTenantId()).ToString(), limit: 1)).FirstOrDefault();
var validInterval = DateTime.UtcNow.Add(-provider.ValidAuthKeyInterval);
if (portalRenameEvent != null)
{
Expand All @@ -226,7 +226,7 @@ public async Task<ValidationResult> ValidateAsync(EmailValidationKeyModel inDto)
var loginEvent = (await loginEventsRepository.GetByFilterAsync(userInfo.Id, MessageAction.LoginSuccessViaApi, limit: 1, fromDate: portalRenameEventDate)).FirstOrDefault();
if (loginEvent == null)
{
checkKeyResult = await provider.ValidateEmailKeyAsync(email + type + portalRenameEventDate.ToString(CultureInfo.InvariantCulture), key, provider.ValidAuthKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type + portalRenameEventDate.ToString(CultureInfo.InvariantCulture), key, provider.ValidAuthKeyInterval);
}
}
}
Expand All @@ -244,19 +244,19 @@ public async Task<ValidationResult> ValidateAsync(EmailValidationKeyModel inDto)

var validTimeInterval = type == ConfirmType.PortalContinue ? TimeSpan.MaxValue : provider.ValidEmailKeyInterval;

checkKeyResult = await provider.ValidateEmailKeyAsync(email + type, key, validTimeInterval);
checkKeyResult = provider.ValidateEmailKey(email + type, key, validTimeInterval);
break;

default:
checkKeyResult = await provider.ValidateEmailKeyAsync(email + type, key, provider.ValidEmailKeyInterval);
checkKeyResult = provider.ValidateEmailKey(email + type, key, provider.ValidEmailKeyInterval);
break;
}

return checkKeyResult;

async Task<bool> CheckOwnerRights(string email)
{
var ownerId = (await tenantManager.GetCurrentTenantAsync()).OwnerId;
var ownerId = (tenantManager.GetCurrentTenant()).OwnerId;
var user = await userManager.GetUserByEmailAsync(email);
return ownerId.Equals(user.Id);
}
Expand Down
Loading

0 comments on commit c6259da

Please sign in to comment.