Beispiel #1
0
        private object GetPropertyRecursively(DirectoryEntry userEntry, string propertyToGet, Type destinationType,
                                              SettingsSerializeAs serializeAs, AttributeMappingList attributeListForContext)
        {
            //Get the Active Directory User attribute mapping
            AttributeMapping attrMap = attributeListForContext[propertyToGet];

            //if no such key exists i.e. attrMap is null, simply return null;

            //Primitives, string, simple collections and serialized complex types
            if (destinationType.IsPrimitive || destinationType.Equals(typeof(string)) ||
                typeof(ICollection).IsAssignableFrom(destinationType) || destinationType.IsArray || destinationType.IsValueType)
            {
                //Get value from Active Directory and convert it into correct type
                object convertedObject = GetConvertedAttributeValueForUser(userEntry, attrMap.ActiveDirectoryUserAttribute, destinationType);

                return(convertedObject);
            }
            //Complex types
            else
            {
                if (serializeAs != SettingsSerializeAs.ProviderSpecific)
                {
                    if (serializeAs == SettingsSerializeAs.String || serializeAs == SettingsSerializeAs.Xml)
                    {
                        return(GetConvertedAttributeValueForUser(userEntry, attrMap.ActiveDirectoryUserAttribute, typeof(string)));
                    }
                    else
                    {
                        return(GetConvertedAttributeValueForUser(userEntry, attrMap.ActiveDirectoryUserAttribute, typeof(byte[])));
                    }
                }
                else
                {
                    //Get the mappings for the inner properties
                    AttributeMappingList innerMappings = attributeListForContext[propertyToGet].InnerMappingList;

                    //Create a new instance of the complex type using Activator.CreateInstance and default constructor.
                    object complexObj = Activator.CreateInstance(destinationType);

                    //Call AD to get value for each inner property
                    foreach (AttributeMapping innerMapping in innerMappings)
                    {
                        //Get the current property
                        PropertyInfo propInfo = destinationType.GetProperty(innerMapping.ProfileAttribute);

                        //Recursively call BuildComplexObject
                        object convertedObject = GetPropertyRecursively(userEntry, innerMapping.ProfileAttribute,
                                                                        propInfo.PropertyType, SettingsSerializeAs.ProviderSpecific, innerMappings);

                        //Set the property of complex type
                        propInfo.SetValue(complexObj, convertedObject, null);
                    }

                    return(complexObj);
                }
            }
        }
Beispiel #2
0
        public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection collection)
        {
            //Get the user name
            string userName = context["UserName"] as string;

            //Get the DirectoryEntry for the user name
            using (DirectoryEntry userEntry = GetUserEntryByUserName(userName))
            {
                //Get each property
                IEnumerator en = collection.GetEnumerator();
                while (en.MoveNext())
                {
                    SettingsPropertyValue spv = (en.Current as SettingsPropertyValue);

                    //The name of the property
                    string propertyToSet = spv.Name;

                    //The AttributeMapping associated with the property
                    AttributeMapping am = AttributeList[propertyToSet];
                    //if no such key exists, simply continue;

                    //Set properties recursievly because it might be a complex object
                    SetPropertyRecursively(userEntry, spv.Deserialized ? spv.PropertyValue : spv.SerializedValue, !spv.Deserialized, am);
                }

                //Set last actvity date
                if (LastActivityDateAttribute != null)
                {
                    userEntry.InvokeSet(LastActivityDateAttribute, DateTime.Now);
                }
                //Set last update date
                if (LastUpdateDateAttribute != null)
                {
                    userEntry.InvokeSet(LastUpdateDateAttribute, DateTime.Now);
                }

                //Commit changes
                userEntry.CommitChanges();
            }
        }
Beispiel #3
0
        private void SetPropertyRecursively(DirectoryEntry userEntry, object propertyValue, bool isSerialized, AttributeMapping am)
        {
            Type propertyType = propertyValue.GetType();

            //Simple property
            if (am.InnerMappingList == null || am.InnerMappingList.Count == 0)
            {
                //Primitives, strings and serialized objects
                if (propertyType.IsPrimitive || propertyType.Equals(typeof(string)) || propertyType.IsValueType || isSerialized)
                {
                    //Set the value of the attribute. Uncomment this
                    //userEntry.InvokeSet(am.ActiveDirectoryUserAttribute, propertyValue);

                    return;
                }
                //Collections and arrays
                else if (typeof(ICollection).IsAssignableFrom(propertyType))
                {
                    ICollection coll = (ICollection)propertyValue;
                    object[]    arr  = new object[coll.Count];

                    //Populate the object array that will be saved.
                    int         i  = 0;
                    IEnumerator en = coll.GetEnumerator();
                    while (en.MoveNext())
                    {
                        arr[i] = en.Current;
                        i++;
                    }

                    //Save. Uncomment this
                    //userEntry.InvokeSet(am.ActiveDirectoryUserAttribute, arr);

                    return;
                }

                throw new Exception("Unknown format");
            }
            //Complex property
            else
            {
                AttributeMappingList innerMappingList = am.InnerMappingList;
                foreach (AttributeMapping innerAm in innerMappingList)
                {
                    SetPropertyRecursively(userEntry, propertyType.GetProperty(innerAm.ProfileAttribute).GetValue(propertyValue, null), false, innerAm);
                }
            }
        }