• Blackpearl 的 Impersonate


    Blackpearl的Connection方法 Impersonate(string name)。这个就是传说中的后门方法,它可以帮你模拟任何一个帐号(域用户或者数据库用户),来做他可以做得事情。当然,你必须在WorkSpace中设置操作这个命令的权限。

    以下代码将给用户设置Impersonate权限,包装成一个Job来定时运行就可以了:

                   WorkflowManagementServer managementServer = new WorkflowManagementServer();
                    managementServer.CreateConnection();
                    managementServer.Connection.Open(GetConnectionString(dataMap));
                    AdminPermissions adminPerms = managementServer.GetAdminPermissions();
                    AdminPermissions newAdminPerms = new AdminPermissions();
                    StaffCollection staffs = Staff.GetAllStaff();
                    foreach (AdminPermission item in adminPerms)
                    {
                        var staffName = item.UserName.Replace("K2SQL:","").Trim();
                        Staff staff = staffs.Find(t => t.EnglishName == staffName );
                        if (staff != null)
                        {
                            newAdminPerms.Add(item);
                            staffs.Remove(staff);
                        }
                        else
                        {
                            if (item.UserName.ToUpper().Contains("K2:TENCENT"))
                            {
                                newAdminPerms.Add(item);
                            }
                        }
                    }
                    foreach (var item in staffs)
                    {
                        AdminPermission adminPermission = new AdminPermission();
                        adminPermission.CanImpersonate = true;
                        adminPermission.UserName = string.Format("K2SQL:{0}", item.EnglishName);
                        newAdminPerms.Add(adminPermission);
                    }
                    managementServer.UpdateAdminUsers(newAdminPerms);

    Impersonate方法的调用可以参照K2 blackpearl Client Impersonation - Suggested Design Pattern包装后使用,下面是我的包装代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SourceCode.Workflow.Client;
    using Tencent.OA.Framework.Workflow.Interfaces;

    namespace Tencent.OA.Framework.Workflow
    {
        /// <summary>
        /// <example>
        ///IK2Connection k2Conn = K2Factory.NewK2Connection("oa-flowdb");;
        ///using(Impersonation.Impersonate(userName, k2Conn)
        ///{
        ///    //Do whatever work you need to do as the impersonated user
        ///}
        /// </example>
        /// </summary>
        public class Impersonation : IDisposable
        {
            private IK2Connection _conn = null;

            private Impersonation(IK2Connection conn)
            {
                _conn = conn;
            }

            #region Impersonate Methods

            /// <summary>
            /// 模拟用户
            /// </summary>
            /// <param name="userName">用户英文名</param>
            /// <param name="conn">代表K2服务器链接</param>
            /// <returns></returns>
            public static Impersonation Impersonate(string userName, IK2Connection conn)
            {
                if (!userName.ToUpper().Contains("K2SQL"))
                {
                    userName = string.Format("K2SQL:{0}", userName);
                }
                conn.Connection.ImpersonateUser(userName);
                return new Impersonation(conn);
            }

            #endregion Impersonate Methods

            #region IDisposable Members
            /// <summary>
            /// 取消用户的模拟
            /// </summary>
            public void Dispose()
            {
                _conn.Connection.RevertUser();
            }

            #endregion
        }
    }

    欢迎大家扫描下面二维码成为我的客户,为你服务和上云

  • 相关阅读:
    IOS GameCenter验证登陆
    BZOJ 1597: [Usaco2008 Mar]土地购买 斜率优化
    Hihocoder #1602 : 本质不同的回文子串的数量 manacher + BKDRhash
    HDU 5343 MZL's Circle Zhou 后缀自动机+DP
    HDU 6208 The Dominator of Strings 后缀自动机
    SPOJ SUBLEX
    SPOJ LCS2
    .NET平台技术体系梳理+初学者学习路径推荐+我们的愿景与目标
    扑克模拟,牌型判断java版
    一道综合练习题实践list及dictionary集合类
  • 原文地址:https://www.cnblogs.com/shanyou/p/1603777.html
Copyright © 2020-2023  润新知