Beispiel #1
0
        /// <summary>
        /// 供代理服务使用
        /// </summary>
        public static void DelegateWorkList()
        {
            try
            {
                //删除已经过期的代理规则
                K2DBHelper.DeleteExpiredDelegations();

                //获取有效的代理规则
                DataTable DelegationRules = K2DBHelper.GetActiveDelegations().Tables[0];

                if (DelegationRules != null && DelegationRules.Rows.Count > 0)
                {
                    using (Connection conn = new Connection())
                    {
                        try
                        {
                            ConnectionSetup conSetup = GetConnectionSetup();
                            conn.Open(conSetup);

                            foreach (DataRow DelegateRule in DelegationRules.Rows)
                            {
                                //获取被代理人的WorkList
                                conn.ImpersonateUser(DelegateRule["FromUserAD"].ToString());
                                Worklist delegateFromWorkList = conn.OpenWorklist();

                                if (delegateFromWorkList != null && delegateFromWorkList.Count > 0)
                                {
                                    foreach (WorklistItem listItem in delegateFromWorkList)
                                    {
                                        //选择状态为Available和Open状态的任务
                                        if (listItem.Status == WorklistStatus.Available || listItem.Status == WorklistStatus.Open)
                                        {
                                            //获取该任务的主流程实例ID
                                            int parentProcInstID = K2DBHelper.GetRootParentsID(listItem.ProcessInstance.ID);
                                            //获取该任务的主流程实例
                                            ProcessInstance inst = conn.OpenProcessInstance(parentProcInstID);
                                            //代理规则中设置的主流程的名称
                                            string delagetionType = DelegateRule["ProcessFullName"].ToString();
                                            //如果代理规则设置的流程名称为All或等主流程的名称
                                            if (delagetionType.Equals("ALL", StringComparison.InvariantCultureIgnoreCase) || delagetionType.Equals(inst.FullName, StringComparison.InvariantCultureIgnoreCase))
                                            {
                                                bool   ContainsTargetUser = false;
                                                string targetUserSL       = SecurityLabelName + ":" + DelegateRule["ToUserAD"].ToString();

                                                //重置任务状态为Available,只有Available状态下的任务才有权限代理
                                                listItem.Release();
                                                //判断该任务是否已经代理给targetUser
                                                foreach (Destination destuser in listItem.DelegatedUsers)
                                                {
                                                    if (destuser.Name.Equals(targetUserSL, StringComparison.InvariantCultureIgnoreCase))
                                                    {
                                                        ContainsTargetUser = true;
                                                        break;
                                                    }
                                                }
                                                if (!ContainsTargetUser)
                                                {
                                                    //如果该任务是其它人代理给sourceUser的,则sourceUser不能再次代理给targetUser,即禁止传递代理
                                                    DataTable DelegationLog = K2DBHelper.GetDelegationsLog(listItem.SerialNumber).Tables[0];
                                                    foreach (DataRow dr in DelegationLog.Rows)
                                                    {
                                                        if (DelegateRule["FromUserAD"].ToString().Equals(dr["ToUser"].ToString(), StringComparison.InvariantCultureIgnoreCase))
                                                        {
                                                            ContainsTargetUser = true;
                                                            break;
                                                        }
                                                    }
                                                }
                                                if (!ContainsTargetUser)
                                                {
                                                    Destination dest = new Destination();
                                                    dest.DestinationType = DestinationType.User;
                                                    for (int i = 0; i < listItem.Actions.Count; i++)
                                                    {
                                                        dest.AllowedActions.Add(listItem.Actions[i].Name);
                                                    }
                                                    dest.Name = targetUserSL;
                                                    //任务代理
                                                    listItem.Delegate(dest);

                                                    //Todo:给代理人邮件

                                                    //Todo:添加代理历史记录
                                                    K2DBHelper.AddDelegationsLog(listItem.ProcessInstance.ID, parentProcInstID, listItem.SerialNumber, DelegateRule["FromUserAD"].ToString(), DelegateRule["ToUserAD"].ToString(), "Server");
                                                }
                                            }
                                        }
                                    }
                                }
                                conn.RevertUser();
                            }
                        }
                        catch
                        {
                            // TODO: throw?
                            throw;
                        }
                        finally
                        {
                            try
                            {
                                conn.RevertUser();
                            }
                            catch
                            {
                                throw;
                            }

                            if (conn != null)
                            {
                                conn.Close();
                            }
                        }
                    }
                }
            }
            catch
            {
                // TODO: throw?
                throw;
            }
        }