其他分享
首页 > 其他分享> > Gitblit同步企业EMS人员信息实现自动删除离职人员的帐号

Gitblit同步企业EMS人员信息实现自动删除离职人员的帐号

作者:互联网

需求:


临近春节,整理了下Gitblit代码库的人员信息,发现好多离职人员,按照常理来讲,人员离职人事部门需要通知我一下收回堡垒机、代码库权限的,但是经常会忘记,所以想跳过她们,直接联系EMS系统的开发同事开接口,根据EMS的人员信息直接就删掉代码库账号了。

思路:


  1. 获取企业EMS系统人员信息List
  2. 获取Gitblit人员信息List
  3. 比较两个List,得到已离职人员
  4. 删除离职人员

实现:


  1. 获取企业EMS系统人员信息,此处就不讲了,各个企业不一样,找相应的开发人员要接口就行了
  2. 获取Gitblit人员信息,主要是调用了下其接口,总共分两步

官网文档:接口地址
admin、password必须为gitblit的管理员帐号密码
调用接口需要设置Gitblit中的web.enableRpcManagement=true,接口文档中有提到,改完需重启服务

       /// <summary>
       /// 模拟登陆gitblit获取cookie
       /// </summary>
       /// <param name="cookie"></param>
       internal static CookieContainer GetCookie()
       {
           //登陆参数
           string loginStr = $"wicket:bookmarkablePage=:com.gitblit.wicket.pages.MyDashboardPage&id1_hf_0=&username={admin}&password={password}";
           //登陆
           CookieContainer cookie = new CookieContainer();
           HttpWebRequest httpRequset = (HttpWebRequest)HttpWebRequest.Create($"{host}/?wicket:interface=:0:userPanel:loginForm::IFormSubmitListener::");
           httpRequset.CookieContainer = cookie;
           httpRequset.Method = "POST";
           httpRequset.KeepAlive = true;
           httpRequset.Headers.Add("Accept-Encoding", "gzip, deflate");
           httpRequset.Headers.Add("Accept-Language", "zh-CN,zh;q=0.9,zh-TW;q=0.8,en-US;q=0.7,en;q=0.6");
           httpRequset.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36";
           httpRequset.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9";
           httpRequset.ContentType = "application/x-www-form-urlencoded";
           byte[] bytes = Encoding.UTF8.GetBytes(loginStr);
           httpRequset.ContentLength = bytes.Length;
           Stream stream = httpRequset.GetRequestStream();
           stream.Write(bytes, 0, bytes.Length);
           stream.Close();
           HttpWebResponse response = (HttpWebResponse)httpRequset.GetResponse();
           using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
           {
               var strWebData = reader.ReadToEnd();
           }
           httpRequset.Abort();
           return cookie;
       }
        /// <summary>
        /// 获取gitblit中的用户信息
        /// </summary>
        /// <param name="cookie"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        internal static List<GitblitUsers> GetGitblitUsers(CookieContainer cookie)
        {
            HttpWebRequest httpRequset = (HttpWebRequest)HttpWebRequest.Create($"{host}/rpc/?req=LIST_USERS");
            httpRequset.CookieContainer = cookie;
            httpRequset.Method = "GET";
            httpRequset.KeepAlive = true;
            httpRequset.UserAgent = "Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14";
            httpRequset.Accept = "text/html, application/xhtml+xml, */*";
            httpRequset.ContentType = "application/x-www-form-urlencoded";
            HttpWebResponse httpResponse = (HttpWebResponse)httpRequset.GetResponse();
            Stream resStream = httpResponse.GetResponseStream();
            string json = new StreamReader(resStream, System.Text.Encoding.UTF8).ReadToEnd();
            List<GitblitUsers> list = JsonConvert.DeserializeObject<List<GitblitUsers>>(json);
            return list;
        }

其中GitblitUsers类定义如下,这边username我设置的是工号(来源自EMS),显示名称是人员姓名

    public class GitblitUsers
    {
        /// <summary>
        /// 帐号
        /// </summary>
        public string username { get; set; }
        /// <summary>
        /// 显示名称
        /// </summary>
        public string displayName { get; set; }
    }
  1. 比较List获取已离职人员,基础操作不多说

  2. 删除离职人员帐号,依旧调Gitblit接口

  1. 删除前先发个企业微信通知给自己 sendTo为需要接收人的企业微信号
  2. 模型转换是接口文档中post body里要求的
  3. host为gitblit的地址
        /// <summary>
        /// 删除代码库中已离职人员
        /// </summary>
        /// <param name="member"></param>
        /// <param name="cookie"></param>
        internal static void DeleteMemberHasQuit(EMSUsers member, CookieContainer cookie)
        {
            //防止误删,先通知下企业微信
            SendToWechat.SendText($"人员:{member.EmployeeName},工号:{member.EmployeeNo}已离职,即将删除代码库相关权限", sendTo);
            //模型转换
            UserModel model = new UserModel()
            {
                username = member.EmployeeNo,
            };

            HttpWebRequest httpRequset = (HttpWebRequest)HttpWebRequest.Create($"{host}/rpc/?req=DELETE_USER&name={member.EmployeeNo}");
            httpRequset.CookieContainer = cookie;
            httpRequset.Method = "POST";
            httpRequset.KeepAlive = true;
            httpRequset.UserAgent = "Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14";
            httpRequset.ContentType = "application/json";
            byte[] bytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(model));
            httpRequset.ContentLength = bytes.Length;
            Stream stream = httpRequset.GetRequestStream();
            stream.Write(bytes, 0, bytes.Length);
            stream.Close();
            try
            {
                HttpWebResponse response = (HttpWebResponse)httpRequset.GetResponse();
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    SendToWechat.SendText($"删除人员:{member.EmployeeName},工号:{member.EmployeeNo}失败", sendTo);
                }
            }
            catch (Exception e)
            {
                SendToWechat.SendText($"删除人员:{member.EmployeeName},工号:{member.EmployeeNo}失败,错误信息:{e.Message}", sendTo);
            }
            httpRequset.Abort();
        }

其中UserModel类定义入下

    public class UserModel
    {
        public string username { get; set; }
        //public string password { get; set; }
        //public bool canAdmin { get; set; }
        //public bool excludeFromFederation { get; set; }
        //public string[] repositories { get; set; }
    }

结束:


至此,一个简单的运维小功能结束,挂个定时任务定期跑一跑

标签:Gitblit,CookieContainer,member,httpRequset,EMS,HttpWebRequest,人员,public
来源: https://www.cnblogs.com/jokker/p/15849994.html