Example #1
0
        private static void PatchName(this Core2User user, PatchOperation operation)
        {
            if (null == operation)
            {
                return;
            }

            if (null == operation.Path)
            {
                return;
            }

            if
            (
                !string.Equals(
                    Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Name,
                    operation.Path.AttributePath,
                    StringComparison.OrdinalIgnoreCase)
            )
            {
                return;
            }

            if (null == operation.Path.ValuePath)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(operation.Path.ValuePath.AttributePath))
            {
                return;
            }

            if
            (
                (
                    operation.Value != null &&
                    operation.Value.Count != 1
                ) ||
                (
                    null == operation.Value &&
                    operation.Name != OperationName.Remove
                )
            )
            {
                return;
            }

            Name nameExisting;
            Name name =
                nameExisting =
                    user.Name;

            if (null == name)
            {
                name = new Name();
            }

            string value;

            if
            (
                string.Equals(
                    Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.GivenName,
                    operation.Path.ValuePath.AttributePath,
                    StringComparison.OrdinalIgnoreCase)
            )
            {
                value = operation.Value != null?GetSingleValue(operation) : null;

                if
                (
                    value != null &&
                    OperationName.Remove == operation.Name &&
                    string.Equals(value, name.GivenName, StringComparison.OrdinalIgnoreCase)
                )
                {
                    value = null;
                }
                name.GivenName = value;
            }

            if
            (
                string.Equals(
                    Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.FamilyName,
                    operation.Path.ValuePath.AttributePath,
                    StringComparison.OrdinalIgnoreCase)
            )
            {
                value = operation.Value != null?GetSingleValue(operation) : null;

                if
                (
                    value != null &&
                    OperationName.Remove == operation.Name &&
                    string.Equals(value, name.FamilyName, StringComparison.OrdinalIgnoreCase)
                )
                {
                    value = null;
                }
                name.FamilyName = value;
            }
            if
            (
                string.Equals(
                    Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Formatted,
                    operation.Path.ValuePath.AttributePath,
                    StringComparison.OrdinalIgnoreCase)
            )
            {
                value = operation.Value != null?GetSingleValue(operation) : null;

                if
                (
                    value != null &&
                    OperationName.Remove == operation.Name &&
                    string.Equals(value, name.Formatted, StringComparison.OrdinalIgnoreCase)
                )
                {
                    value = null;
                }
                name.Formatted = value;
            }



            if (string.IsNullOrWhiteSpace(name.FamilyName) && string.IsNullOrWhiteSpace(name.GivenName))
            {
                if (nameExisting != null)
                {
                    user.Name = null;
                }

                return;
            }

            if (nameExisting != null)
            {
                return;
            }

            user.Name = name;
        }
Example #2
0
 private static void PatchElectronicMailAddresses(this Core2User user, PatchOperation operation)
 {
     user.ElectronicMailAddresses = PatchElectronicMailAddresses(user.ElectronicMailAddresses, operation);
 }
Example #3
0
        private static void PatchAddresses(this Core2User user, PatchOperation operation)
        {
            if (null == operation)
            {
                return;
            }

            if
            (
                !string.Equals(
                    Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Addresses,
                    operation.Path.AttributePath,
                    StringComparison.OrdinalIgnoreCase)
            )
            {
                return;
            }

            if (null == operation.Path.ValuePath)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(operation.Path.ValuePath.AttributePath))
            {
                return;
            }

            IFilter subAttribute = operation.Path.SubAttributes.SingleOrDefault();

            if (null == subAttribute)
            {
                return;
            }

            if
            (
                (
                    operation.Value != null &&
                    operation.Value.Count != 1
                ) ||
                (
                    null == operation.Value &&
                    operation.Name != OperationName.Remove
                )
            )
            {
                return;
            }

            if
            (
                !string.Equals(
                    Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Type,
                    subAttribute.AttributePath,
                    StringComparison.OrdinalIgnoreCase)
            )
            {
                return;
            }

            Address address;
            Address addressExisting;

            if (user.Addresses != null)
            {
                addressExisting =
                    address     =
                        user
                        .Addresses
                        .SingleOrDefault(
                            (Address item) =>
                            string.Equals(subAttribute.ComparisonValue, item.ItemType, StringComparison.Ordinal));
            }
            else
            {
                addressExisting = null;
                address         =
                    new Address()
                {
                    ItemType = subAttribute.ComparisonValue
                };
            }

            string value;

            if (string.Equals(Address.Work, subAttribute.ComparisonValue, StringComparison.Ordinal))
            {
                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Country,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.Country, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.Country = value;
                }

                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Locality,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.Locality, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.Locality = value;
                }

                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.PostalCode,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.PostalCode, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.PostalCode = value;
                }

                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Region,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.Region, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.Region = value;
                }

                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.StreetAddress,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.StreetAddress, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.StreetAddress = value;
                }
                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Formatted,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.Formatted, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.Formatted = value;
                }
            }

            if (string.Equals(Address.Other, subAttribute.ComparisonValue, StringComparison.Ordinal))
            {
                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Country,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.Country, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.Country = value;
                }

                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Locality,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.Locality, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.Locality = value;
                }

                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.PostalCode,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.PostalCode, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.PostalCode = value;
                }

                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Region,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.Region, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.Region = value;
                }

                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.StreetAddress,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.StreetAddress, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.StreetAddress = value;
                }
                if
                (
                    string.Equals(
                        Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Formatted,
                        operation.Path.ValuePath.AttributePath,
                        StringComparison.Ordinal)
                )
                {
                    value = operation.Value != null?GetSingleValue(operation) : null;

                    if
                    (
                        value != null &&
                        OperationName.Remove == operation.Name &&
                        string.Equals(value, address.Formatted, StringComparison.OrdinalIgnoreCase)
                    )
                    {
                        value = null;
                    }
                    address.Formatted = value;
                }
            }

            if
            (
                string.IsNullOrWhiteSpace(address.Country) &&
                string.IsNullOrWhiteSpace(address.Locality) &&
                string.IsNullOrWhiteSpace(address.PostalCode) &&
                string.IsNullOrWhiteSpace(address.Region) &&
                string.IsNullOrWhiteSpace(address.StreetAddress) &&
                string.IsNullOrWhiteSpace(address.Formatted)
            )
            {
                if (addressExisting != null)
                {
                    user.Addresses =
                        user
                        .Addresses
                        .Where(
                            (Address item) =>
                            !string.Equals(subAttribute.ComparisonValue, item.ItemType, StringComparison.Ordinal))
                        .ToArray();
                }

                return;
            }

            if (addressExisting != null)
            {
                return;
            }

            IEnumerable <Address> addresses =
                new Address[]
            {
                address
            };

            if (null == user.Addresses)
            {
                user.Addresses = addresses;
            }
            else
            {
                user.Addresses = user.Addresses.Union(addresses).ToArray();
            }
        }
Example #4
0
        public static void Apply(this Core2User user, PatchOperation operation)
        {
            if (null == operation)
            {
                return;
            }

            if (null == operation.Path || string.IsNullOrWhiteSpace(operation.Path.AttributePath))
            {
                return;
            }


            OperationValue value;

            switch (operation.Path.AttributePath)
            {
            case AttributeNames.Active:
                if (operation.Name != OperationName.Remove)
                {
                    value = operation.Value.SingleOrDefault().ToObject <OperationValue>();
                    if (value != null && !string.IsNullOrWhiteSpace(value.Value) && bool.TryParse(value.Value, out bool active))
                    {
                        user.Active = active;
                    }
                }
                break;

            case AttributeNames.Addresses:
                user.PatchAddresses(operation);
                break;

            case AttributeNames.DisplayName:
                value = operation.Value.SingleOrDefault().ToObject <OperationValue>();

                if (OperationName.Remove == operation.Name)
                {
                    if ((null == value) || string.Equals(user.DisplayName, value.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        value = null;
                    }
                    else
                    {
                        break;
                    }
                }

                if (null == value)
                {
                    user.DisplayName = null;
                }
                else
                {
                    user.DisplayName = value.Value;
                }
                break;

            case AttributeNames.ElectronicMailAddresses:
                user.PatchElectronicMailAddresses(operation);
                break;

            case AttributeNames.ExternalIdentifier:
                value = operation.Value.SingleOrDefault().ToObject <OperationValue>();

                if (OperationName.Remove == operation.Name)
                {
                    if ((null == value) || string.Equals(user.ExternalIdentifier, value.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        value = null;
                    }
                    else
                    {
                        break;
                    }
                }

                if (null == value)
                {
                    user.ExternalIdentifier = null;
                }
                else
                {
                    user.ExternalIdentifier = value.Value;
                }
                break;


            case AttributeNames.Name:
                user.PatchName(operation);
                break;

            case AttributeNames.PhoneNumbers:
                user.PatchPhoneNumbers(operation);
                break;

            case AttributeNames.PreferredLanguage:
                value = operation.Value.SingleOrDefault().ToObject <OperationValue>();

                if (OperationName.Remove == operation.Name)
                {
                    if ((null == value) || string.Equals(user.PreferredLanguage, value.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        value = null;
                    }
                    else
                    {
                        break;
                    }
                }

                if (null == value)
                {
                    user.PreferredLanguage = null;
                }
                else
                {
                    user.PreferredLanguage = value.Value;
                }
                break;

            case AttributeNames.Roles:
                user.PatchRoles(operation);
                break;

            case AttributeNames.Title:
                value = operation.Value.SingleOrDefault().ToObject <OperationValue>();

                if (OperationName.Remove == operation.Name)
                {
                    if ((null == value) || string.Equals(user.Title, value.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        value = null;
                    }
                    else
                    {
                        break;
                    }
                }

                if (null == value)
                {
                    user.Title = null;
                }
                else
                {
                    user.Title = value.Value;
                }
                break;

            case AttributeNames.UserName:
                value = operation.Value.SingleOrDefault().ToObject <OperationValue>();

                if (OperationName.Remove == operation.Name)
                {
                    if ((null == value) || string.Equals(user.UserName, value.Value, StringComparison.OrdinalIgnoreCase))
                    {
                        value = null;
                    }
                    else
                    {
                        break;
                    }
                }

                if (null == value)
                {
                    user.UserName = null;
                }
                else
                {
                    user.UserName = value.Value;
                }
                break;
            }
        }
Example #5
0
 private static void PatchRoles(this Core2User user, PatchOperation operation)
 {
     user.Roles = PatchRoles(user.Roles, operation);
 }
Example #6
0
        private static void PatchPhoneNumbers(this Core2User user, PatchOperation operation)
        {
            if (null == operation)
            {
                return;
            }

            if
            (
                !string.Equals(
                    Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.PhoneNumbers,
                    operation.Path.AttributePath,
                    StringComparison.OrdinalIgnoreCase)
            )
            {
                return;
            }

            if (null == operation.Path.ValuePath)
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(operation.Path.ValuePath.AttributePath))
            {
                return;
            }

            IFilter subAttribute = operation.Path.SubAttributes.SingleOrDefault();

            if (null == subAttribute)
            {
                return;
            }

            if
            (
                (
                    operation.Value != null &&
                    operation.Value.Count != 1
                ) ||
                (
                    null == operation.Value &&
                    operation.Name != OperationName.Remove
                )
            )
            {
                return;
            }

            if
            (
                !string.Equals(
                    Microsoft.AzureAD.Provisioning.ScimReference.Api.Schemas.AttributeNames.Type,
                    subAttribute.AttributePath,
                    StringComparison.OrdinalIgnoreCase)
            )
            {
                return;
            }

            string phoneNumberType = subAttribute.ComparisonValue;

            if
            (
                !string.Equals(phoneNumberType, PhoneNumber.Fax, StringComparison.Ordinal) &&
                !string.Equals(phoneNumberType, PhoneNumber.Mobile, StringComparison.Ordinal) &&
                !string.Equals(phoneNumberType, PhoneNumber.Work, StringComparison.Ordinal)
            )
            {
                return;
            }

            PhoneNumber phoneNumber;
            PhoneNumber phoneNumberExisting;

            if (user.PhoneNumbers != null)
            {
                phoneNumberExisting =
                    phoneNumber     =
                        user
                        .PhoneNumbers
                        .SingleOrDefault(
                            (PhoneNumber item) =>
                            string.Equals(subAttribute.ComparisonValue, item.ItemType, StringComparison.Ordinal));
            }
            else
            {
                phoneNumberExisting = null;
                phoneNumber         =
                    new PhoneNumber()
                {
                    ItemType = subAttribute.ComparisonValue
                };
            }

            string value = operation.Value != null?GetSingleValue(operation) : null;

            if
            (
                value != null &&
                OperationName.Remove == operation.Name &&
                string.Equals(value, phoneNumber.Value, StringComparison.OrdinalIgnoreCase)
            )
            {
                value = null;
            }
            phoneNumber.Value = value;

            if (string.IsNullOrWhiteSpace(phoneNumber.Value))
            {
                if (phoneNumberExisting != null)
                {
                    user.PhoneNumbers =
                        user
                        .PhoneNumbers
                        .Where(
                            (PhoneNumber item) =>
                            !string.Equals(subAttribute.ComparisonValue, item.ItemType, StringComparison.Ordinal))
                        .ToArray();
                }
                return;
            }

            if (phoneNumberExisting != null)
            {
                return;
            }

            IEnumerable <PhoneNumber> phoneNumbers =
                new PhoneNumber[]
            {
                phoneNumber
            };

            if (null == user.PhoneNumbers)
            {
                user.PhoneNumbers = phoneNumbers;
            }
            else
            {
                user.PhoneNumbers = user.PhoneNumbers.Union(phoneNumbers).ToArray();
            }
        }