合并最新
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
using System;
|
||||
using System.Timers;
|
||||
using System.DirectoryServices;
|
||||
|
||||
namespace BLL
|
||||
{
|
||||
public class ADDataInService
|
||||
{
|
||||
#region 启动监视器 系统启动5分钟
|
||||
/// <summary>
|
||||
/// 监视组件
|
||||
/// </summary>
|
||||
private static Timer messageTimer;
|
||||
|
||||
/// <summary>
|
||||
/// 启动监视器,不一定能成功,根据系统设置决定对监视器执行的操作 系统启动5分钟
|
||||
/// </summary>
|
||||
public static void StartMonitor()
|
||||
{
|
||||
var adomain = ADomainService.getADomain();
|
||||
if (adomain != null && adomain.Intervaltime.HasValue)
|
||||
{
|
||||
int adTimeJ = adomain.Intervaltime ?? 600;
|
||||
if (messageTimer != null)
|
||||
{
|
||||
messageTimer.Stop();
|
||||
messageTimer.Dispose();
|
||||
messageTimer = null;
|
||||
}
|
||||
|
||||
messageTimer = new Timer
|
||||
{
|
||||
AutoReset = true
|
||||
};
|
||||
messageTimer.Elapsed += new ElapsedEventHandler(AdUserInProcess);
|
||||
|
||||
messageTimer.Interval = 60000 * adTimeJ;
|
||||
messageTimer.Start();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 流程确认 定时执行 系统启动5分钟
|
||||
/// </summary>
|
||||
/// <param name="sender">Timer组件</param>
|
||||
/// <param name="e">事件参数</param>
|
||||
private static void AdUserInProcess(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
if (messageTimer != null)
|
||||
{
|
||||
messageTimer.Stop();
|
||||
}
|
||||
|
||||
BLL.ADomainService.ADomainUserIn();
|
||||
|
||||
if (messageTimer != null)
|
||||
{
|
||||
messageTimer.Dispose();
|
||||
messageTimer = null;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 启动监视器 定时0:05执行
|
||||
/// <summary>
|
||||
/// 监视组件
|
||||
/// </summary>
|
||||
private static Timer messageTimerEve;
|
||||
|
||||
/// <summary>
|
||||
/// 启动监视器,不一定能成功,根据系统设置决定对监视器执行的操作 定时
|
||||
/// </summary>
|
||||
public static void StartMonitorEve()
|
||||
{
|
||||
if (messageTimerEve != null)
|
||||
{
|
||||
messageTimerEve.Stop();
|
||||
messageTimerEve.Dispose();
|
||||
messageTimerEve = null;
|
||||
}
|
||||
|
||||
messageTimerEve = new Timer
|
||||
{
|
||||
AutoReset = true
|
||||
};
|
||||
messageTimerEve.Elapsed += new ElapsedEventHandler(ColligateFormConfirmProcessEve);
|
||||
messageTimerEve.Interval = GetMessageTimerEveNextInterval();
|
||||
messageTimerEve.Start();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 流程确认 定时执行 定时00:05 执行
|
||||
/// </summary>
|
||||
/// <param name="sender">Timer组件</param>
|
||||
/// <param name="e">事件参数</param>
|
||||
private static void ColligateFormConfirmProcessEve(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (messageTimerEve != null)
|
||||
{
|
||||
messageTimerEve.Stop();
|
||||
}
|
||||
|
||||
BLL.ADomainService.ADomainUserIn();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ErrLogInfo.WriteLog("获取AD域", ex);
|
||||
}
|
||||
finally
|
||||
{
|
||||
messageTimerEve.Interval = GetMessageTimerEveNextInterval();
|
||||
messageTimerEve.Start();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 计算MessageTimerEve定时器的执行间隔
|
||||
/// </summary>
|
||||
/// <returns>执行间隔</returns>
|
||||
private static double GetMessageTimerEveNextInterval()
|
||||
{
|
||||
double returnValue = 0;
|
||||
TimeSpan curentTime = DateTime.Now.TimeOfDay;
|
||||
int hour = 10;
|
||||
//if (!String.IsNullOrEmpty(Funs.AdTimeD))
|
||||
//{
|
||||
// hour = int.Parse(Funs.AdTimeD);
|
||||
//}
|
||||
|
||||
TimeSpan triggerTime = new TimeSpan(hour, 07, 0);
|
||||
if (curentTime > triggerTime)
|
||||
{
|
||||
// 超过了执行时间
|
||||
returnValue = (new TimeSpan(23, 59, 59) - curentTime + triggerTime.Add(new TimeSpan(0, 0, 1))).TotalMilliseconds;
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = (triggerTime - curentTime).TotalMilliseconds;
|
||||
}
|
||||
|
||||
if (returnValue <= 0)
|
||||
{
|
||||
// 误差纠正
|
||||
returnValue = 1;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
namespace BLL
|
||||
{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web.UI.WebControls;
|
||||
using System.Collections;
|
||||
using System.Timers;
|
||||
using System.DirectoryServices;
|
||||
|
||||
public class ADomainService
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取AD域信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Model.Sys_ADomain getADomain()
|
||||
{
|
||||
return Funs.DB.Sys_ADomain.FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除AD域信息
|
||||
/// </summary>
|
||||
/// <param name="FlowSetId"></param>
|
||||
public static void DeleteADomain()
|
||||
{
|
||||
var aDomain = from x in Funs.DB.Sys_ADomain select x;
|
||||
if (aDomain.Count() > 0)
|
||||
{
|
||||
Funs.DB.Sys_ADomain.DeleteAllOnSubmit(aDomain);
|
||||
Funs.DB.SubmitChanges();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 增加AD域设置信息
|
||||
/// </summary>
|
||||
/// <param name="FlowProjectSetName"></param>
|
||||
/// <param name="def"></param>
|
||||
public static void AddADomain(Model.Sys_ADomain aDomain)
|
||||
{
|
||||
using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
|
||||
{
|
||||
aDomain.ADomainId = SQLHelper.GetNewID();
|
||||
db.Sys_ADomain.InsertOnSubmit(aDomain);
|
||||
db.SubmitChanges();
|
||||
}
|
||||
}
|
||||
|
||||
#region 是否连接到域
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="domainName">域名或IP</param>
|
||||
/// <param name="userName">用户名</param>
|
||||
/// <param name="userPwd">密码</param>
|
||||
/// <param name="entry">域</param>
|
||||
/// <returns></returns>
|
||||
public static DirectoryEntry IsConnected(string domainName, string userName, string userPwd)
|
||||
{
|
||||
DirectoryEntry domain = new DirectoryEntry();
|
||||
try
|
||||
{
|
||||
domain.Path = string.Format("LDAP://{0}", domainName);
|
||||
domain.Username = userName;
|
||||
domain.Password = userPwd;
|
||||
domain.AuthenticationType = AuthenticationTypes.Secure;
|
||||
domain.RefreshCache();
|
||||
return domain;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
BLL.ErrLogInfo.WriteLog(ex.ToString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 域中是否存在组织单位
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="entry"></param>
|
||||
/// <param name="ou"></param>
|
||||
/// <returns></returns>
|
||||
public static DirectoryEntry IsExistOU(DirectoryEntry entry, string domainName, string domainOU)
|
||||
{
|
||||
DirectoryEntry ou = new DirectoryEntry();
|
||||
try
|
||||
{
|
||||
string[] ouItem = domainOU.Split(new string[1] { "/" }, StringSplitOptions.RemoveEmptyEntries);
|
||||
foreach (var item in ouItem)
|
||||
{
|
||||
if (!item.Equals(domainName, StringComparison.OrdinalIgnoreCase) && !item.Equals(domainName + ".com", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
entry = entry.Children.Find("OU=" + item);
|
||||
ou = entry;
|
||||
}
|
||||
}
|
||||
|
||||
return ou;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
BLL.ErrLogInfo.WriteLog(ex.ToString());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// 导入AD域用户
|
||||
/// </summary>
|
||||
public static void ADomainUserIn()
|
||||
{
|
||||
var adomain = ADomainService.getADomain();
|
||||
if (adomain != null)
|
||||
{
|
||||
DirectoryEntry domain = BLL.ADomainService.IsConnected(adomain.DomainName, adomain.UserName, adomain.Password);
|
||||
if (domain != null)
|
||||
{
|
||||
DirectoryEntry rootOU = BLL.ADomainService.IsExistOU(domain, adomain.DomainName, adomain.RootOU);
|
||||
if (rootOU != null)
|
||||
{
|
||||
SyncAll(rootOU);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#region 同步所有事件
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="entryOU"></param>
|
||||
public static void SyncAll(DirectoryEntry entryOU)
|
||||
{
|
||||
DirectorySearcher mySearcher = new DirectorySearcher(entryOU, "(objectClass=organizationalUnit)"); //查询组织单位
|
||||
DirectoryEntry root = mySearcher.SearchRoot; //查找根OU
|
||||
SyncRootOU(root);
|
||||
foreach (var item in list)
|
||||
{
|
||||
Model.Sys_User user = new Model.Sys_User();
|
||||
user.Account = item.Account;
|
||||
user.UserCode = item.UserCode;
|
||||
user.Password = item.Password;
|
||||
user.UserName = item.UserName;
|
||||
if (!String.IsNullOrEmpty(item.RoleName))
|
||||
{
|
||||
var role = BLL.RoleService.getRoleByName(item.RoleName);
|
||||
if (role != null)
|
||||
{
|
||||
user.RoleId = role.RoleId;
|
||||
user.IsOffice = true;
|
||||
}
|
||||
}
|
||||
|
||||
user.IsPost = item.IsPost;
|
||||
if (!String.IsNullOrEmpty(item.UnitName))
|
||||
{
|
||||
var unit = BLL.UnitService.getUnitByUnitName(item.UnitName);
|
||||
if (unit != null)
|
||||
{
|
||||
user.UnitId = unit.UnitId;
|
||||
}
|
||||
}
|
||||
if (!String.IsNullOrEmpty(item.DepartName))
|
||||
{
|
||||
var dep = BLL.DepartService.getDepartByDepartName(item.DepartName);
|
||||
if (dep != null)
|
||||
{
|
||||
user.DepartId = dep.DepartId;
|
||||
}
|
||||
}
|
||||
////根据登录名查询用户信息
|
||||
var userSelect = BLL.UserService.GetUserByAccount(item.Account);
|
||||
if (userSelect == null) ///不存在则增加
|
||||
{
|
||||
BLL.UserService.AddUser(user);
|
||||
}
|
||||
else
|
||||
{
|
||||
////存在则更新
|
||||
//user.UserId = userSelect.UserId;
|
||||
//user.UserCode = userSelect.UserCode;
|
||||
//user.Password = userSelect.Password;
|
||||
//user.RoleId = userSelect.RoleId;
|
||||
//user.UnitId = userSelect.UnitId;
|
||||
//user.DepartId = userSelect.DepartId;
|
||||
//BLL.UserService.UpdateUser(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 同步根组织单位
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="entry"></param>
|
||||
public static void SyncRootOU(DirectoryEntry entry)
|
||||
{
|
||||
if (entry.Properties.Contains("ou") && entry.Properties.Contains("objectGUID"))
|
||||
{
|
||||
byte[] bGUID = entry.Properties["objectGUID"][0] as byte[];
|
||||
string id = BitConverter.ToString(bGUID);
|
||||
SyncSubOU(entry, id);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 同步下属组织单位及下属用户
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
/// <param name="entry"></param>
|
||||
/// <param name="parentId"></param>
|
||||
public static void SyncSubOU(DirectoryEntry entry, string parentId)
|
||||
{
|
||||
foreach (DirectoryEntry subEntry in entry.Children)
|
||||
{
|
||||
string entrySchemaClsName = subEntry.SchemaClassName;
|
||||
|
||||
string[] arr = subEntry.Name.Split('=');
|
||||
string categoryStr = arr[0];
|
||||
string nameStr = arr[1];
|
||||
string id = string.Empty;
|
||||
|
||||
if (subEntry.Properties.Contains("objectGUID")) //SID
|
||||
{
|
||||
byte[] bGUID = subEntry.Properties["objectGUID"][0] as byte[];
|
||||
|
||||
id = BitConverter.ToString(bGUID);
|
||||
}
|
||||
|
||||
bool isExist = list.Exists(d => d.Id == id);
|
||||
|
||||
switch (entrySchemaClsName)
|
||||
{
|
||||
case "organizationalUnit":
|
||||
SyncSubOU(subEntry, id);
|
||||
break;
|
||||
case "user":
|
||||
string account = string.Empty;
|
||||
string userCode = string.Empty;
|
||||
string password = BLL.Funs.EncryptionPassword(BLL.Const.Password);
|
||||
string userName = string.Empty;
|
||||
string roleId = string.Empty;
|
||||
bool isPost = true;
|
||||
string unitId = string.Empty;
|
||||
string department = string.Empty;
|
||||
|
||||
if (subEntry.Properties.Contains("samaccountName"))
|
||||
{
|
||||
account = subEntry.Properties["samaccountName"][0].ToString();
|
||||
if (subEntry.Properties["initials"].Value != null)
|
||||
{
|
||||
userCode = subEntry.Properties["initials"].Value.ToString();
|
||||
}
|
||||
if (subEntry.Properties["displayName"].Value != null)
|
||||
{
|
||||
userName = subEntry.Properties["displayName"].Value.ToString();
|
||||
}
|
||||
//if (subEntry.Properties["title"].Value != null)
|
||||
//{
|
||||
// roleId = subEntry.Properties["title"].Value.ToString();
|
||||
//}
|
||||
if (subEntry.Properties["company"].Value != null)
|
||||
{
|
||||
unitId = subEntry.Properties["company"].Value.ToString();
|
||||
}
|
||||
if (subEntry.Properties["department"].Value != null)
|
||||
{
|
||||
department = subEntry.Properties["department"].Value.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
if (!isExist && !String.IsNullOrEmpty(account))
|
||||
{
|
||||
list.Add(new AdModel(id, account, userCode, password, userName, roleId, isPost, unitId, department));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Ad域实体
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static List<AdModel> list = new List<AdModel>();
|
||||
|
||||
/// <summary>
|
||||
/// Ad域实体
|
||||
/// </summary>
|
||||
public class AdModel
|
||||
{
|
||||
public AdModel(string id, string account, string userCode, string password, string userName,
|
||||
string roleName, bool? isPost, string unitName, string departName)
|
||||
{
|
||||
Id = id;
|
||||
Account = account;
|
||||
UserCode = userCode;
|
||||
Password = password;
|
||||
UserName = userName;
|
||||
RoleName = roleName;
|
||||
IsPost = isPost;
|
||||
UnitName = unitName;
|
||||
DepartName = departName;
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
public string Account { get; set; }
|
||||
public string UserCode { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string UserName { get; set; }
|
||||
public string RoleName { get; set; }
|
||||
public bool? IsPost { get; set; }
|
||||
public string UnitName { get; set; }
|
||||
public string DepartName { get; set; }
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user