public static bool AddMultiValue <T>(this Cursor cursor, Columnid columnId, T newValue, IEqualityComparer <T> equalityComparer)
        {
            // Note: Must be in transaction and record must be in editing state.
            ColumnAccessor record        = cursor.EditRecord;
            int            insertAtIndex = 0;
            bool           doUpdate      = true;
            // Move after the last newValue, if any
            // HACK: Report bad behavior of record.SizeOf(columnId, insertAtIndex): It returns 0 even if there has been a value inserted.
            object currentObject;

            while ((currentObject = record[columnId, insertAtIndex]) != DBNull.Value)
            {
                T currentValue = (T)currentObject;
                if (equalityComparer.Equals(currentValue, newValue))
                {
                    // Record already contains the newValue to be inserted, so skip the update
                    doUpdate = false;
                    break;
                }
                insertAtIndex++;
            }
            if (doUpdate)
            {
                record[columnId, insertAtIndex] = newValue;
            }
            return(doUpdate);
        }
Exemple #2
0
        public bool AddAttribute(string name, SecurityIdentifier[] valuesToAdd)
        {
            Columnid columnId   = this.context.Schema.FindColumnId(CommonDirectoryAttributes.SIDHistory);
            bool     hasChanged = this.cursor.AddMultiValue(columnId, valuesToAdd);

            return(hasChanged);
        }
Exemple #3
0
        private void LoadClassList(Cursor dataTableCursor)
        {
            // Initialize the class list
            this.classesByName = new Dictionary <string, int>();

            // Load column IDs. We are in an early stage of schema loading, which means that we cannot search for non-system attributes by name.
            Columnid        dntCol             = this.FindColumnId(CommonDirectoryAttributes.DNTag);
            Columnid        governsIdCol       = this.attributesByInternalId[CommonDirectoryAttributes.GovernsIdId].ColumnID;
            SchemaAttribute ldapDisplayNameAtt = this.attributesByInternalId[CommonDirectoryAttributes.LdapDisplayNameId];

            // Search for all classes using this heuristics: (&(ldapDisplayName=*)(governsId=*))
            dataTableCursor.CurrentIndex = ldapDisplayNameAtt.Index;
            while (dataTableCursor.MoveNext())
            {
                int?governsId = dataTableCursor.RetrieveColumnAsInt(governsIdCol);
                if (!governsId.HasValue)
                {
                    // This is an attribute and not a class, so we skip to the next object.
                    continue;
                }
                // TODO: Load more data about classes
                int    classDNT  = dataTableCursor.RetrieveColumnAsDNTag(dntCol).Value;
                string className = dataTableCursor.RetrieveColumnAsString(ldapDisplayNameAtt.ColumnID);
                classesByName.Add(className, classDNT);
            }
        }
        public static bool SetValue(this Cursor cursor, Columnid columnId, string newValue)
        {
            // TODO: Only one instance of byteArrayComparer
            Func <string, string, bool> stringComparer = (x, y) => String.Compare(x, y) == 0;

            return(cursor.SetValue(columnId, newValue, stringComparer));
        }
Exemple #5
0
        public override void ReadAttribute(string name, out DistinguishedName value)
        {
            Columnid columnId = this.context.Schema.FindColumnId(name);
            var      dnt      = this.cursor.RetrieveColumnAsDNTag(columnId);

            value = this.context.DistinguishedNameResolver.Resolve(dnt.Value);
        }
Exemple #6
0
        private static AttributeSyntax GetSyntaxFromColumnType(Columnid column)
        {
            Type colType = column.Type;

            if (colType == typeof(int))
            {
                return(AttributeSyntax.Int);
            }
            else if (colType == typeof(Int64))
            {
                return(AttributeSyntax.Int64);
            }
            else if (colType == typeof(byte))
            {
                return(AttributeSyntax.Bool);
            }
            else if (colType == typeof(byte[]))
            {
                return(AttributeSyntax.OctetString);
            }
            else
            {
                return(AttributeSyntax.Undefined);
            }
        }
Exemple #7
0
        public bool SetAttribute(string name, byte[] newValue)
        {
            Columnid columnId   = this.context.Schema.FindColumnId(name);
            bool     hasChanged = this.cursor.SetValue(columnId, newValue);

            return(hasChanged);
        }
Exemple #8
0
        public override bool HasAttribute(string name)
        {
            Columnid columnId   = this.context.Schema.FindColumnId(name);
            long     columnSize = cursor.Record.SizeOf(columnId);

            return(columnSize > 0);
        }
Exemple #9
0
        public bool SetAttribute <T>(string name, Nullable <T> newValue) where T : struct
        {
            // TODO: This must be called from a transaction
            Columnid columnId   = this.context.Schema.FindColumnId(name);
            bool     hasChanged = this.cursor.SetValue(columnId, newValue);

            return(hasChanged);
        }
        public static bool AddMultiValue(this Cursor cursor, Columnid columnId, SecurityIdentifier[] valuesToAdd)
        {
            //NOTE: Must be in transaction and record must be in editing state.
            var binarySidHistory = valuesToAdd.Select(sid => sid.GetBinaryForm(true));

            // TODO: Create method , bool[][]?
            return(cursor.AddMultiValue(columnId, binarySidHistory, HashEqualityComparer.GetInstance()));
        }
Exemple #11
0
        private Columnid LoadColumnIdByAttributeName(Cursor cursor, string attributeName)
        {
            Columnid attributeIdCol = this.attributesByInternalId[CommonDirectoryAttributes.AttributeIdId].ColumnID;

            // Assume that attributeNameIndex is set as the current index
            cursor.GotoKey(Key.Compose(attributeName));
            int attributeId = cursor.RetrieveColumnAsInt(attributeIdCol).Value;

            return(this.attributesByInternalId[attributeId].ColumnID);
        }
 public override void ReadAttribute(string name, out long?value)
 {
     if (this.context.Schema.ContainsAttribute(name))
     {
         Columnid columnId = this.context.Schema.FindColumnId(name);
         value = this.cursor.RetrieveColumnAsLong(columnId);
     }
     else
     {
         value = null;
     }
 }
 public override void ReadAttribute(string name, out string[] values)
 {
     if (this.context.Schema.ContainsAttribute(name))
     {
         Columnid columnId = this.context.Schema.FindColumnId(name);
         values = this.cursor.RetrieveColumnAsStringArray(columnId);
     }
     else
     {
         values = null;
     }
 }
 public static SecurityIdentifier RetrieveColumnAsSid(this Cursor cursor, Columnid columnId)
 {
     byte[] binarySid = cursor.RetrieveColumnAsByteArray(columnId);
     if (binarySid != null)
     {
         return(binarySid.ToSecurityIdentifier(true));
     }
     else
     {
         return(null);
     }
 }
        public static bool AddMultiValue <T>(this Cursor cursor, Columnid columnId, IEnumerable <T> valuesToAdd, IEqualityComparer <T> equalityComparer)
        {
            //NOTE: Must be in transaction and record must be in editing state.
            bool hasChanged = false;

            foreach (T valueToAdd in valuesToAdd)
            {
                bool valueAdded = AddMultiValue(cursor, columnId, valueToAdd, equalityComparer);
                hasChanged |= valueAdded;
            }
            return(hasChanged);
        }
        public static byte[] RetrieveColumnAsByteArray(this Cursor cursor, Columnid columnId)
        {
            object value = cursor.Record[columnId];

            if (value != DBNull.Value)
            {
                return((byte[])value);
            }
            else
            {
                return(null);
            }
        }
        public static string RetrieveColumnAsString(this Cursor cursor, Columnid columnId)
        {
            object value = cursor.Record[columnId];

            if (value != DBNull.Value)
            {
                return((string)value);
            }
            else
            {
                return(null);
            }
        }
        public static uint?RetrieveColumnAsUInt(this Cursor cursor, Columnid columnId)
        {
            object value = cursor.Record[columnId];

            if (value != DBNull.Value)
            {
                return((uint)value);
            }
            else
            {
                return(null);
            }
        }
        public static Guid?RetrieveColumnAsGuid(this Cursor cursor, Columnid columnId)
        {
            object value = cursor.Record[columnId];

            if (value != DBNull.Value)
            {
                return(new Guid((byte[])value));
            }
            else
            {
                return(null);
            }
        }
        public static DateTime?RetrieveColumnAsTimestamp(this Cursor cursor, Columnid columnId)
        {
            long?timestamp = cursor.RetrieveColumnAsLong(columnId);

            if (timestamp.HasValue)
            {
                return(DateTime.FromFileTime(timestamp.Value));
            }
            else
            {
                return(null);
            }
        }
        public static bool SetValue(this Cursor cursor, Columnid columnId, DateTime?newValue)
        {
            long?newTimeStamp = null;

            // Convert the value if there is any
            if (newValue.HasValue)
            {
                // Treat the value as generalized time
                newTimeStamp = newValue.Value.ToFileTime() / ADConstants.GeneralizedTimeCoefficient;
            }

            // Push the value to the DB
            return(cursor.SetValue(columnId, newTimeStamp));
        }
        public static bool RetrieveColumnAsBoolean(this Cursor cursor, Columnid columnId)
        {
            object value = cursor.Record[columnId];

            if (value != DBNull.Value)
            {
                //TODO: Test RetrieveColumnAsBoolean
                return((int)value != 0);
            }
            else
            {
                return(false);
            }
        }
        public static DateTime?RetrieveColumnAsGeneralizedTime(this Cursor cursor, Columnid columnId)
        {
            long?timestamp = cursor.RetrieveColumnAsLong(columnId);

            if (timestamp.HasValue)
            {
                // 0 = January 1, 1601 1:00:00 AM = Never
                return(DateTime.FromFileTime(timestamp.Value * ADConstants.GeneralizedTimeCoefficient));
            }
            else
            {
                return(null);
            }
        }
        public static byte[][] RetrieveColumnAsMultiByteArray(this Cursor cursor, Columnid columnId)
        {
            var record = cursor.Record;
            var result = new List <byte[]>();

            int valueIndex = 0;

            while (record.SizeOf(columnId, valueIndex) > 0)
            {
                object value = cursor.Record[columnId, valueIndex];
                result.Add((byte[])value);
                valueIndex++;
            }
            return(result.Count > 0 ? result.ToArray() : null);
        }
 public override bool HasAttribute(string name)
 {
     if (this.context.Schema.ContainsAttribute(name))
     {
         // Read the appropriate column and check if it has a value
         Columnid columnId   = this.context.Schema.FindColumnId(name);
         long     columnSize = cursor.Record.SizeOf(columnId);
         return(columnSize > 0);
     }
     else
     {
         // The schema does not even contain this attribute, so the object cannot have it.
         return(false);
     }
 }
        public static bool SetValue <T>(this Cursor cursor, Columnid columnId, T newValue, Func <T, T, bool> equalityComparer) where T : class
        {
            ColumnAccessor record = cursor.EditRecord;
            T    currentValue     = (T)record[columnId];
            bool hasChanged       = !equalityComparer.Invoke(currentValue, newValue);

            if (hasChanged)
            {
                if (newValue != null)
                {
                    record[columnId] = newValue;
                }
                else
                {
                    // TODO: Test nulling an attribute
                    record[columnId] = DBNull.Value;
                }
            }
            return(hasChanged);
        }
        public static bool SetValue <T>(this Cursor cursor, Columnid columnId, Nullable <T> newValue) where T : struct
        {
            // Note: Must be in transaction and record must be in editing state.
            ColumnAccessor record       = cursor.EditRecord;
            object         currentValue = record[columnId];
            bool           hasChanged   = currentValue != DBNull.Value ? !currentValue.Equals(newValue) : newValue != null;

            if (hasChanged)
            {
                if (newValue.HasValue)
                {
                    record[columnId] = newValue.Value;
                }
                else
                {
                    record[columnId] = DBNull.Value;
                }
            }
            return(hasChanged);
        }
        public static bool SetValue <T>(this Cursor cursor, Columnid columnId, T newValue, IEqualityComparer <T> equalityComparer) where T : class
        {
            // Retrieve the current value and compare it to the new one
            ColumnAccessor record          = cursor.EditRecord;
            object         currentRawValue = record[columnId];
            T    currentValue = currentRawValue != DBNull.Value ? (T)record[columnId] : null;
            bool hasChanged   = !equalityComparer.Equals(currentValue, newValue);

            if (hasChanged)
            {
                if (newValue != null)
                {
                    record[columnId] = newValue;
                }
                else
                {
                    record[columnId] = DBNull.Value;
                }
            }
            return(hasChanged);
        }
Exemple #29
0
        public override void ReadAttribute(string name, out int?value)
        {
            Columnid columnId = this.context.Schema.FindColumnId(name);

            value = this.cursor.RetrieveColumnAsInt(columnId);
        }
Exemple #30
0
        public override void ReadAttribute(string name, out byte[][] value)
        {
            Columnid columnId = this.context.Schema.FindColumnId(name);

            value = this.cursor.RetrieveColumnAsMultiByteArray(columnId);
        }