private static void CreateAttributeChangeIfInSchemaInternal(this CSEntryChange csentry, SchemaType type, string attributeName, AttributeModificationType modificationType, object value)
 {
     if (type.HasAttribute(attributeName))
     {
         csentry.CreateAttributeChange(attributeName, modificationType, value);
     }
 }
        private List <AttributeChange> GetLocalChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            List <AttributeChange> attributeChanges = new List <AttributeChange>();

            Domain entry = source as Domain;

            if (entry == null)
            {
                throw new InvalidOperationException();
            }

            foreach (IAttributeAdapter typeDef in this.SchemaType.AttributeAdapters.Where(t => t.Api == this.Api))
            {
                if (typeDef.IsAnchor)
                {
                    continue;
                }

                foreach (AttributeChange change in typeDef.CreateAttributeChanges(dn, modType, entry))
                {
                    if (type.HasAttribute(change.Name))
                    {
                        attributeChanges.Add(change);
                    }
                }
            }
            return(attributeChanges);
        }
        public IList<AttributeChange> GetChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            List<AttributeChange> attributeChanges = new List<AttributeChange>();

            CourseStudents students = source as CourseStudents;

            if (students == null)
            {
                GoogleCourse course = source as GoogleCourse;

                if (course == null)
                {
                    throw new InvalidOperationException();
                }
                else
                {
                    students = course.Students;
                }
            }

            foreach (IAttributeAdapter typeDef in ManagementAgent.Schema[SchemaConstants.Course].AttributeAdapters.Where(t => t.Api == this.Api))
            {
                foreach (string attributeName in typeDef.MmsAttributeNames)
                {
                    if (type.HasAttribute(attributeName))
                    {
                        attributeChanges.AddRange(typeDef.CreateAttributeChanges(dn, modType, students));
                    }
                }
            }

            return attributeChanges;
        }
 /// <summary>
 /// Creates an AttributeChange of the specified type, provided that the attribute is present in the provided schema type
 /// </summary>
 /// <param name="csentry">The CSEntryChange to add the AttributeChange to</param>
 /// <param name="type">The schema type of the object class of the CSEntryChange</param>
 /// <param name="attributeName">The name of the attribute</param>
 /// <param name="modificationType">The type of modification to apply to the attribute</param>
 /// <param name="valueChanges">The value changes to apply to the modification operation</param>
 public static void CreateAttributeChangeIfInSchema(this CSEntryChange csentry, SchemaType type, string attributeName, AttributeModificationType modificationType, IList <ValueChange> valueChanges)
 {
     if (type.HasAttribute(attributeName) && valueChanges != null && valueChanges.Count > 0)
     {
         csentry.CreateAttributeChange(attributeName, modificationType, valueChanges);
     }
 }
        public IList <AttributeChange> GetChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            List <AttributeChange> attributeChanges = new List <AttributeChange>();

            GroupMembership membership = source as GroupMembership;

            if (membership == null)
            {
                GoogleGroup group = source as GoogleGroup;

                if (group == null)
                {
                    throw new InvalidOperationException();
                }
                else
                {
                    membership = group.Membership;
                }
            }

            this.ApplyRoleInheritance(membership);

            foreach (IAttributeAdapter typeDef in ManagementAgent.Schema[SchemaConstants.Group].AttributeAdapters.Where(t => t.Api == this.Api))
            {
                foreach (string attributeName in typeDef.MmsAttributeNames)
                {
                    if (type.HasAttribute(attributeName))
                    {
                        attributeChanges.AddRange(typeDef.CreateAttributeChanges(dn, modType, membership));
                    }
                }
            }

            return(attributeChanges);
        }
Beispiel #6
0
 public IEnumerable <string> GetFieldNames(SchemaType type, string api)
 {
     if (this.FieldName != null)
     {
         if (type.HasAttribute(this.MmsAttributeName))
         {
             yield return(this.FieldName);
         }
     }
 }
        public IList <AttributeChange> GetChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            List <AttributeChange> attributeChanges = new List <AttributeChange>();

            foreach (IAttributeAdapter typeDef in ManagementAgent.Schema[SchemaConstants.User].AttributeAdapters.Where(t => t.Api == this.Api))
            {
                foreach (string attributeName in typeDef.MmsAttributeNames)
                {
                    if (type.HasAttribute(attributeName))
                    {
                        attributeChanges.AddRange(typeDef.CreateAttributeChanges(dn, modType, source));
                    }
                }
            }

            return(attributeChanges);
        }
        public IList <AttributeChange> GetChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            List <AttributeChange> attributeChanges = new List <AttributeChange>();

            if (!(type.HasAttribute(this.attributeName)))
            {
                return(attributeChanges);
            }

            IAttributeAdapter typeDef = ManagementAgent.Schema[this.typeName].AttributeAdapters.First(t => t.Api == this.Api);

            List <string> delegates = this.config.GmailService.GetDelegates(((User)source).PrimaryEmail).ToList();

            attributeChanges.AddRange(typeDef.CreateAttributeChanges(dn, modType, new { Delegates = delegates }));

            return(attributeChanges);
        }
        public IList <AttributeChange> GetChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            List <AttributeChange> attributeChanges = new List <AttributeChange>();

            if (!type.HasAttribute(this.attributeName))
            {
                return(attributeChanges);
            }

            IAttributeAdapter typeDef = ManagementAgent.Schema[this.typeName].AttributeAdapters.First(t => t.Api == this.Api);

            IList <string> sendAsAddresses = this.GetNonPrimarySendAsFormattedAddresses(((User)source).PrimaryEmail);

            attributeChanges.AddRange(typeDef.CreateAttributeChanges(dn, modType, new { SendAs = sendAsAddresses }));

            return(attributeChanges);
        }
        public IList <AttributeChange> GetChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            if (!(source is CalendarResource calendar))
            {
                throw new InvalidOperationException();
            }

            IList <AclRule>        existingRules = null;
            List <AttributeChange> changes       = new List <AttributeChange>();

            foreach (var kvp in this.attributeRoleMapping)
            {
                if (!type.HasAttribute(kvp.Key))
                {
                    continue;
                }

                if (existingRules == null)
                {
                    // don't populate in advance. Only get the rules if we have a request for a related attribute
                    existingRules = this.GetNonDefaultRules(this.config.CustomerID, calendar.ResourceEmail);
                }

                IList <object> items = this.GetRoleMembers(kvp.Value, existingRules).ToList <object>();

                if (items.Count == 0)
                {
                    continue;
                }

                if (modType == ObjectModificationType.Update)
                {
                    changes.Add(AttributeChange.CreateAttributeReplace(kvp.Key, items));
                }
                else
                {
                    changes.Add(AttributeChange.CreateAttributeAdd(kvp.Key, items));
                }
            }

            return(changes);
        }
Beispiel #11
0
        private List <AttributeChange> GetLocalChanges(string dn, ObjectModificationType modType, SchemaType type, object source)
        {
            List <AttributeChange> attributeChanges = new List <AttributeChange>();

            foreach (IAttributeAdapter typeDef in this.SchemaType.AttributeAdapters.Where(t => t.Api == this.Api))
            {
                if (typeDef.IsAnchor)
                {
                    continue;
                }

                foreach (AttributeChange change in typeDef.CreateAttributeChanges(dn, modType, source))
                {
                    if (type.HasAttribute(change.Name))
                    {
                        attributeChanges.Add(change);
                    }
                }
            }

            return(attributeChanges);
        }
        public IEnumerable <string> GetFieldNames(SchemaType type, string api)
        {
            if (api != null && this.Api != api)
            {
                yield break;
            }

            if (this.FieldName == null)
            {
                yield break;
            }

            HashSet <string> fields = new HashSet <string>();

            foreach (string field in this.Attributes.Where(t => t.FieldName != null && type.HasAttribute(t.AttributeName)).Select(t => t.FieldName))
            {
                fields.Add(field);
            }

            if (fields.Count == 0)
            {
                yield break;
            }

            if (this.IsPrimaryCandidateType)
            {
                fields.Add("primary");
            }

            fields.Add("type");
            fields.Add("customType");

            string childFields = string.Join(",", fields);

            yield return($"{this.FieldName}({childFields})");
        }
Beispiel #13
0
        public IEnumerable <string> GetFieldNames(SchemaType type, string api)
        {
            if (api != null && this.Api != api)
            {
                yield break;
            }

            if (this.FieldName == null)
            {
                yield break;
            }

            string childFields = string.Join(",", this.AttributeAdapters.Where(t => t.FieldName != null && type.HasAttribute(t.AttributeName)).Select(t => t.FieldName));

            if (!string.IsNullOrWhiteSpace(childFields))
            {
                yield return($"{this.FieldName}({childFields})");
            }
        }
        public IList <AttributeChange> ApplyChanges(CSEntryChange csentry, SchemaType type, ref object target, bool patch = false)
        {
            if (!this.attributeRoleMapping.Keys.Any(type.HasAttribute))
            {
                return(new List <AttributeChange>());
            }

            string calendarEmail = ((CalendarResource)target).ResourceEmail;

            IList <AclRule> existingRules;

            if (csentry.ObjectModificationType == ObjectModificationType.Add)
            {
                existingRules = new List <AclRule>();
            }
            else
            {
                existingRules = this.GetNonDefaultRules(this.config.CustomerID, calendarEmail);
            }

            Dictionary <string, AttributeChange> keyedAttributeChanges = new Dictionary <string, AttributeChange>();

            if (csentry.ObjectModificationType == ObjectModificationType.Update)
            {
                foreach (KeyValuePair <string, string> kvp in this.attributeRoleMapping)
                {
                    if (!type.HasAttribute(kvp.Key))
                    {
                        continue;
                    }

                    AttributeChange change = csentry.AttributeChanges.FirstOrDefault(t => t.Name == kvp.Key);

                    if (change == null)
                    {
                        continue;
                    }

                    IList <ValueChange> valueChanges = this.DeleteFromRole(change, existingRules, kvp.Value, calendarEmail);

                    if (valueChanges.Count > 0)
                    {
                        if (!keyedAttributeChanges.ContainsKey(kvp.Key))
                        {
                            keyedAttributeChanges.Add(kvp.Key, AttributeChange.CreateAttributeUpdate(kvp.Key, valueChanges));
                        }
                        else
                        {
                            foreach (ValueChange valueChange in valueChanges)
                            {
                                keyedAttributeChanges[kvp.Key].ValueChanges.Add(valueChange);
                            }
                        }
                    }
                }
            }

            if (csentry.ObjectModificationType == ObjectModificationType.Replace || csentry.ObjectModificationType == ObjectModificationType.Add)
            {
                // If we are replacing, we don't know the existing values, so we can delete and re-add them.
                // If we are adding, we want to clear out the Google-default ACLs, to avoid exported-change-not-reimported errors.
                this.DeleteRules(this.config.CustomerID, calendarEmail, existingRules.ToArray());
            }

            foreach (KeyValuePair <string, string> kvp in this.attributeRoleMapping)
            {
                if (!type.HasAttribute(kvp.Key))
                {
                    continue;
                }

                AttributeChange change = csentry.AttributeChanges.FirstOrDefault(t => t.Name == kvp.Key);

                if (change == null)
                {
                    continue;
                }

                IList <ValueChange> valueChanges = this.AddToRole(change, existingRules, kvp.Value, calendarEmail);

                if (valueChanges.Count == 0)
                {
                    continue;
                }

                if (!keyedAttributeChanges.ContainsKey(kvp.Key))
                {
                    if (csentry.ObjectModificationType == ObjectModificationType.Update)
                    {
                        keyedAttributeChanges.Add(kvp.Key, AttributeChange.CreateAttributeUpdate(kvp.Key, valueChanges));
                    }
                    else
                    {
                        keyedAttributeChanges.Add(kvp.Key, AttributeChange.CreateAttributeAdd(kvp.Key, valueChanges.Select(t => t.Value).ToList()));
                    }
                }
                else
                {
                    foreach (ValueChange valueChange in valueChanges)
                    {
                        keyedAttributeChanges[kvp.Key].ValueChanges.Add(valueChange);
                    }
                }
            }

            return(keyedAttributeChanges.Select(t => t.Value).ToList());
        }
        private async Task CreateChannelCSEntryChanges(string groupid)
        {
            if (!this.context.Types.Types.Contains("publicChannel") && !this.context.Types.Types.Contains("privateChannel"))
            {
                return;
            }

            var channels = await GraphHelperTeams.GetChannels(this.betaClient, groupid, this.token);

            Regex regex = null;

            if (!string.IsNullOrWhiteSpace(this.context.ConfigParameters[ConfigParameterNames.ChannelNameFilter].Value))
            {
                regex = new Regex(this.context.ConfigParameters[ConfigParameterNames.ChannelNameFilter].Value, RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
            }

            foreach (var channel in channels)
            {
                string objectType;

                if (channel.MembershipType.HasValue)
                {
                    if (channel.MembershipType == Beta.ChannelMembershipType.UnknownFutureValue)
                    {
                        logger.Warn($"Ignoring unknown channel {channel.Id} of unknown type");
                        continue;
                    }
                    else
                    {
                        objectType = channel.MembershipType == Beta.ChannelMembershipType.Standard ? "publicChannel" : "privateChannel";
                    }
                }
                else
                {
                    objectType = "publicChannel";
                }

                if (!this.context.Types.Types.Contains(objectType))
                {
                    continue;
                }

                if (regex != null)
                {
                    if (!regex.IsMatch(channel.DisplayName))
                    {
                        logger.Trace($"Dropping channel {channel.Id} with display name {channel.DisplayName} as it did not match the regular expression filter");
                        continue;
                    }
                }

                CSEntryChange c = CSEntryChange.Create();

                c.ObjectModificationType = ObjectModificationType.Add;
                c.AnchorAttributes.Add(AnchorAttribute.Create("id", channel.Id));
                c.AnchorAttributes.Add(AnchorAttribute.Create("teamid", groupid));
                c.DN         = channel.Id;
                c.ObjectType = objectType;
                c.CreateAttributeAdd("team", groupid);

                SchemaType schemaType = this.context.Types.Types[objectType];

                if (schemaType.HasAttribute("displayName"))
                {
                    c.CreateAttributeAdd("displayName", channel.DisplayName);
                }

                if (schemaType.HasAttribute("description") && !string.IsNullOrWhiteSpace(channel.Description))
                {
                    c.CreateAttributeAdd("description", channel.Description);
                }

                if (schemaType.HasAttribute("email") && !string.IsNullOrWhiteSpace(channel.Email))
                {
                    c.CreateAttributeAdd("email", channel.Email);
                }

                if (schemaType.HasAttribute("webUrl") && !string.IsNullOrWhiteSpace(channel.WebUrl))
                {
                    c.CreateAttributeAdd("webUrl", channel.WebUrl);
                }

                if (schemaType.HasAttribute("isFavoriteByDefault"))
                {
                    c.CreateAttributeAdd("isFavoriteByDefault", channel.IsFavoriteByDefault ?? false);
                }

                if (channel.MembershipType == Beta.ChannelMembershipType.Private && (schemaType.HasAttribute("member") || schemaType.HasAttribute("owner")))
                {
                    List <Beta.AadUserConversationMember> members = await GraphHelperTeams.GetChannelMembers(this.betaClient, groupid, channel.Id, this.token);

                    if (members.Count > 0)
                    {
                        List <object> memberList = new List <object>();
                        List <object> ownerList  = new List <object>();

                        foreach (Beta.ConversationMember member in members)
                        {
                            string memberValue;

                            if (member is Beta.AadUserConversationMember conMember)
                            {
                                memberValue = conMember.UserId;
                            }
                            else
                            {
                                if (member.AdditionalData == null)
                                {
                                    logger.Warn("Member has no additional data and therefore no userId\r\n" + JsonConvert.SerializeObject(member));
                                    continue;
                                }

                                if (!member.AdditionalData.ContainsKey("userId"))
                                {
                                    logger.Warn("Member does not have userId\r\n" + JsonConvert.SerializeObject(member));
                                    continue;
                                }

                                memberValue = member.AdditionalData["userId"] as string;
                            }

                            if (memberValue == null)
                            {
                                logger.Warn("Member userId was null\r\n" + JsonConvert.SerializeObject(member));
                                continue;
                            }

                            if (this.userFilter.ShouldExclude(memberValue, this.token))
                            {
                                continue;
                            }

                            if (member.Roles.Contains("owner", StringComparer.OrdinalIgnoreCase))
                            {
                                ownerList.Add(memberValue);
                            }
                            else
                            {
                                memberList.Add(memberValue);
                            }
                        }

                        if (memberList.Count > 0 && schemaType.HasAttribute("member"))
                        {
                            c.CreateAttributeAdd("member", memberList);
                        }

                        if (ownerList.Count > 0 && schemaType.HasAttribute("owner"))
                        {
                            c.CreateAttributeAdd("owner", ownerList);
                        }
                    }
                }

                this.context.ImportItems.Add(c, this.token);
            }
        }