private static void AddLicenseControl(ArtifactoryConfig artifactoryConfig, Build build, BuildInfoLog log)
        {
            if (artifactoryConfig.PropertyGroup.LicenseControlCheck == null)
            {
                return;
            }

            LicenseControl licenseControl = new LicenseControl();

            licenseControl.runChecks    = artifactoryConfig.PropertyGroup.LicenseControlCheck.EnabledLicenseControl;
            licenseControl.autoDiscover = artifactoryConfig.PropertyGroup.LicenseControlCheck.AutomaticLicenseDiscovery;
            licenseControl.includePublishedArtifacts   = artifactoryConfig.PropertyGroup.LicenseControlCheck.IncludePublishedArtifacts;
            licenseControl.licenseViolationsRecipients = new List <string>();
            licenseControl.scopes = new List <string>();

            foreach (Recipient recip in artifactoryConfig.PropertyGroup.LicenseControlCheck.LicenseViolationRecipients.Recipient)
            {
                if (validateEmail(recip))
                {
                    licenseControl.licenseViolationsRecipients.Add(recip.email);
                }
                else
                {
                    log.Warning("Invalid email address, under License Control violation recipients.");
                }
            }

            foreach (Scope scope in artifactoryConfig.PropertyGroup.LicenseControlCheck.ScopesForLicenseAnalysis.Scope)
            {
                licenseControl.scopes.Add(scope.value);
            }

            build.licenseControl = licenseControl;
        }
Esempio n. 2
0
        /// <summary>
        /// Método de processamentoda requisição
        /// </summary>
        /// <param name="sqlConnection">Conexão com o banco de dados MS-SQL</param>
        /// <param name="enterpriseId">ID da empresa</param>
        /// <param name="method">String com o método que deverá ser processado</param>
        /// <param name="auth">String com a chave de autenticação.</param>
        /// <param name="parameters">Dicionário (String, Object) contendo todos os parâmetros necessários</param>
        public override Object iProcess(IAMDatabase database, Int64 enterpriseId, String method, String auth, Dictionary <String, Object> parameters)
        {
            //base.Connection = sqlConnection;

            method = method.ToLower();
            String[] mp = method.Split(".".ToCharArray(), 2);

            if (mp.Length != 2)
            {
                return(null);
            }

            if (this.GetType().Name.ToLower() != mp[0])
            {
                return(null);
            }

            Acl = ValidateCtrl(database, method, auth, parameters, ExternalAccessControl);

            if (!Acl.Result)
            {
                Error(ErrorType.InvalidParameters, "Not authorized", "", null);
                return(null);
            }

            switch (mp[1])
            {
            case "info":
                Dictionary <String, Object> result = new Dictionary <String, Object>();

                LicenseControl lic = LicenseChecker.GetLicenseData(database.Connection, null, enterpriseId);

                result.Add("hasLicense", lic.Valid);
                if (lic.Valid)
                {
                    result.Add("used", lic.Count);
                    result.Add("available", lic.Entities);
                }

                return(result);

                break;

            default:
                Error(ErrorType.InvalidRequest, "JSON-rpc method is unknow.", "", null);
                return(null);

                break;
            }

            return(null);
        }
        private static void AddLicenseControl(ArtifactoryConfig artifactoryConfig, Build build, BuildInfoLog log)
        {
            if (artifactoryConfig.PropertyGroup.LicenseControlCheck == null)
                return;

            LicenseControl licenseControl = new LicenseControl();
            licenseControl.runChecks = artifactoryConfig.PropertyGroup.LicenseControlCheck.EnabledLicenseControl;
            licenseControl.autoDiscover = artifactoryConfig.PropertyGroup.LicenseControlCheck.AutomaticLicenseDiscovery;
            licenseControl.includePublishedArtifacts = artifactoryConfig.PropertyGroup.LicenseControlCheck.IncludePublishedArtifacts;
            licenseControl.licenseViolationsRecipients = new List<string>();
            licenseControl.scopes = new List<string>();
           
            foreach (Recipient recip in artifactoryConfig.PropertyGroup.LicenseControlCheck.LicenseViolationRecipients.Recipient)
            {
                if (validateEmail(recip))
                {
                    licenseControl.licenseViolationsRecipients.Add(recip.email);
                }
                else 
                {
                    log.Warning("Invalid email address, under License Control violation recipients.");
                }                            
            }

            foreach (Scope scope in artifactoryConfig.PropertyGroup.LicenseControlCheck.ScopesForLicenseAnalysis.Scope)
            {
                licenseControl.scopes.Add(scope.value);
            }

            build.licenseControl = licenseControl;
        }
Esempio n. 4
0
        private Int32 _Deploy(Int64 entityId, Int64 resourcePluginId)
        {
            //Busca todos os plugins e recursos a serem publicados
            DataTable dtPlugins = null;
            Dictionary <Int64, LicenseControl> licControl = null;
            DataTable dtEnt        = null;
            Int32     packageCount = 0;

            StringBuilder deployLog = new StringBuilder();

            try
            {
                dtPlugins = db.Select("select r.context_id, p.id, p.scheme, p.uri, p.assembly, p.create_date, rp.id resource_plugin_id, rp.deploy_individual_package, r.id resource_id, r.proxy_id, p1.name as proxy_name, p1.id proxy_id, p1.enterprise_id, rp.deploy_after_login, rp.password_after_login, rp.deploy_process, rp.deploy_all, rp.deploy_password_hash, rp.use_password_salt, rp.password_salt_end, rp.password_salt from plugin p with(nolock)  inner join resource_plugin rp with(nolock) on rp.plugin_id = p.id  inner join [resource] r on r.id = rp.resource_id inner join proxy p1 on r.proxy_id = p1.id  where " + (resourcePluginId > 0 ? " rp.id = " + resourcePluginId + " and " : "") + " r.enabled = 1 and rp.enabled = 1 and rp.enable_deploy = 1 order by rp.[order]");
                if ((dtPlugins == null) || (dtPlugins.Rows.Count == 0))
                {
                    if ((entityId > 0) || (resourcePluginId > 0))
                    {
                        throw new Exception("0 plugin to process");
                    }

                    //TextLog.Log(moduleSender, "\t0 plugin to process");
                    DebugLog(entityId, "0 plugin to process");
                    return(0);
                }

                DebugLog(entityId, dtPlugins.Rows.Count + " plugin to process");

                licControl = new Dictionary <long, LicenseControl>();

                String rolesText = "";

                //Lista todos os plugins e resources habilitados
                foreach (DataRow dr in dtPlugins.Rows)
                {
                    Boolean individualPackage = (Boolean)dr["deploy_individual_package"];

                    deployLog = new StringBuilder();

                    DebugLog(entityId, "proxy_name = " + dr["proxy_name"].ToString() + ", plugin = " + dr["uri"].ToString() + ", deploy_all? " + dr["deploy_all"].ToString());

                    ProxyConfig config = new ProxyConfig(true);
                    config.GetDBCertConfig(db.Connection, Int64.Parse(dr["enterprise_id"].ToString()), dr["proxy_name"].ToString());

                    DirectoryInfo proxyDir = new DirectoryInfo(Path.Combine(outDirBase.FullName, dr["proxy_id"].ToString() + "_" + dr["proxy_name"].ToString() + "\\" + Path.GetFileNameWithoutExtension(dr["assembly"].ToString()) + "\\rp" + dr["resource_plugin_id"].ToString()));

                    List <PluginConnectorBaseDeployPackage> packageList = new List <PluginConnectorBaseDeployPackage>();
                    List <Int64> roles = new List <Int64>();

                    Int64 enterpriseId = (Int64)dr["enterprise_id"];

                    LicenseControl lic = null;
                    if (!licControl.ContainsKey(enterpriseId))
                    {
                        lic = LicenseChecker.GetLicenseData(db.Connection, null, enterpriseId);
                        licControl.Add(enterpriseId, lic);
                    }
                    else
                    {
                        lic = licControl[enterpriseId];
                    }

                    if (!lic.Valid)
                    {
                        if (!lic.Notified)
                        {
                            db.AddUserLog(LogKey.Licence_error, null, "Deploy", UserLogLevel.Error, (Int64)dr["proxy_id"], (Int64)dr["enterprise_id"], 0, (Int64)dr["resource_id"], (Int64)dr["id"], 0, 0, "License error: " + lic.Error);
                        }
                        lic.Notified = true;
                        continue;
                    }


                    if (!(Boolean)dr["deploy_all"])
                    {
                        //Busca os "roles" top
                        String rolesSQL = "select rpr.* from resource_plugin_role rpr with(nolock) inner join resource_plugin rp on rpr.resource_plugin_id = rp.id where rp.resource_id =  " + dr["resource_id"].ToString() + " and rp.plugin_id = " + dr["id"];
                        DebugLog(entityId, "Role SQL = " + rolesSQL);

                        DataTable dtRoles = db.Select(rolesSQL);
                        if (dtRoles == null)
                        {
                            db.AddUserLog(LogKey.Deploy, null, "Deploy", UserLogLevel.Error, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], 0, 0, "DB error: " + (((db.LastDBError != null) && (db.LastDBError != "")) ? db.LastDBError : ""));
                            continue;
                        }

                        List <String> roleNames = new List <String>();

                        //Busca toda a arvore de "roles" a se buscar
                        foreach (DataRow drR in dtRoles.Rows)
                        {
                            DataTable dtR = db.Select("select * from dbo.fn_selectRoleTree(" + drR["role_id"] + ")");
                            if (dtR == null)
                            {
                                continue;
                            }

                            foreach (DataRow drRT in dtR.Rows)
                            {
                                if (!roles.Contains((Int64)drRT["role_id"]))
                                {
                                    roleNames.Add(drRT["name"].ToString());
                                    roles.Add((Int64)drRT["role_id"]);
                                }
                            }
                        }

                        if (roles.Count == 0)
                        {
                            db.AddUserLog(LogKey.Deploy, null, "Deploy", UserLogLevel.Info, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], 0, 0, "Not found roles x identities to deploy");
                            continue;
                        }

                        //Para efeitos de log captura o nome dos roles
                        rolesText = String.Join(", ", roleNames);

                        dtRoles.Clear();
                        dtRoles = null;
                    }

                    //Seleciona todas as entidades do mesmo contexto
                    //Esta listagem considera somente as entidades pertencentes aos plugins de entrada
                    String sql = "select e.id, e.last_login, e.change_password, i.id identity_id from entity e with(nolock) inner join resource r with(nolock) on e.context_id = r.context_id inner join [identity] i with(nolock) on i.entity_id = e.id inner join [resource_plugin] rp with(nolock) on i.resource_plugin_id = rp.id where i.deleted = 0 and e.deleted = 0 {0} and e.context_id = " + dr["context_id"] + (entityId > 0 ? " and e.id = " + entityId : "") + " and not exists (select 1 from identity_block_inheritance bi where bi.identity_id = i.id) group by e.id, e.last_login, e.change_password, i.id";

                    if (!(Boolean)dr["deploy_all"])
                    {
                        sql = "select e.id, e.last_login, e.change_password, i.id identity_id from entity e with(nolock) inner join resource r with(nolock) on e.context_id = r.context_id inner join [identity] i with(nolock) on i.entity_id = e.id inner join [resource_plugin] rp with(nolock) on i.resource_plugin_id = rp.id inner join identity_role ir with(nolock) on ir.identity_id = i.id  inner join (select rpr.role_id from	resource_plugin_role rpr with(nolock) inner join resource_plugin rp with(nolock) on rp.id = rpr.resource_plugin_id inner join resource r with(nolock) on r.id = rp.resource_id where r.id = "+ dr["resource_id"].ToString() + ") ro on ro.role_id =  ir.role_id where i.deleted = 0 and e.deleted = 0 {0} and ir.role_id in (" + String.Join(",", roles) + ")" + (entityId > 0 ? " and e.id = " + entityId : "") + " and not exists (select 1 from identity_block_inheritance bi where bi.identity_id = i.id) and e.context_id = " + dr["context_id"] + " group by e.id, e.last_login, e.change_password, i.id";
                    }

                    DebugLog(entityId, String.Format(sql, "and rp.enable_import = 1 and rp.permit_add_entity = 1"));

                    //Lista todas as entidades e identidades para exportar
                    dtEnt = db.Select(String.Format(sql, "and rp.enable_import = 1 and rp.permit_add_entity = 1"));
                    if (dtEnt == null)
                    {
                        DebugLog(entityId, "SQL result is empty");
                        db.AddUserLog(LogKey.Deploy, null, "Deploy", UserLogLevel.Error, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], 0, 0, "DB error: " + (((db.LastDBError != null) && (db.LastDBError != "")) ? db.LastDBError : ""));
                        continue;
                    }

                    if (dtEnt.Rows.Count == 0)
                    {
                        DebugLog(entityId, "SQL result is empty, trying with all plugins");
                        DebugLog(entityId, String.Format(sql, ""));

                        //Lista todas as entidades e identidades para exportar
                        dtEnt = db.Select(String.Format(sql, ""));
                        if (dtEnt == null)
                        {
                            DebugLog(entityId, "SQL result is empty");
                            db.AddUserLog(LogKey.Deploy, null, "Deploy", UserLogLevel.Error, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], 0, 0, "DB error: " + (((db.LastDBError != null) && (db.LastDBError != "")) ? db.LastDBError : ""));
                            continue;
                        }
                    }
                    sql = null;


                    if ((dtEnt.Rows.Count == 0) && ((Boolean)dr["deploy_all"]))
                    {
                        DebugLog(entityId, "SQL result is empty with all plugins, trying with only entity data");

                        sql = "select e.id, e.last_login, e.change_password, cast(0 as bigint) identity_id from entity e with(nolock) inner join resource r with(nolock) on e.context_id = r.context_id cross join [resource_plugin] rp with(nolock) where e.deleted = 0 {0} and e.context_id = " + dr["context_id"] + (entityId > 0 ? " and e.id = " + entityId : "") + "  group by e.id, e.last_login, e.change_password";

                        DebugLog(entityId, String.Format(sql, "and rp.enable_import = 1 and rp.permit_add_entity = 1"));

                        //Lista todas as entidades e identidades para exportar
                        dtEnt = db.Select(String.Format(sql, "and rp.enable_import = 1 and rp.permit_add_entity = 1"));
                        if (dtEnt == null)
                        {
                            DebugLog(entityId, "SQL result is empty");
                            db.AddUserLog(LogKey.Deploy, null, "Deploy", UserLogLevel.Error, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], 0, 0, "DB error: " + (((db.LastDBError != null) && (db.LastDBError != "")) ? db.LastDBError : ""));
                            continue;
                        }
                    }
                    sql = null;

                    DebugLog(entityId, "SQL result count " + dtEnt.Rows.Count);

                    if ((dtEnt.Rows.Count > 0) && (entityId == 0))
                    {
                        deployLog.AppendLine("Starting check to deploy " + dtEnt.Rows.Count + " identities for " + ((!(Boolean)dr["deploy_all"]) ? rolesText : "all users"));
                    }

                    Int32 total        = dtEnt.Rows.Count;
                    Int32 licError     = 0;
                    Int32 loguedIgnore = 0;
                    Int32 deploy       = 0;

                    //db.AddUserLog(LogKey.Deploy, null, "Deploy", UserLogLevel.Info, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], 0, 0, "Deploy with " + dtEnt.Rows.Count + " identities for " + ((!(Boolean)dr["deploy_all"]) ? rolesText : "all users"));
                    foreach (DataRow drE in dtEnt.Rows)
                    {
                        //Checagens de licenciamento
                        lic.Count++;

                        if ((lic.Entities > 0) && (lic.Count > lic.Entities))
                        {
                            db.AddUserLog(LogKey.Licence_error, null, "Deploy", UserLogLevel.Error, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], (Int64)drE["id"], (Int64)drE["identity_id"], "License error: License limit (" + lic.Entities + " entities) exceeded");
                            licError++;
                            continue;
                        }

                        try
                        {
                            if (((Boolean)dr["deploy_after_login"]) && (drE["last_login"] == DBNull.Value))
                            {
                                db.AddUserLog(LogKey.User_Deploy, null, "Deploy", UserLogLevel.Info, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], (Int64)drE["id"], (Int64)drE["identity_id"], "User NOT addedd in deploy package because the user is not logged in yet");
                                loguedIgnore++;
                                continue;
                            }

                            //db.AddUserLog(LogKey.User_Deploy, null, "Deploy", UserLogLevel.Info, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], (Int64)drE["id"], (Int64)drE["identity_id"], "Identity addedd in deploy package");

                            PluginConnectorBaseDeployPackage newPkg = DeployPackage.GetPackage(db, (Int64)dr["proxy_id"], (Int64)dr["resource_plugin_id"], (Int64)drE["id"], (Int64)drE["identity_id"], (Boolean)dr["password_after_login"], (drE["change_password"] == DBNull.Value ? null : (DateTime?)drE["change_password"]), (dr["deploy_password_hash"] == DBNull.Value ? "none" : dr["deploy_password_hash"].ToString()), (Boolean)dr["use_password_salt"], (Boolean)dr["password_salt_end"], dr["password_salt"].ToString());
                            packageList.Add(newPkg);

                            deploy++;

#if DEBUG
                            try
                            {
                                db.AddUserLog(LogKey.Deploy, null, "Deploy", UserLogLevel.Debug, 0, enterpriseId, 0, (Int64)dr["resource_id"], (Int64)dr["id"], newPkg.entityId, newPkg.identityId, "Package generated: " + newPkg.pkgId, SafeTrend.Json.JSON.Serialize <PluginConnectorBaseDeployPackage>(newPkg));
                            }
                            catch { }
#endif

                            packageCount++;
                        }
                        catch (Exception ex)
                        {
                            db.AddUserLog(LogKey.User_Deploy, null, "Deploy", UserLogLevel.Info, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], (Int64)drE["id"], (Int64)drE["identity_id"], "Erro on deploy user: "******"Total identities: " + total);
                    deployLog.AppendLine("Ignored by licence check: " + licError);
                    deployLog.AppendLine("Ignored by first login rule: " + loguedIgnore);
                    deployLog.AppendLine("Published: " + deploy);

                    db.AddUserLog(LogKey.Deploy, null, "Deploy", UserLogLevel.Info, (Int64)dr["proxy_id"], 0, 0, (Int64)dr["resource_id"], (Int64)dr["id"], 0, 0, "Deploy package generated for " + ((!(Boolean)dr["deploy_all"]) ? rolesText : "all users"), deployLog.ToString());
                }

                db.closeDB();
                db.Dispose();
            }
            catch (Exception ex)
            {
                DebugLog(entityId, "Erro on Deploy: " + ex.Message);
                throw ex;
            }
            finally
            {
                deployLog.Clear();
                deployLog = null;

                if (dtPlugins != null)
                {
                    dtPlugins.Clear();
                }
                dtPlugins = null;

                if (dtEnt != null)
                {
                    dtEnt.Clear();
                }
                dtEnt = null;

                if (licControl != null)
                {
                    try
                    {
                        List <Int64> k = new List <Int64>();
                        k.AddRange(licControl.Keys);

                        foreach (Int64 l in k)
                        {
                            if (licControl[l] != null)
                            {
                                licControl[l].Dispose();
                                licControl[l] = null;
                            }
                        }

                        k.Clear();
                    }
                    catch { }
                }
                licControl = null;
            }

            return(packageCount);
        }
Esempio n. 5
0
        public RegistryProcessStatus Process(EnterpriseKeyConfig enterpriseKey, LockRules lockRules, IgnoreRules ignoreRules, RoleRules roleRules, LicenseControl lic)
        {
            List <UserDataFields> fieldsData = null;
            List <UserDataFields> filter     = null;
            TestTimer             tmp        = null;
            Boolean showError = true;


            SqlTransaction trans = null;

            try
            {
                RegistryProcess.ProccessLog dLog = new RegistryProcess.ProccessLog(delegate(String text)
                {
#if DEBUG
                    Log("\t{profile} " + text);
#endif
                });

                tmp = new TestTimer("Process->Starting", dLog);

                Log("Starting registry processor");
                Log("");

                Log("Plugin Config");
                Log(pluginConfig.ToString());
                Log("");

                Log("Registry data:");
                Log("\tGenerated Date: " + package.build_data);
                Log("\tContext id: " + this.contextId);
                Log("\tResource plugin id: " + this.resourcePluginId);
                Log("\tResource id: " + this.resourceId);
                Log("\tPlugin: " + this.pluginUri);
                Log("\tImport id: " + this.importId);
                Log("\tPackage id: " + this.packageId);
                Log("\tContainer: " + package.container);
                Log("\tGroups: " + (package.groups != null ? String.Join(", ", package.groups) : ""));
                Log("");

                if (this.pluginConfig.mapping == null)
                {
                    if (!pluginConfig.enable_import)
                    {
                        showError = false;
                    }

                    throw new Exception("Plugin mapping is null");
                }

                if (this.pluginConfig.mapping.Count == 0)
                {
                    if (!pluginConfig.enable_import)
                    {
                        showError = false;
                    }

                    throw new Exception("Plugin mapping is empty");
                }

                String where = "ci.status = 'F' and ci.resource_plugin_id = '" + this.resourcePluginId + "' and  ci.import_id = '" + this.importId + "' and ci.package_id = '" + this.packageId + "'";

                tmp.Stop(dbAux.Connection, null);


                /*
                 * ======================================
                 * == Resgata Package Track ID*/


                try
                {
                    DbParameterCollection par = new DbParameterCollection();

                    par.Add("@date", typeof(DateTime)).Value = this.package.GetBuildDate();
                    par.Add("@package_id", typeof(String), this.package.pkgId.Length).Value = this.package.pkgId;

                    this.packageTrackId = dbAux.ExecuteScalar <Int64>("select id from st_package_track where flow = 'inbound' and date = @date and package_id = @package_id", System.Data.CommandType.Text, par, null);
                }
                catch (Exception ex)
                {
#if DEBUG
                    internalLog.AppendLine("Error getting package track entity id: " + ex.Message);
#endif
                }

                /*
                 * == Final do resgate Package Track ID
                 * ======================================*/


                /*
                 * ======================================
                 * == Monta tabela de filtragem*/

                tmp = new TestTimer("Process->Filter table", dLog);


                filter = new List <UserDataFields>();

                //Adiciona os mapeamentos que são ID ou único para filtragem
                foreach (PluginConnectorBasePackageData data in package.properties)
                {
                    if (String.IsNullOrWhiteSpace(data.dataValue))
                    {
                        continue;
                    }

                    foreach (PluginConfigMapping m in this.pluginConfig.mapping)
                    {
                        if ((m.is_id || m.is_unique_property) && (m.data_name.ToLower() == data.dataName.ToLower()) && !filter.Exists(f => (f.Mapping.field_id == m.field_id && f.Equal(data.dataValue.Trim()))))
                        {
                            filter.Add(new UserDataFields((PluginConfigMapping)m.Clone(), data.dataValue.Trim()));
                        }
                    }
                }

                Log("Filter data:");
                foreach (UserDataFields f in filter)
                {
                    Log("\t[" + f.Mapping.data_name.ToLower() + "] is " + (f.Mapping.is_id ? "ID" : "Unique field") + " = " + f.Value);
                }
                Log("");


                tmp.Stop(dbAux.Connection, null);


                /*
                 * == Final tabela de filtragem
                 * ======================================*/

                /*
                 * ======================================
                 * == Monta tabela de dados*/

                tmp = new TestTimer("Process->Data table", dLog);


                //Monta tabela de dados com base no mapeamento e dados recebidos
                fieldsData = new List <UserDataFields>();

                foreach (PluginConnectorBasePackageData data in package.properties)
                {
                    if (String.IsNullOrWhiteSpace(data.dataValue))
                    {
                        continue;
                    }

                    foreach (PluginConfigMapping m in this.pluginConfig.mapping)
                    {
                        if ((m.data_name.ToLower() == data.dataName.ToLower()) && !fieldsData.Exists(f => (f.Mapping.field_id == m.field_id && f.Equal(data.dataValue.Trim()))))
                        {
                            try
                            {
                                fieldsData.Add(new UserDataFields((PluginConfigMapping)m.Clone(), data.dataValue.Trim()));
                            }
                            catch (Exception ex2)
                            {
                                Log(ex2.Message);
                            }
                        }
                    }
                }

                Log("Proccess data: " + (fieldsData.Count == 0 ? "empty" : ""));
                foreach (UserDataFields f in fieldsData)
                {
                    Log("\t[" + f.Mapping.data_name.ToLower() + "] Flags (" + (f.Mapping.is_login ? "is_login " : "") + (f.Mapping.is_name ? "is_name " : "") + (f.Mapping.is_password ? "is_password " : "") + ") " + (f.Mapping.is_id ? "is ID" : (f.Mapping.is_unique_property ? "is Unique field" : "")) + " = " + (f.Mapping.is_password ? "*****" : f.Value));
                }
                Log("");

                tmp.Stop(dbAux.Connection, null);


                /*
                 * == Final tabela de dados
                 * ======================================*/


                /*
                 * ======================================
                 * == Cria o objeto do usuário e tenta localiza-lo*/
                tmp = new TestTimer("Process->Create user object", dLog);

                userData        = new UserData(db.Connection, this.pluginConfig, enterpriseKey, enterpriseId, contextId, resourcePluginId, resourceId, pluginId, pluginConfig.mail_domain, pluginConfig.mail_field_id, filter, fieldsData, package.container);
                userData.OnLog += Log;
                userData.CheckUser();

                tmp.Stop(dbAux.Connection, null);

                tmp = new TestTimer("Process->Check exists and import enabled", dLog);

                //Não existe e não é possível adicionar
                if ((userData.EntityId == 0) && ((!pluginConfig.permit_add_entity) || (!pluginConfig.enable_import)))
                {
                    String sId = "";
                    foreach (UserDataFields f in filter)
                    {
                        if (sId != "")
                        {
                            sId += ", ";
                        }
                        sId += f.Mapping.data_name + " = " + f.Value;
                    }

                    //Add identity to audit
                    userData.AddToAudit("not_exists", null);

                    throw new Exception("Entity not found and this plugin " + (!pluginConfig.enable_import ? "is disabled to import" : "not permit add entity") + ": " + sId);
                    return(RegistryProcessStatus.Error);
                }


                tmp.Stop(dbAux.Connection, null);

                tmp = new TestTimer("Process->Check deleted", dLog);


                if (userData.Deleted)
                {
                    String sId = "";
                    foreach (UserDataFields f in filter)
                    {
                        if (sId != "")
                        {
                            sId += ", ";
                        }
                        sId += f.Mapping.data_name + " = " + f.Value;
                    }

                    //Add identity to audit
                    //userData.AddToAudit("deleted");

                    throw new Exception("Entity found but marked as deleted: " + sId);
                    return(RegistryProcessStatus.Error);
                }


                tmp.Stop(dbAux.Connection, null);

                //Verifica se o registro deve ser ignorado
                //Se sim, nada será realizado, nem bloqueio, nem explusão, nem adição....
                tmp = new TestTimer("Process->Check ignore", dLog);
                if (userData.Ignore(ignoreRules, this.pluginUri))
                {
                    DbParameterCollection par = new DbParameterCollection();
                    par.Add("@resource_plugin_id", typeof(Int64)).Value = resourcePluginId;
                    par.Add("@import_id", typeof(String)).Value         = importId;
                    par.Add("@package_id", typeof(String)).Value        = packageId;
                    par.Add("@status", typeof(String)).Value            = 'F';
                    par.Add("@new_status", typeof(String)).Value        = 'I';

                    ExecuteNonQuery(db.Connection, "sp_migrate_imported2", CommandType.StoredProcedure, par, null);

                    par.Clear();
                    par = null;

                    return(RegistryProcessStatus.Ignored);
                }

                tmp.Stop(dbAux.Connection, null);


                //Esta parte do código está propositalmente depois da verificação de existência e se permite add o login
                //Pois este código é dispendioso, e só deve ser executado quando realmente necessario
                tmp = new TestTimer("Process->Check lock", dLog);
                userData.CheckLock(lockRules, this.pluginUri);
                tmp.Stop(dbAux.Connection, null);

                if ((userData.EntityId == 0) && (userData.Locked))
                {
                    tmp = new TestTimer("Process->Check exists and locked", dLog);

                    String sId = "";
                    foreach (UserDataFields f in filter)
                    {
                        if (sId != "")
                        {
                            sId += ", ";
                        }
                        sId += f.Mapping.data_name + " = " + f.Value;
                    }

                    //userData.AddToAudit("locked", trans);

                    throw new Exception("Entity not found and this user is locked: " + sId);
                    return(RegistryProcessStatus.Error);
                }
                else if (userData.EntityId == 0)//Não existe a entidade
                {
                    tmp = new TestTimer("Process->Add entity (check lic)", dLog);

                    lic.Count++;

                    if ((lic.Entities > 0) && (lic.Count > lic.Entities))
                    {
                        String sId = "";
                        foreach (UserDataFields f in filter)
                        {
                            if (sId != "")
                            {
                                sId += ", ";
                            }
                            sId += f.Mapping.data_name + " = " + f.Value;
                        }

                        throw new Exception("License error: Entity not found and license limit (" + lic.Entities + " entities) exceeded. " + sId);
                        return(RegistryProcessStatus.Error);
                    }

                    tmp.Stop(dbAux.Connection, null);


                    userData.NewUser = true;

                    tmp = new TestTimer("Process->Add entity (UpdateName)", dLog);


                    userData.UpdateName();


                    tmp.Stop(dbAux.Connection, null);


                    //Cria o login
                    tmp = new TestTimer("Process->Add entity (MakeLogin)", dLog);

                    //Define o campo de login com base nas informações recebidas
                    foreach (UserDataFields f in fieldsData)
                    {
                        if (f.Mapping.is_login && !String.IsNullOrEmpty(f.Value.ToString()) && !String.IsNullOrWhiteSpace(f.Value.ToString()))
                        {
                            userData.Login = f.Value.ToString();
                        }
                    }

                    Log("Build login...");
                    userData.MakeLogin(pluginConfig.build_login, null);

                    tmp.Stop(dbAux.Connection, null);


                    tmp = new TestTimer("Process->Add entity (MakeEmail)", dLog);

                    //Cria o e-mail
                    Log("Build e-mail...");
                    if (pluginConfig.build_mail)
                    {
                        userData.MakeEmail(null, pluginConfig.mail_domain, pluginConfig.mail_field_id);
                    }

                    tmp.Stop(dbAux.Connection, null);

                    if (userData.FullName == null)
                    {
                        userData.FullName = userData.Login;
                    }

                    trans = db.Connection.BeginTransaction();

                    tmp = new TestTimer("Process->Add entity", dLog);

                    DbParameterCollection par = new DbParameterCollection();
                    par.Add("@resourcePluginId", typeof(Int64)).Value = resourcePluginId;
                    par.Add("@alias", typeof(String)).Value           = userData.FullName;
                    par.Add("@full_name", typeof(String)).Value       = userData.FullName;

                    DataTable dtEnt = ExecuteDataTable(db.Connection, "sp_new_entity_and_identity", CommandType.StoredProcedure, par, trans);
                    if ((dtEnt == null) || (dtEnt.Rows.Count == 0))
                    {
                        throw new Exception("Erro on insert entity & identity");
                    }

                    par.Clear();
                    par = null;

                    userData.EntityId   = (Int64)dtEnt.Rows[0]["id"];
                    userData.IdentityId = (Int64)dtEnt.Rows[0]["identity_id"];

                    Log("New entity/identity");

                    AddUserLog(db.Connection, LogKey.User_Added, null, "Engine", UserLogLevel.Info, 0, 0, 0, this.resourceId, this.pluginId, userData.EntityId, userData.IdentityId, "User added in IAM Database", this.internalLog.ToString(), trans);

                    tmp.Stop(dbAux.Connection, null);
                }
                else if (userData.IdentityId == 0)//Existe a entidade porém não a identidade
                {
                    tmp = new TestTimer("Process->Add identity", dLog);


                    trans = db.Connection.BeginTransaction();

                    DbParameterCollection par1 = new DbParameterCollection();
                    par1.Add("@entityId", typeof(Int64)).Value         = userData.EntityId;
                    par1.Add("@resourcePluginId", typeof(Int64)).Value = resourcePluginId;

                    DataTable dtEnt = ExecuteDataTable(db.Connection, "sp_new_identity", CommandType.StoredProcedure, par1, trans);
                    if ((dtEnt == null) || (dtEnt.Rows.Count == 0))
                    {
                        throw new Exception("Erro on insert identity");
                    }

                    par1.Clear();
                    par1 = null;

                    if ((Boolean)dtEnt.Rows[0]["new_identity"])
                    {
                        Log("New identity");
                    }

                    userData.IdentityId = (Int64)dtEnt.Rows[0]["identity_id"];

                    AddUserLog(db.Connection, LogKey.User_Added, null, "Engine", UserLogLevel.Info, 0, 0, 0, this.resourceId, this.pluginId, userData.EntityId, userData.IdentityId, "Identity added", this.internalLog.ToString(), trans);

                    tmp.Stop(dbAux.Connection, null);
                }

                try
                {
                    DbParameterCollection par = new DbParameterCollection();
                    par.Add("@entity_id", typeof(Int64)).Value = userData.EntityId;
                    par.Add("@date", typeof(DateTime)).Value   = this.package.GetBuildDate();
                    par.Add("@package_id", typeof(String), this.package.pkgId.Length).Value = this.package.pkgId;

                    dbAux.ExecuteNonQuery("UPDATE st_package_track SET entity_id = @entity_id where flow = 'inbound' and date = @date and package_id = @package_id", System.Data.CommandType.Text, par, null);
                }
                catch (Exception ex) {
#if DEBUG
                    internalLog.AppendLine("Error updating package track entity id: " + ex.Message);
#endif
                }

                if (trans == null)
                {
                    trans = db.Connection.BeginTransaction();
                }

                try
                {
                    tmp = new TestTimer("Process->Lockunlock", dLog);

                    //Só permite alterar este status se for um plugin de entrada
                    if ((pluginConfig.permit_add_entity) && (userData.Locked != userData.LastLocked))
                    {
                        Log((userData.Locked ? "Locking user" : "Unlocking user"));
                        AddUserLog(db.Connection, (userData.Locked ? LogKey.User_Locked : LogKey.User_Unlocked), null, "Engine", UserLogLevel.Debug, 0, 0, 0, this.resourceId, this.pluginId, userData.EntityId, userData.IdentityId, (userData.Locked ? "Locking user" : "Unlocking user"), (userData != null ? userData.LockedInfo : ""), trans);
                    }
                    else
                    {
                        //Caso não permitido retorna ao estado anterior
                        userData.Locked = userData.LastLocked;
                    }

                    tmp.Stop(dbAux.Connection, null);
                    tmp = new TestTimer("Process->UpdateFields", dLog);


                    //Atualiza as propriedades (fields)
                    Log("Updating user values...");
                    userData.UpdateFields(trans, pluginConfig.enable_import);

                    tmp.Stop(dbAux.Connection, null);


                    if (pluginConfig.enable_import)
                    {
                        tmp = new TestTimer("Process->BuildPassword", dLog);

                        Log("Building password...");
                        userData.BuildPassword(trans);

                        tmp.Stop(dbAux.Connection, null);
                        tmp = new TestTimer("Process->UpdateUser", dLog);

                        //Registro tudo que está pendente no banco
                        Log("Updating user data (name, login and password)...");
                        userData.UpdateUser(trans);

                        tmp.Stop(dbAux.Connection, null);
                        tmp = new TestTimer("Process->UpdateGroups", dLog);

                        //Registro tudo que está pendente no banco
                        if (pluginConfig.import_groups)
                        {
                            Log("Updating user groups...");
                            userData.UpdateGroups(trans, package.groups);
                        }

                        tmp.Stop(dbAux.Connection, null);
                    }

                    tmp = new TestTimer("Process->update collector_imports", dLog);

                    //Excluir estes registros processados
                    //ExecuteNonQuery(conn,"delete from collector_imports where " + where.Replace("ci.", ""), CommandType.Text, null, trans);
                    //ExecuteNonQuery(conn,"update collector_imports set status = 'I' where " + where.Replace("ci.", ""), CommandType.Text, null, trans);

                    /*	@plugin_uri varchar(500),
                     *  @resource_id bigint,
                     * @import_id varchar(40),
                     * @registry_id varchar(40),
                     * @status varchar(2),
                     * @new_status varchar(2)*/


                    tmp.Stop(dbAux.Connection, null);
                    tmp = new TestTimer("Process->Commit", dLog);

                    Log("Commit user data on database");
                    trans.Commit();
                    trans = null;

                    //try to rebuild user index
                    for (Int32 i = 0; i <= 5; i++)
                    {
                        try
                        {
                            if (pluginConfig.enable_import)
                            {
                                userData.RebuildIndexes(null);
                                break;
                            }
                            else
                            {
                                break;
                            }
                        }
                        catch {
                            Thread.Sleep(2000);
                        }
                    }

                    tmp.Stop(dbAux.Connection, null);

                    DbParameterCollection par = new DbParameterCollection();
                    par.Add("@resource_plugin_id", typeof(Int64)).Value = resourcePluginId;
                    par.Add("@import_id", typeof(String)).Value         = importId;
                    par.Add("@package_id", typeof(String)).Value        = packageId;
                    par.Add("@status", typeof(String)).Value            = 'F';
                    par.Add("@new_status", typeof(String)).Value        = 'I';

                    ExecuteNonQuery(db.Connection, "sp_migrate_imported2", CommandType.StoredProcedure, par, null);

                    par.Clear();
                    par = null;


                    /*
                     * ======================================*/
                }
                catch (Exception ex)
                {
                    if (trans != null)
                    {
                        trans.Rollback();
                    }

                    trans = null;

                    throw ex;
                }


                tmp = new TestTimer("Process->UpdateRoles", dLog);


                //Por fim verifica as roles
                if (pluginConfig.enable_import)
                {
                    userData.UpdateRoles(null, roleRules, this.pluginUri);
                }


                tmp.Stop(dbAux.Connection, null);


                try
                {
                    dbAux.AddPackageTrack(this.packageTrackId, "engine", "Process sucess: " + this.internalLog.ToString());
                }
                catch { }

#if DEBUG
                AddUserLog(dbAux.Connection, LogKey.User_ImportInfo, null, "Engine", UserLogLevel.Debug, 0, 0, 0, this.resourceId, this.pluginId, (userData != null ? userData.EntityId : 0), (userData != null ? userData.IdentityId : 0), "User process status", this.internalLog.ToString());
#endif

                Log("Success");
                return(RegistryProcessStatus.OK);
            }
            catch (Exception ex)
            {
                if (tmp != null)
                {
                    tmp.Stop(dbAux.Connection, null);
                }

                String traceError = "";
                traceError += "Erro: " + ex.Message + ex.StackTrace;

                Log("Erro: " + ex.Message);
                if (ex.InnerException != null)
                {
                    Log("Erro: " + ex.InnerException.Message);
                }

#if DEBUG
                Log("StackTrace: " + ex.StackTrace);
#endif

                if (showError)
                {
                    if (ex is SqlException)
                    {
                        AddUserLog(dbAux.Connection, LogKey.User_ImportError, null, "Engine", UserLogLevel.Error, 0, 0, 0, this.resourceId, this.pluginId, (userData != null ? userData.EntityId : 0), (userData != null ? userData.IdentityId : 0), ex.Message, SafeTrend.Json.JSON.Serialize2(new { import_id = importId, package_id = packageId, db_laet_error = LastDBError }));
                    }
                    else
                    {
                        AddUserLog(dbAux.Connection, LogKey.User_ImportError, null, "Engine", UserLogLevel.Error, 0, 0, 0, this.resourceId, this.pluginId, (userData != null ? userData.EntityId : 0), (userData != null ? userData.IdentityId : 0), ex.Message, SafeTrend.Json.JSON.Serialize2(new { import_id = importId, package_id = packageId, trace_error = traceError }));
                    }
                }

                try
                {
                    dbAux.AddPackageTrack(this.packageTrackId, "engine", "Process error: " + SafeTrend.Json.JSON.Serialize2(new { error_message = ex.Message, error_stack_trace = ex.StackTrace, import_id = importId, package_id = packageId, trace_error = traceError }));
                }
                catch { }

                //Se o erro for de deadlock, mantem o registro na base para ser reprocessado
                if (!(ex is SqlException) || ((ex is SqlException) && (ex.Message.IndexOf("deadlock") == -1)))
                {
                    ExecuteNonQuery(dbAux.Connection, "update collector_imports set status = 'E' where status = 'F' and resource_plugin_id = '" + this.resourcePluginId + "' and  import_id = '" + this.importId + "' and package_id = '" + this.packageId + "'", CommandType.Text, null);
                    ExecuteNonQuery(dbAux.Connection, "delete from collector_imports where status = 'E' and resource_plugin_id = '" + this.resourcePluginId + "' and  import_id = '" + this.importId + "' and package_id = '" + this.packageId + "'", CommandType.Text, null);
                }

                //Console.ReadLine();

                //System.Diagnostics.Process.GetCurrentProcess().Kill();
                //throw ex;


                if (trans != null)
                {
                    trans.Rollback();
                }

                trans = null;

                return(RegistryProcessStatus.Error);
            }
            finally
            {
                Log("End of registry processor");

                if (fieldsData != null)
                {
                    fieldsData.Clear();
                }
                fieldsData = null;
            }
        }
Esempio n. 6
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.HttpMethod != "POST")
            {
                return;
            }

            String area = "";

            if (!String.IsNullOrWhiteSpace((String)RouteData.Values["area"]))
            {
                area = (String)RouteData.Values["area"];
            }

            Int64 enterpriseId = 0;

            if ((Session["enterprise_data"]) != null && (Session["enterprise_data"] is EnterpriseData))
            {
                enterpriseId = ((EnterpriseData)Session["enterprise_data"]).Id;
            }

            Boolean newItem = false;

            if ((RouteData.Values["new"] != null) && (RouteData.Values["new"] == "1"))
            {
                newItem = true;
            }


            String ApplicationVirtualPath = Session["ApplicationVirtualPath"].ToString();

            LMenu menu1 = null;
            LMenu menu2 = null;
            LMenu menu3 = null;

            WebJsonResponse contentRet = null;
            HashData        hashData   = new HashData(this);

            String html  = "";
            String eHtml = "";
            String js    = null;



            String errorTemplate = "<span class=\"empty-results\">{0}</span>";
            String infoTemplate  = "<tr><td class=\"col1\">{0}</td><td class=\"col2\"><span class=\"no-edit\">{1}</span></td></tr>";

            switch (area)
            {
            case "":
            case "content":

                if (newItem)
                {
                    html += "<div id=\"upload-box\" class=\"upload-box\"><input type=\"file\" name=\"files[]\" multiple=\"\"><div class=\"drag-content\"><span class=\"upload-button-text\">Selecione arquivos para enviar</span><span class=\"upload-drag-drop-description\">Ou arraste e solte aqui</span></div><div class=\"dragDrop-content\"><span class=\"label l1\">Arraste a licença até aqui</span><span class=\"label l2\">Solte a licença</span></div></div>";

                    html += "<h3>Informações da licença</h3>";
                    html += "<form  id=\"form_plugin_add\"  method=\"POST\" action=\"" + ApplicationVirtualPath + "admin/license/action/add_new/\">";
                    html += "<div id=\"files\" class=\"box-container\"><div class=\"no-tabs pb10 none\">Nenhum upload realizado</div></div>";

                    html += "<button type=\"submit\" id=\"upload-save\" class=\"button secondary floatleft\">Aplicar licença</button>    <a href=\"" + ApplicationVirtualPath + "admin/license/" + (Request.Form["hashtag"] != null ? "#" + Request.Form["hashtag"].ToString() : "") + "\" class=\"button link floatleft\">Cancelar</a></form>";
                    html += "</form>";

                    contentRet    = new WebJsonResponse("#content-wrapper", (eHtml != "" ? eHtml : html));
                    contentRet.js = "iamadmin.licenseUploader($('#upload-box'), '" + ApplicationVirtualPath + "admin/license/action/upload/','" + ApplicationVirtualPath + "admin/license/action/upload_item_template/')";
                }
                else
                {
                    html += "<h3>" + MessageResource.GetMessage("licensing");

                    if (hashData.GetValue("edit") != "1")
                    {
                        html += "<div class=\"btn-box\"><div class=\"a-btn ico icon-change\" onclick=\"window.location='" + ApplicationVirtualPath + "admin/license/new/'\">Carregar nova licença</div></div>";
                    }

                    html += "</h3>";

                    LicenseControl lic = null;

                    using (IAMDatabase db = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                        lic = LicenseChecker.GetLicenseData(db.Connection, null, enterpriseId);

                    //Carrega o certificado licenciado
                    if (lic != null)
                    {
                        html += "<div class=\"no-tabs fields\"><table><tbody>";

                        html += String.Format(infoTemplate, "Chave de instalação", lic.InstallationKey);
                        html += String.Format(infoTemplate, "Status da licença", (lic.Valid ? "Válida" : lic.Error));

                        html += "</tbody></table><div class=\"clear-block\"></div></div>";
                    }
                    else
                    {
                        eHtml += String.Format(errorTemplate, "Falha ao carregar a licença");
                    }
                    contentRet = new WebJsonResponse("#content-wrapper", (eHtml != "" ? eHtml : html));
                }
                break;

            case "sidebar":
                if (menu1 != null)
                {
                    html += "<div class=\"section-nav-header\">";
                    html += "    <div class=\"crumbs\">";
                    html += "        <div class=\"subject subject-color\">";
                    html += "            <a href=\"" + menu1.HRef + "\">" + menu1.Name + "</a>";
                    html += "        </div>";
                    if (menu2 != null)
                    {
                        html += "        <div class=\"topic topic-color\">";
                        html += "            <a href=\"" + menu2.HRef + "\">" + menu2.Name + "</a>";
                        html += "        </div>";
                    }
                    html += "    </div>";
                    if (menu3 != null)
                    {
                        html += "    <div class=\"crumbs tutorial-title\">";
                        html += "        <h2 class=\"title tutorial-color\">" + menu3.Name + "</h2>";
                        html += "    </div>";
                    }
                    html += "</div>";
                }


                DbParameterCollection par = new DbParameterCollection();;
                par.Add("@enterpriseId", typeof(Int64)).Value = enterpriseId;

                using (IAMDatabase db = new IAMDatabase(IAMDatabase.GetWebConnectionString()))
                {
                    DataTable dtUsers = db.ExecuteDataTable("sp_user_statistics", System.Data.CommandType.StoredProcedure, par);
                    if ((dtUsers != null) && (dtUsers.Rows.Count > 0))
                    {
                        Int64 total  = (Int64)dtUsers.Rows[0]["total"];
                        Int64 locked = (Int64)dtUsers.Rows[0]["locked"];
                        Int64 logged = (Int64)dtUsers.Rows[0]["logged"];

                        Int32 pLocked = 0;
                        Int32 pLogged = 0;

                        try
                        {
                            pLocked = (Int32)(((Double)locked / (Double)total) * 100F);
                        }
                        catch { }

                        try
                        {
                            pLogged = (Int32)(((Double)logged / (Double)total) * 100F);
                        }
                        catch { }

                        html += "<div class=\"ds1\">";
                        html += "<div class=\"center\">" + MessageResource.GetMessage("entity") + "</div>";
                        html += "<div class=\"center\"><span class=\"big\">" + total + "</span><span class=\"small\"> " + MessageResource.GetMessage("total") + "</span></div>";

                        html += "<div class=\"center\"><canvas id=\"usrLockChart\" width=\"30\" height=\"30\"></canvas><span class=\"big txt1\">" + locked + "<span> <span class=\"small\">" + MessageResource.GetMessage("locked") + "<span></div>";
                        html += "<div class=\"center\"><canvas id=\"usrLoggedChart\" width=\"30\" height=\"30\"></canvas><span class=\"big txt1\">" + logged + "<span> <span class=\"small\">" + MessageResource.GetMessage("logged") + "<span></div>";
                        //html += "<div class=\"center\"><canvas id=\"usrTotalChart\" width=\"30\" height=\"30\"></canvas><span class=\"big txt1\">" + total + "<span> <span class=\"small\">Total<span></div>";

                        html += "</div>";


                        //js += "iamadmin.buildPercentChart('#usrTotalChart',100,{color:'#2d88b4',showText:false});";
                        js += "iamadmin.buildPercentChart('#usrLockChart'," + pLocked + ",{color:'#f5663a',showText:false});";
                        js += "iamadmin.buildPercentChart('#usrLoggedChart'," + pLogged + ",{color:'#76c558',showText:false});";
                    }

                    html += "<div class=\"ds2\">";
                    html += "<div class=\"center\">" + MessageResource.GetMessage("licensing") + "</div>";


                    try
                    {
                        String rData = "";
                        String query = "";

                        if (!String.IsNullOrWhiteSpace((String)RouteData.Values["query"]))
                        {
                            query = (String)RouteData.Values["query"];
                        }

                        var tmpReq = new
                        {
                            jsonrpc    = "1.0",
                            method     = "license.info",
                            parameters = new String[0],
                            id         = 1
                        };

                        rData = SafeTrend.Json.JSON.Serialize2(tmpReq);

                        String jData = "";
                        try
                        {
                            jData = WebPageAPI.ExecuteLocal(db, this, rData);
                        }
                        finally
                        {
                        }

                        if (String.IsNullOrWhiteSpace(jData))
                        {
                            throw new Exception("");
                        }

                        License ret2 = JSON.Deserialize <License>(jData);
                        if (ret2 == null)
                        {
                            eHtml += String.Format(errorTemplate, MessageResource.GetMessage("api_error"));
                        }
                        else if (ret2.error != null)
                        {
                            eHtml += String.Format(errorTemplate, ret2.error.data);
                        }
                        else if (ret2.result == null)
                        {
                            eHtml += String.Format(errorTemplate, MessageResource.GetMessage("api_error"));
                        }
                        else
                        {
                            Int32 percent = 0;
                            if (ret2.result.hasLicense)
                            {
                                if (ret2.result.available == 0)    //Licença ilimitada
                                {
                                    percent = 0;
                                }
                                else if (ret2.result.used > ret2.result.available)
                                {
                                    percent = 100;
                                }
                                else
                                {
                                    percent = (ret2.result.used / ret2.result.available) * 100;
                                }
                            }
                            else
                            {
                                percent = 100;
                            }


                            String color = "#76c558";
                            if (percent < 70)
                            {
                                color = "#76c558";
                            }
                            else if (percent < 85)
                            {
                                color = "#f5663a";
                            }
                            else
                            {
                                color = "rgb(202, 52, 56)";
                            }

                            js += "iamadmin.buildPercentChart('#licChart'," + percent + ",{color:'" + color + "',showText:true});";

                            html += "<canvas id=\"licChart\" width=\"100\" height=\"100\" class=\"big-center\"></canvas>";
                            if (ret2.result.hasLicense)
                            {
                                html += "<div class=\"center\">" + MessageResource.GetMessage("licensing_total") + "</div>";
                                html += "<div class=\"center\"><span class=\"big\">" + ret2.result.used + "</span><span class=\"small\"> " + MessageResource.GetMessage("of") + " " + (ret2.result.available == 0 ? MessageResource.GetMessage("unlimited") : ret2.result.available.ToString()) + "</span></div>";
                            }
                            else
                            {
                                html += "<div class=\"center\"><span class=\"big\">" + MessageResource.GetMessage("no_licecse") + "</span></div>";
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        eHtml += String.Format(errorTemplate, MessageResource.GetMessage("api_error"));
                        //ret = new WebJsonResponse("", MessageResource.GetMessage("api_error"), 3000, true);
                    }


                    html += "</div>";
                }

                contentRet    = new WebJsonResponse("#main aside", (eHtml != "" ? eHtml : html));
                contentRet.js = js;

                break;

            case "mobilebar":
                break;


            case "buttonbox":
                break;
            }

            if (contentRet != null)
            {
                if (!String.IsNullOrWhiteSpace((String)Request["cid"]))
                {
                    contentRet.callId = (String)Request["cid"];
                }

                Retorno.Controls.Add(new LiteralControl(contentRet.ToJSON()));
            }
        }
Esempio n. 7
0
        private void TmrCallback(Object o)
        {
            if (executing)
            {
                return;
            }

            executing = true;

            TextLog.Log("Engine", "Importer", "Starting registry processor timer");
            Console.WriteLine("Starting registry processor timer");
            IAMDatabase db        = null;
            Stopwatch   stopWatch = new Stopwatch();

            stopWatch.Start();


            Dictionary <Int64, PluginConfig> resourcePluginCache = new Dictionary <Int64, PluginConfig>();

            StringBuilder procLog  = new StringBuilder();
            Boolean       writeLog = false;

            last_status = "Iniciando...";
            try
            {
                db = new IAMDatabase(localConfig.SqlServer, localConfig.SqlDb, localConfig.SqlUsername, localConfig.SqlPassword);
                db.openDB();
                db.Timeout = 600;
                //db.Debug = true;

                Console.WriteLine("Select data...");

                Taskbar.TaskbarProgress.SetProgressState(Taskbar.TaskbarProgressState.Indeterminate);
                startTime = DateTime.Now;
                newUsers  = 0;
                errors    = 0;
                totalReg  = 0;
                ignored   = 0;
                atualReg  = 0;

                //Seleciona os registros prontos para serem importados
                //Não colocar order neste select, fica extremamente lento
                //Coloca um limite de 500.000 somente p/ não estourar memória
                last_status = "Selecionando registros a serem processados";
                DataTable dtRegs = db.Select("select top 5000 * from vw_collector_imports_regs with(nolock) order by priority desc");

                if (dtRegs == null)
                {
                    TextLog.Log("Engine", "Importer", "\tError on select registries: " + db.LastDBError);
                    db.AddUserLog(LogKey.Engine, null, "Engine", UserLogLevel.Error, 0, 0, 0, 0, 0, 0, 0, "Error on select registries: " + db.LastDBError);
                    executing = false;
                    return;
                }

                if (dtRegs.Rows.Count == 0)
                {
                    TextLog.Log("Engine", "Importer", "\t0 registers to process");
                    Console.WriteLine("0 registers to process");
                    executing = false;
                    return;
                }

                totalReg = dtRegs.Rows.Count;

                TextLog.Log("Engine", "Importer", "\t" + dtRegs.Rows.Count + " registers to process");
                procLog.AppendLine("[" + DateTime.Now.ToString("o") + "] " + dtRegs.Rows.Count + " registers to process");
                Console.WriteLine(dtRegs.Rows.Count + " registers to process");

                //Carrega todos os logins do sistema
                Console.WriteLine("Fetch logins...");
                last_status = "Listando login do sistema";
                DataTable dtLogins = db.Select("select context_id,id,login from vw_entity_logins2 with(nolock)");
                if ((dtLogins != null) || (dtLogins.Rows.Count > 0))
                {
                    foreach (DataRow dr in dtLogins.Rows)
                    {
                        LoginCache.AddItem((Int64)dr["context_id"], (Int64)dr["id"], dr["login"].ToString());
                    }
                }

                //Carrega todos os e-mails do sistema
                Console.WriteLine("Fetch e-mails...");
                last_status = "Listando e-mails do sistema";
                DataTable dtEmails = db.Select("select context_id, entity_id, mail from vw_entity_mails with(nolock)");
                if ((dtEmails != null) || (dtEmails.Rows.Count > 0))
                {
                    foreach (DataRow dr in dtEmails.Rows)
                    {
                        EmailCache.AddItem((Int64)dr["context_id"], (Int64)dr["entity_id"], dr["mail"].ToString());
                    }
                }


                //Calcula a quantidade de threads com base na quantidade de registros
                Int32 tCount = dtRegs.Rows.Count / 10;

                if (tCount < 1)
                {
                    tCount = 1;
                }
                else if (tCount > this.maxThreads)
                {
                    tCount = this.maxThreads;
                }

#if DEBUG
                tCount = 1;
#endif

                DebugMessage dbgC = new DebugMessage(delegate(String message)
                {
                    procLog.AppendLine(message);
                });



                Console.WriteLine("Starting...");
                queueManager = new QueueManager <RegistryProcessStarter>(tCount, ProcQueue);
                queueManager.OnThreadStart += new QueueManager <RegistryProcessStarter> .StartThread(delegate(Int32 threadIndex)
                {
                    LocalTheadObjects obj = new LocalTheadObjects();
                    for (Int32 t = 0; t <= 10; t++)
                    {
                        try
                        {
                            obj.db = new IAMDatabase(localConfig.SqlServer, localConfig.SqlDb, localConfig.SqlUsername, localConfig.SqlPassword);
                            obj.db.openDB();
                            obj.db.Timeout = 600;

#if DEBUG
                            //obj.db.Debug = true;
#endif

                            obj.lockRules   = new LockRules();
                            obj.ignoreRules = new IgnoreRules();
                            obj.roleRules   = new RoleRules();
                            obj.lockRules.GetDBConfig(obj.db.Connection);
                            obj.ignoreRules.GetDBConfig(obj.db.Connection);
                            obj.roleRules.GetDBConfig(obj.db.Connection);
                            obj.debugCallback = dbgC;
                            break;
                        }
                        catch (Exception ex) {
                            if (t >= 10)
                            {
                                throw ex;
                            }
                        }
                    }

                    return(obj);
                });

                queueManager.OnThreadStop += new QueueManager <RegistryProcessStarter> .ThreadStop(delegate(Int32 threadIndex, Object state)
                {
                    if ((state != null) && (state is LocalTheadObjects))
                    {
                        ((LocalTheadObjects)state).Dispose();
                    }

                    state = null;
                });


                Console.WriteLine("Starting treads...");
                last_status = "Iniciando treads";
                queueManager.Start();

                if (queueManager.ExecutingCount == 0)
                {
                    throw new Exception("Erro on start queue manager");
                }

                /*
                 * _queue = new RegistryQueue[tCount];
                 * Int32 qIndex = 0;
                 *
                 * for (Int32 i = 0; i < _queue.Length; i++)
                 *  _queue[i] = new RegistryQueue();
                 */

                Taskbar.TaskbarProgress.SetProgressState(Taskbar.TaskbarProgressState.Normal);
                Taskbar.TaskbarProgress.SetProgressValue(0, (Int32)totalReg, System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);

                Int32 addCount = 0;
                last_status = "Processando registros";
                foreach (DataRow dr in dtRegs.Rows)
                {
                    Int64 enterpriseId = (Int64)dr["enterprise_id"];
                    Int64 contextId    = (Int64)dr["context_id"];

                    LicenseControl lic = null;
                    if (!licControl.ContainsKey(enterpriseId))
                    {
                        lic = LicenseChecker.GetLicenseData(db.Connection, null, enterpriseId);
                        licControl.Add(enterpriseId, lic);
                    }
                    else
                    {
                        lic = licControl[enterpriseId];
                    }

                    if (!lic.Valid)
                    {
                        if (!lic.Notified)
                        {
                            db.AddUserLog(LogKey.Licence_error, null, "Engine", UserLogLevel.Error, 0, enterpriseId, 0, (Int64)dr["resource_id"], (Int64)dr["plugin_id"], 0, 0, "License error: " + lic.Error);
                        }
                        lic.Notified = true;

                        db.ExecuteNonQuery("update collector_imports set status = 'LE' where status = 'F' and resource_plugin_id = '" + dr["resource_id"] + "' and  import_id = '" + dr["import_id"] + "' and package_id = '" + dr["package_id"] + "'", CommandType.Text, null);

                        continue;
                    }

                    if ((lic.Entities > 0) && (lic.Count > lic.Entities))
                    {
                        if (!lic.Notified)
                        {
                            db.AddUserLog(LogKey.Licence_error, null, "Engine", UserLogLevel.Error, 0, enterpriseId, 0, (Int64)dr["resource_id"], (Int64)dr["plugin_id"], 0, 0, "License error: License limit (" + lic.Entities + " entities) exceeded");
                        }
                        lic.Notified = true;

                        db.ExecuteNonQuery("update collector_imports set status = 'LE' where status = 'F' and resource_plugin_id = '" + dr["resource_id"] + "' and  import_id = '" + dr["import_id"] + "' and package_id = '" + dr["package_id"] + "'", CommandType.Text, null);

                        continue;
                    }


                    if (!entKeys.ContainsKey(enterpriseId))
                    {
                        entKeys.Add(enterpriseId, new EnterpriseKeyConfig(db.Connection, enterpriseId));
                    }

                    if (entKeys[enterpriseId] == null)
                    {
                        entKeys[enterpriseId] = new EnterpriseKeyConfig(db.Connection, enterpriseId);
                    }

                    addCount++;
                    queueManager.AddItem(new RegistryProcessStarter(enterpriseId, contextId, new Uri(dr["plugin_uri"].ToString()), Int64.Parse(dr["resource_id"].ToString()), Int64.Parse(dr["plugin_id"].ToString()), Int64.Parse(dr["resource_plugin_id"].ToString()), (String)dr["import_id"], (String)dr["package_id"], (String)dr["package"]));

                    //A cada 100 registros monitora a CPU para adicionar mais registros
                    //O Objetivo deste processo é controlar a carga de processamento
                    if (addCount >= 100)
                    {
                        addCount = 0;
                        Int32 c = 0;
                        while (((c = queueManager.QueueCount) > 500) || ((getCPUCounter() >= 70) && (c > 0)))
                        {
                            Thread.Sleep(500);
                        }
                    }


                    /*
                     * _queue[qIndex].Add(enterpriseId, contextId, Int64.Parse(dr["plugin_id"].ToString()), (String)dr["plugin_uri"], Int64.Parse(dr["resource_id"].ToString()), (String)dr["import_id"], (String)dr["registry_id"]);
                     *
                     * qIndex++;
                     * if (qIndex > _queue.Length - 1) qIndex = 0;
                     */
                }



                /*
                 * for (Int32 i = 0; i < _queue.Length; i++)
                 * {
                 *  Thread procQueue = new Thread(new ParameterizedThreadStart(ProcQueue));
                 *  procQueue.Start(i);
                 *  //Thread.Sleep(1000);
                 * }*/

                Console.WriteLine("Waiting treads execution...");

                /*
                 * Int64 rest = 0;
                 * Double percent = 0;
                 * Int32 iPercent = 0;
                 * do
                 * {
                 *  rest = 0;
                 *
                 *  rest = queueManager.QueueCount;
                 *
                 *  //for (Int32 i = 0; i < _queue.Length; i++)
                 *  //    rest += _queue[i].Count;
                 *
                 *  percent = ((Double)(totalReg - rest) / (Double)totalReg) * 100F;
                 *
                 *  if (iPercent != (Int32)percent)
                 *  {
                 *      iPercent = (Int32)percent;
                 *      procLog.AppendLine("[" + DateTime.Now.ToString("o") + "] " + iPercent + "%");
                 *      TextLog.Log("Engine", "Importer", "\t" + iPercent + "%");
                 *      Console.Write(" " + iPercent + "% ");
                 *
                 *      Taskbar.TaskbarProgress.SetProgressValue((Int32)(totalReg - rest), (Int32)totalReg, System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
                 *
                 *  }
                 *
                 *  Thread.Sleep(1000);
                 *
                 * } while (rest > 0);*/


                //Envia comando para finalizar a execução e aguarda a finalização
                last_status = "Processando registros";
                queueManager.StopAndWait();


                Taskbar.TaskbarProgress.SetProgressState(Taskbar.TaskbarProgressState.Indeterminate);

                last_status = "Finalizando";
                Console.WriteLine("Finishing...");

                if (dtRegs.Rows.Count > 0)
                {
                    writeLog = true;
                }

                procLog.AppendLine("New users: " + newUsers);
                procLog.AppendLine("Errors: " + errors);
                procLog.AppendLine("Ignored: " + ignored);
                procLog.AppendLine("Updated: " + (totalReg - errors - ignored - newUsers));

                procLog.AppendLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] Import registry processed with " + dtRegs.Rows.Count + " registers");

                //Joga todos os registros para a tabela de importados
                //e exclui da atual
                db.ExecuteNonQuery("sp_migrate_imported", CommandType.StoredProcedure, null);


                //Reconstroi os índices das tabelas de entidades e identidades
                try
                {
                    db.ExecuteNonQuery("sp_reindex_entity", CommandType.StoredProcedure, null);
                    db.ExecuteNonQuery("sp_rebuild_entity_keys", CommandType.StoredProcedure, null);
                }
                catch { }

                Console.WriteLine("");
            }
            catch (SqlException e)
            {
                procLog.AppendLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] DB Error on registry processor: " + e.Message);
                procLog.AppendLine(db.LastDBError);

                db.AddUserLog(LogKey.Import, null, "Engine", UserLogLevel.Error, 0, 0, 0, 0, 0, 0, 0, "DB Error on registry processor", procLog.ToString());
                TextLog.Log("Engine", "Importer", "\tError on registry processor timer " + e.Message + " " + db.LastDBError);
            }
            catch (OutOfMemoryException ex)
            {
                procLog.AppendLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] Error on registry processor: " + ex.Message);

                db.AddUserLog(LogKey.Import, null, "Engine", UserLogLevel.Error, 0, 0, 0, 0, 0, 0, 0, "Out Of Memory processing registry, killing processor", procLog.ToString());
                TextLog.Log("Engine", "Importer", "\tError on registry processor timer " + ex.Message);

                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
            catch (Exception ex)
            {
                procLog.AppendLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] Error on registry processor: " + ex.Message);

                db.AddUserLog(LogKey.Import, null, "Engine", UserLogLevel.Error, 0, 0, 0, 0, 0, 0, 0, "Error on registry processor", procLog.ToString());
                TextLog.Log("Engine", "Importer", "\tError on registry processor timer " + ex.Message);
            }
            finally
            {
                stopWatch.Stop();
                TimeSpan ts = stopWatch.Elapsed;

                executing   = false;
                last_status = "";

                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:0000}", ts.TotalHours, ts.Minutes, ts.Seconds, ts.Milliseconds);
                TextLog.Log("Engine", "Importer", "\tElapsed time: " + elapsedTime);

                TextLog.Log("Engine", "Importer", "\tScheduled for new registry processor in 60 seconds");
                TextLog.Log("Engine", "Importer", "Finishing registry processor timer");

                procLog.AppendLine("[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "] Elapsed time: " + elapsedTime);

                Console.WriteLine("Import registry processed " + procLog.ToString());
                Console.WriteLine("Elapsed time: " + elapsedTime);

                if (writeLog)
                {
                    db.AddUserLog(LogKey.Import, null, "Engine", UserLogLevel.Info, 0, 0, 0, 0, 0, 0, 0, "Import registry processed", procLog.ToString());
                }

                Taskbar.TaskbarProgress.SetProgressState(Taskbar.TaskbarProgressState.NoProgress);

                startTime = new DateTime(1970, 1, 1);

                try
                {
                    List <Int64> keys = new List <Int64>();
                    if ((entKeys != null) && (entKeys.Count > 0))
                    {
                        keys.AddRange(entKeys.Keys);
                        foreach (Int64 k in keys)
                        {
                            try
                            {
                                if (entKeys[k] != null)
                                {
                                    entKeys[k].Dispose();
                                    entKeys[k] = null;
                                }
                            }
                            catch { }
                            try
                            {
                                entKeys.Remove(k);
                            }
                            catch { }
                        }
                    }
                }
                catch { }

                try
                {
                    licControl.Clear();
                }
                catch { }

                try
                {
                    LoginCache.Clear();
                }
                catch { }

                if (db != null)
                {
                    db.Dispose();
                }

                db = null;

                Thread.CurrentThread.Abort();
            }
        }