Exemplo n.º 1
0
        protected virtual void SetPropFromProperties <T>(
            T model,
            PropertyInfo prop,
            EntryAttributeDictionary entryProps
            )
            where T : IEntry
        {
            var attr = prop.GetCustomAttribute <LdapFieldAttribute>();

            if (attr == null)
            {
                return;
            }

            var ldapAttrName = attr?.Name ?? prop.Name;

            if (!prop.CanWrite)
            {
                throw new ArgumentException(
                          $"Column attribute '{ldapAttrName}' applied to property '{prop.Name}' on type '{typeof(T).FullName}', "
                          + $"but {prop.Name} is not writable.");
            }

            try
            {
                var val = GetAttributeRawValue <T>(ldapAttrName, prop, entryProps, attr);
                ValidateTypeConvertAndSet(model, val, prop, ldapAttrName);
            }
            catch (Exception ex)
            {
                throw new ArgumentException(
                          $"Error converting LDAP attribute '{ldapAttrName}' for property '{prop.Name}' on type '{typeof(T).FullName}'."
                          , ex);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Creates a new instance of an LDAP Entry model and populates its fields.
 /// </summary>
 /// <param name="entryProps">A proxy dictionary of the LDAP entry's fields.</param>
 /// <param name="dn">DistinguishedName field of this entry.</param>
 /// <typeparam name="T">The destination IEntry model type.</typeparam>
 /// <returns>A populated instance of T.</returns>
 public T Create <T>(EntryAttributeDictionary entryProps, string dn = null)
     where T : IEntry, new()
 => Create(new T()
 {
     DistinguishedName = dn,
     Attributes        = entryProps
 }, entryProps);
Exemplo n.º 3
0
        public void Constructs()
        {
            var props = new EntryAttributeDictionary(new Dictionary <string, Core.Proxies.AttributeValueList>());
            var path  = "something";
            var p     = new SearchResultProxy(null, props, path);

            Assert.Equal(path, p.Path);
            Assert.Equal(props, p.Properties);
        }
Exemplo n.º 4
0
        public void Create_ConversionError_InformsOfAttribute()
        {
            var props      = new Dictionary <string, AttributeValueList>();
            var properties = new EntryAttributeDictionary(props);
            var ex         = Assert.Throws <ArgumentException>(() => ModelCreator.Create <MyModel>(properties, "bogus path"));

            Assert.Contains("mail", ex.Message);
            Assert.NotNull(ex.InnerException);
        }
Exemplo n.º 5
0
        public void Contains_ReturnsMockResults()
        {
            var m = new Dictionary <string, Core.Proxies.AttributeValueList>()
            {
                { "a key", new Core.Proxies.AttributeValueList(new List <object>()) }
            };
            var p = new EntryAttributeDictionary(m);

            Assert.True(p.ContainsKey("a key"));
            Assert.False(p.ContainsKey("another key"));
        }
Exemplo n.º 6
0
        public void Count_ReturnsMockResults()
        {
            var m = new Dictionary <string, Core.Proxies.AttributeValueList>()
            {
                { "a key", new Core.Proxies.AttributeValueList(new List <object>()) },
                { "2 key", new Core.Proxies.AttributeValueList(new List <object>()) },
                { "3 key", new Core.Proxies.AttributeValueList(new List <object>()) }
            };
            var p = new EntryAttributeDictionary(m);

            Assert.Equal(3, p.Count);
        }
Exemplo n.º 7
0
        public void Create_EmptyManyProp_Optional_YieldsEmptyField()
        {
            var path       = "test path";
            var properties = new EntryAttributeDictionary(
                new Dictionary <string, Core.Proxies.AttributeValueList>()
            {
                { "alt-mails", new string[] { } }
            }
                );
            var b = Creator.Create <MyTestModel>(properties, path);

            Assert.Empty(b.AltMails);
        }
Exemplo n.º 8
0
        public void Create_EmptyProp_Optional_YieldsNullField()
        {
            var path       = "test path";
            var properties = new EntryAttributeDictionary(
                new Dictionary <string, Core.Proxies.AttributeValueList>()
            {
                { "mail", new string[] { } },
            }
                );
            var b = Creator.Create <MyTestModel>(properties, path);

            Assert.Null(b.Mail);
        }
Exemplo n.º 9
0
        public void Create_NullManyProp_Optional_YieldsNullField()
        {
            var path       = "test path";
            var properties = new EntryAttributeDictionary(
                new Dictionary <string, Core.Proxies.AttributeValueList>()
            {
                { "mail", new string[] { "*****@*****.**" } },
                // { "alt-mails", new string[] { "*****@*****.**", "*****@*****.**", "*****@*****.**" } }
            }
                );
            var b = Creator.Create <MyTestModel>(properties, path);

            Assert.Null(b.AltMails);
        }
Exemplo n.º 10
0
        public void Create_CanConvertToSimpleStringFromBytes()
        {
            var path       = "test path";
            var teststr    = "*****@*****.**";
            var properties = new EntryAttributeDictionary(
                new Dictionary <string, AttributeValueList>()
            {
                { "mail", new AttributeValueList(new object[] { Encoding.UTF8.GetBytes(teststr) }) }
            }
                );
            var b = Creator.Create <MyTestModel>(properties, path);

            Assert.Equal(teststr, b.Mail);
        }
Exemplo n.º 11
0
        public void Create_CanConvertCustomFieldTypes()
        {
            var props = new Dictionary <string, AttributeValueList>()
            {
                { "mail", new AttributeValueList("*****@*****.**") },
                { "alt-mails", new AttributeValueList("*****@*****.**", "*****@*****.**") },
                { "number", new AttributeValueList(123) }
            };
            var properties = new EntryAttributeDictionary(props);
            var result     = ModelCreator.Create <MyModel>(properties, "bogus path");

            Assert.Equal(props["mail"][0] as string, result.Mail);
            Assert.Equal(result.Mail2, result.Mail);
            Assert.Equal(result.Number2, (int)result.Number);
        }
Exemplo n.º 12
0
        /// <summary>
        /// Given an IEntry class, T, populate its LdapField properties from the given
        /// collection of directory entry attributes.
        /// </summary>
        /// <typeparam name="T">A type implementing IEntry.</typeparam>
        /// <param name="model">An object whose type implements IEntry.</param>
        /// <param name="entryProps">A collection of directory entry properties and values.</param>
        /// <returns></returns>
        public virtual T Create <T>(
            T model,
            EntryAttributeDictionary entryProps
            )
            where T : IEntry
        {
            var t     = typeof(T);
            var props = t.GetProperties();

            foreach (var prop in props)
            {
                SetPropFromProperties(model, prop, entryProps);
            }

            return(model);
        }
Exemplo n.º 13
0
        protected internal AttributeValueList GetAttributeRawValue <T>(
            string ldapAttrName,
            PropertyInfo prop,
            EntryAttributeDictionary entryProps,
            LdapFieldAttribute attr)
        {
            if (!(entryProps.ContainsKey(ldapAttrName) || attr.Optional))
            {
                throw new ArgumentException(
                          $"LDAP attribute '{attr.Name}' for property '{prop.Name}' on type '{typeof(T).FullName}'"
                          + " is not marked as optional, but was not found in the store.");
            }

            if (entryProps.TryGetValue(ldapAttrName, out var val))
            {
                return(val);
            }

            return(null);
        }
Exemplo n.º 14
0
        public void Create_ResultProperties_To_Model()
        {
            var path       = "test path";
            var properties = new EntryAttributeDictionary(
                new Dictionary <string, Core.Proxies.AttributeValueList>()
            {
                { "dn", new string[] { "ou=some, ou=dn" } },
                { "cn", new string[] { "example" } },
                { "objectclass", new string[] { "testuser" } },
                { "objectsid", new Core.Proxies.AttributeValueList(new List <object> {
                        new byte[] { 0x31, 0x41 }
                    }) },
                { "userprincipalname", new string[] { "testuser" } },
                { "samaccountname", new string[] { "testuser" } },
                { "mail", new string[] { "*****@*****.**" } },
                { "alt-mails", new string[] { "*****@*****.**", "*****@*****.**", "*****@*****.**" } }
            }
                );
            var b = Creator.Create <MyTestModel>(properties, path);

            Assert.Equal(properties, b.Attributes);
            Assert.Equal(path, b.DistinguishedName);
        }
Exemplo n.º 15
0
 public SearchResultProxy(SearchResult result, EntryAttributeDictionary properties, string path)
 {
     Result     = result;
     Properties = properties;
     Path       = path;
 }