Beispiel #1
0
        private static List <DirectoryItem> LoadADInformation(string ParentGroupName, string GroupName)
        {
            List <DirectoryItem> directoryItems = new List <DirectoryItem>();

            // We need both the properties AND method names to look for.
            List <string> fields = DirectoryItemServices.GetDirectoryItemADProperties().Select(p => p.Key).ToList();

            fields.AddRange(DirectoryItemServices.GetDirectoryItemADMethods().Select(p => p.Key).ToList());


            try
            {
                foreach (SearchResult searchResult in
                         HajClassLib.ADInfo.SearchADInformation(
                             new TupleList <string, string> {
                    { "ou", "people" }
                },
                             new List <string>()
                {
                    "objectCategory=user", "memberOf=CN=" + GroupName + ",OU=" + ParentGroupName + ",OU=Groups,DC=Hajoca,DC=com"
                },
                             fields))
                {
                    string        output = Newtonsoft.Json.JsonConvert.SerializeObject(searchResult.Properties);
                    DirectoryItem di     = JsonConvert.DeserializeObject <DirectoryItem>(output, new ADDirectoryItemConverter());
                    directoryItems.Add(di);
                }
            }
            catch (Exception ex)
            {
                ErrorReporting.ReportError(ErrorReporting.ErrorReportType.HAJNET_Error, "InteractiveDirectory.Library", MethodBase.GetCurrentMethod(), ex);
            }

            return(directoryItems);
        }
Beispiel #2
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var directoryItem = new DirectoryItem();

            // Get the list of properties and methods that have been marked with the ADIdentifier attribute.
            Dictionary <string, string> objProps   = DirectoryItemServices.GetDirectoryItemADProperties();
            Dictionary <string, string> objMethods = DirectoryItemServices.GetDirectoryItemADMethods();
            string propertyName;
            string methodName;

            // Move through the SearchResult object.
            while (reader.Read())
            {
                // Loop through until we're at an AD field name.
                if (reader.TokenType == JsonToken.PropertyName)
                {
                    // Read in the AD field name.
                    string adIdentifier = reader.Value.ToString().ToLower();

                    //read in the property value.
                    if (reader.Read())
                    {
                         
                        {
                            // LDAP results serialize as an arrary, so we need to read the first value.
                            if (reader.TokenType == JsonToken.StartArray)
                            {
                                reader.Read();
                            }

                            // Double check this is a field we actually care about and get the name of the field it maps to.
                            if (objProps.TryGetValue(adIdentifier, out propertyName)) // Property Mapping
                            {
                                // Get the field's type and convert and store the value.
                                PropertyInfo pi             = directoryItem.GetType().GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                                var          convertedValue = Convert.ChangeType(reader.Value, pi.PropertyType);
                                pi.SetValue(directoryItem, convertedValue, null);
                            }
                            else if (objMethods.TryGetValue(adIdentifier, out methodName)) // Method Mapping
                            {
                                // Get the method and execute it.
                                MethodInfo mi             = directoryItem.GetType().GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                                var        convertedValue = Convert.ChangeType(reader.Value, mi.GetParameters()[0].ParameterType);
                                mi.Invoke(directoryItem, new object[] { convertedValue });
                            }
                        }
                    }
                }
            }
            return(directoryItem);
        }
Beispiel #3
0
        /// <summary>
        /// Pulls the information from AD to be included in the direcotry.  It looks for all users that are
        /// currently in the groups that are named in the web.config (WEB_CONFIG_SERVICE_CENTER_EMPLOYEES and
        /// WEB_CONFIG_REGION_STAFF).
        /// </summary>
        /// <returns>List of directory items from Active Directory.</returns>
        private static List <DirectoryItem> LoadADInformation()
        {
            List <DirectoryItem> directoryItems = new List <DirectoryItem>();

            // We need both the properties AND method names to look for.
            List <string> fields = DirectoryItemServices.GetDirectoryItemADProperties().Select(p => p.Key).ToList();

            fields.AddRange(DirectoryItemServices.GetDirectoryItemADMethods().Select(p => p.Key).ToList());

            // Pull in the Service Center Employees
            string GroupName = ConfigurationManager.AppSettings[WEB_CONFIG_SERVICE_CENTER_EMPLOYEES];

            try
            {
                /// Because AD does not return a clean data table like the AS400 we need to manually go through
                /// the results and add the directory items one at a time.
                foreach (SearchResult searchResult in
                         HajClassLib.ADInfo.SearchADInformation(
                             new TupleList <string, string> {
                    { "ou", "people" }
                },
                             new List <string>()
                {
                    "objectCategory=user", "memberOf=CN=" + GroupName + ",OU=Distribution Groups,OU=Groups,DC=Hajoca,DC=com"
                },
                             fields))
                {
                    string        output = Newtonsoft.Json.JsonConvert.SerializeObject(searchResult.Properties);
                    DirectoryItem di     = JsonConvert.DeserializeObject <DirectoryItem>(output, new ADDirectoryItemConverter());
                    di.IsServiceCenter = true;
                    directoryItems.Add(di);
                }
            }
            catch (Exception)
            {
                //todo: Report the problem.
                throw;
            }

            // Pull in the Region Staff Employees
            GroupName = ConfigurationManager.AppSettings[WEB_CONFIG_REGION_STAFF];
            try
            {
                /// Because AD does not return a clean data table like the AS400 we need to manually go through
                /// the results and add the directory items one at a time.
                foreach (SearchResult searchResult in
                         HajClassLib.ADInfo.SearchADInformation(
                             new TupleList <string, string> {
                    { "ou", "people" }
                },
                             new List <string>()
                {
                    "objectCategory=user", "memberOf=CN=" + GroupName + " Staff,OU=Distribution Groups,OU=Groups,DC=Hajoca,DC=com"
                },
                             fields))
                {
                    // Serialize the object to a string and then send it into our custom deserialization/mapping process.
                    string        output = Newtonsoft.Json.JsonConvert.SerializeObject(searchResult.Properties);
                    DirectoryItem di     = JsonConvert.DeserializeObject <DirectoryItem>(output, new ADDirectoryItemConverter());

                    // Add some specific property info and add it to our list.
                    di.IsRegionStaff = true;
                    directoryItems.Add(di);
                }
            }
            catch (Exception)
            {
                //todo: Report the problem.
                throw;
            }
            return(directoryItems);
        }