Ejemplo n.º 1
0
        /// <summary>
        /// Verifies whether or not the <see cref="ODataQueryOptions(TEntity)"/> are allowed or not.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="odataOptions">The odata options from the request.</param>
        /// <param name="allowedQueryStructure">Each web api will have their individual allowed set of query strutures.</param>
        /// <param name="isFeatureEnabled">The configuration state for the feature.</param>
        /// <param name="telemetryContext">Information to be used by the telemetry events.</param>
        /// <returns>True if the options are allowed.</returns>
        public static bool AreODataOptionsAllowed <TEntity>(ODataQueryOptions <TEntity> odataOptions,
                                                            ODataQueryFilter allowedQueryStructure,
                                                            bool isFeatureEnabled,
                                                            string telemetryContext)
        {
            // If validation of the ODataQueryOptions fails, we will not reject the request.
            var isAllowed = true;

            try
            {
                isAllowed = allowedQueryStructure.IsAllowed(odataOptions);
            }
            catch (Exception ex)
            {
                var telemetryProperties = new Dictionary <string, string>();
                telemetryProperties.Add(TelemetryService.CallContext, $"{telemetryContext}:{nameof(AreODataOptionsAllowed)}");
                telemetryProperties.Add(TelemetryService.IsEnabled, $"{isFeatureEnabled}");

                // Log and do not throw
                Telemetry.TrackException(ex, telemetryProperties);
            }

            _telemetryService.TrackODataQueryFilterEvent(
                callContext: $"{telemetryContext}:{nameof(AreODataOptionsAllowed)}",
                isEnabled: isFeatureEnabled,
                isAllowed: isAllowed,
                queryPattern: ODataQueryFilter.ODataOptionsMap(odataOptions).ToString());

            return(isFeatureEnabled ? isAllowed : true);
        }
Ejemplo n.º 2
0
 internal static string GetValidationFailedMessage <T>(ODataQueryOptions <T> options)
 {
     return($"A query with \"{ODataQueryFilter.ODataOptionsMap(options)}\" set of operators is not supported. Please refer to : https://github.com/NuGet/Home/wiki/Filter-OData-query-requests for additional information.");
 }