Esempio n. 1
0
        //_________________________________________________________________________________________________________
        //_________________________________________________________________________________________________________
        private Dictionary <string, string> AddPropertiesLoaded(SearchResult r, bool bAllAtribs = false, string sOtherAttribs = "")
        {
            Dictionary <string, string> salida = new Dictionary <string, string>();

            for (int i = 0; i < tAttributes.Rows.Count; i++)
            {
                string sUsage = tAttributes.Rows[i]["Usage"].ToString();
                string name   = tAttributes.Rows[i][Row["Alias"].ToString()].ToString();
                string alias  = tAttributes.Rows[i]["Alias"].ToString();
                if (bAllAtribs || (sUsage.Contains("Link") || sUsage.Contains("ID") || sUsage.Contains("Alias") || sOtherAttribs.Contains(name)))
                {
                    if (r.Properties[name].Count == 0)
                    {
                        salida.Add(alias, name == "" ? "" : "<ND>");
                    }
                    if (r.Properties[name].Count == 1 && sUsage.Contains("Jpeg") && !sUsage.Contains("Guid"))
                    {
                        salida.Add(alias, BitConverter.ToString(r.Properties[name][0] as byte[]).Replace("-", "").Substring(0, 30));
                        using (MemoryStream ms = new MemoryStream(r.Properties[name][0] as byte[])) {
                            Bitmap.FromStream(ms).Save(Config.PathImages + this.Name + "_" + alias + "_0.jpg");
                        }
                    }
                    if (r.Properties[name].Count == 1 && !sUsage.Contains("Jpeg") && !sUsage.Contains("Guid"))
                    {
                        salida.Add(alias, r.Properties[name][0].ToString());
                    }
                    if (r.Properties[name].Count == 1 && sUsage.Contains("Guid"))
                    {
                        salida.Add(alias, name == "objectGUID" ? ObjectsHelper.ConvertGuidToString((byte[])r.Properties[name][0]) : r.Properties[name][0].ToString());
                    }
                    if (r.Properties[name].Count == 1 && sUsage.Contains("ID") && !sUsage.Contains("Guid"))
                    {
                        salida.Add("#ID#", r.Properties[name][0].ToString());
                    }
                    if (r.Properties[name].Count > 1)
                    {
                        string s = "";
                        for (int j = 0; j < r.Properties[name].Count; j++)
                        {
                            if (sUsage.Contains("Jpeg"))
                            {
                                s += (s == "" ? "" : Config.Separator) + BitConverter.ToString(r.Properties[name][j] as byte[]).Replace("-", "").Substring(0, 30);
                                using (MemoryStream ms = new MemoryStream(r.Properties[name][j] as byte[])) {
                                    Bitmap.FromStream(ms).Save(Config.PathImages + this.Name + "_" + alias + "_" + j + ".jpg");
                                }
                            }
                            else
                            {
                                s += (s == "" ? "" : Config.Separator) + r.Properties[name][j].ToString();
                            }
                        }
                        salida.Add(alias, s);
                    }
                }
            }
            return(salida);
        }
Esempio n. 2
0
        //_________________________________________________________________________________________________________
        //_________________________________________________________________________________________________________
        public string CreateObject(Dictionary <string, string> data)
        {
            if (Row["Writable"].ToString().ToLower() != "yes")
            {
                return("");
            }
            string sPath = "", objclass = "", objclassID = "";

            ExtensionHelper.GetDataForADObjectCreation(this.Rol, this.Name, Config.dConfig, data, ref sPath, ref objclass, ref objclassID);

            sPath += Row["Base"].ToString();
            DirectoryEntry parentDE = new DirectoryEntry(adPath + "/" + sPath, Row["Usr"].ToString(), Row["Pwd"].ToString());
            DirectoryEntry newObjDE = null;

            LogHelper.Msg(objclass + "|" + "CN=" + data["cn"] + "|" + adPath + "/" + sPath);
            try { newObjDE = parentDE.Children.Add("CN=" + data["cn"], objclass); }
            catch (Exception ex) { LogHelper.Msg("CreateObject: " + ex.Message); MessageBox.Show(ex.Message + "\n" + parentDE.Path, "CreateObject", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(""); }

            // Mandatory attributes
            for (int i = 0; i < tAttributes.Rows.Count; i++)
            {
                string sUsage    = tAttributes.Rows[i]["Usage"].ToString();
                string Tipo      = tAttributes.Rows[i]["Type"].ToString();
                string sAtrName  = tAttributes.Rows[i][this.Name].ToString();
                string sAtrAlias = tAttributes.Rows[i]["Alias"].ToString();

                if (Tipo.Contains(objclassID) && sUsage.Contains("Mandatory") && sAtrAlias != "" && sAtrName != "" && data.ContainsKey(sAtrAlias))
                {
                    newObjDE.Properties[sAtrName].Add(data[sAtrAlias]);
                }
            }
            newObjDE.CommitChanges();
            // Other additional attributes
            Dictionary <string, string> additionalAttributes = ExtensionHelper.SetAdditionalAttributesToNewADObject(this.Rol, this.Name, Config.dConfig, data, sPath, objclass, objclassID);

            foreach (string s in additionalAttributes.Keys)
            {
                bool   retry    = true;
                int    cont     = 0;
                string sValue   = additionalAttributes[s];
                string sAtrName = tAttributes.Rows[AtrByAlias(s)][Name].ToString();
                sAtrName = sAtrName == "cn" || sAtrName == "" ? s : sAtrName;//Si es vacío no está mapeado pero debería estarlo. No puede ser 'cn' ya que es de los principales a asignar por lo que significa que no lo ha encontrado en la lista
                while (retry)
                {
                    newObjDE.Properties[sAtrName].Clear();
                    newObjDE.Properties[sAtrName].Add(sValue);
                    try { newObjDE.CommitChanges(); retry = false; }
                    catch (Exception ex) {
                        LogHelper.Msg("Error setting [" + sAtrName + "] with [" + sValue + "]: " + ex.Message);
                        sValue = additionalAttributes[s] + "X" + cont++;
                        if (ex.Message.Contains("already exist"))
                        {
                            retry = true;
                        }
                        if (ex.Message.Contains("constraint violation"))
                        {
                            retry = true;
                        }
                    }
                }
            }

            newObjDE.CommitChanges();
            byte[] temp  = (byte[])newObjDE.Properties["objectGUID"].Value;
            string sGuid = ObjectsHelper.ConvertGuidToString(temp);

            // Optional attributes
            for (int i = 0; i < tAttributes.Rows.Count; i++)
            {
                string sUsage    = tAttributes.Rows[i]["Usage"].ToString();
                string Tipo      = tAttributes.Rows[i]["Type"].ToString();
                string sAtrName  = tAttributes.Rows[i][this.Name].ToString();
                string sAtrAlias = tAttributes.Rows[i]["Alias"].ToString();
                if (sAtrName == "objectGUID")
                {
                    continue;
                }
                if (Tipo.Contains(objclassID) &&                      // If attribute is for created class
                    !sUsage.Contains("Mandatory") &&                  // If attribute is not mandatory (mandatory attr previously processed)
                    !sUsage.Contains("Auto") &&                       // If attribute is not auto-filled
                    sAtrName != "" &&                                 // If attribute has a valid name for this directory
                    data.ContainsKey(sAtrAlias) &&                    // If attribute has a value
                    data[sAtrAlias].Replace("<ND>", "") != "")        // If attribute has a valid value
                {
                    if (sUsage.Contains("Jpeg"))
                    {
                        string[] lNames = data[sAtrAlias].Split(Config.SeparatorC);
                        newObjDE.Properties[sAtrName].Clear();
                        foreach (string sName in lNames)
                        {
                            if (System.IO.File.Exists(sName))
                            {
                                newObjDE.Properties[sAtrName].Add(System.IO.File.ReadAllBytes(sName));
                            }
                        }
                    }
                    else if (tAttributes.Rows[i]["MV"].ToString() == "N")
                    {
                        // newObjDE.Properties[sAtrName].Add(data[sAtrAlias]); // Do not use this way: you get an error message with numeric attributes
                        newObjDE.Properties[sAtrName].Value = data[sAtrAlias]; // This way is always valid
                    }
                    else if (tAttributes.Rows[i]["MV"].ToString() == "B")
                    {
                        if (data[sAtrAlias].ToLower() == "true")
                        {
                            newObjDE.Properties[sAtrName].Add(true);
                        }
                        if (data[sAtrAlias].ToLower() == "false")
                        {
                            newObjDE.Properties[sAtrName].Add(false);
                        }
                    }
                    else
                    {
                        newObjDE.Properties[sAtrName].Value = ((object[])(data[sAtrAlias].Split(Config.SeparatorC)));
                    }
                    LogHelper.Msg("Adding " + sAtrName);
                    try { newObjDE.CommitChanges(); }
                    catch (Exception ex) { LogHelper.Msg("Adding Attribute: " + sAtrName + " in " + this.Name + " Error :" + ex.Message); MessageBox.Show("Adding Attribute: " + sAtrName + " in " + this.Name + "\nError :" + ex.Message, "CreateObject", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); }
                }
            }
            if (objclass == "user" && this.Rol != "AI")
            {
                LogHelper.Msg("Enabling User");
                EnableUser(newObjDE, data);
            }
            return(sGuid);
        }