public async Task <object> Any(CheckCrudEvents request) { var appHost = HostContext.AppHost; var feature = appHost.AssertPlugin <AutoQueryFeature>(); RequestUtils.AssertAccessRole(base.Request, accessRole: feature.AccessRole, authSecret: request.AuthSecret); if (string.IsNullOrEmpty(request.Model)) { throw new ArgumentNullException(nameof(request.Model)); } var ids = request.Ids?.Count > 0 ? request.Ids : throw new ArgumentNullException(nameof(request.Ids)); var dto = appHost.Metadata.FindDtoType(request.Model); var namedConnection = dto?.FirstAttribute <NamedConnectionAttribute>()?.Name; using var useDb = namedConnection != null ? await DbFactory.OpenDbConnectionAsync(namedConnection) : await DbFactory.OpenDbConnectionAsync(); var q = useDb.From <CrudEvent>() .Where(x => x.Model == request.Model) .And(x => ids.Contains(x.ModelId)) .SelectDistinct(x => x.ModelId); var results = await useDb.ColumnAsync <string>(q); return(new CheckCrudEventsResponse { Results = results.ToList(), }); }
public async Task <object> Any(GetValidationRules request) { var feature = HostContext.AssertPlugin <ValidationFeature>(); RequestUtils.AssertAccessRole(base.Request, accessRole: feature.AccessRole, authSecret: request.AuthSecret); var type = HostContext.Metadata.FindDtoType(request.Type); if (type == null) { throw HttpError.NotFound(request.Type); } return(new GetValidationRulesResponse { Results = await ValidationSource.GetAllValidateRulesAsync(request.Type).ConfigAwait(), }); }
public async Task <object> Any(GetCrudEvents request) { var appHost = HostContext.AppHost; var feature = appHost.AssertPlugin <AutoQueryFeature>(); RequestUtils.AssertAccessRole(base.Request, accessRole: feature.AccessRole, authSecret: request.AuthSecret); if (string.IsNullOrEmpty(request.Model)) { throw new ArgumentNullException(nameof(request.Model)); } var dto = appHost.Metadata.FindDtoType(request.Model); var namedConnection = dto?.FirstAttribute <NamedConnectionAttribute>()?.Name; using var useDb = namedConnection != null ? await DbFactory.OpenDbConnectionAsync(namedConnection) : await DbFactory.OpenDbConnectionAsync(); var q = AutoQuery.CreateQuery(request, Request, useDb); var response = await AutoQuery.ExecuteAsync(request, q, Request, useDb); return(response); }
public async Task Any(EvalScript request) { var feature = HostContext.AssertPlugin <DesktopFeature>(); RequestUtils.AssertAccessRole(base.Request, accessRole: feature.AccessRole, authSecret: request.AuthSecret); var appHost = HostContext.AppHost; string script; var method = ((script = request.EvaluateScript) != null ? nameof(request.EvaluateScript) : (script = request.EvaluateCode) != null ? nameof(request.EvaluateCode) : (script = request.EvaluateLisp) != null ? nameof(request.EvaluateLisp) : (script = request.RenderScript) != null ? nameof(request.RenderScript) : (script = request.RenderCode) != null ? nameof(request.RenderCode) : (script = request.RenderLisp) != null ? nameof(request.RenderLisp) : null) ?? ((script = request.EvaluateScriptAsync) != null ? nameof(request.EvaluateScriptAsync) : (script = request.EvaluateCodeAsync) != null ? nameof(request.EvaluateCodeAsync) : (script = request.EvaluateLispAsync) != null ? nameof(request.EvaluateLispAsync) : (script = request.RenderScriptAsync) != null ? nameof(request.RenderScriptAsync) : (script = request.RenderCodeAsync) != null ? nameof(request.RenderCodeAsync) : (script = request.RenderLispAsync) != null ? nameof(request.RenderLispAsync) : null) ?? throw new ArgumentNullException(nameof(request.EvaluateCode)); async Task HandleExceptionAsync(Exception e) { log.Error(e.Message, e); base.Response.StatusCode = 500; base.Response.StatusDescription = e.GetType().Name; base.Response.ContentType = MimeTypes.PlainText; await base.Response.OutputStream.WriteAsync(MemoryProvider.Instance.ToUtf8(e.ToString().AsSpan())); await base.Response.EndRequestAsync(skipHeaders : true); } async Task SetResult(object value, string resultType = " result") { base.Response.ContentType = MimeTypes.Json; base.Response.StatusCode = 200; base.Response.StatusDescription = method + resultType; await base.Response.EndRequestAsync(skipHeaders : false, async res => { using var ms = MemoryStreamFactory.GetStream(); JsonSerializer.SerializeToStream(value, ms); ms.Position = 0; await ms.CopyToAsync(base.Response.OutputStream); }); } async Task SetOutput(PageResult result) { base.Response.StatusCode = 200; base.Response.StatusDescription = method + " result"; base.Response.ContentType = MimeTypes.PlainText; await base.Response.EndRequestAsync(skipHeaders : false, async res => { using var ms = MemoryStreamFactory.GetStream(); await result.RenderToStreamAsync(ms); ms.Position = 0; await ms.CopyToAsync(res.OutputStream); }); } var args = new Dictionary <string, object> { [ScriptConstants.Request] = base.Request, }; if (method.EqualsIgnoreCase(nameof(ScriptTemplateUtils.EvaluateScript))) { await SetResult(await appHost.ScriptContext.EvaluateAsync(script, args)); } else if (method.EqualsIgnoreCase(nameof(ScriptTemplateUtils.RenderScript))) { await SetOutput(new PageResult(appHost.ScriptContext.SharpScriptPage(script)).AssignArgs(args)); } else if (method.EqualsIgnoreCase(nameof(ScriptCodeUtils.EvaluateCode))) { await SetResult(await appHost.ScriptContext.EvaluateCodeAsync(ScriptCodeUtils.EnsureReturn(script), args)); } else if (method.EqualsIgnoreCase(nameof(ScriptCodeUtils.RenderCode))) { await SetOutput(new PageResult(appHost.ScriptContext.CodeSharpPage(script)).AssignArgs(args)); } else if (method.EqualsIgnoreCase(nameof(ScriptLispUtils.EvaluateLisp))) { await SetResult(await appHost.ScriptContext.EvaluateLispAsync(ScriptLispUtils.EnsureReturn(script), args)); } else if (method.EqualsIgnoreCase(nameof(ScriptLispUtils.RenderLisp))) { await SetOutput(new PageResult(appHost.ScriptContext.LispSharpPage(script)).AssignArgs(args)); } if (base.Response.IsClosed) { return; } async Task setResultAsync(Task <object> valueTask, string resultType = " result") { try { base.Response.ContentType = MimeTypes.Json; base.Response.StatusCode = 200; base.Response.StatusDescription = method + resultType; await base.Response.EndRequestAsync(skipHeaders : false, async res => { using var ms = MemoryStreamFactory.GetStream(); JsonSerializer.SerializeToStream(await valueTask, ms); await ms.CopyToAsync(base.Response.OutputStream); }); } catch (Exception e) { await HandleExceptionAsync(e); } } async Task setOutputAsync(PageResult result) { try { base.Response.StatusCode = 200; base.Response.StatusDescription = method + " async result"; base.Response.ContentType = MimeTypes.PlainText; await base.Response.EndRequestAsync(skipHeaders : false, async res => { using var ms = MemoryStreamFactory.GetStream(); await result.RenderToStreamAsync(ms); await ms.CopyToAsync(res.OutputStream); }); } catch (Exception e) { await HandleExceptionAsync(e); } } if (method.EqualsIgnoreCase(nameof(ScriptTemplateUtils.EvaluateScriptAsync))) { await Task.Run(async() => await setResultAsync(appHost.ScriptContext.EvaluateAsync(script, args), " async result")); } else if (method.EqualsIgnoreCase(nameof(ScriptTemplateUtils.RenderScriptAsync))) { await Task.Run(async() => await setOutputAsync(new PageResult(appHost.ScriptContext.SharpScriptPage(script)).AssignArgs(args))); } else if (method.EqualsIgnoreCase(nameof(ScriptCodeUtils.EvaluateCodeAsync))) { await Task.Run(async() => await setResultAsync(appHost.ScriptContext.EvaluateCodeAsync(ScriptCodeUtils.EnsureReturn(script), args), " async result")); } else if (method.EqualsIgnoreCase(nameof(ScriptCodeUtils.RenderCodeAsync))) { await Task.Run(async() => await setOutputAsync(new PageResult(appHost.ScriptContext.CodeSharpPage(script)).AssignArgs(args))); } else if (method.EqualsIgnoreCase(nameof(ScriptLispUtils.EvaluateLispAsync))) { await Task.Run(async() => await setResultAsync(appHost.ScriptContext.EvaluateLispAsync(ScriptLispUtils.EnsureReturn(script), args), " async result")); } else if (method.EqualsIgnoreCase(nameof(ScriptLispUtils.RenderLispAsync))) { await Task.Run(async() => await setOutputAsync(new PageResult(appHost.ScriptContext.LispSharpPage(script)).AssignArgs(args))); } else { throw new NotSupportedException($"Unsupported script API '{method}', supported: " + "EvaluateScript/Async, EvaluateCode/Async, EvaluateLisp/Async"); } }
public async Task Any(ModifyValidationRules request) { var appHost = HostContext.AssertAppHost(); var feature = appHost.AssertPlugin <ValidationFeature>(); RequestUtils.AssertAccessRole(base.Request, accessRole: feature.AccessRole, authSecret: request.AuthSecret); var utcNow = DateTime.UtcNow; var userName = base.GetSession().GetUserAuthName(); var rules = request.SaveRules; if (!rules.IsEmpty()) { foreach (var rule in rules) { if (rule.Type == null) { throw new ArgumentNullException(nameof(rule.Type)); } var existingType = appHost.Metadata.FindDtoType(rule.Type); if (existingType == null) { throw new ArgumentException(@$ "{rule.Type} does not exist", nameof(rule.Type)); } if (rule.Validator == "") { rule.Validator = null; } if (rule.Condition == "") { rule.Condition = null; } if (rule.Field == "") { rule.Field = null; } if (rule.ErrorCode == "") { rule.ErrorCode = null; } if (rule.Message == "") { rule.Message = null; } if (rule.Notes == "") { rule.Notes = null; } if (rule.Field != null && TypeProperties.Get(existingType).GetAccessor(rule.Field) == null) { throw new ArgumentException(@$ "{rule.Field} does not exist on {rule.Type}", nameof(rule.Field)); } if (rule.Validator != null) { object validator; try { validator = appHost.EvalExpression(rule.Validator); if (validator == null) { throw new ArgumentException(@$ "Validator does not exist", nameof(rule.Validator)); } } catch (Exception e) { throw new ArgumentException(@$ "Invalid Validator: " + e.Message, nameof(rule.Validator)); } var validators = (validator as List <object>) ?? TypeConstants.EmptyObjectList; var firstValidator = validator is IPropertyValidator pv ? pv : validator is ITypeValidator tv ? tv : validators?.FirstOrDefault() ?? validator; if (rule.Field != null && !(firstValidator is IPropertyValidator && validators.All(v => v is IPropertyValidator))) { throw new ArgumentException(@$ "{nameof(IPropertyValidator)} is expected but was {(validators?.FirstOrDefault(v => !(v is IPropertyValidator)) ?? firstValidator).GetType().Name}", nameof(rule.Validator)); } if (rule.Field == null && !(firstValidator is ITypeValidator && validators.All(v => v is ITypeValidator))) { throw new ArgumentException(@$ "{nameof(ITypeValidator)} is expected but was {(validators?.FirstOrDefault(v => !(v is IPropertyValidator)) ?? firstValidator).GetType().Name}", nameof(rule.Validator)); } if (rule.Condition != null) { throw new ArgumentException(@$ "Only {nameof(rule.Validator)} or {nameof(rule.Condition)} can be specified, not both", nameof(rule.Condition)); } } else { if (rule.Condition == null) { throw new ArgumentNullException(nameof(rule.Validator), @$ "{nameof(rule.Validator)} or {nameof(rule.Condition)} is required"); } try { var ast = Validators.ParseCondition(appHost.ScriptContext, rule.Condition); await ast.Init().ConfigAwait(); } catch (Exception e) { var useEx = e is ScriptException se ? se.InnerException ?? e : e; throw new ArgumentException(useEx.Message, nameof(rule.Condition)); } } if (rule.CreatedBy == null) { rule.CreatedBy = userName; rule.CreatedDate = utcNow; } rule.ModifiedBy = userName; rule.ModifiedDate = utcNow; } await ValidationSource.SaveValidationRulesAsync(rules).ConfigAwait(); } if (!request.SuspendRuleIds.IsEmpty()) { var suspendRules = await ValidationSource.GetValidateRulesByIdsAsync(request.SuspendRuleIds).ConfigAwait(); foreach (var suspendRule in suspendRules) { suspendRule.SuspendedBy = userName; suspendRule.SuspendedDate = utcNow; } await ValidationSource.SaveValidationRulesAsync(suspendRules).ConfigAwait(); } if (!request.UnsuspendRuleIds.IsEmpty()) { var unsuspendRules = await ValidationSource.GetValidateRulesByIdsAsync(request.UnsuspendRuleIds).ConfigAwait(); foreach (var unsuspendRule in unsuspendRules) { unsuspendRule.SuspendedBy = null; unsuspendRule.SuspendedDate = null; } await ValidationSource.SaveValidationRulesAsync(unsuspendRules).ConfigAwait(); } if (!request.DeleteRuleIds.IsEmpty()) { await ValidationSource.DeleteValidationRulesAsync(request.DeleteRuleIds.ToArray()).ConfigAwait(); } if (request.ClearCache.GetValueOrDefault()) { await ValidationSource.ClearCacheAsync().ConfigAwait(); } }
public async Task Any(ModifyValidationRules request) { var appHost = HostContext.AssertAppHost(); var container = appHost.GetContainer(); var feature = appHost.AssertPlugin <ValidationFeature>(); RequestUtils.AssertAccessRole(base.Request, accessRole: feature.AccessRole, authSecret: request.AuthSecret); var utcNow = DateTime.UtcNow; var userName = base.GetSession().GetUserAuthName(); var rules = request.SaveRules; if (!rules.IsEmpty()) { foreach (var rule in rules) { if (rule.Type == null) { throw new ArgumentNullException(nameof(rule.Type)); } if (rule.CreatedBy == null) { rule.CreatedBy = userName; rule.CreatedDate = utcNow; } rule.ModifiedBy = userName; rule.ModifiedDate = utcNow; } await ValidationSource.SaveValidationRulesAsync(rules); } if (!request.SuspendRuleIds.IsEmpty()) { var suspendRules = await ValidationSource.GetValidateRulesByIdsAsync(request.SuspendRuleIds); foreach (var suspendRule in suspendRules) { suspendRule.SuspendedBy = userName; suspendRule.SuspendedDate = utcNow; } await ValidationSource.SaveValidationRulesAsync(suspendRules); } if (!request.UnsuspendRuleIds.IsEmpty()) { var unsuspendRules = await ValidationSource.GetValidateRulesByIdsAsync(request.UnsuspendRuleIds); foreach (var unsuspendRule in unsuspendRules) { unsuspendRule.SuspendedBy = null; unsuspendRule.SuspendedDate = null; } await ValidationSource.SaveValidationRulesAsync(unsuspendRules); } if (!request.DeleteRuleIds.IsEmpty()) { await ValidationSource.DeleteValidationRulesAsync(request.DeleteRuleIds.ToArray()); } }