Exemple #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            List <DirectoryItem> directoryItems = new List <DirectoryItem>();
            DirectoryItem        directoryItem  = null;

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

            /* Conversions aren't done like "rows" and "fields" in a data table.  What we actually get are markers
             * (TokenTypes) telling us what this particular piece of information is.  So we need to loop through it
             * all and watch the TokenTypes to know if this is the equivalent of a new "row" or a "field" in the
             * current row.*/
            while (reader.Read())
            {
                // Loop through until we're at new "row" (StartObject) or an AD field name (PropertyName) or
                // we know we've gotten all the properties of the current "row" (EndObject).
                switch (reader.TokenType)
                {
                case JsonToken.StartObject:     // This is a new "row".
                    directoryItem = new DirectoryItem();
                    break;

                case JsonToken.PropertyName:     // This is a "field".
                    // Read in the field name.
                    string fieldIdentifier = reader.Value.ToString().ToLower();

                    // Read in the property value.  It will be next in the reader.
                    if (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(fieldIdentifier, out propertyName))      // Look for propery.
                        {
                            // 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(fieldIdentifier, out methodName))      // Look for method.
                        {
                            // 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 });
                        }
                    }
                    break;

                case JsonToken.EndObject:     // We've reached the end of the "row".
                    directoryItems.Add(directoryItem);
                    break;

                default:
                    break;
                }
            }
            return(directoryItems);
        }
Exemple #2
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);
        }
Exemple #3
0
        /// <summary>
        /// Pulls the information from AS400 to be included in the directory. We grab all the attritubte names
        /// to map and build the list of fields for the PcTable function.
        /// </summary>
        /// <returns></returns>
        private static List <DirectoryItem> LoadPcInformation()
        {
            List <DirectoryItem> directoryItems = new List <DirectoryItem>();

            // Build a comman delimeted list of field names from both the properties and methosd marked in the class
            // with the AS400 attribute.
            string fieldsFromProperties = string.Join(",", DirectoryItemServices.GetDirectoryItemAS400Properties().Select(p => p.Key).ToArray());
            string fieldsFromMethods    = string.Join(",", DirectoryItemServices.GetDirectoryItemAS400Methods().Select(p => p.Key).ToArray());

            //  Get the data table from AS400.
            HajProfitCenter hajProfitCenter = new HajProfitCenter();
            DataTable       dt = hajProfitCenter.PcTable(
                fieldsFromProperties + ((fieldsFromProperties.Length > 0) & (fieldsFromMethods.Length > 0) ? "," : "") + fieldsFromMethods,
                HajProfitCenter.TableOrder.OrderByPc, HajProfitCenter.PcType.TypeDirectory, false, true, true
                );

            // Serialize the object to a string and then send it into our custom deserialization/mapping process.
            string output = JsonConvert.SerializeObject(dt, new DataSetConverter());

            directoryItems = JsonConvert.DeserializeObject <List <DirectoryItem> >(output, new AS400DirectoryItemConverter());

            // Add some specific property info for our newly created list.
            // todo: this needs to be redone to set Service Center on certain PCs and Region Staff on other PCs.  We are still waiting on the magic number that determines which is which.
            directoryItems.ForEach(di =>
            {
                di.IsProfitCenter  = true;
                di.IsRegionManager = true;
                di.IsServiceCenter = false;
                di.IsRegionStaff   = false;
            });

            return(directoryItems);
        }
Exemple #4
0
        /// <summary>
        /// Pulls the information from AS400 to be included in the directory. We grab all the attritubte names
        /// to map and build the list of fields for the PcTable function.
        /// </summary>
        /// <returns></returns>
        private static List <DirectoryItem> LoadPcInformation()
        {
            List <DirectoryItem> directoryItems = new List <DirectoryItem>();

            // Build a comman delimeted list of field names from both the properties and methosd marked in the class
            // with the AS400 attribute.
            string fieldsFromProperties = string.Join(",", DirectoryItemServices.GetDirectoryItemAS400Properties().Select(p => p.Key).ToArray());
            string fieldsFromMethods    = string.Join(",", DirectoryItemServices.GetDirectoryItemAS400Methods().Select(p => p.Key).ToArray());

            //  Get the data table from AS400.
            //HajProfitCenter hajProfitCenter = new HajProfitCenter();

            /*DataTable dt = hajProfitCenter.PcTable(
             *  fieldsFromProperties + ((fieldsFromProperties.Length > 0) & (fieldsFromMethods.Length > 0) ? "," : "") + fieldsFromMethods,
             *  HajProfitCenter.TableOrder.OrderByPc, HajProfitCenter.PcType.TypeDirectory, false, true, true
             * );*/
            DataTable dt = GetPcInfoWithCoords();


            // Serialize the object to a string and then send it into our custom deserialization/mapping process.
            string output = JsonConvert.SerializeObject(dt, new DataSetConverter());

            directoryItems = JsonConvert.DeserializeObject <List <DirectoryItem> >(output, new AS400DirectoryItemConverter());

            // Add some specific property info for our newly created list.
            directoryItems.ForEach(di =>
            {
                if ((di.ManagerID ?? "").Length > 0)
                {
                    foreach (SearchResult searchResult in
                             HajClassLib.ADInfo.SearchADInformation(new TupleList <string, string> {
                        { "ou", "people" }
                    }, new List <string>()
                    {
                        "sAMAccountName=" + di.ManagerID
                    }, new List <string>()
                    {
                        "mobile"
                    }))
                    {
                        if (searchResult.Properties.Contains("mobile"))
                        {
                            di.Mobile = (string)searchResult.Properties["mobile"][0];
                        }
                    }
                }
                di.IsProfitCenter  = true;
                di.IsPCManager     = false;
                di.IsRegionManager = false;
                di.IsServiceCenter = false;
                di.IsRegionSupport = false;
            });

            return(directoryItems);
        }
Exemple #5
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);
        }
Exemple #6
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);
        }