コード例 #1
0
        /// <summary>
        /// Creates a structured query expression.
        /// </summary>
        /// <remarks>
        /// This method does not add the new expression to the target structured query, however it will add new tree nodes to the structured query as they are requred.
        /// </remarks>
        public ScalarExpression CreateQueryEngineExpression(IExpression expression, QueryBuilderSettings settings)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            Expression expressionToCreate = expression as Expression;

            if (expressionToCreate == null)
            {
                throw new ArgumentException("Expected instance of type Expression.", "expression");
            }

            using (EntryPointContext.AppendEntryPoint("ExprQuery"))
                using (new SecurityBypassContext( ))
                    using (Profiler.Measure("ExpressionEngine.CreateQueryEngineExpression"))
                    {
                        if (settings == null)
                        {
                            settings = new QueryBuilderSettings();
                        }

                        var queryContext = new QueryBuilderContext
                        {
                            Settings = settings
                        };

                        // Add nodes to the query tree, if necessary.
                        expressionToCreate.ListRoot.BuildQueryNode(queryContext, true);

                        // Build up the expression itself
                        ScalarExpression result = expressionToCreate.Root.BuildQuery(queryContext);

                        // Special handing for bools
                        //if (expressionToCreate.ResultType.Type == DataType.Bool)
                        //{
                        //    if (settings.ConvertBoolsToString)
                        //        result = CastFromBool.CastToString(result);
                        //}

                        // Set result type
                        // TODO: return entity type
                        // TODO: return for any expression type
                        var calcExpression = result as CalculationExpression;
                        if (calcExpression != null)
                        {
                            calcExpression.DisplayType = ExprTypeHelper.ToDatabaseType(expressionToCreate.ResultType);
                        }

                        return(result);
                    }
        }
コード例 #2
0
        /// <summary>
        /// Convert a string into an expression tree and perform static evaluation.
        /// </summary>
        /// <param name="script">The script to parse.</param>
        /// <param name="settings">Additional settings that control parsing. In particular, contains the type of the root context object, if any.</param>
        /// <returns>An expression tree.</returns>
        public IExpression Compile(string script, BuilderSettings settings)
        {
            using (EntryPointContext.AppendEntryPoint("ExprCompile"))
                using (new SecurityBypassContext( ))
                    using (Profiler.Measure("ExpressionEngine.Compile"))
                    {
                        // Parse the expression
                        ParseTree     parseTree = ExpressionGrammar.ParseMacro(script);
                        ParseTreeNode parseRoot = parseTree.Root;

                        // Compile the parse tree
                        IExpression result = CompileParseTreeNode(parseRoot, settings);
                        return(result);
                    }
        }
コード例 #3
0
        /// <summary>
        /// Log the access control check before passing it to <see cref="Checker"/>.
        /// </summary>
        /// <param name="entities">
        ///     The entities to check. This cannot be null or contain null.
        /// </param>
        /// <param name="permissions">
        ///     The permissions or operations to check. This cannot be null or contain null.
        /// </param>
        /// <param name="user">
        ///     The user requesting access. This cannot be null.
        /// </param>
        /// <returns>
        /// A mapping of each entity ID to whether the user has access (true) or not (false).
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// No argument can be null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Neither <paramref name="entities"/> nor <paramref name="permissions"/> can contain null.
        /// </exception>
        public IDictionary <long, bool> CheckAccess(IList <EntityRef> entities, IList <EntityRef> permissions, EntityRef user)
        {
            if (entities == null)
            {
                throw new ArgumentNullException("entities");
            }
            if (permissions == null)
            {
                throw new ArgumentNullException("permissions");
            }
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            IDictionary <long, bool> result;
            Stopwatch stopwatch = null;

            using (EntryPointContext.AppendEntryPoint("AccessControl"))
            {
                try
                {
                    stopwatch = Stopwatch.StartNew( );
                    result    = Checker.CheckAccess(entities, permissions, user);
                }
                finally
                {
                    if (stopwatch != null)
                    {
                        stopwatch.Stop( );
                    }
                }
            }

            if (!EntityAccessControlChecker.SkipCheck(user))
            {
                Dictionary <long, bool> cacheResult = null;
                var cachingResult = result as ICachingEntityAccessControlCheckerResult;
                if (cachingResult != null)
                {
                    cacheResult = cachingResult.CacheResult;
                }

                Trace.TraceCheckAccess(result, permissions, user, cacheResult, stopwatch.ElapsedMilliseconds);
            }

            return(result);
        }
コード例 #4
0
        /// <summary>
        /// Wrap an action so it can be run in a separate thread.
        /// </summary>
        protected Action WrapActionForThread(Action act)
        {
            var originalRunContext = WorkflowRunContext.Current;

            return(() =>
            {
                using (EntryPointContext.AppendEntryPoint("WorkflowTrigger"))
                    using (new WorkflowRunContext {
                        TriggerDepth = originalRunContext.TriggerDepth, RunTriggersInCurrentThread = true
                    })                                                                                                               // Note that the context is coppied across from the original thread in the WorkflowRunContext DefaultQueueTask
                    {
                        EventLog.Application.WriteTrace("Starting workflow thread at depth {0}", originalRunContext.TriggerDepth);

                        act();
                    }
            });
        }
コード例 #5
0
        /// <summary>
        /// Is there an access rule for the specified type(s) that includes the requested permission? E.g. create.
        /// </summary>
        /// <param name="entityTypes">The <see cref="EntityType"/>s to check. This cannot be null or contain null.</param>
        /// <param name="permission">The permission being sought.</param>
        /// <param name="user"> The user requesting access. This cannot be null. </param>
        /// <returns>
        /// A mapping the entity type ID to whether the user can create instances of that
        /// type (true) or not (false).
        /// </returns>
        public IDictionary <long, bool> CheckTypeAccess(IList <EntityType> entityTypes, EntityRef permission, EntityRef user)
        {
            if (entityTypes == null)
            {
                throw new ArgumentNullException(nameof(entityTypes));
            }
            if (permission == null)
            {
                throw new ArgumentNullException(nameof(permission));
            }
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            IDictionary <long, bool> result;
            Stopwatch stopwatch = null;

            using (EntryPointContext.AppendEntryPoint("AccessControl"))
            {
                try
                {
                    stopwatch = Stopwatch.StartNew( );
                    result    = Checker.CheckTypeAccess(entityTypes, permission, user);
                }
                finally
                {
                    stopwatch?.Stop( );
                }
            }

            if (!EntityAccessControlChecker.SkipCheck(user))
            {
                Trace.TraceCheckTypeAccess(
                    result,
                    permission,
                    user,
                    entityTypes.Select(et => new EntityRef(et)).ToList(),
                    stopwatch.ElapsedMilliseconds);
            }

            return(result);
        }
コード例 #6
0
        /// <summary>
        /// Evaluates an expression tree.
        /// </summary>
        /// <param name="expression">The expression tree.</param>
        /// <param name="settings">Additional settings to be used in evaluation, such as the root context object, timezone info, etc.</param>
        /// <returns>The result of the evaluation.</returns>
        public ExpressionRunResult Run(IExpression expression, EvaluationSettings settings)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            Expression expressionToRun = expression as Expression;

            if (expressionToRun == null)
            {
                throw new ArgumentException("Expected instance of type Expression.", "expression");
            }
            if (expressionToRun.Root == null)
            {
                throw new ArgumentException("expression.Root");
            }

            using (EntryPointContext.AppendEntryPoint("ExprRun"))
                using (Profiler.Measure("ExpressionEngine.Run"))
                {
                    var evalContext = new EvaluationContext
                    {
                        Settings = settings,
                    };

                    object result = expressionToRun.Evaluate(evalContext);

                    var resultList = result as IEnumerable <IEntity>;
                    if (resultList != null)
                    {
                        result = resultList.ToList( );
                    }

                    ExpressionRunResult runResult = new ExpressionRunResult(result);
                    return(runResult);
                }
        }