// public override PluginConnectorBaseFetchResult FetchFields(Dictionary <String, Object> config) { PluginConnectorBaseFetchResult ret = new PluginConnectorBaseFetchResult(); LogEvent iLog = new LogEvent(delegate(Object sender, PluginLogType type, string text) { if (Log != null) { Log(sender, type, text); } }); if (!CheckInputConfig(config, true, iLog, true, true)) { ret.success = false; return(ret); } List <PluginConfigFields> cfg = new List <PluginConfigFields>(); PluginConfigFields[] tmpF = this.GetConfigFields(); foreach (PluginConfigFields cf in tmpF) { try { iLog(this, PluginLogType.Information, "Field " + cf.Name + " (" + cf.Key + "): " + (config.ContainsKey(cf.Key) ? config[cf.Key].ToString() : "empty")); } catch (Exception ex) { iLog(this, PluginLogType.Information, "Field " + cf.Name + " (" + cf.Key + "): error on get data -> " + ex.Message); } } try { WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent()); iLog(this, PluginLogType.Information, "Current user: "******"Has administrative right: " + pricipal.IsInRole(WindowsBuiltInRole.Administrator)); } catch { } try { LocalWindows lWin = new LocalWindows(config["server"].ToString(), config["username"].ToString(), config["password"].ToString()); try { lWin.Bind(); } catch (Exception ex) { iLog(this, PluginLogType.Error, "Error on connect to Windows '" + config["server"].ToString() + "': " + ex.Message + (ex.InnerException != null ? " " + ex.InnerException.Message : "")); lWin = null; ret.success = false; return(ret); } Log(this, PluginLogType.Information, "Successfully connected on " + config["server"].ToString()); Log(this, PluginLogType.Information, "Trying to list the users..."); Int32 count = 0; try { foreach (DirectoryEntry user in lWin.ListAllUsers()) { if (count >= 20) { break; } try { foreach (PropertyValueCollection property in user.Properties) { if (!ret.fields.ContainsKey(property.PropertyName)) { ret.fields.Add(property.PropertyName, new List <string>()); } //Separa os itens que mecessita algum tratamento switch (property.PropertyName.ToLower()) { default: foreach (Object p1 in property) { ret.fields[property.PropertyName].Add(p1.ToString()); } break; } } count++; } catch (Exception ex) { Log(this, PluginLogType.Error, "Erro ao importar o registro (" + user.Path + "): " + ex.Message); } } } catch (Exception ex) { Log(this, PluginLogType.Error, "Erro listar os usuários"); throw ex; } ret.success = true; } catch (Exception ex) { iLog(this, PluginLogType.Error, ex.Message); } return(ret); }
public override void ProcessImport(String cacheId, String importId, Dictionary <String, Object> config, List <PluginConnectorBaseDeployPackageMapping> fieldMapping) { if (!CheckInputConfig(config, true, Log)) { return; } List <String> prop = new List <String>(); try { LocalWindows lWin = new LocalWindows(config["server"].ToString(), config["username"].ToString(), config["password"].ToString()); try { lWin.Bind(); } catch (Exception ex) { Log2(this, PluginLogType.Error, 0, 0, "Error on connect to Windows '" + config["server"].ToString() + "': " + ex.Message + (ex.InnerException != null ? ex.InnerException.Message : ""), ""); lWin = null; return; } Log(this, PluginLogType.Information, "Successfully connected on " + config["server"].ToString()); Log(this, PluginLogType.Information, "Trying to list the users..."); foreach (DirectoryEntry user in lWin.ListAllUsers()) { PluginConnectorBaseImportPackageUser package = new PluginConnectorBaseImportPackageUser(importId); try { object obGroups = user.Invoke("Groups"); foreach (object ob in (IEnumerable)obGroups) { // Create object for each group. DirectoryEntry obGpEntry = new DirectoryEntry(ob); package.AddProperty("memberOf", obGpEntry.Name, (fieldMapping.Exists(f => (f.dataName == "memberOf")) ? fieldMapping.Find(f => (f.dataName == "memberOf")).dataType : "string")); //Registry(importId, regId, "memberOf", obGpEntry.Name, (fieldMapping.Exists(f => (f.dataName == "memberOf")) ? fieldMapping.Find(f => (f.dataName == "memberOf")).dataType : "string")); } foreach (String p in user.Properties.PropertyNames) { //Separa os itens que mecessita algum tratamento switch (p.ToLower()) { case "lastlogin": try { foreach (Object p1 in user.Properties[p]) { DateTime tmp2 = DateTime.Parse(p1.ToString()); if (tmp2.Year > 1970) //Se a data for inferior nem envia { package.AddProperty(p, tmp2.ToString("yyyy-MM-dd HH:mm:ss"), (fieldMapping.Exists(f => (f.dataName == p)) ? fieldMapping.Find(f => (f.dataName == p)).dataType : "string")); } } } catch (Exception ex) { } break; case "loginhours": break; case "objectsid": try { Byte[] tmp2 = (Byte[])user.Properties[p][0]; package.AddProperty(p, BitConverter.ToString(tmp2).Replace("-", ""), (fieldMapping.Exists(f => (f.dataName == p)) ? fieldMapping.Find(f => (f.dataName == p)).dataType : "string")); } catch (Exception ex) { } break; default: foreach (Object p1 in user.Properties[p]) { package.AddProperty(p, p1.ToString(), (fieldMapping.Exists(f => (f.dataName == p)) ? fieldMapping.Find(f => (f.dataName == p)).dataType : "string")); } break; } } ImportPackageUser(package); } catch (Exception ex) { Log(this, PluginLogType.Error, "Erro ao importar o registro (" + user.Path + "): " + ex.Message); } finally { package.Dispose(); package = null; } } } catch (Exception ex) { Log(this, PluginLogType.Error, ex.Message); } }