示例#1
0
        public static IEndpointConventionBuilder UseFilteredAutoForeignKeys(this IEndpointRouteBuilder builder, string explicitTenantParam, bool forAreas, bool withAuthorization = true)
        {
            bool forExplicitTenants = !string.IsNullOrEmpty(explicitTenantParam);

            ContextExtensions.Init();
            RequestDelegate dlg = async context =>
            {
                //{{connection:regex(^[\\w_]+$)}}/{{table:regex(^[\\w_]+$)}}
                RouteData        routeData        = context.GetRouteData();
                ActionDescriptor actionDescriptor = new ActionDescriptor();
                ActionContext    actionContext    = new ActionContext(context, routeData, actionDescriptor);
                var ok = !withAuthorization || context.RequestServices.VerifyCurrentUser();
                if (ok)
                {
                    if (context.Request.RouteValues.ContainsKey("dataResolveHint"))
                    {
                        var baseHint = ((string)context.Request.RouteValues["dataResolveHint"])?.Split("/")
                                       .Select(n => HttpUtility.UrlDecode(n)).ToArray();
                        if (baseHint is { Length : 2 })
                        {
                            string area = null;
                            if (context.Request.RouteValues.ContainsKey("area"))
                            {
                                area = (string)context.Request.RouteValues["area"];
                            }

                            var connection =
                                RegexValidate(baseHint[0], "^[\\w_]+$")
                                    ? baseHint[0]
                                    : null; //(string) context.Request.RouteValues["connection"];
                            var dbContext = context.RequestServices.ContextForFkQuery(connection, area);
                            if (dbContext != null)
                            {
                                var table = RegexValidate(baseHint[1], dbContext.CustomFkSettings?.CustomTableValidation ?? "^[\\w_]+$")
                                    ? baseHint[1]
                                    : null; //(string) context.Request.RouteValues["table"];

                                FormReader former          = new FormReader(context.Request.Body);
                                var        formsDictionary = await former.ReadFormAsync();

                                //LogEnvironment.LogEvent(Stringify(formsDictionary), LogSeverity.Report);
                                var        newDic     = TranslateForm(formsDictionary, true);
                                JsonResult result     = null;
                                bool       authorized = true;
                                try
                                {
                                    result = new JsonResult(dbContext.ReadForeignKey(table, postedFilter: newDic)
                                                            .ToDummyDataSourceResult());
                                }
                                catch (SecurityException)
                                {
                                    authorized = false;
                                }

                                if (authorized)
                                {
                                    await result.ExecuteResultAsync(actionContext);

                                    return;
                                }

                                UnauthorizedResult ill = new UnauthorizedResult();
                                await ill.ExecuteResultAsync(actionContext);

                                return;
                            }
                        }
                    }