refactoring

This commit is contained in:
pavelbannov 2022-10-10 14:32:48 +03:00
parent a563bbe3cf
commit 397af4e55d
6 changed files with 41 additions and 53 deletions

View File

@ -162,7 +162,7 @@ public class AuthenticationController : ControllerBase
sms = true;
_smsManager.ValidateSmsCode(user, inDto.Code, true);
}
else if (TfaAppAuthSettingsHelper.IsVisibleSettings && _tfaAppAuthSettingsHelper.TfaEnabledForUser(user.Id))
else if (_tfaAppAuthSettingsHelper.IsVisibleSettings && _tfaAppAuthSettingsHelper.TfaEnabledForUser(user.Id))
{
if (_tfaManager.ValidateAuthCode(user, inDto.Code, true, true))
{
@ -238,7 +238,7 @@ public class AuthenticationController : ControllerBase
};
}
if (TfaAppAuthSettingsHelper.IsVisibleSettings && _tfaAppAuthSettingsHelper.TfaEnabledForUser(user.Id))
if (_tfaAppAuthSettingsHelper.IsVisibleSettings && _tfaAppAuthSettingsHelper.TfaEnabledForUser(user.Id))
{
if (!TfaAppUserSettings.EnableForUser(_settingsManager, user.Id))
{
@ -373,8 +373,8 @@ public class AuthenticationController : ControllerBase
var requestIp = MessageSettings.GetIP(Request);
user = _bruteForceLoginManager.Attempt(inDto.UserName, inDto.PasswordHash, requestIp, out _);
}
user = _bruteForceLoginManager.Attempt(inDto.UserName, inDto.PasswordHash, requestIp, out _);
}
else
{
if (!(_coreBaseSettings.Standalone || _tenantManager.GetTenantQuota(_tenantManager.GetCurrentTenant().Id).Oauth))

View File

@ -95,9 +95,9 @@ public class TfaappController : BaseSettingsController
{
var result = new List<TfaSettingsDto>();
var SmsVisible = StudioSmsNotificationSettingsHelper.IsVisibleSettings();
var SmsVisible = _studioSmsNotificationSettingsHelper.IsVisibleSettings;
var SmsEnable = SmsVisible && _smsProviderManager.Enabled();
var TfaVisible = TfaAppAuthSettingsHelper.IsVisibleSettings;
var TfaVisible = _tfaAppAuthSettingsHelper.IsVisibleSettings;
var tfaAppSettings = _settingsManager.Load<TfaAppAuthSettings>();
var tfaSmsSettings = _settingsManager.Load<StudioSmsNotificationSettings>();
@ -148,7 +148,7 @@ public class TfaappController : BaseSettingsController
{
var user = _userManager.GetUsers(_authContext.CurrentAccount.ID);
if (StudioSmsNotificationSettingsHelper.IsVisibleSettings() && _studioSmsNotificationSettingsHelper.TfaEnabledForUser(user.Id))// && smsConfirm.ToLower() != "true")
if (_studioSmsNotificationSettingsHelper.IsVisibleSettings && _studioSmsNotificationSettingsHelper.TfaEnabledForUser(user.Id))// && smsConfirm.ToLower() != "true")
{
var confirmType = string.IsNullOrEmpty(user.MobilePhone) ||
user.MobilePhoneActivationStatus == MobilePhoneActivationStatus.NotActivated
@ -158,7 +158,7 @@ public class TfaappController : BaseSettingsController
return _commonLinkUtility.GetConfirmationEmailUrl(user.Email, confirmType);
}
if (TfaAppAuthSettingsHelper.IsVisibleSettings && _tfaAppAuthSettingsHelper.TfaEnabledForUser(user.Id))
if (_tfaAppAuthSettingsHelper.IsVisibleSettings && _tfaAppAuthSettingsHelper.TfaEnabledForUser(user.Id))
{
var confirmType = TfaAppUserSettings.EnableForUser(_settingsManager, _authContext.CurrentAccount.ID)
? ConfirmType.TfaAuth
@ -208,7 +208,7 @@ public class TfaappController : BaseSettingsController
break;
case "app":
if (!TfaAppAuthSettingsHelper.IsVisibleSettings)
if (!_tfaAppAuthSettingsHelper.IsVisibleSettings)
{
throw new Exception(Resource.TfaAppNotAvailable);
}
@ -280,7 +280,7 @@ public class TfaappController : BaseSettingsController
ApiContext.AuthByClaim();
var currentUser = _userManager.GetUsers(_authContext.CurrentAccount.ID);
if (!TfaAppAuthSettingsHelper.IsVisibleSettings ||
if (!_tfaAppAuthSettingsHelper.IsVisibleSettings ||
!_settingsManager.Load<TfaAppAuthSettings>().EnableSetting ||
TfaAppUserSettings.EnableForUser(_settingsManager, currentUser.Id))
{
@ -300,7 +300,7 @@ public class TfaappController : BaseSettingsController
{
var currentUser = _userManager.GetUsers(_authContext.CurrentAccount.ID);
if (!TfaAppAuthSettingsHelper.IsVisibleSettings || !TfaAppUserSettings.EnableForUser(_settingsManager, currentUser.Id))
if (!_tfaAppAuthSettingsHelper.IsVisibleSettings || !TfaAppUserSettings.EnableForUser(_settingsManager, currentUser.Id))
{
throw new Exception(Resource.TfaAppNotAvailable);
}
@ -318,7 +318,7 @@ public class TfaappController : BaseSettingsController
{
var currentUser = _userManager.GetUsers(_authContext.CurrentAccount.ID);
if (!TfaAppAuthSettingsHelper.IsVisibleSettings || !TfaAppUserSettings.EnableForUser(_settingsManager, currentUser.Id))
if (!_tfaAppAuthSettingsHelper.IsVisibleSettings || !TfaAppUserSettings.EnableForUser(_settingsManager, currentUser.Id))
{
throw new Exception(Resource.TfaAppNotAvailable);
}
@ -346,7 +346,7 @@ public class TfaappController : BaseSettingsController
throw new SecurityAccessDeniedException(Resource.ErrorAccessDenied);
}
if (!TfaAppAuthSettingsHelper.IsVisibleSettings || !TfaAppUserSettings.EnableForUser(_settingsManager, user.Id))
if (!_tfaAppAuthSettingsHelper.IsVisibleSettings || !TfaAppUserSettings.EnableForUser(_settingsManager, user.Id))
{
throw new Exception(Resource.TfaAppNotAvailable);
}

View File

@ -42,7 +42,7 @@ public class StudioSmsNotificationSettings : TfaSettingsBase<StudioSmsNotificati
}
[Scope]
public class StudioSmsNotificationSettingsHelper : TfaSettingsHelperBase
public class StudioSmsNotificationSettingsHelper : TfaSettingsHelperBase<StudioSmsNotificationSettings>
{
private readonly TenantExtra _tenantExtra;
private readonly CoreBaseSettings _coreBaseSettings;
@ -57,8 +57,8 @@ public class StudioSmsNotificationSettingsHelper : TfaSettingsHelperBase
SetupInfo setupInfo,
SettingsManager settingsManager,
SmsProviderManager smsProviderManager,
UserManager userManager)
: base(httpContextAccessor, userManager)
UserManager userManager)
: base(settingsManager, httpContextAccessor, userManager)
{
_tenantExtra = tenantExtra;
_coreBaseSettings = coreBaseSettings;
@ -67,14 +67,9 @@ public class StudioSmsNotificationSettingsHelper : TfaSettingsHelperBase
_smsProviderManager = smsProviderManager;
}
public static bool IsVisibleSettings()
{
return SetupInfo.IsVisibleSettings<StudioSmsNotificationSettings>();
}
public bool IsVisibleAndAvailableSettings()
{
return IsVisibleSettings() && IsAvailableSettings();
return IsVisibleSettings && IsAvailableSettings();
}
public bool IsAvailableSettings()
@ -87,13 +82,6 @@ public class StudioSmsNotificationSettingsHelper : TfaSettingsHelperBase
&& !quota.Open);
}
public bool TfaEnabledForUser(Guid userGuid)
{
var settings = _settingsManager.Load<StudioSmsNotificationSettings>();
return TfaEnabledForUser(settings, userGuid);
}
public bool Enable
{
get { return _settingsManager.Load<StudioSmsNotificationSettings>().EnableSetting && _smsProviderManager.Enabled(); }

View File

@ -46,22 +46,26 @@ public abstract class TfaSettingsBase<T> : ISettings<T> where T : ISettings<T>
}
public abstract class TfaSettingsHelperBase
public abstract class TfaSettingsHelperBase<T> where T : TfaSettingsBase<T>
{
private readonly UserManager _userManager;
private readonly UserManager _userManager;
private readonly SettingsManager _settingsManager;
private readonly IHttpContextAccessor _httpContextAccessor;
public TfaSettingsHelperBase(
public TfaSettingsHelperBase(
SettingsManager settingsManager,
IHttpContextAccessor httpContextAccessor,
UserManager userManager)
{
{
_settingsManager = settingsManager;
_httpContextAccessor = httpContextAccessor;
_userManager = userManager;
}
public bool TfaEnabledForUser<T>(TfaSettingsBase<T> settings, Guid userGuid) where T : ISettings<T>
{
public bool TfaEnabledForUser(Guid userGuid)
{
var settings = _settingsManager.Load<T>();
if (!settings.EnableSetting)
{
return false;
@ -91,5 +95,10 @@ public abstract class TfaSettingsHelperBase
}
return true;
}
public bool IsVisibleSettings
{
get { return SetupInfo.IsVisibleSettings<T>(); }
}
}

View File

@ -42,26 +42,19 @@ public class TfaAppAuthSettings : TfaSettingsBase<TfaAppAuthSettings>
}
[Scope]
public class TfaAppAuthSettingsHelper : TfaSettingsHelperBase
public class TfaAppAuthSettingsHelper : TfaSettingsHelperBase<TfaAppAuthSettings>
{
private readonly SettingsManager _settingsManager;
public TfaAppAuthSettingsHelper(
IHttpContextAccessor httpContextAccessor,
UserManager userManager,
SettingsManager settingsManager)
: base(httpContextAccessor, userManager)
SettingsManager settingsManager)
: base(settingsManager, httpContextAccessor, userManager)
{
_settingsManager = settingsManager;
}
public bool TfaEnabledForUser(Guid userGuid)
{
var settings = _settingsManager.Load<TfaAppAuthSettings>();
return TfaEnabledForUser(settings, userGuid);
}
public bool Enable
{
get { return _settingsManager.Load<TfaAppAuthSettings>().EnableSetting; }
@ -80,9 +73,4 @@ public class TfaAppAuthSettingsHelper : TfaSettingsHelperBase
_settingsManager.Save(settings);
}
}
public static bool IsVisibleSettings
{
get { return SetupInfo.IsVisibleSettings<TfaAppAuthSettings>(); }
}
}

View File

@ -67,6 +67,7 @@ public class TfaManager
private readonly Signature _signature;
private readonly InstanceCrypto _instanceCrypto;
private readonly MachinePseudoKeys _machinePseudoKeys;
private readonly TfaAppAuthSettingsHelper _tfaAppAuthSettingsHelper;
public TfaManager(
SettingsManager settingsManager,
@ -76,9 +77,11 @@ public class TfaManager
Signature signature,
InstanceCrypto instanceCrypto,
MachinePseudoKeys machinePseudoKeys,
ICache cache)
ICache cache,
TfaAppAuthSettingsHelper tfaAppAuthSettingsHelper)
{
Cache = cache;
_tfaAppAuthSettingsHelper = tfaAppAuthSettingsHelper;
_settingsManager = settingsManager;
_securityContext = securityContext;
_cookiesManager = cookiesManager;
@ -95,7 +98,7 @@ public class TfaManager
public bool ValidateAuthCode(UserInfo user, string code, bool checkBackup = true, bool isEntryPoint = false)
{
if (!TfaAppAuthSettingsHelper.IsVisibleSettings
if (!_tfaAppAuthSettingsHelper.IsVisibleSettings
|| !_settingsManager.Load<TfaAppAuthSettings>().EnableSetting)
{
return false;