zoukankan      html  css  js  c++  java
  • 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
        }
    }

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

  • 相关阅读:
    decimal赋值
    "errcode":40001,"errmsg":"invalid credential, access_token is invalid or not latest hint: [d0tQ_02368635
    scope.row中属性值展示
    echarts.baidu 官网
    PowerDesigner使用
    解决 office2016 意外的严重错误 不能启动程序
    Jenkins 关闭和重启
    解决 weblogic poi3.9 报错 a different type with name "javax/xml/namespace/QName"
    WINDOWS7 下 xclient 802.1x 客户端 停止运行的解决办法
    前台进程转后台运行
  • 原文地址:https://www.cnblogs.com/shanyou/p/1603777.html
Copyright © 2011-2022 走看看