Exemple #1
0
        /// <summary>
        /// Método privado para processamento do método 'user.resetpassword'
        /// </summary>
        /// <param name="sqlConnection">Conexão com o banco de dados MS-SQL</param>
        /// <param name="parameters">Dicionário (String, Object) contendo todos os parâmetros necessários</param>
        private Dictionary <String, Object> change(IAMDatabase database, Dictionary <String, Object> parameters)
        {
            Dictionary <String, Object> result = new Dictionary <String, Object>();

            if (!parameters.ContainsKey("enterpriseid"))
            {
                Error(ErrorType.InvalidRequest, "Parameter enterpriseid is not defined.", "", null);
                return(null);
            }

            String enterprise = parameters["enterpriseid"].ToString();

            if (String.IsNullOrWhiteSpace(enterprise))
            {
                Error(ErrorType.InvalidRequest, "Parameter enterpriseid is not defined.", "", null);
                return(null);
            }

            Int64 enterpriseid = 0;

            try
            {
                enterpriseid = Int64.Parse(enterprise);
            }
            catch
            {
                Error(ErrorType.InvalidRequest, "Parameter enterpriseid is not a long integer.", "", null);
                return(null);
            }


            DbParameterCollection par = new DbParameterCollection();

            par.Add("@enterprise_id", typeof(Int64)).Value = enterpriseid;

            DataTable dtEnterprise = database.ExecuteDataTable("select * from enterprise where id = @enterprise_id", CommandType.Text, par, null);

            if (dtEnterprise == null)
            {
                Error(ErrorType.InternalError, "", "", null);
                return(null);
            }

            if (dtEnterprise.Rows.Count == 0)
            {
                Error(ErrorType.InvalidRequest, "Enterprise not found.", "", null);
                return(null);
            }

            List <String> hosts = new List <String>();
            Dictionary <String, String> pgValues = new Dictionary <string, string>();
            Uri pluginUri = null;

            String  updateSQL      = "update enterprise set ";
            String  updateFields   = "";
            Boolean update         = false;
            Boolean updateHosts    = false;
            Boolean updateAuthPars = false;

            foreach (String key in parameters.Keys)
            {
                switch (key.ToLower())
                {
                case "name":
                    String name = parameters["name"].ToString();
                    if (!String.IsNullOrWhiteSpace(name))
                    {
                        par.Add("@name", typeof(String)).Value = name;
                        if (updateFields != "")
                        {
                            updateFields += ", ";
                        }
                        updateFields += "name = @name";
                        update        = true;
                    }
                    else
                    {
                        Error(ErrorType.InvalidRequest, "Parameter name is empty.", "", null);
                        return(null);
                    }
                    break;

                case "auth_plugin":

                    String auth_plugin = parameters["auth_plugin"].ToString();
                    if (!String.IsNullOrWhiteSpace(auth_plugin))
                    {
                        try
                        {
                            Uri tmp = new Uri(auth_plugin);
                            if (tmp.Scheme.ToLower() != "auth")
                            {
                                throw new Exception();
                            }
                        }
                        catch
                        {
                            Error(ErrorType.InvalidRequest, "Parameter auth_plugin is not a valid uri.", "", null);
                            return(null);
                        }

                        try
                        {
                            AuthBase plugin = AuthBase.GetPlugin(new Uri(auth_plugin));
                            if (plugin == null)
                            {
                                throw new Exception();
                            }
                        }
                        catch
                        {
                            Error(ErrorType.InvalidRequest, MessageResource.GetMessage("invalid_auth_service"), "", null);
                            break;
                        }


                        par.Add("@auth_plugin", typeof(String)).Value = auth_plugin;
                        if (updateFields != "")
                        {
                            updateFields += ", ";
                        }
                        updateFields += "auth_plugin = @auth_plugin";
                        update        = true;
                    }
                    else
                    {
                        Error(ErrorType.InvalidRequest, "Parameter auth_plugin is empty.", "", null);
                        return(null);
                    }
                    break;

                case "fqdn_alias":
                    if (parameters[key] is ArrayList)
                    {
                        updateHosts = true;

                        List <Object> ht = new List <Object>();
                        ht.AddRange(((ArrayList)parameters[key]).ToArray());
                        foreach (String host in ht)
                        {
                            if (!String.IsNullOrWhiteSpace(host))
                            {
                                try
                                {
                                    Uri tmp = new Uri("http://" + host);
                                    hosts.Add(host);
                                }
                                catch
                                {
                                    Error(ErrorType.InvalidRequest, "Parameter fqdn_alias->" + host + " is not a valid hostname.", "", null);
                                    return(null);
                                }
                            }
                        }
                    }
                    break;


                case "auth_paramters":
                    if (parameters[key] is Dictionary <String, Object> )
                    {
                        if (!parameters.ContainsKey("auth_plugin"))
                        {
                            Error(ErrorType.InvalidRequest, "Parameter auth_plugin is not defined.", "", null);
                            return(null);
                        }

                        if (String.IsNullOrWhiteSpace(parameters["auth_plugin"].ToString()))
                        {
                            Error(ErrorType.InvalidRequest, "Parameter auth_plugin is not defined.", "", null);
                            return(null);
                        }

                        try
                        {
                            Uri tmp = new Uri(parameters["auth_plugin"].ToString());
                            if (tmp.Scheme.ToLower() != "auth")
                            {
                                throw new Exception();
                            }
                        }
                        catch
                        {
                            Error(ErrorType.InvalidRequest, "Parameter auth_plugin is not a valid uri.", "", null);
                            return(null);
                        }

                        AuthBase plugin = null;
                        try
                        {
                            plugin = AuthBase.GetPlugin(new Uri(parameters["auth_plugin"].ToString()));
                            if (plugin == null)
                            {
                                throw new Exception();
                            }
                        }
                        catch
                        {
                            Error(ErrorType.InvalidRequest, MessageResource.GetMessage("invalid_auth_service"), "", null);
                            break;
                        }

                        pluginUri = plugin.GetPluginId();

                        updateAuthPars = true;

                        Dictionary <String, Object> p1 = (Dictionary <String, Object>)parameters[key];

                        AuthConfigFields[] fields = plugin.GetConfigFields();
                        foreach (AuthConfigFields f in fields)
                        {
                            String value = "";

                            if (p1.ContainsKey(f.Key))
                            {
                                value = p1[f.Key].ToString();
                            }

                            if (!String.IsNullOrEmpty(value))
                            {
                                pgValues.Add(f.Key, value);
                            }

                            if (f.Required && !pgValues.ContainsKey(f.Key))
                            {
                                Error(ErrorType.InvalidRequest, MessageResource.GetMessage("required_field") + " " + f.Name, "", null);
                                break;
                            }
                        }
                    }
                    break;
                }
            }

            if (update)
            {
                updateSQL += updateFields + " where id = @enterprise_id";
                database.ExecuteNonQuery(updateSQL, CommandType.Text, par);
            }

            if (updateHosts)
            {
                foreach (String host in hosts)
                {
                    if (!String.IsNullOrWhiteSpace(host))
                    {
                        DbParameterCollection par1 = new DbParameterCollection();
                        par1.Add("@enterprise_id", typeof(Int64)).Value = enterpriseid;
                        par1.Add("@fqdn", typeof(String)).Value         = host;

                        database.ExecuteNonQuery("insert into enterprise_fqdn_alias (enterprise_id, fqdn) select @enterprise_id, @fqdn where not exists (select 1 from enterprise_fqdn_alias where enterprise_id = @enterprise_id and fqdn = @fqdn) ", CommandType.Text, par1);
                    }
                }

                database.ExecuteNonQuery("delete from enterprise_fqdn_alias where enterprise_id = @enterprise_id " + (hosts.Count > 0 ? " and fqdn not in ('" + String.Join("', '", hosts) + "')" : ""), CommandType.Text, par);
            }


            if (updateAuthPars)
            {
                database.ExecuteNonQuery("delete from enterprise_auth_par where enterprise_id = @enterprise_id and plugin = '" + pluginUri.AbsoluteUri + "'", CommandType.Text, par);

                foreach (String key in pgValues.Keys)
                {
                    if (!String.IsNullOrWhiteSpace(pgValues[key]))
                    {
                        DbParameterCollection par1 = new DbParameterCollection();
                        par1.Add("@enterprise_id", typeof(Int64)).Value = enterpriseid;
                        par1.Add("@plugin", typeof(String)).Value       = pluginUri.AbsoluteUri;
                        par1.Add("@key", typeof(String)).Value          = key;
                        par1.Add("@value", typeof(String)).Value        = pgValues[key];

                        database.ExecuteNonQuery("insert into enterprise_auth_par (enterprise_id, plugin,[key],[value]) VALUES(@enterprise_id, @plugin, @key, @value)", CommandType.Text, par1);
                    }
                }
            }

            //Atualiza a busca com os dados atualizados
            return(get(database, parameters));
        }