void btnSync_Click(object sender, EventArgs e) { this._domainName = ConfigurationManager.AppSettings["mmsDomainName"].ToString(); this._adminOfDC = ConfigurationManager.AppSettings["mmsAdminOfDC"].ToString(); this._pwdOfDC = ConfigurationManager.AppSettings["mmsPwdOfDC"].ToString(); this._nameOfRootOU = ConfigurationManager.AppSettings["mmsNameOfRootOU"].ToString(); this._domainAbbreviate = ConfigurationManager.AppSettings["mmsDomainAbbreviate"].ToString(); this._mmsRightGroup = ConfigurationManager.AppSettings["mmsRightGroup"].ToString(); using (MMSProDBDataContext db = new MMSProDBDataContext(ConfigurationManager.ConnectionStrings["mmsConString"].ConnectionString)) { using (DirectoryContext dc = new DirectoryContext(this._domainName, this._adminOfDC, this._pwdOfDC)) { if (db.EmpInfo.SingleOrDefault(emp => emp.Account.ToLower() == SPContext.Current.Web.CurrentUser.LoginName.ToLower()) == null) { ous = new List <DirectoryOrganizationalUnit>(); users = new List <DirectoryUser>(); ous = dc.OrganizationalUnits; users = dc.Users; noSyncDeps = new List <string>(); var rootOU = ous.SingleOrDefault(ou => ou.Name == this._nameOfRootOU); GetChildUser(rootOU); var rootUsers = users.Where(u => u.ParentGuid == rootOU.Guid); if (rootUsers != null && rootUsers.Count() > 0) { foreach (var curUser in rootUsers) { var eiTemp = db.EmpInfo.SingleOrDefault(u => u.Account == this._domainAbbreviate + "\\" + curUser.LogonName); #region if (eiTemp == null) //表示员工中不存在该员工才添加 { EmpInfo ei = new EmpInfo(); ei.Account = this._domainAbbreviate + "\\" + curUser.LogonName; ei.EmpName = curUser.DisplayName; var diTemp = db.DepInfo.SingleOrDefault(u => u.DepName == rootOU.Name); if (diTemp != null) //表示部门表中存在该部门才添加 { ei.DepID = diTemp.DepID; db.EmpInfo.InsertOnSubmit(ei); db.SubmitChanges(); //WSSRightHelper rightHelper = new WSSRightHelper(); //SPWeb rWeb = SPContext.Current.Web; //string addLoginName = this._domainAbbreviate + "\\" + curUser.LogonName; //if (rightHelper.IsExistGroup(rWeb, this._mmsRightGroup)) //{ // if (!rightHelper.IsExistUser(rWeb, addLoginName, this._mmsRightGroup)) // { // rightHelper.AddUserToGroup(rWeb, addLoginName, this._mmsRightGroup, curUser.Mail, curUser.DisplayName, curUser.Note); // } //} } else { noSyncDeps.Add(rootOU.Name);//记录未同步到系统中的部门 } } #endregion /*---------------------Begin 为了避免员工表中存在该员工时不能通步AD账户到Sharepoint权限里面-------------*/ WSSRightHelper rightHelper = new WSSRightHelper(); SPWeb rWeb = SPContext.Current.Web; string addLoginName = this._domainAbbreviate + "\\" + curUser.LogonName; if (rightHelper.IsExistGroup(rWeb, this._mmsRightGroup)) { if (!rightHelper.IsExistUser(rWeb, addLoginName, this._mmsRightGroup)) { rightHelper.AddUserToGroup(rWeb, addLoginName, this._mmsRightGroup, curUser.Mail, curUser.DisplayName, curUser.Note); } } /*---------------------End 为了避免员工表中存在该员工时不能通步AD账户到Sharepoint权限里面-------------*/ } } } else { ClientScript.RegisterClientScriptBlock(typeof(string), "ShowMessage", "<script>alert('您没有权利同步AD账户!')</script>"); return; } } } string arrNoSyncDeps = string.Empty; noSyncDeps = noSyncDeps.Distinct().ToList(); foreach (var no in noSyncDeps) { arrNoSyncDeps += no + ";"; } if (arrNoSyncDeps.Length != 0) { arrNoSyncDeps += "等部门"; ClientScript.RegisterClientScriptBlock(typeof(string), "ShowMessage", "<script>alert('" + arrNoSyncDeps + "没有从AD同步到系统,请先同步这些部门!');window.location.href='EmpManager.aspx';</script>"); return; } Response.Redirect("EmpManager.aspx"); }
/// <summary> /// 递归获取任意员工 /// </summary> /// <param name="parentOU">父OU</param> protected void GetChildUser(DirectoryOrganizationalUnit parentOU) { //string domainName = ConfigurationManager.AppSettings["mmsDomainName"].ToString(); //string adminOfDC = ConfigurationManager.AppSettings["mmsAdminOfDC"].ToString(); //string pwdOfDC = ConfigurationManager.AppSettings["mmsPwdOfDC"].ToString(); //string nameOfRootOU = ConfigurationManager.AppSettings["mmsNameOfRootOU"].ToString(); //string domainAbbreviate = ConfigurationManager.AppSettings["mmsDomainAbbreviate"].ToString(); var childOUs = ous.Where(u => u.ParentGuid == parentOU.Guid); EmpInfo ei; if (childOUs != null && childOUs.Count() > 0) { foreach (var curChildOU in childOUs)//遍历当前OU下的所有OU { using (MMSProDBDataContext db = new MMSProDBDataContext(ConfigurationManager.ConnectionStrings["mmsConString"].ConnectionString)) { var curChildUsers = users.Where(u => u.ParentGuid == curChildOU.Guid); if (curChildUsers != null && curChildUsers.Count() > 0) { using (DirectoryContext dc = new DirectoryContext(this._domainName, this._adminOfDC, this._pwdOfDC)) { foreach (var curUser in curChildUsers)//遍历curChildOU下的所有用户 { var eiTemp = db.EmpInfo.SingleOrDefault(u => u.Account == this._domainAbbreviate + "\\" + curUser.LogonName); if (eiTemp == null) //表示员工表中不存在该员工才添加 { ei = new EmpInfo(); ei.Account = this._domainAbbreviate + "\\" + curUser.LogonName; ei.EmpName = curUser.DisplayName; var diTemp = db.DepInfo.SingleOrDefault(u => u.DepName == curChildOU.Name); //根据组织单元的名称找到相应的部门 if (diTemp != null) //表示部门表中存在该部门才添加 { ei.DepID = diTemp.DepID; //把员工关联到相应部门 db.EmpInfo.InsertOnSubmit(ei); db.SubmitChanges(); //WSSRightHelper rightHelper = new WSSRightHelper(); //SPWeb rWeb = SPContext.Current.Web; //string addLoginName = this._domainAbbreviate + "\\" + curUser.LogonName; //if (rightHelper.IsExistGroup(rWeb, this._mmsRightGroup)) //{ // if (!rightHelper.IsExistUser(rWeb, addLoginName, this._mmsRightGroup)) // { // rightHelper.AddUserToGroup(rWeb, addLoginName, this._mmsRightGroup, curUser.Mail, curUser.DisplayName, curUser.Note); // } //} } else { noSyncDeps.Add(curChildOU.Name);//记录未同步到系统中的部门 } } /*---------------------Begin 为了避免员工表中存在该员工时不能通步AD账户到Sharepoint权限里面-------------*/ WSSRightHelper rightHelper = new WSSRightHelper(); SPWeb rWeb = SPContext.Current.Web; string addLoginName = this._domainAbbreviate + "\\" + curUser.LogonName; if (rightHelper.IsExistGroup(rWeb, this._mmsRightGroup)) { if (!rightHelper.IsExistUser(rWeb, addLoginName, this._mmsRightGroup)) { rightHelper.AddUserToGroup(rWeb, addLoginName, this._mmsRightGroup, curUser.Mail, curUser.DisplayName, curUser.Note); } } /*---------------------End 为了避免员工表中存在该员工时不能通步AD账户到Sharepoint权限里面-------------*/ } } } GetChildUser(curChildOU); //递归遍历curChildOU下的所有OU(部门),从而递归遍历所有用户(员工) } } } }