/// <summary>
        /// Applies a join-based filter to the criteria for the specified authorization view.
        /// </summary>
        /// <param name="criteria">The criteria to which filters should be applied.</param>
        /// <param name="whereJunction">The <see cref="ICriterion" /> container for adding WHERE clause criterion.</param>
        /// <param name="parameters">The named parameters to be used to satisfy additional filtering requirements.</param>
        /// <param name="viewName">The name of the view to be filtered.</param>
        /// <param name="joinPropertyName">The name of the property to be joined between the entity being queried and the authorization view.</param>
        /// <param name="filterPropertyName">The name of the property to be used for applying filter values.</param>
        /// <param name="joinType">The <see cref="JoinType" /> to be used.</param>
        public static void ApplyJoinFilter(
            this ICriteria criteria,
            Junction whereJunction,
            IDictionary <string, object> parameters,
            string viewName,
            string joinPropertyName,
            string filterPropertyName,
            JoinType joinType)
        {
            string authViewAlias = $"authView{viewName}";

            // Apply authorization join using ICriteria
            criteria.CreateEntityAlias(
                authViewAlias,
                Restrictions.EqProperty($"aggregateRoot.{joinPropertyName}", $"{authViewAlias}.{joinPropertyName}"),
                joinType,
                $"{viewName.GetAuthorizationViewClassName()}".GetFullNameForView());

            object value;

            // Defensive check to ensure required parameter is present
            if (!parameters.TryGetValue(filterPropertyName, out value))
            {
                throw new Exception($"Unable to find parameter for filtering '{filterPropertyName}' on view '{viewName}'.");
            }

            var arrayOfValues = value as object[];

            if (arrayOfValues != null)
            {
                if (joinType == JoinType.InnerJoin)
                {
                    whereJunction.Add(Restrictions.In($"{authViewAlias}.{filterPropertyName}", arrayOfValues));
                }
                else
                {
                    var and = new AndExpression(
                        Restrictions.In($"{authViewAlias}.{filterPropertyName}", arrayOfValues),
                        Restrictions.IsNotNull($"{authViewAlias}.{joinPropertyName}"));

                    whereJunction.Add(and);
                }
            }
            else
            {
                if (joinType == JoinType.InnerJoin)
                {
                    whereJunction.Add(Restrictions.Eq($"{authViewAlias}.{filterPropertyName}", value));
                }
                else
                {
                    var and = new AndExpression(
                        Restrictions.Eq($"{authViewAlias}.{filterPropertyName}", value),
                        Restrictions.IsNotNull($"{authViewAlias}.{joinPropertyName}"));

                    whereJunction.Add(and);
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Applies a join-based filter to the criteria for the specified authorization view.
        /// </summary>
        /// <param name="criteria">The criteria to which filters should be applied.</param>
        /// <param name="whereJunction">The <see cref="ICriterion" /> container for adding WHERE clause criterion.</param>
        /// <param name="parameters">The named parameters to be used to satisfy additional filtering requirements.</param>
        /// <param name="viewName">The name of the view to be filtered.</param>
        /// <param name="subjectEndpointName">The name of the property to be joined for the entity being queried .</param>
        /// <param name="viewTargetEndpointName">The name of the property to be joined for the other property as authorization view.</param>
        /// <param name="joinType">The <see cref="JoinType" /> to be used.</param>
        /// <param name="authViewAlias">The name of the property to be used for auth View Alias name.</param>
        public static void ApplyJoinFilter(
            this ICriteria criteria,
            Junction whereJunction,
            IDictionary <string, object> parameters,
            string viewName,
            string subjectEndpointName,
            string viewTargetEndpointName,
            JoinType joinType,
            string authViewAlias = null)
        {
            authViewAlias = string.IsNullOrWhiteSpace(authViewAlias) ? $"authView{viewName}" : $"authView{authViewAlias}";

            // Apply authorization join using ICriteria
            criteria.CreateEntityAlias(
                authViewAlias,
                Restrictions.EqProperty($"aggregateRoot.{subjectEndpointName}",
                                        $"{authViewAlias}.{viewTargetEndpointName}"),
                joinType,
                $"{viewName.GetAuthorizationViewClassName()}".GetFullNameForView());

            // Defensive check to ensure required parameter is present
            if (!parameters.TryGetValue(RelationshipAuthorizationConventions.ClaimsParameterName, out object value))
            {
                throw new Exception($"Unable to find parameter for filtering '{RelationshipAuthorizationConventions.ClaimsParameterName}' on view '{viewName}'. Available parameters: '{string.Join("', '", parameters.Keys)}'");
            }

            if (value is object[] arrayOfValues)
            {
                if (joinType == JoinType.InnerJoin)
                {
                    whereJunction.Add(Restrictions.In($"{authViewAlias}.{RelationshipAuthorizationConventions.ViewSourceColumnName}", arrayOfValues));
                }
                else
                {
                    var and = new AndExpression(
                        Restrictions.In($"{authViewAlias}.{RelationshipAuthorizationConventions.ViewSourceColumnName}", arrayOfValues),
                        Restrictions.IsNotNull($"{authViewAlias}.{viewTargetEndpointName}"));

                    whereJunction.Add(and);
                }
            }
            else
            {
                if (joinType == JoinType.InnerJoin)
                {
                    whereJunction.Add(Restrictions.Eq($"{authViewAlias}.{RelationshipAuthorizationConventions.ViewSourceColumnName}", value));
                }
                else
                {
                    var and = new AndExpression(
                        Restrictions.Eq($"{authViewAlias}.{RelationshipAuthorizationConventions.ViewSourceColumnName}", value),
                        Restrictions.IsNotNull($"{authViewAlias}.{viewTargetEndpointName}"));

                    whereJunction.Add(and);
                }
            }
        }