/// <summary>
        /// Performs a validation if current type of margin trading is enabled globally and for the particular client
        /// (which is extracted from the action parameters).
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Using this type of margin trading is restricted for client or
        /// a controller action has more then one AccountId in its parameters.
        /// </exception>
        private void ValidateMarginTradingEnabled(ActionExecutingContext context)
        {
            if (!(context.ActionDescriptor is ControllerActionDescriptor controllerActionDescriptor))
            {
                return;
            }

            var cacheKey = CacheKeyBuilder.Create(nameof(MarginTradingEnabledFilter), nameof(GetSingleAccountIdGetter),
                                                  controllerActionDescriptor.DisplayName);
            var accountIdGetter = _cacheProvider.Get(cacheKey,
                                                     () => new CachableResult <AccountIdGetter>(GetSingleAccountIdGetter(controllerActionDescriptor),
                                                                                                CachingParameters.InfiniteCache));

            if (accountIdGetter != null)
            {
                var accountId = accountIdGetter(context.ActionArguments);
                if (!string.IsNullOrWhiteSpace(accountId))
                {
                    var isAccEnabled = _marginTradingSettingsCacheService.IsMarginTradingEnabledByAccountId(accountId);
                    if (isAccEnabled == null)
                    {
                        throw new InvalidOperationException($"Account {accountId} does not exist");
                    }

                    if (!(bool)isAccEnabled)
                    {
                        throw new InvalidOperationException(
                                  $"Using this type of margin trading is restricted for account {accountId}. Error Code: {CommonErrorCodes.AccountDisabled}");
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Performs a validation if current type of margin trading is enabled globally and for the particular client
        /// (which is extracted from the action parameters).
        /// Skips validation if current action method is marked with <see cref="SkipMarginTradingEnabledCheckAttribute"/>.
        /// </summary>
        /// <exception cref="InvalidOperationException">
        /// Using this type of margin trading is restricted for client or
        /// a controller action has more then one ClientId in its parameters.
        /// </exception>
        private async Task ValidateMarginTradingEnabledAsync(ActionExecutingContext context)
        {
            var controllerActionDescriptor = context.ActionDescriptor as ControllerActionDescriptor;

            if (controllerActionDescriptor == null)
            {
                return;
            }

            var cacheKey       = CacheKeyBuilder.Create(nameof(MarginTradingEnabledFilter), nameof(GetSingleClientIdGetter), controllerActionDescriptor.DisplayName);
            var clientIdGetter = _cacheProvider.Get(cacheKey, () => new CachableResult <ClientIdGetter>(GetSingleClientIdGetter(controllerActionDescriptor), CachingParameters.InfiniteCache));

            if (clientIdGetter != null)
            {
                var clientId = clientIdGetter(context.ActionArguments);
                if (string.IsNullOrWhiteSpace(clientId))
                {
                    await _log.WriteWarningAsync(nameof(MarginTradingEnabledFilter), nameof(ValidateMarginTradingEnabledAsync), context.ActionDescriptor.DisplayName, "ClientId is null but is expected. No validation will be performed");
                }
                else if (!await _marginTradingSettingsCacheService.IsMarginTradingEnabled(clientId, _marginSettings.IsLive))
                {
                    throw new InvalidOperationException("Using this type of margin trading is restricted for client " + clientId);
                }
            }
        }
Exemplo n.º 3
0
 public void ShouldCreate()
 {
     Assert.AreEqual("{A}", CacheKeyBuilder.Create("A"));
     Assert.AreEqual("{A}{B}", CacheKeyBuilder.Create("A", "B"));
     Assert.AreEqual("{A}{B}{C}", CacheKeyBuilder.Create("A", "B", "C"));
     Assert.AreEqual("{A}{}{C}", CacheKeyBuilder.Create("A", null, "C"));
     Assert.AreEqual("{:1:2:3:}", CacheKeyBuilder.Create(new[] { 1, 2, 3 }));
     Assert.AreEqual("{A}{:1:2:3:}", CacheKeyBuilder.Create("A", new[] { 1, 2, 3 }));
     Assert.AreEqual("{A}{B}{:1:2:3:}", CacheKeyBuilder.Create("A", "B", new[] { 1, 2, 3 }));
     Assert.AreEqual("{A}{B}{:1:2:3:}{C}", CacheKeyBuilder.Create("A", "B", new[] { 1, 2, 3 }, "C"));
     Assert.AreEqual("{A}{}{:1:2:3:}{}", CacheKeyBuilder.Create("A", null, new[] { 1, 2, 3 }, null));
     Assert.AreEqual("{A}{::1:2:::3:4:::5:6::}", CacheKeyBuilder.Create("A", new[] { new[] { 1, 2 }, new[] { 3, 4 }, new[] { 5, 6 } }));
 }
Exemplo n.º 4
0
 public void ShouldCreateFromObjectsList()
 {
     Assert.AreEqual("{:1:abc::}", CacheKeyBuilder.Create(new object[] { 1, "abc", null }));
 }
Exemplo n.º 5
0
 private string GetClientTradingEnabledCacheKey(string clientId) =>
 CacheKeyBuilder.Create(nameof(MarginTradingEnabledCacheService), nameof(GetClientTradingEnabledCacheKey),
                        clientId);