public Connection GetWorkflowClientConnection()
        {
            if (_workflowClientConnection == null)
            {
                lock (_wfconnectionlock)
                {
                    if (_workflowClientConnection == null)
                    {
                        _workflowClientConnection = new Connection();
                        try
                        {
                            var connectionSetup = new ConnectionSetup();
                            connectionSetup.ParseConnectionString(_workflowConnectionString);
                            _workflowClientConnection.Open(connectionSetup);
                        }
                        catch (Exception ex)
                        {
                            if (_workflowClientConnection != null)
                            {
                                _workflowClientConnection.Dispose();
                            }

                            throw new Exception("Failed to create Connection to K2.", ex);
                        }
                    }
                }
            }
            return _workflowClientConnection;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get Workflow Client with impersonate user
        /// </summary>
        /// <returns>The worklist connection to K2 Workflow server</returns>
        public Connection GetWorkflowClient()
        {
            ConnectionStringSettings setting = ConfigurationManager.ConnectionStrings["WorkflowServer"];

            if (_userAccount == null)
            {
                return(null);
            }

            if (setting == null)
            {
                return(null);
            }

            if (string.IsNullOrEmpty(setting.ConnectionString))
            {
                return(null);
            }

            ConnectionSetup connectionSetup = new ConnectionSetup();

            connectionSetup.ParseConnectionString(setting.ConnectionString);
            Connection connection = new Connection();

            connection.Open(connectionSetup);
            if (connection.User == null || string.Compare(connection.User.Name, _userAccount, StringComparison.OrdinalIgnoreCase) != 0)
            {
                connection.ImpersonateUser(_userAccount);
            }
            return(connection);
        }
Exemplo n.º 3
0
        public K2Client(string identity)
        {
            this.ManagedUser = identity.FQNWithK2Label();

            if (_connection == null)
            {
                var setup   = new ConnectionSetup();
                var setting = ConfigurationManager.ConnectionStrings["WorkflowServer"];
                setup.ParseConnectionString(setting.ConnectionString);
                _connection = new Connection();
                _connection.Open(setup);
                if (_connection.User == null || string.Compare(_connection.User.Name, ManagedUser, StringComparison.OrdinalIgnoreCase) != 0)
                {
                    _connection.ImpersonateUser(ManagedUser);
                }
            }
        }
Exemplo n.º 4
0
        public K2Connection(IServiceMarshalling serviceMarshalling, IServerMarshaling serverMarshaling)
        {
            // These values are static because they won't change on the server.
            // This code will only execute once after K2HostServer starts
            if (string.IsNullOrEmpty(defaultWorkflowServerConnectionString) || workflowServerPort == 0)
            {
                defaultWorkflowServerConnectionString = ConfigurationManager.ConnectionStrings["WorkflowServer"].ConnectionString;

                ConnectionSetup workflowConnectionSetup = new ConnectionSetup();
                workflowConnectionSetup.ParseConnectionString(defaultWorkflowServerConnectionString);

                workflowServerPort = int.Parse(workflowConnectionSetup.ConnectionParameters[SourceCode.Workflow.Client.ConnectionSetup.ParamKeys.Port]);
            }

            sessionManager = serverMarshaling.GetSessionManagerContext();

            var sessionCookie = SessionManager.CurrentSessionCookie;

            sessionConnectionString = K2NEServiceBroker.SecurityManager.GetSessionConnectionString(sessionCookie);
        }
        public ContinuationAction MessageReceived(ListenerContext e)
        {
            // You can inherit from MessageExtendedInformation if you wish,
            // the likely scenario for this is if you support plugins yourself.
            var extended      = e.ReceivedInformation.Message.GetExtendedInformation <MessageExtendedInformation>();
            var replyExtended = extended.Invert(null);

            string workflowName;

            if (extended.Message.Title.StartsWith("START: ", StringComparison.OrdinalIgnoreCase))
            {
                workflowName = extended.Message.Title.Substring(7).Trim();
            }
            else
            {
                return(ContinuationAction.Continue); // Allow other plugins to execute.
            }

            // Get the message body.
            string body;

            try
            {
                using (var bodyStream = e.ReceivedInformation.Message.OpenView(new ContentType("text/plain")))
                {
                    if (bodyStream != null)
                    {
                        // Another plugin may have moved the position within the stream.
                        bodyStream.Seek(0, System.IO.SeekOrigin.Begin);
                        using (var sr = new StreamReader(bodyStream))
                        {
                            body = sr.ReadToEnd().Trim();
                        }
                    }
                    else
                    {
                        body = string.Empty;
                    }
                }
            }
            catch
            {
                // TODO: Logging etc.
                return(ContinuationAction.Continue); // Allow other plugins to execute.
            }

            // TODO: Get data from the body somehow.
            // You can also look into attachments for e.g. InfoPath forms:
            // e.ReceivedInformation.Message.Attachments

            try
            {
                var con = new Connection();
                var cs  = new ConnectionSetup();
                cs.ParseConnectionString(_k2ServerConnectionString);

                try
                {
                    Exception lastException = null;
                    con.Open(cs);

                    // AlternateIdentities are identities with the same email
                    // address, most likely due badly configured claims.
                    for (var i = 0; i < e.AlternateIdentities.Length; i++)
                    {
                        var    alt = e.AlternateIdentities[i];
                        string fqn;

                        // Search for a FQN in the identity.
                        if (alt.TryGetValue("Fqn", out fqn))
                        {
                            try
                            {
                                con.RevertUser();
                                con.ImpersonateUser(fqn);

                                var pi = con.CreateProcessInstance(workflowName);
                                // TODO: Set data in the workflow.
                                con.StartProcessInstance(pi);

                                // Tell the user the workflow was started.
                                _destination.ReplyTo(e.ReceivedInformation.Message, new MessageBodyReader("text/plain", "Workflow started"), replyExtended);

                                e.ReceivedInformation.Commit();  // Indicate we were able to handle the message.
                                return(ContinuationAction.Halt); // Stop other plugins from executing.
                            }
                            catch (Exception ex)
                            {
                                // TODO: Logging etc.
                                // This isn't nessecarily a complete failure,
                                // one of the other alternate identities may be
                                // able to action this.
                                lastException = ex;
                            }
                        }
                    }

                    string message;
                    if (lastException != null)
                    {
                        // Identities exist, but the user likely doesn't have rights.
                        message = lastException.ToString();
                    }
                    else
                    {
                        // No identities exist.
                        message = "Could not find a K2 user for your email address.";
                    }

                    message = "The workflow could not be started: " + message;

                    // Respond with the error.
                    _destination.ReplyTo(e.ReceivedInformation.Message, new MessageBodyReader("text/plain", message), replyExtended);

                    e.ReceivedInformation.Commit();  // Indicate we were able to handle the message.
                    return(ContinuationAction.Halt); // Stop other plugins from executing.
                }
                finally
                {
                    if (con != null)
                    {
                        con.Close();
                        con.Dispose();
                    }
                }
            }
            catch
            {
                // TODO: Logging etc.
                return(ContinuationAction.Continue); // Allow other plugins to execute.
            }
        }
        public ContinuationAction MessageReceived(ListenerContext e)
        {
            // You can inherit from MessageExtendedInformation if you wish,
            // the likely scenario for this is if you support plugins yourself.
            var extended = e.ReceivedInformation.Message.GetExtendedInformation<MessageExtendedInformation>();
            var replyExtended = extended.Invert(null);

            string workflowName;
            if (extended.Message.Title.StartsWith("START: ", StringComparison.OrdinalIgnoreCase))
            {
                workflowName = extended.Message.Title.Substring(7).Trim();
            }
            else
            {
                return ContinuationAction.Continue; // Allow other plugins to execute.
            }

            // Get the message body.
            string body;
            try
            {
                using (var bodyStream = e.ReceivedInformation.Message.OpenView(new ContentType("text/plain")))
                {
                    if (bodyStream != null)
                    {
                        // Another plugin may have moved the position within the stream.
                        bodyStream.Seek(0, System.IO.SeekOrigin.Begin);
                        using (var sr = new StreamReader(bodyStream))
                        {
                            body = sr.ReadToEnd().Trim();
                        }
                    }
                    else
                    {
                        body = string.Empty;
                    }
                }
            }
            catch
            {
                // TODO: Logging etc.
                return ContinuationAction.Continue; // Allow other plugins to execute.
            }

            // TODO: Get data from the body somehow.
            // You can also look into attachments for e.g. InfoPath forms:
            // e.ReceivedInformation.Message.Attachments

            try
            {
                var con = new Connection();
                var cs = new ConnectionSetup();
                cs.ParseConnectionString(_k2ServerConnectionString);

                try
                {
                    Exception lastException = null;
                    con.Open(cs);

                    // AlternateIdentities are identities with the same email
                    // address, most likely due badly configured claims.
                    for (var i = 0; i < e.AlternateIdentities.Length; i++)
                    {
                        var alt = e.AlternateIdentities[i];
                        string fqn;

                        // Search for a FQN in the identity.
                        if (alt.TryGetValue("Fqn", out fqn))
                        {
                            try
                            {
                                con.RevertUser();
                                con.ImpersonateUser(fqn);

                                var pi = con.CreateProcessInstance(workflowName);
                                // TODO: Set data in the workflow.
                                con.StartProcessInstance(pi);

                                // Tell the user the workflow was started.
                                _destination.ReplyTo(e.ReceivedInformation.Message, new MessageBodyReader("text/plain", "Workflow started"), replyExtended);

                                e.ReceivedInformation.Commit(); // Indicate we were able to handle the message.
                                return ContinuationAction.Halt; // Stop other plugins from executing.
                            }
                            catch (Exception ex)
                            {
                                // TODO: Logging etc.
                                // This isn't nessecarily a complete failure,
                                // one of the other alternate identities may be
                                // able to action this.
                                lastException = ex;
                            }
                        }
                    }

                    string message;
                    if (lastException != null)
                    {
                        // Identities exist, but the user likely doesn't have rights.
                        message = lastException.ToString();
                    }
                    else
                    {
                        // No identities exist.
                        message = "Could not find a K2 user for your email address.";
                    }

                    message = "The workflow could not be started: " + message;

                    // Respond with the error.
                    _destination.ReplyTo(e.ReceivedInformation.Message, new MessageBodyReader("text/plain", message), replyExtended);

                    e.ReceivedInformation.Commit(); // Indicate we were able to handle the message.
                    return ContinuationAction.Halt; // Stop other plugins from executing.
                }
                finally
                {
                    if (con != null)
                    {
                        con.Close();
                        con.Dispose();
                    }
                }
            }
            catch
            {
                // TODO: Logging etc.
                return ContinuationAction.Continue; // Allow other plugins to execute.
            }
        }