public void TestConnectorObject()
 {
     ConnectorObjectBuilder builder =
        new ConnectorObjectBuilder();
     builder.SetName("myname");
     builder.SetUid("myuid");
     builder.AddAttribute(CreateTestAttribute());
     ConnectorObject v1 = builder.Build();
     ConnectorObject v2 = CreateTestNormalizer().NormalizeObject(v1);
     builder =
         new ConnectorObjectBuilder();
     builder.SetName("myname");
     builder.SetUid("myuid");
     builder.AddAttribute(CreateNormalizedTestAttribute());
     ConnectorObject expected = builder.Build();
     Assert.AreEqual(expected, v2);
     Assert.IsFalse(expected.Equals(v1));
 }
Esempio n. 2
0
        public void TestConnectorObject()
        {
            ConnectorObjectBuilder bld = new ConnectorObjectBuilder();
            bld.SetUid("foo");
            bld.SetName("fooToo");

            ConnectorObject v1 = bld.Build();
            ConnectorObject v2 = (ConnectorObject)CloneObject(v1);

            Assert.AreEqual(v1, v2);
        }
Esempio n. 3
0
        public void TestSyncDelta()
        {
            ConnectorObjectBuilder bld = new ConnectorObjectBuilder();
            bld.SetUid("foo");
            bld.SetName("name");
            SyncDeltaBuilder builder = new SyncDeltaBuilder();
            builder.PreviousUid = (new Uid("mypreviousuid"));
            builder.Uid = (new Uid("myuid"));
            builder.DeltaType = (SyncDeltaType.CREATE_OR_UPDATE);
            builder.Token = (new SyncToken("mytoken"));
            builder.Object = (bld.Build());
            SyncDelta v1 = builder.Build();
            SyncDelta v2 = (SyncDelta)CloneObject(v1);
            Assert.AreEqual(new Uid("mypreviousuid"), v2.PreviousUid);
            Assert.AreEqual(new Uid("foo"), v2.Uid);
            Assert.AreEqual(new SyncToken("mytoken"), v2.Token);
            Assert.AreEqual(SyncDeltaType.CREATE_OR_UPDATE, v2.DeltaType);
            Assert.AreEqual(v1, v2);

            builder = new SyncDeltaBuilder();
            builder.DeltaType = SyncDeltaType.DELETE;
            builder.Token = new SyncToken("mytoken");
            builder.ObjectClass = ObjectClass.ACCOUNT;
            builder.Uid = new Uid("foo");
            v1 = builder.Build();
            v2 = (SyncDelta)CloneObject(v1);
            Assert.AreEqual(ObjectClass.ACCOUNT, v2.ObjectClass);
            Assert.AreEqual(new Uid("foo"), v2.Uid);
            Assert.AreEqual(new SyncToken("mytoken"), v2.Token);
            Assert.AreEqual(SyncDeltaType.DELETE, v2.DeltaType);
            Assert.AreEqual(v1, v2);
        }
        /// <summary>
        /// Process the Hashtable result and convert it to a SyncDelta object
        /// ready to be processed by the sync handler 
        /// </summary>
        /// <remarks>
        /// The result Hashtable must follow a specific format and contain the following key/value:
        /// 
        /// "Token": (Object) token object (could be Integer, Date, String), [!! could be null]
        /// "DeltaType": (String) ("CREATE|UPDATE|CREATE_OR_UPDATE"|"DELETE"),
        /// "Uid": (String) uid  (uid of the entry),
        /// "PreviousUid": (String) previous uid (This is for rename ops),
        /// "Object": Hashtable(String,List) of attributes name/values describing the object
        /// "ObjectClass": (String) must be set if Operation = DELETE and Object = null
        /// </remarks>
        /// <param name="result"></param>
        /// <returns></returns>
        public Object Process(Hashtable result)
        {
            var syncbld = new SyncDeltaBuilder();
            var cobld = new ConnectorObjectBuilder();
            Uid uid;

            // SyncToken
            // Mandatory here
            if (result.ContainsKey(SyncTokenKeyName))
            {
                syncbld.Token = result[SyncTokenKeyName] == null ? new SyncToken(0L) : new SyncToken(result[SyncTokenKeyName]);
            }
            else
                throw new ArgumentException("SyncToken is missing in Sync result");

            // SyncDelta
            // Mandatory here
            if (isValidKeyAndValue(result,DeltaTypeKeyName))
            {
                var op = result[DeltaTypeKeyName];
                if (SyncDeltaType.CREATE.ToString().Equals(op as String, StringComparison.OrdinalIgnoreCase))
                    syncbld.DeltaType = SyncDeltaType.CREATE;
                else if (SyncDeltaType.UPDATE.ToString().Equals(op as String, StringComparison.OrdinalIgnoreCase))
                    syncbld.DeltaType = SyncDeltaType.UPDATE;
                else if (SyncDeltaType.DELETE.ToString().Equals(op as String, StringComparison.OrdinalIgnoreCase))
                    syncbld.DeltaType = SyncDeltaType.DELETE;
                else if (SyncDeltaType.CREATE_OR_UPDATE.ToString().Equals(op as String, StringComparison.OrdinalIgnoreCase))
                    syncbld.DeltaType = SyncDeltaType.CREATE_OR_UPDATE;
                else
                    throw new ArgumentException("Unrecognized DeltaType in Sync result");
            }
            else
                throw new ArgumentException("DeltaType is missing in Sync result");

            // Uid
            // Mandatory
            if (isValidKeyAndValue(result, UidKeyName))
            {
                var value = result[UidKeyName];
                if (value is String)
                {
                    uid = new Uid(value as String);
                } else if (value is Uid)
                {
                    uid = value as Uid;
                }
                else
                {
                    throw new ArgumentException("Unrecognized Uid in Sync result");
                }
                syncbld.Uid = uid;
                cobld.SetUid(uid);
            }
            else
            {
                throw new ArgumentException("Uid is missing in Sync result");
            }

            // PreviousUid
            // Not valid if DELETE
            if (isValidKeyAndValue(result, PreviousUidKeyName))
            {
                var value = result[PreviousUidKeyName];
                Uid previousUid;
                if (value is String)
                {
                    previousUid = new Uid(value as String);
                }
                else if (value is Uid)
                {
                    previousUid = value as Uid;
                }
                else
                {
                    throw new ArgumentException("Unrecognized PreviousUid in Sync result");
                }
                syncbld.PreviousUid = previousUid;
            }
            if (syncbld.PreviousUid != null && syncbld.DeltaType == SyncDeltaType.DELETE)
            {
                throw new ArgumentException("PreviousUid can only be specified for Create or Update.");
            }

            // Connector object
            // Mandatory unless DELETE
            if (result.ContainsKey(ConnectorObjectKeyName) && result[ConnectorObjectKeyName] is Hashtable)
            {
                var attrs = result[ConnectorObjectKeyName] as Hashtable;

                if (!attrs.ContainsKey(Name.NAME))
                    throw new ArgumentException("The Object must contain a Name");

                foreach (DictionaryEntry attr in attrs)
                {
                    var attrName = attr.Key as String;
                    var attrValue = attr.Value;

                    if (Name.NAME.Equals(attrName))
                        cobld.SetName(attrValue as String);
                    else if (Uid.NAME.Equals((attrName)))
                    {
                        if (!uid.GetUidValue().Equals(attrValue))
                            throw new ArgumentException("Uid from Object is different than Uid from Sync result");
                    }
                    else if (OperationalAttributes.ENABLE_NAME.Equals((attrName)))
                        cobld.AddAttribute(ConnectorAttributeBuilder.BuildEnabled(attr.Value is bool && (bool) attr.Value));
                    else
                    {
                        if (attrValue == null)
                        {
                            cobld.AddAttribute(ConnectorAttributeBuilder.Build(attrName));
                        }
                        else if (attrValue.GetType() == typeof(Object[]) || attrValue.GetType() == typeof(System.Collections.ICollection))
                        {
                            var list = new Collection<object>();
                            foreach (var val in (ICollection)attrValue)
                            {
                                list.Add(FrameworkUtil.IsSupportedAttributeType(val.GetType()) ? val : val.ToString());
                            }
                            cobld.AddAttribute(ConnectorAttributeBuilder.Build(attrName, list));
                        }
                        else
                        {
                            cobld.AddAttribute(ConnectorAttributeBuilder.Build(attrName, attrValue));
                        }
                    }
                }
                cobld.ObjectClass = _objectClass;
                syncbld.Object = cobld.Build();
            }
            // If operation is DELETE and the ConnectorObject is null,
            // we need to set the ObjectClass at the SyncDelta level
            else if ((SyncDeltaType.DELETE == syncbld.DeltaType) && isValidKeyAndValue(result, ObjectClassKeyName))
            {
                var objclass = result[ObjectClassKeyName];
                if (objclass is ObjectClass)
                {
                    syncbld.ObjectClass = objclass as ObjectClass;
                }
                else if (objclass is String)
                {
                    syncbld.ObjectClass = new ObjectClass(objclass as String);
                }
                else
                {
                    throw new ArgumentException("Unrecognized ObjectClass in Sync result");
                }
            }
            else
            {
                throw new ArgumentException("Object is missing in Sync result");
            }

            return _handler.Handle(syncbld.Build());
        }
        public void TestSyncDelta()
        {
            ConnectorObjectBuilder objbuilder =
                new ConnectorObjectBuilder();
            objbuilder.SetName("myname");
            objbuilder.SetUid("myuid");
            objbuilder.AddAttribute(CreateTestAttribute());
            ConnectorObject obj = objbuilder.Build();

            SyncDeltaBuilder builder =
                new SyncDeltaBuilder();
            builder.DeltaType = (SyncDeltaType.DELETE);
            builder.Token = (new SyncToken("mytoken"));
            builder.Uid = (new Uid("myuid"));
            builder.Object = (obj);
            SyncDelta v1 = builder.Build();
            SyncDelta v2 = CreateTestNormalizer().NormalizeSyncDelta(v1);
            builder =
                new SyncDeltaBuilder();
            builder.DeltaType = (SyncDeltaType.DELETE);
            builder.Token = (new SyncToken("mytoken"));
            builder.Uid = (new Uid("myuid"));
            objbuilder =
                new ConnectorObjectBuilder();
            objbuilder.SetName("myname");
            objbuilder.SetUid("myuid");
            objbuilder.AddAttribute(CreateNormalizedTestAttribute());
            builder.Object = objbuilder.Build();
            SyncDelta expected = builder.Build();
            Assert.AreEqual(expected, v2);
            Assert.IsFalse(expected.Equals(v1));
        }
        /// <summary>
        /// Finds the attributes in connector object and rename it according to input array of names, but only
        /// if the atribute name is in attributes to get
        /// </summary>
        /// <param name="cobject">ConnectorObject which attributes should be replaced</param>
        /// <param name="attsToGet">Attributes to get list</param>
        /// <param name="map">Replace mapping</param>
        /// <returns>ConnectorObject with replaced attributes</returns>        
        /// <exception cref="ArgumentNullException">If some of the params is null</exception>
        internal static ConnectorObject ReplaceAttributes(ConnectorObject cobject, ICollection<string> attsToGet, IDictionary<string, string> map)
        {
            Assertions.NullCheck(cobject, "cobject");
            Assertions.NullCheck(map, "map");
            if (attsToGet == null)
            {
                return cobject;
            }

            var attributes = cobject.GetAttributes();
            var builder = new ConnectorObjectBuilder();
            foreach (ConnectorAttribute attribute in attributes)
            {
                string newName;
                if (map.TryGetValue(attribute.Name, out newName) && attsToGet.Contains(newName))
                {
                    var newAttribute = RenameAttribute(attribute, newName);
                    builder.AddAttribute(newAttribute);
                    break;
                }

                builder.AddAttribute(attribute);
            }

            builder.AddAttributes(attributes);
            builder.ObjectClass = cobject.ObjectClass;
            builder.SetName(cobject.Name);
            builder.SetUid(cobject.Uid);
            return builder.Build();
        }
        public Object Process(Object result)
        {
            if (result == null)
            {
                return true;
            }

            if (result is ConnectorObject)
            {
                return _handler.Handle(result as ConnectorObject);
            }

            var cobld = new ConnectorObjectBuilder();
            var res = result as Hashtable;
            foreach (String key in res.Keys)
            {
                var attrName = key;
                var attrValue = res[key];
                if ("__UID__".Equals(attrName))
                {
                    if (attrValue == null)
                    {
                        throw new ConnectorException("Uid can not be null");
                    }
                    cobld.SetUid(attrValue.ToString());
                }
                else if ("__NAME__".Equals(attrName))
                {
                    if (attrValue == null)
                    {
                        throw new ConnectorException("Name can not be null");
                    }
                    cobld.SetName(attrValue.ToString());
                }
                else
                {
                    if (attrValue == null)
                    {
                        cobld.AddAttribute(ConnectorAttributeBuilder.Build(attrName));
                    }
                    else if (attrValue.GetType() == typeof(Object[]) || attrValue.GetType() == typeof(System.Collections.ICollection))
                    {
                        var list = new Collection<object>();
                        foreach (var val in (ICollection)attrValue)
                        {
                            list.Add(FrameworkUtil.IsSupportedAttributeType(val.GetType()) ? val : val.ToString());
                        }
                        cobld.AddAttribute(ConnectorAttributeBuilder.Build(attrName, list));
                    }
                    else
                    {
                        cobld.AddAttribute(ConnectorAttributeBuilder.Build(attrName, attrValue));
                    }
                }
            }
            cobld.ObjectClass = _objectClass;
            return _handler.Handle(cobld.Build());
        }
Esempio n. 8
0
 private static void copyAttribute(ConnectorObjectBuilder builder, ConnectorObject cobject, string src, string dest)
 {
     ConnectorAttribute srcAttr = cobject.GetAttributeByName(src);
     if (srcAttr != null)
     {
         builder.AddAttribute(RenameAttribute(srcAttr, dest));
     }
 }
Esempio n. 9
0
        /// <summary>
        /// Finds the attributes in connector object and rename it according to input array of names, but only
        /// if the atribute name is in attributes to get
        /// </summary>
        /// <param name="cobject">ConnectorObject which attributes should be replaced</param>
        /// <param name="map">Replace mapping</param>
        /// <returns>ConnectorObject with replaced attributes</returns>        
        /// <exception cref="ArgumentNullException">If some of the params is null</exception>
        internal static ConnectorObject ConvertAdAttributesToExchange(ConnectorObject cobject)
        {
            Assertions.NullCheck(cobject, "cobject");

            var attributes = cobject.GetAttributes();
            var builder = new ConnectorObjectBuilder();

            bool emailAddressPolicyEnabled = true;
            foreach (ConnectorAttribute attribute in attributes)
            {
                string newName;
                if (attribute.Is(ExchangeConnectorAttributes.AttMsExchPoliciesExcludedADName))
                {
                    if (attribute.Value != null && attribute.Value.Contains("{26491cfc-9e50-4857-861b-0cb8df22b5d7}"))
                    {
                        emailAddressPolicyEnabled = false;
                    }
                }
                else if (attribute.Is(ExchangeConnectorAttributes.AttAddressBookPolicyADName))
                {
                    var newAttribute = ExtractCommonName(attribute, ExchangeConnectorAttributes.AttAddressBookPolicy);
                    builder.AddAttribute(newAttribute);
                    builder.AddAttribute(attribute);        // keep the original one as well
                }
                else if (attribute.Is(ExchangeConnectorAttributes.AttOfflineAddressBookADName))
                {
                    var newAttribute = ExtractCommonName(attribute, ExchangeConnectorAttributes.AttOfflineAddressBook);
                    builder.AddAttribute(newAttribute);
                    builder.AddAttribute(attribute);        // keep the original one as well
                }
                else if (ExchangeConnectorAttributes.AttMapFromAD.TryGetValue(attribute.Name, out newName))
                {
                    var newAttribute = RenameAttribute(attribute, newName);
                    builder.AddAttribute(newAttribute);
                }
                else
                {
                    builder.AddAttribute(attribute);
                }
            }

            builder.AddAttribute(ConnectorAttributeBuilder.Build(ExchangeConnectorAttributes.AttEmailAddressPolicyEnabled, emailAddressPolicyEnabled));

            copyAttribute(builder, cobject, ExchangeConnectorAttributes.AttPrimarySmtpAddressADName, ExchangeConnectorAttributes.AttPrimarySmtpAddress);

            // derive recipient type
            string recipientType = GetRecipientType(cobject);
            if (recipientType != null)
            {
                builder.AddAttribute(ConnectorAttributeBuilder.Build(ExchangeConnectorAttributes.AttRecipientType, new string[] { recipientType }));
            }

            builder.ObjectClass = cobject.ObjectClass;
            builder.SetName(cobject.Name);
            builder.SetUid(cobject.Uid);
            return builder.Build();
        }
Esempio n. 10
0
        internal ConnectorObject CreateConnectorObject(ExchangeConnector connector, PSObject psobject, ObjectClass objectClass)
        {
            ConnectorObjectBuilder builder = new ConnectorObjectBuilder();

            string guid = (string)psobject.Properties["guid"].Value.ToString();
            string name = (string)psobject.Properties["name"].Value;

            builder.SetUid(new Uid(guid));
            builder.SetName(new Name(name));

            ObjectClassInfo ocinfo = connector.GetSchema().FindObjectClassInfo(objectClass.GetObjectClassValue());

            IDictionary<string,PSPropertyInfo> properties = psobject.Properties.ToDictionary(psinfo => psinfo.Name);

            LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, "Building connector object with UID = {0} and Name = {1}", guid, name);
            foreach (ConnectorAttributeInfo cai in ocinfo.ConnectorAttributeInfos) {
                if (cai.IsReadable && properties.ContainsKey(cai.Name)) {
                    object value = properties[cai.Name].Value;
                    LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, " - attribute {0} = {1}", cai.Name, value);

                    if (value is PSObject) {
                        var ps = value as PSObject;
                        value = ps.BaseObject;
                        LOGGER.TraceEvent(TraceEventType.Verbose, CAT_DEFAULT, " - attribute {0} UNWRAPPED = {1} ({2})", cai.Name, value, value.GetType());
                    }
                    builder.AddAttribute(cai.Name, CommonUtils.ConvertToSupportedForm(cai, value));
                }
            }
            return builder.Build();
        }