Пример #1
0
        public List <TypeAccessReason> Get(string subjectAliasOrId, [FromUri] string advanced = null)
        {
            EntityRef subjectRef = WebApiHelpers.GetId(subjectAliasOrId);

            // Demand read access to the admin role, as a proxy for determining who can use this service
            EntityRepository.Get("core:administratorRole");

            // Ensure that the requested ID is a role or user.
            Subject subject = EntityRepository.Get <Subject>(subjectRef.Id);

            if (subject == null)
            {
                throw new WebArgumentNotFoundException("subject");
            }

            using (new SecurityBypassContext( ))
            {
                var settings = new TypeAccessReasonSettings(advanced == "true");

                // Get list of reasons
                IReadOnlyList <AccessReason> reasons = TypeAccessReasonService.GetTypeAccessReasons(subjectRef.Id, settings);

                // Format result
                List <TypeAccessReason> result = new List <TypeAccessReason>( );
                foreach (AccessReason reason in reasons)
                {
                    TypeAccessReason formattedReason = FormatAccessReason(reason);
                    result.Add(formattedReason);
                }

                return(result);
            }
        }
Пример #2
0
        /// <summary>
        ///     Related entities.
        /// </summary>
        /// <param name="related">The related.</param>
        /// <param name="nodeIds">The node ids.</param>
        /// <returns></returns>
        private IEnumerable <RelatedResource> RelatedEntities(IEnumerable <JsonRelatedEntityInQuery> related, Dictionary <string, Guid> nodeIds)
        {
            var relatedEntities = new List <RelatedResource>( );

            if (related != null)
            {
                foreach (JsonRelatedEntityInQuery r in related)
                {
                    string asId = r.Related.As ?? "";

                    if (nodeIds.ContainsKey(asId))
                    {
                        continue;
                    }

                    nodeIds.Add(asId, Guid.NewGuid( ));

                    var re = new RelatedResource
                    {
                        NodeId                = nodeIds[asId],
                        RelationshipTypeId    = WebApiHelpers.GetId(r.Related.Id),
                        RelationshipDirection = r.Forward ? RelationshipDirection.Forward : RelationshipDirection.Reverse,
                        ResourceMustExist     = r.MustExist
                    };
                    re.RelatedEntities.AddRange(RelatedEntities(r.Related.Related, nodeIds));
                    relatedEntities.Add(re);
                }
            }

            return(relatedEntities);
        }
Пример #3
0
        /// <summary>
        ///     Inners the compile.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        private static CompileResult InnerCompile(EvalRequest request)
        {
            try
            {
                var settings = new BuilderSettings( );

                if (!string.IsNullOrEmpty(request.Context))
                {
                    EntityRef contextType = WebApiHelpers.GetId(request.Context);
                    settings.RootContextType = ExprTypeHelper.EntityOfType(contextType);
                }
                if (!string.IsNullOrEmpty(request.Host))
                {
                    settings.ScriptHost = ( ScriptHostType )Enum.Parse(typeof(ScriptHostType), request.Host);
                }

                if (request.ExpectedResultType != null)
                {
                    settings.ExpectedResultType = request.ExpectedResultType.ToExprType();
                }

                var paramTypes = new Dictionary <string, ExprType>( );

                if (request.Parameters != null)
                {
                    foreach (ExpressionParameter p in request.Parameters)
                    {
                        if (p.Name == null)
                        {
                            throw new WebArgumentException("Param 'name' was not specified");
                        }
                        if (p.TypeName == null)
                        {
                            throw new WebArgumentException("Param 'type' was not specified");
                        }

                        var type = ( DataType )Enum.Parse(typeof(DataType), p.TypeName);
                        if (type == DataType.Entity)
                        {
                            if (p.EntityTypeId == null)
                            {
                                throw new WebArgumentException("Param 'entityTypeId' was not specified");
                            }

                            long typeId;
                            paramTypes[p.Name] = long.TryParse(p.EntityTypeId, out typeId)
                                                                ? ExprTypeHelper.EntityOfType(new EntityRef(typeId))
                                                                : ExprTypeHelper.EntityOfType(new EntityRef(p.EntityTypeId));
                        }
                        else
                        {
                            paramTypes[p.Name] = new ExprType(type);
                        }
                        paramTypes[p.Name].IsList = p.IsList;
                    }
                    settings.ParameterNames          = paramTypes.Keys.ToList( );
                    settings.StaticParameterResolver = paramName =>
                    {
                        ExprType result2;
                        return(paramTypes.TryGetValue(paramName, out result2) ? result2 : null);
                    };
                }

                // Compile
                IExpression expression = Factory.ExpressionCompiler.Compile(request.Expression, settings);

                // Return success result
                return(new CompileResult
                {
                    ResultType = expression.ResultType.Type,
                    IsList = expression.ResultType.IsList,
                    EntityTypeId = expression.ResultType.EntityTypeId
                });
            }
            catch (ParseException ex)
            {
                // Return script error result
                return(new CompileResult
                {
                    ErrorMessage = ex.Message
                });
            }
        }
Пример #4
0
        public HttpResponseMessage <ReportDataDefinition> Query([FromBody] JsonStructuredQuery jsonQuery)
        {
            if (jsonQuery == null)
            {
                throw new WebArgumentNullException("jsonQuery");
            }
            if (jsonQuery.Root == null)
            {
                throw new WebArgumentNullException("jsonQuery.Root");
            }

            try
            {
                var    query   = new StructuredQuery( );
                var    nodeIds = new Dictionary <string, Guid>( );
                string rootAs  = jsonQuery.Root.As ?? "";

                nodeIds.Add(rootAs, Guid.NewGuid( ));
                query.RootEntity = new ResourceEntity
                {
                    EntityTypeId = WebApiHelpers.GetId(jsonQuery.Root.Id),
                    NodeId       = nodeIds[rootAs]
                };

                query.RootEntity.RelatedEntities.AddRange(RelatedEntities(jsonQuery.Root.Related, nodeIds));

                query.SelectColumns.Add(new SelectColumn
                {
                    ColumnId   = Guid.NewGuid( ),
                    ColumnName = "_Id",
                    IsHidden   = true,
                    Expression = new IdExpression
                    {
                        NodeId = query.RootEntity.NodeId
                    }
                });
                if (jsonQuery.Selects != null)
                {
                    foreach (JsonSelectInQuery f in jsonQuery.Selects)
                    {
                        if (!string.IsNullOrEmpty(f.Field) && f.Field.ToLower( ) == "_id" && f.On != null)
                        {
                            if (f.On != null)
                            {
                                query.SelectColumns.Add(new SelectColumn
                                {
                                    ColumnId   = Guid.NewGuid( ),
                                    ColumnName = f.DisplayAs ?? f.On + "_Id",
                                    IsHidden   = true,
                                    Expression = new IdExpression
                                    {
                                        NodeId = nodeIds[f.On]
                                    }
                                });
                            }
                        }
                        else
                        {
                            var field = new ResourceDataColumn
                            {
                                FieldId = WebApiHelpers.GetId(f.Field),
                                NodeId  = nodeIds[f.On ?? rootAs]
                            };
                            string name = field.FieldId.Entity.Cast <Resource>( ).Name;
                            var    sc   = new SelectColumn
                            {
                                ColumnId    = Guid.NewGuid( ),
                                DisplayName = f.DisplayAs ?? name,
                                ColumnName  = name,
                                Expression  = field
                            };
                            query.SelectColumns.Add(sc);
                        }
                    }
                }
                if (jsonQuery.Conds != null)
                {
                    foreach (JsonCondition c in jsonQuery.Conds)
                    {
                        var qc = new QueryCondition
                        {
                            Operator = GetConditionOperator(c.Operation),
                            Argument = new TypedValue(c.Value)
                        };
                        if (!string.IsNullOrEmpty(c.Expression.Field) && c.Expression.Field.ToLower( ) == "_id")
                        {
                            qc.Expression = new IdExpression
                            {
                                NodeId = nodeIds[c.Expression.On ?? rootAs]
                            };
                        }
                        else
                        {
                            qc.Expression = new ResourceDataColumn
                            {
                                FieldId = WebApiHelpers.GetId(c.Expression.Field),
                                NodeId  = nodeIds[c.Expression.On ?? rootAs]
                            };
                        }
                        query.Conditions.Add(qc);
                    }
                }

                QueryResult result = Factory.QueryRunner.ExecuteQuery(query, new QuerySettings
                {
                    SecureQuery = true
                });

                // debug
                LogResult(result);

                return(new HttpResponseMessage <ReportDataDefinition>(PackReportResponse(result)));
            }
            catch (HttpResponseException)
            {
                throw;
            }
            catch (PlatformSecurityException)
            {
                throw;
            }
            catch (Exception e)
            {
                if (e is ArgumentException)                   // would be better if there was a more specific exception for 'not found'
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }

                throw;
            }
        }