Ejemplo n.º 1
0
        public bool VertifyRule(TRule rule, out IList <Exception> exceptions)
        {
            string cacheKey = GetCacheKey(rule);

            if (_cache.ContainsKey(cacheKey))
            {
                exceptions = null;
                return(true);
            }
            var visitResult = DataPrivilegeVisitor.Visit(rule.ConditionExpression);

            exceptions = visitResult.Exceptions;
            if (visitResult.Success)
            {
                ReplacementVisitor visitor = new ReplacementVisitor(Expression.Constant(this), visitResult.PredicateExpression.Parameters[0], _parameterExpression);
                var newExpression          = visitor.Visit(visitResult.PredicateExpression);
                if (newExpression.CanReduce)
                {
                    newExpression = newExpression.Reduce();
                }
                DataPrivilegeInfo <TEntity> result = new DataPrivilegeInfo <TEntity>(newExpression as Expression <Func <TEntity, bool> >, visitResult.CustomFields);

                _cache.TryAdd(cacheKey, result);
            }
            return(visitResult.Success);
        }
Ejemplo n.º 2
0
        private DataPrivilegeInfo <TEntity> GetDataPrevilegeInfoByRule(TRule rule)
        {
            string cacheKey = GetCacheKey(rule);

            return(_cache.GetOrAdd(cacheKey, _ =>
            {
                var visitResult = DataPrivilegeVisitor.Visit(rule.ConditionExpression);
                if (!visitResult.Success)
                {
                    throw new AggregateException(visitResult.Exceptions);
                }
                ReplacementVisitor visitor = new ReplacementVisitor(Expression.Constant(this), visitResult.PredicateExpression.Parameters[0], _parameterExpression);
                var newExpression = visitor.Visit(visitResult.PredicateExpression);
                DataPrivilegeInfo <TEntity> result = new DataPrivilegeInfo <TEntity>(newExpression as Expression <Func <TEntity, bool> >, visitResult.CustomFields);
                return result;
            }));
        }
Ejemplo n.º 3
0
        private DataPrivilegeInfo <TEntity> GetDataPrevilegeInfo()
        {
            if (Rules.Count == 1)
            {
                return(GetDataPrevilegeInfoByRule(Rules[0]));
            }
            string key = GetCacheKey(Rules);

            return(_cache.GetOrAdd(key, _ =>
            {
                List <DataPrivilegeInfo <TEntity> > list = new List <DataPrivilegeInfo <TEntity> >(Rules.Count);
                foreach (TRule rule in Rules)
                {
                    list.Add(GetDataPrevilegeInfoByRule(rule));
                }
                Expression expression = null;
                List <string> customFields = new List <string>();
                foreach (var item in list)
                {
                    if (expression == null)
                    {
                        expression = item.PredicateExpression.Body;
                    }
                    else
                    {
                        expression = Expression.OrElse(expression, item.PredicateExpression.Body);
                    }
                    foreach (string field in item.CustomFields)
                    {
                        if (!customFields.Contains(field))
                        {
                            customFields.Add(field);
                        }
                    }
                }
                var predicate = Expression.Lambda <Func <TEntity, bool> >(expression, _parameterExpression);
                DataPrivilegeInfo <TEntity> dataPrevilegeInfo = new DataPrivilegeInfo <TEntity>(predicate, customFields);

                return dataPrevilegeInfo;
            }));
        }