示例#1
0
        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
            {
                iLog(this, PluginLogType.Error, "Operação não implementada neste plugin");

                //if (!ret.fields.ContainsKey(property.PropertyName))
                //    ret.fields.Add(property.PropertyName, new List<string>());

                //ret.fields[property.PropertyName].Add(tmp2.ToString("yyyy-MM-dd HH:mm:ss"));

                //ret.success = true;
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
            }

            return(ret);
        }
示例#2
0
        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.Error, "Field " + cf.Name + " (" + cf.Key + "): error on get data -> " + ex.Message);
                }
            }


            String server   = config["server"].ToString();
            String username = config["username"].ToString();
            String password = config["password"].ToString();


            try
            {
                PasswordConnectionInfo connectionInfo = new PasswordConnectionInfo(server, username, password);
                connectionInfo.Timeout = new TimeSpan(0, 1, 0);

                using (SshClient client = new SshClient(connectionInfo))
                {
                    try
                    {
                        client.Connect();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Erro on connect SSH", ex);
                    }



                    List <UserData> users = GetList(client, config);

                    ret.fields.Add("username", new List <string>());
                    ret.fields.Add("user_id", new List <string>());
                    ret.fields.Add("default_group", new List <string>());
                    ret.fields.Add("information", new List <string>());
                    ret.fields.Add("home_path", new List <string>());
                    ret.fields.Add("bash", new List <string>());


                    foreach (UserData u in users)
                    {
                        ret.fields["username"].Add(u.Username);

                        ret.fields["user_id"].Add(u.UserId);

                        ret.fields["default_group"].Add(u.DefaultGroup);

                        ret.fields["information"].Add(u.Information);

                        ret.fields["home_path"].Add(u.HomePath);

                        ret.fields["bash"].Add(u.Bash);
                    }

                    client.Disconnect();
                }

                ret.success = true;
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
            }

            return(ret);
        }
示例#3
0
        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);
                }
            }


            String table  = config["table"].ToString();
            String filter = config["select_filter"].ToString();

            OdbcDB db = null;

            try
            {
                db = new OdbcDB(config["system_dsn"].ToString(), (config.ContainsKey("username") ? config["username"].ToString() : ""), (config.ContainsKey("password") ? config["password"].ToString() : ""));
                db.openDB();


                iLog(this, PluginLogType.Error, "Listatando schema da tabela...");
                DataTable dtSchema = db.GetSchema(table);

                if (dtSchema == null)
                {
                    throw new Exception("Erro ao listar o schema da tabela: " + db.LastError);
                }

                try
                {
                    foreach (DataColumn dc in dtSchema.Columns)
                    {
                        if (!ret.fields.ContainsKey(dc.ColumnName))
                        {
                            try
                            {
                                ret.fields.Add(dc.ColumnName, new List <string>());

                                iLog(this, PluginLogType.Information, "Column " + dc.ColumnName + ": DataType=" + dc.DataType.ToString() + ", MaxLength=" + dc.MaxLength + ", AllowDBNull=" + dc.AllowDBNull);
                            }
                            catch (Exception ex)
                            {
                                iLog(this, PluginLogType.Information, "Column " + dc.ColumnName + ": error on get data -> " + ex.Message);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    iLog(this, PluginLogType.Error, "Erro ao listar as colunas: " + ex.Message);
                }


                String sql = "select * from " + table;

                if (!String.IsNullOrWhiteSpace(filter))
                {
                    filter = filter.Trim();
                    if (filter.IndexOf("where") != 0)
                    {
                        filter = "where " + filter;
                    }

                    sql = sql + " " + filter;
                }

                DataTable dtSource = db.Select(sql, 0, 10);

                if (dtSource == null)
                {
                    throw new Exception("Erro on select: " + db.LastError);
                }

                try
                {
                    foreach (DataColumn dc in dtSource.Columns)
                    {
                        if (!ret.fields.ContainsKey(dc.ColumnName))
                        {
                            ret.fields.Add(dc.ColumnName, new List <string>());
                        }
                    }
                }
                catch (Exception ex)
                {
                    iLog(this, PluginLogType.Error, "Erro ao listar as colunas: " + ex.Message);
                }

                foreach (DataRow dr in dtSource.Rows)
                {
                    String regId = Guid.NewGuid().ToString();

                    try
                    {
                        foreach (DataColumn dc in dtSource.Columns)
                        {
                            if (!ret.fields.ContainsKey(dc.ColumnName))
                            {
                                ret.fields.Add(dc.ColumnName, new List <string>());
                            }

                            ret.fields[dc.ColumnName].Add(dr[dc.ColumnName].ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        iLog(this, PluginLogType.Error, "Erro ao importar o registro: " + ex.Message);
                    }
                }

                ret.success = true;
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
            }

            return(ret);
        }
示例#4
0
文件: akna.cs 项目: radtek/safeid
        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
            {
                ret.fields.Add("email", new List <string>());
                ret.fields["email"].Add("E-mail do usuário");

                ret.fields.Add("sequencia", new List <string>());
                ret.fields["sequencia"].Add("Sequencia do usuário");

                ret.fields.Add("nome", new List <string>());
                ret.fields["nome"].Add("Nome do usuário");

                ret.fields.Add("idade", new List <string>());
                ret.fields["idade"].Add("Idade do usuário");

                ret.fields.Add("data_nascimento", new List <string>());
                ret.fields["data_nascimento"].Add("Data de nascimento do usuário");

                ret.fields.Add("sexo", new List <string>());
                ret.fields["sexo"].Add("Sexo do usuário");
                //ret.fields["sexo"].Add("M");
                //ret.fields["sexo"].Add("F");

                ret.fields.Add("endereco", new List <string>());
                ret.fields["endereco"].Add("Endereço do usuário");

                ret.fields.Add("complemento", new List <string>());
                ret.fields["complemento"].Add("Complemento do endereço do usuário");

                ret.fields.Add("bairro", new List <string>());
                ret.fields["bairro"].Add("Bairro do endereço do usuário");

                ret.fields.Add("cidade", new List <string>());
                ret.fields["cidade"].Add("Cidade do usuário");

                ret.fields.Add("estado", new List <string>());
                ret.fields["estado"].Add("Estado do usuário");

                ret.success = true;
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
                ret.success = false;
            }

            return(ret);
        }
示例#5
0
        //

        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);
        }
示例#6
0
        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);
                }
            }


            String connectionstring = "Data Source=" + config["server"].ToString() + ";Initial Catalog=master;User Id=" + config["username"].ToString() + ";Password='******';";

            MSSQLDB db = null;

            try
            {
                db = new MSSQLDB(connectionstring);
                db.openDB();

                String sql = "SELECT name AS Login_Name FROM sys.server_principals  WHERE TYPE IN ('S') and name not like '%##%' ORDER BY name, type_desc";

                DataTable dtSource = db.Select(sql);

                if (dtSource == null)
                {
                    throw new Exception("Erro on select: " + db.LastError);
                }

                try
                {
                    foreach (DataColumn dc in dtSource.Columns)
                    {
                        if (!ret.fields.ContainsKey(dc.ColumnName))
                        {
                            ret.fields.Add(dc.ColumnName, new List <string>());
                        }
                    }
                }
                catch (Exception ex)
                {
                    iLog(this, PluginLogType.Error, "Erro ao listar as colunas: " + ex.Message);
                }

                foreach (DataRow dr in dtSource.Rows)
                {
                    String regId = Guid.NewGuid().ToString();

                    try
                    {
                        foreach (DataColumn dc in dtSource.Columns)
                        {
                            if (!ret.fields.ContainsKey(dc.ColumnName))
                            {
                                ret.fields.Add(dc.ColumnName, new List <string>());
                            }

                            ret.fields[dc.ColumnName].Add(dr[dc.ColumnName].ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        iLog(this, PluginLogType.Error, "Erro ao importar o registro: " + ex.Message);
                    }
                }

                ret.success = true;
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
            }

            return(ret);
        }
示例#7
0
        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.Error, "Field " + cf.Name + " (" + cf.Key + "): error on get data -> " + ex.Message);
                }
            }

            try
            {
                DirectoryInfo importDir = null;;
                try
                {
                    importDir = new DirectoryInfo(config["import_folder"].ToString());
                    if (!importDir.Exists)
                    {
                        throw new DirectoryNotFoundException();
                    }
                }
                catch (Exception ex)
                {
                    iLog(this, PluginLogType.Error, "Erro ao localizar o diretório de importação (" + config["import_folder"].ToString() + "): " + ex.Message);
                    return(ret);
                }

                foreach (FileInfo f in importDir.GetFiles("*.csv"))
                {
                    iLog(this, PluginLogType.Information, "Iniciando mapeamento do arquivo '" + f.Name + "'");

                    try
                    {
                        String[] firstLineData = new String[0];
                        Boolean  firstLine     = true;
                        Int32    count         = 0;
                        Int32    cLine         = 0;

                        using (FileStream fs = f.OpenRead())
                            using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
                            {
                                while (!reader.EndOfStream)
                                {
                                    cLine++;

                                    String line = reader.ReadLine();

                                    try
                                    {
                                        String[] parts = line.Split(config["delimiter"].ToString().ToCharArray());

                                        if (firstLine)
                                        {
                                            firstLineData = parts;
                                            firstLine     = false;

                                            for (Int32 c = 0; c < firstLineData.Length; c++)
                                            {
                                                if (!String.IsNullOrWhiteSpace(firstLineData[c]) && !ret.fields.ContainsKey(firstLineData[c]))
                                                {
                                                    ret.fields.Add(firstLineData[c], new List <string>());
                                                }
                                            }


                                            if (!ret.fields.ContainsKey("csv_file"))
                                            {
                                                ret.fields.Add("csv_file", new List <string>());
                                            }

                                            if (!ret.fields.ContainsKey("csv_file_line"))
                                            {
                                                ret.fields.Add("csv_file_line", new List <string>());
                                            }
                                        }
                                        else
                                        {
                                            if (firstLineData.Length != parts.Length)
                                            {
                                                throw new Exception("Linha com número diferente de colunas");
                                            }

                                            count++;

                                            if (count >= 10)
                                            {
                                                break;
                                            }

                                            ret.fields["csv_file"].Add(f.Name);
                                            ret.fields["csv_file_line"].Add(cLine.ToString());

                                            for (Int32 c = 0; c < firstLineData.Length; c++)
                                            {
                                                ret.fields[firstLineData[c]].Add(parts[c]);
                                            }
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        iLog(this, PluginLogType.Error, "Falha ao importar a linha '" + line + "' do arquivo '" + f.Name + "': " + ex.Message);
                                    }
                                }
                            }
                    }
                    catch (Exception ex)
                    {
                        iLog(this, PluginLogType.Error, "Falha ao mapear os dados do arquivo '" + f.Name + "': " + ex.Message);
                    }
                }


                ret.success = true;
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
            }

            return(ret);
        }
示例#8
0
        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);
                }
            }

            SeniorAPI api = new SeniorAPI(config["username"].ToString(), config["password"].ToString(), config["numemp"].ToString(), new Uri(config["server_uri"].ToString()));

            XML.DebugMessage dbgC = new XML.DebugMessage(delegate(String data, String debug)
            {
#if DEBUG
                iLog(this, PluginLogType.Information, "## JSON Debug message: " + data + debug);
#endif
            });

            try
            {
                List <Dictionary <String, String> > users = api.GetUsers(dbgC);
                Int32 cnt = 0;
                foreach (Dictionary <String, String> u in users)
                {
                    foreach (String key in u.Keys)
                    {
                        if (!ret.fields.ContainsKey(key))
                        {
                            ret.fields.Add(key, new List <string>());
                        }

                        ret.fields[key].Add(u[key]);
                    }

                    cnt++;
                    if (cnt >= 10)
                    {
                        break;
                    }
                }



                ret.success = true;
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
                ret.success = false;
            }

            return(ret);
        }
示例#9
0
        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);
                }
            }


            String ldapServer = config["ldap_server"].ToString();
            String username   = config["username"].ToString();
            String password   = config["password"].ToString();

            //Create a dictionary with the most knolege properties
            Dictionary <String, String> mostKnolege = GetCommonItems();

            foreach (String k in mostKnolege.Keys)
            {
                if (!ret.fields.ContainsKey(k))
                {
                    ret.fields.Add(k, new List <string>());
                }

                ret.fields[k].Add(mostKnolege[k]);
            }

            try
            {
                DirectoryEntry entry = new DirectoryEntry("LDAP://" + ldapServer, username, password, AuthenticationTypes.Secure);

                DirectorySearcher search = new DirectorySearcher(entry);
                search.SearchScope = SearchScope.Subtree;
                //search.Filter = "(&(objectClass=user)(sAMAccountName=helvio.junior))";
                search.Filter = "(samAccountType=805306368)";
                search.PropertiesToLoad.Add("distinguishedName");
                search.PropertiesToLoad.Add("company");
                search.PropertiesToLoad.Add("department");

                SearchResultCollection result = search.FindAll();

                if (result != null)
                {
                    Int32 count = 0;
                    foreach (SearchResult sr in result)
                    {
                        if (count >= 20)
                        {
                            break;
                        }

                        try
                        {
                            DirectoryEntry entry1 = new DirectoryEntry("LDAP://" + ldapServer + "/" + sr.Properties["distinguishedName"][0].ToString(), username, password);
                            entry1.AuthenticationType = AuthenticationTypes.Secure;
                            foreach (PropertyValueCollection property in entry1.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())
                                {
                                case "lastlogon":
                                case "whencreated":
                                case "lockouttime":
                                    try
                                    {
                                        Int64    tmp  = Int64.Parse(property[0].ToString());
                                        DateTime tmp2 = DateTime.FromFileTime(tmp);

                                        if (tmp2.Year > 1970)    //Se a data for inferior nem envia
                                        {
                                            ret.fields[property.PropertyName].Add(tmp2.ToString("yyyy-MM-dd HH:mm:ss"));
                                        }
                                    }
                                    catch (Exception ex)
                                    { }
                                    break;

                                case "useraccountcontrol":
                                    foreach (Object p1 in property)
                                    {
                                        UserAccountControl ctrl = (UserAccountControl)p1;

                                        foreach (UserAccountControl c in Enum.GetValues(typeof(UserAccountControl)))
                                        {
                                            //Verifica se está utilizando
                                            if ((ctrl & c) == c)
                                            {
                                                ret.fields[property.PropertyName].Add(c.ToString());
                                            }
                                        }
                                    }

                                    break;

                                default:
                                    foreach (Object p1 in property)
                                    {
                                        ret.fields[property.PropertyName].Add(p1.ToString());
                                    }
                                    break;
                                }
                            }


                            count++;
                        }
                        catch (Exception ex)
                        {
                            iLog(this, PluginLogType.Error, "Erro ao importar o registro (" + sr.Path + "): " + ex.Message);
                        }
                    }
                }

                ret.success = true;
                search.Dispose();
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
            }

            return(ret);
        }
示例#10
0
        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);
                }
            }


            String table = config["sheet"].ToString();

            table = table.Trim("$ []".ToCharArray());

            try
            {
                DirectoryInfo importDir = null;;
                try
                {
                    importDir = new DirectoryInfo(config["import_folder"].ToString());
                    if (!importDir.Exists)
                    {
                        throw new DirectoryNotFoundException();
                    }
                }
                catch (Exception ex)
                {
                    iLog(this, PluginLogType.Error, "Erro ao localizar o diretório de importação (" + config["import_folder"].ToString() + "): " + ex.Message);
                    return(ret);
                }

                foreach (FileInfo f in importDir.GetFiles("*.xls"))
                {
                    iLog(this, PluginLogType.Information, "Iniciando mapeamento do arquivo '" + f.Name + "'");

                    OdbcDB db = null;
                    try
                    {
                        db = new OdbcDB(f);
                        db.openDB();


                        iLog(this, PluginLogType.Error, "Listatando schema da tabela...");
                        DataTable dtSchema = db.GetSchema(table);

                        if (dtSchema == null)
                        {
                            throw new Exception("Erro ao listar o schema da tabela: " + db.LastError);
                        }

                        try
                        {
                            foreach (DataColumn dc in dtSchema.Columns)
                            {
                                if (!ret.fields.ContainsKey(dc.ColumnName))
                                {
                                    try
                                    {
                                        ret.fields.Add(dc.ColumnName, new List <string>());

                                        iLog(this, PluginLogType.Information, "Column " + dc.ColumnName + ": DataType=" + dc.DataType.ToString() + ", MaxLength=" + dc.MaxLength + ", AllowDBNull=" + dc.AllowDBNull);
                                    }
                                    catch (Exception ex)
                                    {
                                        iLog(this, PluginLogType.Information, "Column " + dc.ColumnName + ": error on get data -> " + ex.Message);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            iLog(this, PluginLogType.Error, "Erro ao listar as colunas: " + ex.Message);
                        }


                        String sql = "select * from [" + table + "$]";

                        DataTable dtSource = db.Select(sql, 0, 10);

                        if (dtSource == null)
                        {
                            throw new Exception("Erro on select: " + db.LastError);
                        }

                        try
                        {
                            foreach (DataColumn dc in dtSource.Columns)
                            {
                                if (!ret.fields.ContainsKey(dc.ColumnName))
                                {
                                    ret.fields.Add(dc.ColumnName, new List <string>());
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            iLog(this, PluginLogType.Error, "Erro ao listar as colunas: " + ex.Message);
                        }

                        Int32 qty = 0;

                        foreach (DataRow dr in dtSource.Rows)
                        {
                            qty++;

                            if (qty < 10)
                            {
                                break;
                            }

                            String regId = Guid.NewGuid().ToString();

                            try
                            {
                                foreach (DataColumn dc in dtSource.Columns)
                                {
                                    if (!ret.fields.ContainsKey(dc.ColumnName))
                                    {
                                        ret.fields.Add(dc.ColumnName, new List <string>());
                                    }

                                    ret.fields[dc.ColumnName].Add(dr[dc.ColumnName].ToString());
                                }
                            }
                            catch (Exception ex)
                            {
                                iLog(this, PluginLogType.Error, "Erro ao importar o registro: " + ex.Message);
                            }
                        }

                        ret.success = true;
                    }
                    catch (Exception ex)
                    {
                        iLog(this, PluginLogType.Error, "Falha ao mapear os dados do arquivo '" + f.Name + "': " + ex.Message);
                    }
                    finally
                    {
                        if (db != null)
                        {
                            db.closeDB();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                iLog(this, PluginLogType.Error, ex.Message);
            }

            return(ret);
        }