コード例 #1
0
        public static void onNewMessage(byte[] body, string listenerName)
        {
            var               core     = COREobject.i;
            DBEntities        context  = core.Context;
            TCPSocketListener listener = context.TCPListeners.Where(l => l.Name == listenerName).FirstOrDefault();

            if (listener != null)
            {
                Block block = GetBlockWithWF(context, listener.ApplicationId, listener.BlockName.RemoveDiacritics());

                if (block != null)
                {
                    core.Application = listener.Application;

                    try {
                        PersonaAppRole role = context.AppRoles.FirstOrDefault(r => r.Name == "System" && r.ApplicationId == listener.ApplicationId);
                        core.User = context.Users.FirstOrDefault(u => u.Users_Roles.Any(r => r.RoleName == role.Name && r.ApplicationId == role.ApplicationId));
                    }
                    catch (Exception e) {
                        OmniusInfo.Log($"Chyba při zpracování socketu: {listenerName} ({e})", OmniusLogSource.Nexus, null, null);
                    }

                    OmniusInfo.Log($"Začátek zpracování socketu: {listener.Name} / Blok {listener.BlockName} / Button {listener.WorkflowName}", OmniusLogSource.Nexus, listener.Application, core.User);

                    FormCollection fc = new FormCollection();
                    Dictionary <string, object> vars = new Dictionary <string, object>();
                    vars.Add("__SocketRequestBody__", body);

                    var runResult = new Modules.Tapestry.Tapestry(core).run(block, listener.WorkflowName, -1, fc, 0, null, vars);

                    OmniusInfo.Log($"Konec zpraconání mailu: {listener.Name} / Blok {listener.BlockName} / Button {listener.WorkflowName}", OmniusLogSource.Hermes, listener.Application, core.User);
                }
            }
        }
コード例 #2
0
        private void Run()
        {
            try
            {
                // System - call method
                if (Application.IsSystem)
                {
                    string   typeName = BlockName;
                    string[] splitted = Executor.Split('/');
                    /// Post/string:ahoj;int:4/string:aho
                    string   methodName      = splitted[0];
                    object[] objectArguments = splitted.Length < 2
                        ? new object[0]
                        : getArguments(splitted[1]);
                    object[] callArguments = splitted.Length < 3
                        ? new object[0]
                        : getArguments(splitted[2]);

                    Type       type   = Assembly.GetCallingAssembly().GetType(typeName);
                    MethodInfo method = type.GetMethod(methodName);

                    if (method.IsStatic)
                    {
                        method.Invoke(null, callArguments);
                    }
                    else
                    {
                        method.Invoke(Activator.CreateInstance(type, objectArguments), callArguments);
                    }

                    return;
                }

                var core = COREobject.i;
                core.User        = core.Context.Users.Single(u => u.UserName == "system");
                core.Application = core.Context.Applications.Find(ApplicationId);
                var block = core.Context.Blocks.Single(b => b.WorkFlow.ApplicationId == ApplicationId && b.Name.ToLower() == BlockName.ToLower());

                var tapestry = new Modules.Tapestry.Tapestry(core);
                var result   = tapestry.innerRun(block, Executor ?? "INIT", ModelId ?? -1, null, -1);

                if (result.Item1.Type != MessageType.Error)
                {
                    LastEndTask = DateTime.UtcNow;
                    COREobject.i.Context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Modules.Watchtower.OmniusException.Log(ex, source: Modules.Watchtower.OmniusLogSource.Cortex, application: Application);
            }
        }
コード例 #3
0
        public JToken Run(string appName, string button, [FromBody] JToken body, string blockIdentify, [FromUri] int modelId = -1)
        {
            COREobject core        = COREobject.i;
            DBEntities context     = core.Context;
            User       currentUser = core.User;
            var        fc          = new M.FormCollection();
            var        inputObject = body as JObject;

            if (inputObject != null)
            {
                foreach (var pair in inputObject)
                {
                    fc.Add(pair.Key, pair.Value.ToString());
                }
            }

            // get block
            Block block = null;

            try
            {
                int blockId = Convert.ToInt32(blockIdentify);
                block = context.Blocks.FirstOrDefault(b => b.Id == blockId);
            }
            catch (FormatException)
            {
                block = context.Blocks.FirstOrDefault(b => b.Name.ToLower() == blockIdentify.ToLower() && b.WorkFlow.ApplicationId == core.Application.Id);
            }

            try
            {
                block = block ?? context.WorkFlows.FirstOrDefault(w => w.ApplicationId == core.Application.Id && w.InitBlockId != null).InitBlock;
            }
            catch (NullReferenceException)
            {
                return(null);
            }

            // RUN
            var result = new Modules.Tapestry.Tapestry(core).jsonRun(block, button, modelId, fc);

            return(result);
        }
コード例 #4
0
        public static void onNewMessage(object model, BasicDeliverEventArgs args)
        {
            var body    = args.Body;
            var message = Encoding.UTF8.GetString(body);

            var        core    = COREobject.i;
            DBEntities context = core.Context;

            Entitron.Entity.Nexus.RabbitMQ listener = context.RabbitMQs.Where(q => q.Name == args.ConsumerTag).FirstOrDefault();
            if (listener != null)
            {
                Block block = GetBlockWithWF(context, listener.ApplicationId.Value, listener.BlockName.RemoveDiacritics());

                if (block != null)
                {
                    core.Application = listener.Application;

                    try {
                        PersonaAppRole role = context.AppRoles.FirstOrDefault(r => r.Name == "System" && r.ApplicationId == listener.ApplicationId);
                        core.User = context.Users.FirstOrDefault(u => u.Users_Roles.Any(r => r.RoleName == role.Name && r.ApplicationId == role.ApplicationId));

                        OmniusInfo.Log($"Začátek zpracování RabbitMQ: {listener.Name} / Blok {listener.BlockName} / Button {listener.WorkflowName}", OmniusLogSource.Nexus, listener.Application, core.User);

                        FormCollection fc = new FormCollection();
                        Dictionary <string, object> vars = new Dictionary <string, object>();
                        vars.Add("__RabbitMQMessage__", message);

                        var runResult = new Modules.Tapestry.Tapestry(core).run(block, listener.WorkflowName, -1, fc, 0, null, vars);

                        OmniusInfo.Log($"Konec zpracování RabbitMQ: {listener.Name} / Blok {listener.BlockName} / Button {listener.WorkflowName}", OmniusLogSource.Hermes, listener.Application, core.User);

                        if (runResult.Item1.Errors.Count == 0)
                        {
                            listeners[args.ConsumerTag].Channel.BasicAck(args.DeliveryTag, false);
                        }
                    }
                    catch (Exception e) {
                        OmniusInfo.Log($"Chyba při zpracování RabbitMQ: {args.ConsumerTag} ({e})", OmniusLogSource.Nexus, null, null);
                    }
                }
            }
        }
コード例 #5
0
        public ActionResult Index(string path)
        {
            RunController.startTime = DateTime.Now;

            JToken              response  = new JObject();
            List <string>       fragments = path.Split('/').ToList();
            string              appName   = fragments[0];
            string              apiName   = fragments[1];
            NameValueCollection fc        = new NameValueCollection();

            fc.Add(Request.Form);

            core    = COREobject.i;
            context = core.AppContext;
            var masterContext = core.Context;

            if (core.User == null && !TryBasicAuth())
            {
                Response.StatusCode = 401;
                Response.Headers.Remove("WWW-Authenticate");
                Response.Headers.Add("WWW-Authenticate", "Basic realm=\"Omnius\"");
                return(new EmptyResult());
            }

            // Zkusme najít vyhovující api
            API api = masterContext.APIs.Where(a => a.Name == apiName).FirstOrDefault();

            if (api == null)
            {
                Response.StatusCode = 404;
                return(new EmptyResult());
            }

            // Máme api - rozparsujeme definici
            Deserializer deserializer = new Deserializer();
            var          defYaml      = deserializer.Deserialize(new StringReader(api.Definition));
            JToken       def          = JToken.FromObject(defYaml);

            // sestavíme regulární výrazy pro cesty, pokud nejsou
            //if(!urlList.ContainsKey((int)api.Id)) {
            BuildUrlList((int)api.Id, def);
            //}

            // Vyhledáme správnou cestu
            if (!path.EndsWith("/"))
            {
                path += "/";
            }
            path = path.Substring(path.IndexOf('/'));

            bool isMatch = false;

            foreach (PathDef pd in urlList[(int)api.Id])
            {
                if (pd.rx.IsMatch(path))
                {
                    // Předáme proměnné do form collection
                    if (pd.vars.Count() > 0)
                    {
                        Match m = pd.rx.Match(path);

                        foreach (KeyValuePair <int, string> var in pd.vars)
                        {
                            fc.Add(var.Value, m.Groups[var.Key].Value);
                        }
                    }
                    isMatch = true;
                    break;
                }
            }

            if (!isMatch)
            {
                Response.StatusCode = 404;
                return(new EmptyResult());
            }

            // Zpracujeme GET proměnné
            if (Request.QueryString.Count > 0)
            {
                foreach (string key in Request.QueryString.AllKeys)
                {
                    fc.Add(key, Request.QueryString[key]);
                }
            }

            // Zpracujeme body
            Stream req = Request.InputStream;

            req.Seek(0, System.IO.SeekOrigin.Begin);
            string jsonString = new StreamReader(req).ReadToEnd();

            fc.Add("__RequestBody__", jsonString);

            // JSON-RPC?
            bool   isJsonRpc = false;
            JToken rpc       = null;

            if (!string.IsNullOrEmpty(jsonString))
            {
                try {
                    rpc = JToken.Parse(jsonString);
                    if (rpc is JToken && rpc != null && !string.IsNullOrEmpty((string)rpc["jsonrpc"]))
                    {
                        isJsonRpc = true;
                    }
                }
                catch (Exception) {}
            }

            string wfName = Request.HttpMethod.ToUpper();

            if (isJsonRpc)
            {
                wfName += $"_{(string)rpc["method"]}";
            }

            Application app   = core.Application.similarApp;
            Block       block = getBlockWithResource(app.Id, apiName);

            if (block == null)
            {
                Response.StatusCode = 404;
                if (isJsonRpc)
                {
                    response["jsonrpc"]          = (string)rpc["jsonrpc"];
                    response["error"]            = new JObject();
                    response["error"]["code"]    = -32601;
                    response["error"]["message"] = "Method not found";
                    response["id"] = (string)rpc["id"];

                    return(Content(response.ToString(), "application/json"));
                }
                else
                {
                    return(new EmptyResult());
                }
            }

            // Check if user has one of allowed roles, otherwise return 403
            if (!String.IsNullOrEmpty(block.RoleWhitelist))
            {
                bool userIsAllowed = false;
                foreach (var role in block.RoleWhitelist.Split(',').ToList())
                {
                    if (core.User.HasRole(role, core.Application.Id))
                    {
                        userIsAllowed = true;
                    }
                }
                if (!userIsAllowed)
                {
                    Response.StatusCode = 403;
                    if (isJsonRpc)
                    {
                        response["jsonrpc"]          = (string)rpc["jsonrpc"];
                        response["error"]            = new JObject();
                        response["error"]["code"]    = -32600;
                        response["error"]["message"] = "You are not allowed to use this endpoint and method";
                        response["id"] = (string)rpc["id"];
                    }
                    else
                    {
                        response["status"]  = "failed";
                        response["message"] = new JArray("You are not allowed to use this endpoint");
                    }
                }
            }

            if (block.SourceTo_ActionRules.Where(r => r.ExecutedBy == wfName).Count() == 0)  // Neimplementovaná metoda
            {
                Response.StatusCode = 405;
                if (isJsonRpc)
                {
                    response["jsonrpc"]          = (string)rpc["jsonrpc"];
                    response["error"]            = new JObject();
                    response["error"]["code"]    = -32601;
                    response["error"]["message"] = "Method not found";
                    response["id"] = (string)rpc["id"];
                }
                else
                {
                    response["status"]  = "failed";
                    response["message"] = new JArray("Method not allowed");
                }
            }

            if (Response.StatusCode == 200)
            {
                Message message = new Message(core);
                var     result  = new Modules.Tapestry.Tapestry(core).jsonRun(block, wfName, -1, fc, out message);
                if (message.Type == MessageType.Error)
                {
                    Response.StatusCode = 500;
                    if (isJsonRpc)
                    {
                        response["jsonrpc"]          = (string)rpc["jsonrpc"];
                        response["error"]            = new JObject();
                        response["error"]["code"]    = -32603;
                        response["error"]["message"] = string.Join(" ", message.Errors);
                        response["id"] = (string)rpc["id"];
                    }
                    else
                    {
                        response["status"]  = "failed";
                        response["message"] = JArray.FromObject(message.Errors);
                    }
                }
                else
                {
                    if (isJsonRpc)
                    {
                        response["jsonrpc"] = (string)rpc["jsonrpc"];
                        response["result"]  = result;
                        response["id"]      = (string)rpc["id"];
                    }
                    else
                    {
                        response = result;
                    }
                }
            }

            return(Content(response.ToString(), "application/json"));
        }