private static ReturnMessageWrapper ResponseClient(MessageWrapper wrapper, string result, string operation)
        {
            var eventdata = new Common.DTO.EventDTO(result, wrapper.Clientid, wrapper.Messageid);

            Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.GetRouteFunction(operation), eventdata);

            if (result != null)
            {
                return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                {
                    new LocalizedString()
                    {
                        Lang = "en", Text = "OK"
                    }
                }, result));
            }
            else
            {
                return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                {
                    new LocalizedString()
                    {
                        Lang = "en", Text = "Missing rights"
                    }
                }, result));
            }
        }
        /// <summary>
        /// Handles messages regardring cluster
        /// </summary>
        /// <param name="topicparts">topic parts</param>
        /// <param name="wrapper">wrapper</param>
        /// <returns>null</returns>
        public static ReturnMessageWrapper HandleMessage(string[] topicparts, Common.DTO.MessageWrapper wrapper)
        {
            CallContext cc = new CallContext(
                wrapper.OrgContext,
                new Common.DTO.Token()
            {
                Scope = GlobalValues.Scope, Tokenid = wrapper.UserContextToken
            },
                wrapper.IssuedDate);

            if (cc.IsSystemAdministrator)
            {
                var operation = topicparts[1];
                Monosoft.Auth.DTO.Cluster cluster = new Auth.DTO.Cluster();
                if (wrapper.MessageData != null)
                {
                    Auth.DTO.Cluster result = null;
                    switch (operation)
                    {
                    case "create":     // TESTET OK: 30-09-2018
                        cluster = Common.DTO.MessageWrapperHelper <Monosoft.Auth.DTO.Cluster> .GetData(wrapper);

                        result = Cluster.ConvertToDTO(Cluster.Create(cluster, cc.OrganisationId.Value));
                        Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteClusterCreated, new Common.DTO.EventDTO(result, wrapper.Clientid, wrapper.Messageid));
                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, result));

                    case "update":     // TESTET OK: 30-09-2018
                        cluster = Common.DTO.MessageWrapperHelper <Monosoft.Auth.DTO.Cluster> .GetData(wrapper);

                        result = Cluster.ConvertToDTO(Cluster.Update(cluster, cc.OrganisationId.Value));
                        Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteClusterUpdated, new Common.DTO.EventDTO(result, wrapper.Clientid, wrapper.Messageid));
                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, result));

                    case "delete":     // TESTET OK: 30-09-2018
                        var clusterid = Common.DTO.MessageWrapperHelper <Monosoft.Common.DTO.IntIdDTO> .GetData(wrapper);

                        Common.DTO.Success isDeleted = new Common.DTO.Success();
                        isDeleted.Succeeded = Cluster.Delete(clusterid.Id, cc.OrganisationId.Value);
                        Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteClusterDeleted, new Common.DTO.EventDTO(isDeleted, wrapper.Clientid, wrapper.Messageid));
                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, isDeleted));

                    case "getbyorganisation":     // TESTET OK: 30-09-2018
                        var organisationId = Common.DTO.MessageWrapperHelper <Monosoft.Common.DTO.GuidIdDTO> .GetData(wrapper);

                        Auth.DTO.Clusters getresult = new Auth.DTO.Clusters();
                        getresult.Cluster = Cluster.Read(organisationId.Id).Select(p => Cluster.ConvertToDTO(p)).ToArray();
                        Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteClusterRead, new Common.DTO.EventDTO(getresult, wrapper.Clientid, wrapper.Messageid));
                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, result));

                    default:     /*log error event*/
                        Common.MessageQueue.Diagnostics.Instance.LogEvent("Unknow topic for Cluster.", operation + " is unknown", Common.DTO.Severity.Information, wrapper.OrgContext);
                        break;
                    }
                }
            }

            return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
            {
                new LocalizedString()
                {
                    Lang = "en", Text = "missing credentials"
                }
            }, null));
        }
        /// <summary>
        /// The message handler logic for organisation messages
        /// </summary>
        /// <param name="topicparts">topic parts</param>
        /// <param name="wrapper">wrapper object</param>
        /// <returns>resulting object or null</returns>
        public static ReturnMessageWrapper HandleMessage(string[] topicparts, Common.DTO.MessageWrapper wrapper)
        {
            //CallContext cc = new CallContext(wrapper.OrgContext, new Common.DTO.Token() { Tokenid = wrapper.UserContextToken, Scope = GlobalValues.Scope }, wrapper.IssuedDate);
            var             operation       = topicparts[1];
            FunctionHandler functionHandler = new FunctionHandler();
            DBHandler       dBHandler       = new DBHandler();
            bool            useMongoDBjs    = true;

            if (true) // cc.IsServerSideFunctionsAdmin
            {
                if (wrapper.MessageData != null)
                {
                    switch (operation)
                    {
                    case "create":
                        var createFuncDef = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string createResult = functionHandler.CreateFunction(createFuncDef);

                        return(ResponseClient(wrapper, createResult, operation));

                    case "delete":
                        var deleteFuncDef = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string deleteResult = functionHandler.DeleteFunction(deleteFuncDef.Name, Language.Sharp);

                        return(ResponseClient(wrapper, deleteResult, operation));

                    case "update":
                        var updateFuncDef = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string updateResult = functionHandler.UpdateFunction(updateFuncDef);

                        return(ResponseClient(wrapper, updateResult, operation));

                    case "read":
                        var readFuncDef = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string readResult = functionHandler.ReadFunction(readFuncDef.Name, Language.Sharp);

                        return(ResponseClient(wrapper, readResult, operation));

                    case "readall":
                        var readallFuncDef = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string readallResult = functionHandler.ReadAllFunctions(readallFuncDef.Name, Language.Sharp);

                        return(ResponseClient(wrapper, readallResult, operation));

                    case "run":
                        var runFuncDef = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        object[] parameters = functionHandler.ConvertToObjectArray(runFuncDef.FunctionData);
                        if (parameters == null)
                        {
                            return(ResponseClient(wrapper, "WRONG INPUT DATA", operation));
                        }
                        var runResult = functionHandler.RunFunction(runFuncDef.Name, parameters);
                        return(ResponseClient(wrapper, runResult.ToString(), operation));

                    case "createjs":
                        var createFuncDefJS = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string createResultJS;
                        if (useMongoDBjs)
                        {
                            createResultJS = dBHandler.CreateFunction(createFuncDefJS);
                        }
                        else
                        {
                            createResultJS = functionHandler.CreateFunctionJS(createFuncDefJS);
                        }
                        return(ResponseClient(wrapper, createResultJS, operation));

                    case "deletejs":
                        var deleteFuncDefJS = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string deleteResultJS;
                        if (useMongoDBjs)
                        {
                            deleteResultJS = dBHandler.DeleteFunction(deleteFuncDefJS.Name);
                        }
                        else
                        {
                            deleteResultJS = functionHandler.DeleteFunction(deleteFuncDefJS.Name, Language.JS);
                        }
                        return(ResponseClient(wrapper, deleteResultJS, operation));

                    case "updatejs":
                        var updateFuncDefJS = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string updateResultJS;
                        if (useMongoDBjs)
                        {
                            updateResultJS = dBHandler.UpdateFunction(updateFuncDefJS);
                        }
                        else
                        {
                            updateResultJS = functionHandler.UpdateFunctionJS(updateFuncDefJS);
                        }
                        return(ResponseClient(wrapper, updateResultJS, operation));

                    case "readjs":
                    case "runjs":
                        var readFuncDefJS = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string readResultJS;
                        if (useMongoDBjs)
                        {
                            readResultJS = dBHandler.ReadFunction(readFuncDefJS.Name);
                        }
                        else
                        {
                            readResultJS = functionHandler.ReadFunction(readFuncDefJS.Name, Language.JS);
                        }
                        return(ResponseClient(wrapper, readResultJS, operation));

                    case "readalljs":
                        var readallFuncDefJS = Common.DTO.MessageWrapperHelper <DTO.FunctionDefinitions> .GetData(wrapper);

                        string readallResultJS;
                        if (useMongoDBjs)
                        {
                            readallResultJS = dBHandler.ReadAllFunctions(readallFuncDefJS.Name);
                        }
                        else
                        {
                            readallResultJS = functionHandler.ReadAllFunctions(readallFuncDefJS.Name, Language.JS);
                        }
                        return(ResponseClient(wrapper, readallResultJS, operation));

                    default:     /*log error event*/
                        Common.MessageQueue.Diagnostics.Instance.LogEvent("Unknow topic for ServerSideFunctions.", operation + " is unknown", Common.DTO.Severity.Information, System.Guid.Empty);
                        break;
                    }
                }
            }
            return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
            {
                new LocalizedString()
                {
                    Lang = "en", Text = "unknown situation"
                }
            }, null));
        }
Beispiel #4
0
        /// <summary>
        /// The message handler logic for organisation messages
        /// </summary>
        /// <param name="topicparts">topic parts</param>
        /// <param name="wrapper">wrapper object</param>
        /// <returns>resulting object or null</returns>
        public static ReturnMessageWrapper HandleMessage(string[] topicparts, Common.DTO.MessageWrapper wrapper)
        {
            CallContext cc = new CallContext(
                wrapper.OrgContext,
                new Common.DTO.Token()
            {
                Tokenid = wrapper.UserContextToken, Scope = GlobalValues.Scope
            },
                wrapper.IssuedDate);
            var operation = topicparts[1];

            Monosoft.Auth.DTO.Organisation organisation = new Auth.DTO.Organisation();

            if (cc.IsKeyAccountManager)
            {
                if (wrapper.MessageData != null)
                {
                    Auth.DTO.Organisation result = null;
                    switch (operation)
                    {
                    case "create":     // TESTET OK: 30-09-2018
                        organisation = Common.DTO.MessageWrapperHelper <Monosoft.Auth.DTO.Organisation> .GetData(wrapper);

                        result = Organisation.ConvertToDTO(cc.Scope, Organisation.Create(organisation, cc.Scope));
                        Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteOrganisationCreated, new Common.DTO.EventDTO(result, wrapper.Clientid, wrapper.Messageid));
                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, result));

                    case "update":     // TESTET OK: 30-09-2018
                        organisation = Common.DTO.MessageWrapperHelper <Monosoft.Auth.DTO.Organisation> .GetData(wrapper);

                        result = Organisation.ConvertToDTO(cc.Scope, Organisation.Update(organisation, cc.Scope));
                        Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteOrganisationUpdated, new Common.DTO.EventDTO(result, wrapper.Clientid, wrapper.Messageid));
                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, result));

                    case "delete":     // TESTET OK: 30-09-2018
                        var deleteid = Common.DTO.MessageWrapperHelper <Monosoft.Common.DTO.GuidIdDTO> .GetData(wrapper);

                        Common.DTO.Success isDeleted = new Common.DTO.Success();
                        isDeleted.Succeeded = Organisation.Delete(deleteid.Id);
                        Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteOrganisationDeleted, new Common.DTO.EventDTO(isDeleted, wrapper.Clientid, wrapper.Messageid));
                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, isDeleted));

                    default:     /*log error event*/
                        // Common.MessageQueue.Diagnostics.Instance.LogEvent("Unknow topic for Organisation.", operation + " is unknown", Common.DTO.Severity.Information, wrapper.OrgContext);
                        break;
                    }
                }
            }

            if (wrapper.MessageData != null)
            { // operations without any security conserns
                switch (operation)
                {
                case "create":
                case "update":
                case "delete":
                    Common.MessageQueue.Diagnostics.Instance.LogEvent("Missing credentials.", "Missing credentials for " + operation + " is unknown", Common.DTO.Severity.Information, wrapper.OrgContext);
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "missing credentials"
                        }
                    }, null));

                case "getbyids":
                    var getbyid = Common.DTO.MessageWrapperHelper <Monosoft.Common.DTO.GuidIdsDTO> .GetData(wrapper);

                    var orgsList = Organisation.GetByIds(getbyid.Ids).Select(p => Organisation.ConvertToDTOWoDetails(cc.Scope, p)).ToList();
                    var results  = Organisation.ConvertToDTO(cc.Scope, orgsList);
                    Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteOrganisationRead, new Common.DTO.EventDTO(results, wrapper.Clientid, wrapper.Messageid));
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, results));

                default:     /*log error event*/
                    Common.MessageQueue.Diagnostics.Instance.LogEvent("Unknow topic for Organisation.", operation + " is unknown", Common.DTO.Severity.Information, wrapper.OrgContext);
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = operation + " is unknown"
                        }
                    }, null));
                }
            }

            Common.MessageQueue.Diagnostics.Instance.LogEvent("Missing MessageData.", "MessageData is null", Common.DTO.Severity.Information, wrapper.OrgContext);
            return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
            {
                new LocalizedString()
                {
                    Lang = "en", Text = "MessageData is null"
                }
            }, null));
        }
        /// <summary>
        /// Handle incomming usergroup requests
        /// </summary>
        /// <param name="topicparts">list of topicparts</param>
        /// <param name="wrapper">messagewrapper</param>
        /// <returns>null</returns>
        public static ReturnMessageWrapper HandleMessage(string[] topicparts, Common.DTO.MessageWrapper wrapper)
        {
            var operation = topicparts[1];

            if (wrapper.MessageData != null)
            {
                CallContext cc = new CallContext(
                    wrapper.OrgContext,
                    new Common.DTO.Token()
                {
                    Scope = wrapper.Scope, Tokenid = wrapper.UserContextToken
                },
                    null,
                    wrapper.IssuedDate,
                    Monosoft.User.Service.GlobalValues.Scope);

                if (cc.IsAdministrator)
                {
                    switch (operation)
                    {
                    case "create":     // TESTET OK: 28-09-2018
                        var createusergrp = Common.DTO.MessageWrapperHelper <Monosoft.Auth.DTO.UserGroup> .GetData(wrapper);

                        var affectedUsersCreate = UserGroup.CreateUserGroup(cc, createusergrp);
                        if (affectedUsersCreate != null)
                        {
                            Common.MessageQueue.EventClient.Instance.RaiseEvent(
                                Monosoft.User.Service.GlobalValues.RouteUserGroupCreated + "." + wrapper.OrgContext,
                                new Common.DTO.EventDTO(createusergrp, wrapper.Clientid, wrapper.Messageid));

                            var eventdata = CreateEventData(wrapper.Clientid, wrapper.Messageid, affectedUsersCreate);
                            Common.MessageQueue.EventClient.Instance.RaiseEvent(
                                Monosoft.User.Service.GlobalValues.RouteTokenInvalidateUser,
                                eventdata);
                        }

                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, createusergrp));

                    case "update":     // TESTET OK: 28-09-2018
                        var updateusergrp = Common.DTO.MessageWrapperHelper <Monosoft.Auth.DTO.UserGroup> .GetData(wrapper);

                        var affectedUsersUpdate = UserGroup.UpdateUserGroup(cc, updateusergrp);
                        if (affectedUsersUpdate != null)
                        {
                            Common.MessageQueue.EventClient.Instance.RaiseEvent(
                                Monosoft.User.Service.GlobalValues.RouteUserGroupUpdated + "." + wrapper.OrgContext,
                                new Common.DTO.EventDTO(updateusergrp, wrapper.Clientid, wrapper.Messageid));

                            var eventdata = CreateEventData(wrapper.Clientid, wrapper.Messageid, affectedUsersUpdate);
                            Common.MessageQueue.EventClient.Instance.RaiseEvent(
                                Monosoft.User.Service.GlobalValues.RouteTokenInvalidateUser,
                                eventdata);
                        }

                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, updateusergrp));

                    case "delete":     // TESTET OK: 28-09-2018
                        var deleteusrgrp = Common.DTO.MessageWrapperHelper <Monosoft.Auth.DTO.UserGroupId> .GetData(wrapper);

                        var affectedUsersDelete = UserGroup.DeleteUserGroup(cc, deleteusrgrp);
                        if (affectedUsersDelete != null)
                        {
                            Common.MessageQueue.EventClient.Instance.RaiseEvent(
                                Monosoft.User.Service.GlobalValues.RouteUserGroupDeleted + "." + wrapper.OrgContext,
                                new Common.DTO.EventDTO(deleteusrgrp, wrapper.Clientid, wrapper.Messageid));

                            var eventdata = CreateEventData(wrapper.Clientid, wrapper.Messageid, affectedUsersDelete);
                            Common.MessageQueue.EventClient.Instance.RaiseEvent(
                                Monosoft.User.Service.GlobalValues.RouteTokenInvalidateUser,
                                eventdata);
                        }

                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, deleteusrgrp));

                    case "get":     // TESTET OK: 28-09-2018
                        var res = UserGroup.ReadUserGroup(cc);
                        Common.MessageQueue.EventClient.Instance.RaiseEvent(
                            Monosoft.User.Service.GlobalValues.RouteUserGroupRead,
                            new Common.DTO.EventDTO(res, wrapper.Clientid, wrapper.Messageid));

                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, res));

                    default:     /*log error event*/
                        Common.MessageQueue.Diagnostics.Instance.LogEvent(
                            "Unknow topic for UserGroup.",
                            operation + " is unknown",
                            Common.DTO.Severity.Information,
                            wrapper.OrgContext);
                        break;
                    }
                }
            }

            return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
            {
                new LocalizedString()
                {
                    Lang = "en", Text = "message data missing"
                }
            }, null));
        }
        /// <summary>
        /// Handle incomming token requests
        /// </summary>
        /// <param name="topicparts">list of topics</param>
        /// <param name="wrapper">messagewrapper</param>
        /// <returns>tokendata as byte array - but only on verify, otherwise null is returned</returns>
        public static ReturnMessageWrapper HandleMessage(string[] topicparts, Common.DTO.MessageWrapper wrapper)
        {
            var operation = topicparts[1];

            if (wrapper.MessageData != null)
            {
                switch (operation)
                {
                // case "resetpassword"://RPC (evt. FAF for logging!!!)
                //    //TODO: implement... request reset password.... benyt otp til reset?
                //    return BsonHelper.ToBson<Auth.DTO.TokenData>(null);
                // case "otp"://RPC (evt. FAF for logging!!!)
                //    //TODO: implement... request otp, send out OTP-code, and allow that code on login below
                //    return BsonHelper.ToBson<Auth.DTO.TokenData>(null);
                case "login":     // TESTET OK: 28-09-2018
                    var loginRes = Database.Auth.Datalayer.Token.Login(
                        Common.DTO.MessageWrapperHelper <Monosoft.Auth.DTO.Login> .GetData(wrapper),
                        wrapper.CallerIp,
                        wrapper.OrgContext,
                        wrapper.Scope);

                    foreach (var old in loginRes.oldToken)
                    {
                        Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteTokenInvalidateToken, new Common.DTO.EventDTO(old, wrapper.Clientid, wrapper.Messageid));
                    }

                    Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteTokenLogin, new Common.DTO.EventDTO(loginRes.newToken, wrapper.Clientid, wrapper.Messageid));
                    if (loginRes.newToken.Tokenid == Guid.Empty)
                    {
                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "Uknown user or invalid password"
                            }
                        }, loginRes.newToken));
                    }
                    else
                    {
                        return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                        {
                            new LocalizedString()
                            {
                                Lang = "en", Text = "OK"
                            }
                        }, loginRes.newToken));
                    }

                case "verify":     // TESTET OK: 28-09-2018
                    var verifyRes = Database.Auth.Datalayer.Token.Verify(
                        Common.DTO.MessageWrapperHelper <Monosoft.Common.DTO.Token> .GetData(wrapper),
                        wrapper.OrgContext);
                    Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteTokenVerify, new Common.DTO.EventDTO(verifyRes, wrapper.Clientid, wrapper.Messageid));
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, verifyRes));

                case "logout":     // TESTET OK: 28-09-2018
                    var logoutToken = Common.DTO.MessageWrapperHelper <Monosoft.Common.DTO.Token> .GetData(wrapper);

                    var usr = Database.Auth.Datalayer.Token.Logout(logoutToken, wrapper.OrgContext);
                    Common.MessageQueue.EventClient.Instance.RaiseEvent(GlobalValues.RouteTokenInvalidateToken, new Common.DTO.EventDTO(usr, wrapper.Clientid, wrapper.Messageid));
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, usr));

                default:     /*log error event*/
                    Common.MessageQueue.Diagnostics.Instance.LogEvent("Unknow topic for Event.", operation + " is unknown", Common.DTO.Severity.Information, System.Guid.Empty);
                    break;
                }
            }

            return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
            {
                new LocalizedString()
                {
                    Lang = "en", Text = "missing messagedata"
                }
            }, null));
        }
        /// <summary>
        /// Handle an incomming message
        /// </summary>
        /// <param name="topicparts">The topic/route as a list of strings</param>
        /// <param name="wrapper">Message wrapper</param>
        /// <returns>NULL</returns>
        public static ReturnMessageWrapper HandleMessage(string[] topicparts, Common.DTO.MessageWrapper wrapper)
        {
            var operation = topicparts[1];

            Monosoft.Auth.DTO.User user = new Monosoft.Auth.DTO.User();
            if (wrapper.MessageData != null)
            {
                user = Common.DTO.MessageWrapperHelper <Monosoft.Auth.DTO.User> .GetData(wrapper);
            }

            CallContext cc = new CallContext(
                wrapper.OrgContext,
                new Common.DTO.Token()
            {
                Scope = wrapper.Scope, Tokenid = wrapper.UserContextToken
            },
                user,
                wrapper.IssuedDate,
                Monosoft.User.Service.GlobalValues.Scope);

            Common.DTO.EventDTO eventdata = null;
            switch (operation)
            {
            case "create":     // TESTET OK: 28-09-2018
                var createRes = User.CreateUser(cc);
                var res       = createRes == null ? null : createRes.Convert2DTO(cc);
                eventdata = new Common.DTO.EventDTO(
                    res,
                    wrapper.Clientid,
                    wrapper.Messageid);
                Common.MessageQueue.EventClient.Instance.RaiseEvent(Monosoft.User.Service.GlobalValues.RouteUserCreated + "." + wrapper.OrgContext, eventdata);
                if (createRes == null)
                {
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, res));
                }
                else
                {
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "Missing rights"
                        }
                    }, res));
                }

            case "update":     // TESTET OK: 28-09-2018
                var updatedUser   = User.UpdateUser(cc);
                var updatedResult = updatedUser == null ? null : updatedUser.Convert2DTO(cc);
                eventdata = new Common.DTO.EventDTO(
                    updatedResult,
                    wrapper.Clientid,
                    wrapper.Messageid);
                Common.MessageQueue.EventClient.Instance.RaiseEvent(Monosoft.User.Service.GlobalValues.RouteUserUpdated + "." + wrapper.OrgContext, eventdata);
                if (updatedUser == null)
                {
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, updatedResult));
                }
                else
                {
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "Missing rights"
                        }
                    }, updatedResult));
                }

            case "delete":     // TESTET OK: 28-09-2018
                User.DeleteUser(cc);
                eventdata = new Common.DTO.EventDTO(
                    user,
                    wrapper.Clientid,
                    wrapper.Messageid);
                Common.MessageQueue.EventClient.Instance.RaiseEvent(Monosoft.User.Service.GlobalValues.RouteTokenInvalidateUser, eventdata);
                return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                {
                    new LocalizedString()
                    {
                        Lang = "en", Text = "OK"
                    }
                }, null));

            case "get":
                var userRes = User.ReadUser(cc);
                Common.MessageQueue.EventClient.Instance.RaiseEvent(
                    Monosoft.User.Service.GlobalValues.RouteUserRead,
                    new Common.DTO.EventDTO(userRes, wrapper.Clientid, wrapper.Messageid));
                return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                {
                    new LocalizedString()
                    {
                        Lang = "en", Text = "OK"
                    }
                }, userRes));

            case "getall":     // TESTET OK: 28-09-2018
                var usersRes = User.ReadUsers(cc);
                Common.MessageQueue.EventClient.Instance.RaiseEvent(
                    Monosoft.User.Service.GlobalValues.RouteUserRead + "." + wrapper.OrgContext,
                    new Common.DTO.EventDTO(usersRes.ToArray(), wrapper.Clientid, wrapper.Messageid));    //TODO ikke lovlig resultat...
                return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                {
                    new LocalizedString()
                    {
                        Lang = "en", Text = "OK"
                    }
                }, usersRes.ToArray())); //TODO ikke lovlig resultat...

            default:                     /*log error event*/
                Common.MessageQueue.Diagnostics.Instance.LogEvent(
                    "Unknow topic for User.",
                    operation + " is unknown",
                    Common.DTO.Severity.Information,
                    wrapper.OrgContext);
                break;
            }

            return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
            {
                new LocalizedString()
                {
                    Lang = "en", Text = "unknown situation"
                }
            }, null));
        }
        /// <summary>
        /// Handle an incomming message
        /// </summary>
        /// <param name="topicparts">The topic/route as a list of strings</param>
        /// <param name="wrapper">Message wrapper</param>
        /// <returns>NULL</returns>
        public static ReturnMessageWrapper HandleMessage(string[] topicparts, Common.DTO.MessageWrapper wrapper)
        {
            CallContext cc = new CallContext(
                wrapper.OrgContext,
                new Common.DTO.Token()
            {
                Tokenid = wrapper.UserContextToken, Scope = GlobalValues.Scope
            },
                wrapper.IssuedDate);

            var operation = topicparts[1];

            switch (operation)
            {
            case "download":     // BEMÆRK RPC !!!
                ServiceDownloadDefinition downloadDef = Common.DTO.MessageWrapperHelper <ServiceDownloadDefinition> .GetData(wrapper);

                var foundupdate = downloadDef.FindUpdate();
                Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.newversion." + downloadDef.ServiceId, new Common.DTO.EventDTO(foundupdate, wrapper.Clientid, wrapper.Messageid));
                return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                {
                    new LocalizedString()
                    {
                        Lang = "en", Text = "OK"
                    }
                }, foundupdate));

            // break;
            case "upload":
                if (cc.IsServiceStoreAdmin)
                {
                    ServiceProgramVersion program = Common.DTO.MessageWrapperHelper <ServiceProgramVersion> .GetData(wrapper);

                    program.SaveToDisk();
                    Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.newserviceupdate", new Common.DTO.EventDTO(program.Version, string.Empty, string.Empty));
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, program.Version));
                }
                break;

            case "create":
                if (cc.IsServiceStoreAdmin)
                {
                    Monosoft.ServiceStore.DTO.Service createservice = Common.DTO.MessageWrapperHelper <Monosoft.ServiceStore.DTO.Service> .GetData(wrapper);

                    createservice.SaveToDisk(false);
                    Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.newservice", new Common.DTO.EventDTO(createservice, string.Empty, string.Empty));
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, createservice));
                }

                break;

            case "update":
                if (cc.IsServiceStoreAdmin)
                {
                    Monosoft.ServiceStore.DTO.Service updateservice = Common.DTO.MessageWrapperHelper <Monosoft.ServiceStore.DTO.Service> .GetData(wrapper);

                    updateservice.SaveToDisk(true);
                    Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.serviceupdate", new Common.DTO.EventDTO(updateservice, string.Empty, string.Empty));
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, updateservice));
                }

                break;

            case "delete":
                if (cc.IsServiceStoreAdmin)
                {
                    Monosoft.ServiceStore.DTO.Service deleteservice = Common.DTO.MessageWrapperHelper <Monosoft.ServiceStore.DTO.Service> .GetData(wrapper);

                    deleteservice.Delete();
                    Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.servicedelete", new Common.DTO.EventDTO(deleteservice, string.Empty, string.Empty));
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, deleteservice));
                }

                break;

            case "get":
                if (cc.IsServiceStoreAdmin)
                {
                    var res = ServicesInformation.ServiceInformation();
                    Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.servicelist", new Common.DTO.EventDTO(res, wrapper.Clientid, wrapper.Messageid));
                    return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
                    {
                        new LocalizedString()
                        {
                            Lang = "en", Text = "OK"
                        }
                    }, res));
                }

                break;

            default:     /*log error event*/
                Common.MessageQueue.Diagnostics.Instance.LogEvent(
                    "Unknow topic for ServiceStore.",
                    operation + " is unknown",
                    Common.DTO.Severity.Information,
                    wrapper.OrgContext);
                break;
            }

            return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
            {
                new LocalizedString()
                {
                    Lang = "en", Text = "missing credentials"
                }
            }, null));
        }
    {        /// <summary>
             /// Handle an incomming message
             /// </summary>
             /// <param name="topicparts">The topic/route as a list of strings</param>
             /// <param name="wrapper">Message wrapper</param>
             /// <returns>topicparts[1] == "login" ? LoginInformation DTO</returns>
        public static ReturnMessageWrapper HandleMessage(string[] topicparts, Common.DTO.MessageWrapper wrapper)
        {
            var operation = topicparts[1];

            switch (operation)
            {
            case "login":
                byte[] tokenBytes = RequestClient.Instance.Rpc("token.login", MessageDataHelper.ToMessageData(wrapper));
                ReturnMessageWrapper returnTokenData = MessageDataHelper.FromMessageData <ReturnMessageWrapper>(tokenBytes);
                TokenData            token           = MessageDataHelper.FromMessageData <TokenData>(returnTokenData.Data);

                Organisations organisations = null;
                User          user          = null;

                if (token.Tokenid != Guid.Empty)
                {
                    // Get full user data on user id
                    MessageWrapper messageWrapper = new MessageWrapper(wrapper);
                    messageWrapper.UserContextToken = token.Tokenid;
                    User userData = new User();
                    userData.Userid            = token.Userid;
                    messageWrapper.MessageData = MessageDataHelper.ToMessageData(userData);
                    byte[] returnUserBytes = RequestClient.Instance.Rpc("user.get", MessageDataHelper.ToMessageData(messageWrapper));
                    ReturnMessageWrapper returnUserData = MessageDataHelper.FromMessageData <ReturnMessageWrapper>(returnUserBytes);
                    user = MessageDataHelper.FromMessageData <User>(returnUserData.Data);

                    // Get full organisation data on organisation ids
                    MessageWrapper orgMessageWrapper = new MessageWrapper(messageWrapper);
                    GuidIdsDTO     orgIds            = new GuidIdsDTO();
                    if (user.Organisations == null)
                    {
                        orgIds.Ids = new Guid[0];
                    }
                    else
                    {
                        orgIds.Ids = user.Organisations.Select(p => p.Id).ToArray();
                        orgMessageWrapper.MessageData = MessageDataHelper.ToMessageData(orgIds);
                        byte[] returbOrgsBytes = RequestClient.Instance.Rpc("organisation.getbyids", MessageDataHelper.ToMessageData(orgMessageWrapper));
                        ReturnMessageWrapper returbOrgsData = MessageDataHelper.FromMessageData <ReturnMessageWrapper>(returbOrgsBytes);
                        organisations = MessageDataHelper.FromMessageData <Organisations>(returbOrgsData.Data);
                    }
                }

                // Constructing our combined result
                LoginInformation result = new LoginInformation(token, user, organisations);

                EventClient.Instance.RaiseEvent(GlobalValues.RouteLoginRead, new EventDTO(result, wrapper.Clientid, wrapper.Messageid));
                return(ReturnMessageWrapper.CreateResult(true, wrapper, new List <LocalizedString>()
                {
                    new LocalizedString()
                    {
                        Lang = "en", Text = "OK"
                    }
                }, result));

            case "initorganisation":     // createorganisation":
                var tokendata = Monosoft.Common.DTO.MemoryCache.FindToken(new Common.DTO.Token()
                {
                    Scope = GlobalValues.Scope, Tokenid = wrapper.UserContextToken
                }, wrapper.OrgContext);
                if (tokendata.IsValidToken())
                {
                    // 1. create organisation
                    byte[] returnUserBytes             = RequestClient.Instance.Rpc("organisation.create", MessageDataHelper.ToMessageData(wrapper));
                    ReturnMessageWrapper returnOrgData = MessageDataHelper.FromMessageData <ReturnMessageWrapper>(returnUserBytes);
                    if (returnOrgData.Success)
                    {
                        Organisation org = MessageDataHelper.FromMessageData <Organisation>(returnOrgData.Data);

                        // 2. create user relation to the organisation
                        Monosoft.Auth.DTO.UserGroup defaultUserGroup = new UserGroup()
                        {
                            Name   = "Default",
                            Claims = new MetaData[] { new MetaData()
                                                      {
                                                          Key = "isAdmin", Scope = "Monosoft.Service.AUTH", Value = "true"
                                                      } },
                            Organisationid = org.Id,
                            Users          = new User[] { new User()
                                                          {
                                                              Userid = tokendata.Userid
                                                          } }
                        };
                        MessageWrapper messageWrapperUserGroup = new MessageWrapper(wrapper);
                        messageWrapperUserGroup.MessageData = MessageDataHelper.ToMessageData(defaultUserGroup);
                        byte[] returnUsergroupBytes = RequestClient.Instance.Rpc("usergroup.create", MessageDataHelper.ToMessageData(messageWrapperUserGroup));
                        ReturnMessageWrapper returnUserGroupData = MessageDataHelper.FromMessageData <ReturnMessageWrapper>(returnUserBytes);
                        if (returnUserGroupData.Success)
                        {
                            var createOrgresult = new Monosoft.Common.DTO.BoolDTO()
                            {
                                Value = true
                            };
                            EventClient.Instance.RaiseEvent(GlobalValues.RouteLoginRead, new EventDTO(createOrgresult, wrapper.Clientid, wrapper.Messageid));
                            return(ReturnMessageWrapper.CreateResult(true, wrapper, new List <LocalizedString>()
                            {
                                new LocalizedString()
                                {
                                    Lang = "en", Text = "OK"
                                }
                            }, createOrgresult));
                        }
                    }
                }

                var createOrgNegativeResult = new Monosoft.Common.DTO.BoolDTO()
                {
                    Value = false
                };
                EventClient.Instance.RaiseEvent(GlobalValues.RouteLoginRead, new EventDTO(createOrgNegativeResult, wrapper.Clientid, wrapper.Messageid));
                return(ReturnMessageWrapper.CreateResult(true, wrapper, new List <LocalizedString>()
                {
                    new LocalizedString()
                    {
                        Lang = "en", Text = "Something went wrong, please contact support"
                    }
                }, createOrgNegativeResult));

            default:     /* log error event*/
                Common.MessageQueue.Diagnostics.Instance.LogEvent(
                    "Unknow topic for Auth.",
                    operation + " is unknown",
                    Common.DTO.Severity.Information,
                    wrapper.OrgContext);
                break;
            }

            return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>()
            {
                new LocalizedString()
                {
                    Lang = "en", Text = "unknown situation"
                }
            }, null));
        }