public static SCIMRepresentationAttributeModel ToModel(this SCIMRepresentationAttribute a)
        {
            var result = new SCIMRepresentationAttributeModel
            {
                Id              = a.Id,
                ValuesBoolean   = a.ValuesBoolean,
                ValuesDateTime  = a.ValuesDateTime,
                ValuesInteger   = a.ValuesInteger,
                ValuesReference = a.ValuesReference,
                ValuesString    = a.ValuesString,
                ValuesByte      = a.ValuesBinary,
                ValuesDecimal   = a.ValuesDecimal,
                SchemaAttribute = a.SchemaAttribute.ToModel()
            };

            if (a.Values != null && a.Values.Any())
            {
                foreach (var r in a.Values)
                {
                    result.Children.Add(ToModel(r));
                }
            }

            return(result);
        }
示例#2
0
        private static SCIMRepresentationAttribute BuildAttribute(JToken jsonProperty, SCIMSchemaAttribute schemaAttribute)
        {
            var result = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute);

            switch (schemaAttribute.Type)
            {
            case SCIMSchemaAttributeTypes.BOOLEAN:
                result.Add(bool.Parse(jsonProperty.ToString()));
                break;

            case SCIMSchemaAttributeTypes.INTEGER:
                result.Add(int.Parse(jsonProperty.ToString()));
                break;

            case SCIMSchemaAttributeTypes.DATETIME:
                result.Add(DateTime.Parse(jsonProperty.ToString()));
                break;

            case SCIMSchemaAttributeTypes.STRING:
                result.Add(jsonProperty.ToString());
                break;

            case SCIMSchemaAttributeTypes.COMPLEX:
                result.Values = BuildRepresentationAttributes(jsonProperty as JObject, schemaAttribute.SubAttributes);
                break;

            case SCIMSchemaAttributeTypes.REFERENCE:
                // REFERENCE.
                break;
            }

            return(result);
        }
示例#3
0
        public static SCIMRepresentationAttribute ToDomain(this SCIMRepresentationAttributeModel representationAttribute, SCIMRepresentationAttribute parent = null)
        {
            var result = new SCIMRepresentationAttribute
            {
                Id              = representationAttribute.Id,
                Parent          = parent == null ? null : parent,
                SchemaAttribute = representationAttribute.SchemaAttribute.ToDomain(),
                ValuesBoolean   = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.BOOLEAN ? new List <bool>() : representationAttribute.ValuesBoolean,
                ValuesDateTime  = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.DATETIME ? new List <DateTime>() : representationAttribute.ValuesDateTime,
                ValuesInteger   = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.INTEGER ? new List <int>() : representationAttribute.ValuesInteger,
                ValuesReference = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.REFERENCE ? new List <string>() : representationAttribute.ValuesReference,
                ValuesString    = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.STRING ? new List <string>() : representationAttribute.ValuesString,
                Values          = new List <SCIMRepresentationAttribute>()
            };

            if (representationAttribute.Children != null && representationAttribute.Children.Any())
            {
                foreach (var val in representationAttribute.Children)
                {
                    result.Values.Add(val.ToDomain(result));
                }
            }


            return(result);
        }
        public static SCIMRepresentationAttribute ToDomain(this SCIMRepresentationAttributeModel representationAttribute, SCIMRepresentationAttribute parent = null)
        {
            var result = new SCIMRepresentationAttribute
            {
                Id              = representationAttribute.Id,
                Parent          = parent == null ? (representationAttribute.Parent == null ? null : representationAttribute.Parent.ToDomain()) : parent,
                SchemaAttribute = representationAttribute.SchemaAttribute.ToDomain(),
                ValuesBoolean   = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.BOOLEAN ? new List <bool>() : representationAttribute.Values.Select(v => v.ValueBoolean.Value).ToList(),
                ValuesDateTime  = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.DATETIME ? new List <DateTime>() : representationAttribute.Values.Select(v => v.ValueDateTime.Value).ToList(),
                ValuesInteger   = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.INTEGER ? new List <int>() : representationAttribute.Values.Select(v => v.ValueInteger.Value).ToList(),
                ValuesReference = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.REFERENCE ? new List <string>() : representationAttribute.Values.Select(v => v.ValueReference).ToList(),
                ValuesString    = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.STRING ? new List <string>() : representationAttribute.Values.Select(v => v.ValueString).ToList(),
                ValuesDecimal   = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.DECIMAL ? new List <decimal>() : representationAttribute.Values.Select(v => v.ValueDecimal.Value).ToList(),
                ValuesBinary    = representationAttribute.SchemaAttribute.Type != SCIMSchemaAttributeTypes.BINARY ? new List <byte[]>() : representationAttribute.Values.Select(v => v.ValueByte).ToList(),
                Values          = new List <SCIMRepresentationAttribute>()
            };

            if (representationAttribute.Children != null && representationAttribute.Children.Any())
            {
                foreach (var val in representationAttribute.Children)
                {
                    result.Values.Add(val.ToDomain(result));
                }
            }


            return(result);
        }
示例#5
0
        private static ICollection <SCIMRepresentationAttribute> BuildAttributes(JArray jArr, SCIMSchemaAttribute schemaAttribute, bool ignoreUnsupportedCanonicalValues)
        {
            var result = new List <SCIMRepresentationAttribute>();

            if (schemaAttribute.Type == SCIMSchemaAttributeTypes.COMPLEX)
            {
                foreach (var jsonProperty in jArr)
                {
                    var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute)
                    {
                        Values = BuildRepresentationAttributes(jsonProperty as JObject, schemaAttribute.SubAttributes, ignoreUnsupportedCanonicalValues)
                    };
                    result.Add(record);
                }
            }
            else
            {
                var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute);
                switch (schemaAttribute.Type)
                {
                case SCIMSchemaAttributeTypes.BOOLEAN:
                    record.ValuesBoolean = jArr.Select(j => bool.Parse(j.ToString())).ToList();
                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    record.ValuesInteger = jArr.Select(j => int.Parse(j.ToString())).ToList();
                    break;

                case SCIMSchemaAttributeTypes.DATETIME:
                    record.ValuesDateTime = jArr.Select(j => DateTime.Parse(j.ToString())).ToList();
                    break;

                case SCIMSchemaAttributeTypes.STRING:
                    record.ValuesString = jArr.Select(j => j.ToString()).ToList();
                    if (schemaAttribute.CanonicalValues != null && schemaAttribute.CanonicalValues.Any() && !ignoreUnsupportedCanonicalValues && !record.ValuesString.All(_ => schemaAttribute.CanonicalValues.Contains(_)))
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidCanonicalValue, schemaAttribute.Name));
                    }

                    break;

                case SCIMSchemaAttributeTypes.REFERENCE:
                    record.ValuesReference = jArr.Select(j => j.ToString()).ToList();
                    break;

                case SCIMSchemaAttributeTypes.DECIMAL:
                    record.ValuesDecimal = jArr.Select(j => decimal.Parse(j.ToString())).ToList();
                    break;

                case SCIMSchemaAttributeTypes.BINARY:
                    record.ValuesBinary = jArr.Select(j => Convert.FromBase64String(j.ToString())).ToList();
                    break;
                }

                result.Add(record);
            }

            return(result);
        }
示例#6
0
        public static SCIMRepresentationAttributeModel ToModel(this SCIMRepresentationAttribute a, string representationId)
        {
            var values = new List <SCIMRepresentationAttributeValueModel>();

            values.AddRange(a.ValuesBoolean.Select(b => new SCIMRepresentationAttributeValueModel
            {
                ValueBoolean = b,
                Id           = Guid.NewGuid().ToString()
            }));
            values.AddRange(a.ValuesDateTime.Select(d => new SCIMRepresentationAttributeValueModel
            {
                ValueDateTime = d,
                Id            = Guid.NewGuid().ToString()
            }));
            values.AddRange(a.ValuesInteger.Select(i => new SCIMRepresentationAttributeValueModel
            {
                ValueInteger = i,
                Id           = Guid.NewGuid().ToString()
            }));
            values.AddRange(a.ValuesReference.Select(r => new SCIMRepresentationAttributeValueModel
            {
                ValueReference = r,
                Id             = Guid.NewGuid().ToString()
            }));
            values.AddRange(a.ValuesString.Select(s => new SCIMRepresentationAttributeValueModel
            {
                ValueString = s,
                Id          = Guid.NewGuid().ToString()
            }));
            values.AddRange(a.ValuesDecimal.Select(s => new SCIMRepresentationAttributeValueModel
            {
                ValueDecimal = s,
                Id           = Guid.NewGuid().ToString()
            }));
            values.AddRange(a.ValuesBinary.Select(s => new SCIMRepresentationAttributeValueModel
            {
                ValueByte = s,
                Id        = Guid.NewGuid().ToString()
            }));
            var result = new SCIMRepresentationAttributeModel
            {
                Id                = a.Id,
                Values            = values,
                RepresentationId  = representationId,
                ParentId          = a.Parent == null ? null : a.Parent.Id,
                SchemaAttributeId = a.SchemaAttribute.Id,
                Children          = new List <SCIMRepresentationAttributeModel>()
            };

            if (a.Values != null && a.Values.Any())
            {
                foreach (var r in a.Values)
                {
                    result.Children.Add(ToModel(r, representationId));
                }
            }

            return(result);
        }
示例#7
0
        protected virtual void BuildScimRepresentationAttribute(string attributeId, SCIMRepresentation targetRepresentation, SCIMRepresentation sourceRepresentation, string sourceResourceType)
        {
            var rootSchema            = targetRepresentation.GetRootSchema();
            var attributes            = new List <SCIMRepresentationAttribute>();
            var targetSchemaAttribute = rootSchema.GetAttributeById(attributeId);
            var values  = rootSchema.GetChildren(targetSchemaAttribute);
            var value   = values.FirstOrDefault(s => s.Name == "value");
            var display = values.FirstOrDefault(s => s.Name == "display");
            var type    = values.FirstOrDefault(s => s.Name == "type");

            if (value != null)
            {
                attributes.Add(new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), value, value.SchemaId)
                {
                    ValueString = sourceRepresentation.Id
                });
            }

            if (display != null)
            {
                attributes.Add(new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), display, display.SchemaId)
                {
                    ValueString = sourceRepresentation.DisplayName
                });
            }

            if (type != null)
            {
                attributes.Add(new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), type, type.SchemaId)
                {
                    ValueString = sourceResourceType
                });
            }

            var attrId = Guid.NewGuid().ToString();
            var attrs  = targetRepresentation.GetAttributesByAttrSchemaId(targetSchemaAttribute.Id);

            if (attrs.Any())
            {
                attrId = attrs.First().AttributeId;
            }

            var parentAttr = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attrId, targetSchemaAttribute, targetSchemaAttribute.SchemaId)
            {
                SchemaAttribute = targetSchemaAttribute
            };

            targetRepresentation.AddAttribute(parentAttr);
            foreach (var attr in attributes)
            {
                targetRepresentation.AddAttribute(parentAttr, attr);
            }
        }
示例#8
0
        public SCIMRepresentationBuilder AddComplexAttribute(string name, Action <SCIMRepresentationAttributeBuilder> callback)
        {
            var builder = new SCIMRepresentationAttributeBuilder(null);

            callback(builder);
            var id           = Guid.NewGuid().ToString();
            var newAttribute = new SCIMRepresentationAttribute(id, null);

            foreach (var subAttribute in builder.Build())
            {
                newAttribute.Add(subAttribute);
            }

            _attributes.Add(newAttribute);
            return(this);
        }
        public SCIMRepresentationBuilder AddComplexAttribute(string name, string schemaId, Action <SCIMRepresentationAttributeBuilder> callback)
        {
            var schemaAttribute = _schemas.First(s => s.Id == schemaId).Attributes.FirstOrDefault(a => a.Name == name);
            var builder         = new SCIMRepresentationAttributeBuilder(schemaAttribute);

            callback(builder);
            var id           = Guid.NewGuid().ToString();
            var newAttribute = new SCIMRepresentationAttribute(id, schemaAttribute);

            foreach (var subAttribute in builder.Build())
            {
                newAttribute.Add(subAttribute);
            }

            _attributes.Add(newAttribute);
            return(this);
        }
示例#10
0
        private static ICollection <SCIMRepresentationAttribute> BuildAttributes(JArray jArr, SCIMSchemaAttribute schemaAttribute)
        {
            var result = new List <SCIMRepresentationAttribute>();

            if (schemaAttribute.Type == SCIMSchemaAttributeTypes.COMPLEX)
            {
                foreach (var jsonProperty in jArr)
                {
                    var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute)
                    {
                        Values = BuildRepresentationAttributes(jsonProperty as JObject, schemaAttribute.SubAttributes)
                    };
                    result.Add(record);
                }
            }
            else
            {
                var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute);
                switch (schemaAttribute.Type)
                {
                case SCIMSchemaAttributeTypes.BOOLEAN:
                    record.ValuesBoolean = jArr.Select(j => bool.Parse(j.ToString())).ToList();
                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    record.ValuesInteger = jArr.Select(j => int.Parse(j.ToString())).ToList();
                    break;

                case SCIMSchemaAttributeTypes.DATETIME:
                    record.ValuesDateTime = jArr.Select(j => DateTime.Parse(j.ToString())).ToList();
                    break;

                case SCIMSchemaAttributeTypes.STRING:
                    record.ValuesString = jArr.Select(j => j.ToString()).ToList();
                    break;

                case SCIMSchemaAttributeTypes.REFERENCE:
                    record.ValuesReference = jArr.Select(j => j.ToString()).ToList();
                    break;
                }

                result.Add(record);
            }

            return(result);
        }
        private async Task EnrichWithOuterKeys(
            SCIMAttributeMapping attributeMapping,
            IEnumerable <SCIMRepresentation> representationLst,
            string baseUrl,
            IEnumerable <string> ids)
        {
            var values = representationLst.SelectMany(r => r.GetAttributesByAttrSchemaId(attributeMapping.SourceValueAttributeId)).SelectMany(a => a.ValuesString);
            var targetRepresentations = await _scimRepresentationQueryRepository.FindSCIMRepresentationByAttributes(attributeMapping.TargetAttributeId, ids, attributeMapping.TargetResourceType);

            if (!targetRepresentations.Any())
            {
                targetRepresentations = await _scimRepresentationQueryRepository.FindSCIMRepresentationByIds(values, attributeMapping.TargetResourceType);
            }

            foreach (var representation in representationLst)
            {
                var filteredRepresentations = targetRepresentations.Where(a => a.GetAttributesByAttrSchemaId(attributeMapping.TargetAttributeId).Any(attr => attr.ValuesString.Contains(representation.Id)));
                if (!filteredRepresentations.Any())
                {
                    continue;
                }

                foreach (var filteredRepresentation in filteredRepresentations)
                {
                    var refLst        = BuildAttributes(filteredRepresentation, attributeMapping, baseUrl);
                    var refsAttribute = new SCIMRepresentationAttribute
                    {
                        SchemaAttribute = new SCIMSchemaAttribute(attributeMapping.SourceAttributeSelector)
                        {
                            Id          = attributeMapping.SourceAttributeId,
                            Name        = attributeMapping.SourceAttributeSelector,
                            MultiValued = true,
                            Type        = SCIMSchemaAttributeTypes.COMPLEX
                        },
                        Values = refLst
                    };
                    representation.AddAttribute(refsAttribute);
                }
            }
        }
        public async Task Enrich(string resourceType, IEnumerable <SCIMRepresentation> representationLst, string baseUrl)
        {
            var attributeMappingLst = await _scimAttributeMappingQueryRepository.GetBySourceResourceType(resourceType);

            if (!attributeMappingLst.Any())
            {
                return;
            }

            var ids = representationLst.Select(r => r.Id);

            foreach (var attributeMapping in attributeMappingLst)
            {
                var targetRepresentations = await _scimRepresentationQueryRepository.FindSCIMRepresentationByAttributes(attributeMapping.TargetAttributeId, ids, attributeMapping.TargetResourceType);

                foreach (var representation in representationLst)
                {
                    var filteredRepresentations = targetRepresentations.Where(a => a.GetAttributesByAttrSchemaId(attributeMapping.TargetAttributeId).Any(attr => attr.ValuesString.Contains(representation.Id)));
                    if (!filteredRepresentations.Any())
                    {
                        continue;
                    }

                    var refLst        = new List <SCIMRepresentationAttribute>();
                    var refsAttribute = new SCIMRepresentationAttribute
                    {
                        SchemaAttribute = new SCIMSchemaAttribute(attributeMapping.SourceAttributeSelector)
                        {
                            Name        = attributeMapping.SourceAttributeSelector,
                            MultiValued = true,
                            Type        = SCIMSchemaAttributeTypes.COMPLEX
                        },
                        Values = refLst
                    };
                    foreach (var filteredRepresentation in filteredRepresentations)
                    {
                        refLst.Add(new SCIMRepresentationAttribute
                        {
                            SchemaAttribute = new SCIMSchemaAttribute("id")
                            {
                                Name        = "id",
                                MultiValued = false,
                                Type        = SCIMSchemaAttributeTypes.STRING
                            },
                            ValuesString = new List <string>
                            {
                                filteredRepresentation.Id
                            }
                        });
                        refLst.Add(new SCIMRepresentationAttribute
                        {
                            SchemaAttribute = new SCIMSchemaAttribute("display")
                            {
                                Name        = "display",
                                MultiValued = false,
                                Type        = SCIMSchemaAttributeTypes.STRING
                            },
                            ValuesString = filteredRepresentation.Attributes.First(a => a.SchemaAttribute.Name == "displayName").ValuesString
                        });
                        refLst.Add(new SCIMRepresentationAttribute
                        {
                            SchemaAttribute = new SCIMSchemaAttribute("$ref")
                            {
                                Name        = "$ref",
                                MultiValued = false,
                                Type        = SCIMSchemaAttributeTypes.STRING
                            },
                            ValuesString = new List <string>
                            {
                                $"{baseUrl}/{attributeMapping.TargetResourceType}/{filteredRepresentation.Id}"
                            }
                        });
                    }

                    representation.AddAttribute(refsAttribute);
                }
            }
        }
示例#13
0
        public static ICollection <SCIMRepresentationAttribute> BuildAttributes(JArray jArr, SCIMSchemaAttribute schemaAttribute, SCIMSchema schema, bool ignoreUnsupportedCanonicalValues)
        {
            var result = new List <SCIMRepresentationAttribute>();

            if (schemaAttribute.Mutability == SCIMSchemaAttributeMutabilities.READONLY)
            {
                return(result);
            }

            var attributeId = Guid.NewGuid().ToString();

            if (schemaAttribute.Type == SCIMSchemaAttributeTypes.COMPLEX)
            {
                if (!jArr.Any())
                {
                    result.Add(new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id));
                }
                else
                {
                    foreach (var jsonProperty in jArr)
                    {
                        var rec = jsonProperty as JObject;
                        if (rec == null)
                        {
                            throw new SCIMSchemaViolatedException(string.Format(Global.NotValidJSON, jsonProperty.ToString()));
                        }

                        var subAttributes = schema.GetChildren(schemaAttribute).ToList();
                        CheckRequiredAttributes(schema, subAttributes, rec);
                        var resolutionResult = Resolve(rec, schema, subAttributes);
                        var parent           = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id);
                        var children         = BuildRepresentationAttributes(resolutionResult, subAttributes, ignoreUnsupportedCanonicalValues);
                        foreach (var child in children)
                        {
                            if (SCIMRepresentation.GetParentPath(child.FullPath) == parent.FullPath)
                            {
                                child.ParentAttributeId = parent.Id;
                            }

                            result.Add(child);
                        }

                        result.Add(parent);
                    }
                }
            }
            else
            {
                switch (schemaAttribute.Type)
                {
                case SCIMSchemaAttributeTypes.BOOLEAN:
                    var valuesBooleanResult = Extract <bool>(jArr);
                    if (valuesBooleanResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidBoolean, string.Join(",", valuesBooleanResult.InvalidValues)));
                    }

                    foreach (var b in valuesBooleanResult.Values)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueBoolean: b);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    var valuesIntegerResult = Extract <int>(jArr);
                    if (valuesIntegerResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidInteger, string.Join(",", valuesIntegerResult.InvalidValues)));
                    }

                    foreach (var i in valuesIntegerResult.Values)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueInteger: i);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.DATETIME:
                    var valuesDateTimeResult = Extract <DateTime>(jArr);
                    if (valuesDateTimeResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidDateTime, string.Join(",", valuesDateTimeResult.InvalidValues)));
                    }

                    foreach (var d in valuesDateTimeResult.Values)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueDateTime: d);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.STRING:
                    var strs = jArr.Select(j => j.ToString()).ToList();
                    if (schemaAttribute.CanonicalValues != null &&
                        schemaAttribute.CanonicalValues.Any() &&
                        !ignoreUnsupportedCanonicalValues &&
                        !strs.All(_ => schemaAttribute.CaseExact ?
                                  schemaAttribute.CanonicalValues.Contains(_)
                                : schemaAttribute.CanonicalValues.Contains(_, StringComparer.OrdinalIgnoreCase))
                        )
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidCanonicalValue, schemaAttribute.Name));
                    }

                    foreach (var s in strs)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueString: s);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.REFERENCE:
                    var refs = jArr.Select(j => j.ToString()).ToList();
                    foreach (var reference in refs)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueReference: reference);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.DECIMAL:
                    var valuesDecimalResult = Extract <decimal>(jArr);
                    if (valuesDecimalResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidDecimal, string.Join(",", valuesDecimalResult.InvalidValues)));
                    }

                    foreach (var d in valuesDecimalResult.Values)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueDecimal: d);
                        result.Add(record);
                    }
                    break;

                case SCIMSchemaAttributeTypes.BINARY:
                    var invalidValues = new List <string>();
                    var valuesBinary  = new List <string>();
                    foreach (var rec in jArr)
                    {
                        try
                        {
                            Convert.FromBase64String(rec.ToString());
                            valuesBinary.Add(rec.ToString());
                        }
                        catch (FormatException)
                        {
                            invalidValues.Add(rec.ToString());
                        }
                    }

                    if (invalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidBase64, string.Join(",", invalidValues)));
                    }

                    foreach (var b in valuesBinary)
                    {
                        var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), attributeId, schemaAttribute, schema.Id, valueBinary: b);
                        result.Add(record);
                    }
                    break;
                }
            }

            return(result);
        }
        public static ICollection <SCIMRepresentationAttribute> BuildRepresentationAttributes(ResolutionResult resolutionResult, ICollection <SCIMSchemaAttribute> allSchemaAttributes, bool ignoreUnsupportedCanonicalValues, bool ignoreDefaultAttrs = false)
        {
            var attributes = new List <SCIMRepresentationAttribute>();

            foreach (var record in resolutionResult.Rows)
            {
                if (record.SchemaAttribute.Mutability == SCIMSchemaAttributeMutabilities.READONLY)
                {
                    continue;
                }

                // Add attribute
                if (record.SchemaAttribute.MultiValued)
                {
                    var jArr = record.Content as JArray;
                    if (jArr == null)
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.AttributeIsNotArray, record.SchemaAttribute.Name));
                    }


                    attributes.AddRange(BuildAttributes(jArr, record.SchemaAttribute, record.Schema, ignoreUnsupportedCanonicalValues));
                }
                else
                {
                    var jArr = new JArray();
                    jArr.Add(record.Content);
                    attributes.AddRange(BuildAttributes(jArr, record.SchemaAttribute, record.Schema, ignoreUnsupportedCanonicalValues));
                }
            }

            if (ignoreDefaultAttrs)
            {
                return(attributes);
            }

            var defaultAttributes = allSchemaAttributes.Where(a => !attributes.Any(at => at.SchemaAttribute.Name == a.Name) && a.Mutability == SCIMSchemaAttributeMutabilities.READWRITE);

            foreach (var defaultAttr in defaultAttributes)
            {
                var attr = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), defaultAttr);
                switch (defaultAttr.Type)
                {
                case SCIMSchemaAttributeTypes.STRING:
                    if (defaultAttr.DefaultValueString.Any())
                    {
                        var defaultValueStr = defaultAttr.DefaultValueString;
                        if (!defaultAttr.MultiValued)
                        {
                            defaultValueStr = new List <string> {
                                defaultValueStr.First()
                            };
                        }

                        foreach (var str in defaultValueStr)
                        {
                            attr.Add(str);
                        }

                        attributes.Add(attr);
                    }

                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    if (defaultAttr.DefaultValueInt.Any())
                    {
                        var defaultValueInt = defaultAttr.DefaultValueInt;
                        if (!defaultAttr.MultiValued)
                        {
                            defaultValueInt = new List <int> {
                                defaultValueInt.First()
                            };
                        }

                        foreach (var i in defaultValueInt)
                        {
                            attr.Add(i);
                        }

                        attributes.Add(attr);
                    }

                    break;
                }
            }

            return(attributes);
        }
示例#15
0
        public static ICollection <SCIMRepresentationAttribute> BuildRepresentationAttributes(JObject json, IEnumerable <SCIMSchemaAttribute> attrsSchema)
        {
            var result = new List <SCIMRepresentationAttribute>();

            foreach (var jsonProperty in json)
            {
                if (jsonProperty.Key == SCIMConstants.StandardSCIMRepresentationAttributes.Schemas)
                {
                    continue;
                }

                var attrSchema = attrsSchema.FirstOrDefault(a => a.Name == jsonProperty.Key);
                if (attrSchema == null)
                {
                    throw new SCIMSchemaViolatedException("unrecognizedAttribute", $"attribute {jsonProperty.Key} is not recognized by the SCIM schema");
                }

                if (attrSchema.Mutability == SCIMSchemaAttributeMutabilities.READONLY)
                {
                    continue;
                }

                if (attrSchema.MultiValued)
                {
                    var jArr = jsonProperty.Value as JArray;
                    if (jArr == null)
                    {
                        throw new SCIMSchemaViolatedException("badFormatAttribute", $"attribute {jsonProperty.Key} is not an array");
                    }

                    foreach (var subJson in jArr)
                    {
                        result.Add(BuildAttribute(subJson, attrSchema));
                    }
                }
                else
                {
                    result.Add(BuildAttribute(jsonProperty.Value, attrSchema));
                }
            }

            var defaultAttributes = attrsSchema.Where(a => !json.ContainsKey(a.Name) && a.Mutability == SCIMSchemaAttributeMutabilities.READWRITE);

            foreach (var defaultAttr in defaultAttributes)
            {
                var attr = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), defaultAttr);
                switch (defaultAttr.Type)
                {
                case SCIMSchemaAttributeTypes.STRING:
                    if (defaultAttr.DefaultValueString.Any())
                    {
                        var defaultValueStr = defaultAttr.DefaultValueString;
                        if (!defaultAttr.MultiValued)
                        {
                            defaultValueStr = new List <string> {
                                defaultValueStr.First()
                            };
                        }

                        foreach (var str in defaultValueStr)
                        {
                            attr.Add(str);
                        }

                        result.Add(attr);
                    }

                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    if (defaultAttr.DefaultValueInt.Any())
                    {
                        var defaultValueInt = defaultAttr.DefaultValueInt;
                        if (!defaultAttr.MultiValued)
                        {
                            defaultValueInt = new List <int> {
                                defaultValueInt.First()
                            };
                        }

                        foreach (var i in defaultValueInt)
                        {
                            attr.Add(i);
                        }

                        result.Add(attr);
                    }

                    break;
                }
            }

            return(result);
        }
示例#16
0
        public static ICollection <SCIMRepresentationAttribute> BuildRepresentationAttributes(JObject json, IEnumerable <SCIMSchemaAttribute> attrsSchema, bool ignoreUnsupportedCanonicalValues, bool ignoreDefaultAttrs = false)
        {
            var attributes = new List <SCIMRepresentationAttribute>();

            foreach (var jsonProperty in json)
            {
                if (jsonProperty.Key == SCIMConstants.StandardSCIMRepresentationAttributes.Schemas || SCIMConstants.StandardSCIMCommonRepresentationAttributes.Contains(jsonProperty.Key))
                {
                    continue;
                }

                var attrSchema = attrsSchema.FirstOrDefault(a => a.Name == jsonProperty.Key);
                if (attrSchema == null)
                {
                    throw new SCIMSchemaViolatedException(string.Format(Global.AttributeIsNotRecognirzed, jsonProperty.Key));
                }

                if (attrSchema.Mutability == SCIMSchemaAttributeMutabilities.READONLY)
                {
                    continue;
                }

                if (attrSchema.MultiValued)
                {
                    var jArr = jsonProperty.Value as JArray;
                    if (jArr == null)
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.AttributeIsNotArray, jsonProperty.Key));
                    }


                    attributes.AddRange(BuildAttributes(jArr, attrSchema, ignoreUnsupportedCanonicalValues));
                }
                else
                {
                    var jArr = new JArray();
                    jArr.Add(jsonProperty.Value);
                    attributes.AddRange(BuildAttributes(jArr, attrSchema, ignoreUnsupportedCanonicalValues));
                }
            }

            if (ignoreDefaultAttrs)
            {
                return(attributes);
            }

            var defaultAttributes = attrsSchema.Where(a => !json.ContainsKey(a.Name) && a.Mutability == SCIMSchemaAttributeMutabilities.READWRITE);

            foreach (var defaultAttr in defaultAttributes)
            {
                var attr = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), defaultAttr);
                switch (defaultAttr.Type)
                {
                case SCIMSchemaAttributeTypes.STRING:
                    if (defaultAttr.DefaultValueString.Any())
                    {
                        var defaultValueStr = defaultAttr.DefaultValueString;
                        if (!defaultAttr.MultiValued)
                        {
                            defaultValueStr = new List <string> {
                                defaultValueStr.First()
                            };
                        }

                        foreach (var str in defaultValueStr)
                        {
                            attr.Add(str);
                        }

                        attributes.Add(attr);
                    }

                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    if (defaultAttr.DefaultValueInt.Any())
                    {
                        var defaultValueInt = defaultAttr.DefaultValueInt;
                        if (!defaultAttr.MultiValued)
                        {
                            defaultValueInt = new List <int> {
                                defaultValueInt.First()
                            };
                        }

                        foreach (var i in defaultValueInt)
                        {
                            attr.Add(i);
                        }

                        attributes.Add(attr);
                    }

                    break;
                }
            }

            return(attributes);
        }
        public static ICollection <SCIMRepresentationAttribute> BuildAttributes(JArray jArr, SCIMSchemaAttribute schemaAttribute, SCIMSchema schema, bool ignoreUnsupportedCanonicalValues)
        {
            var result = new List <SCIMRepresentationAttribute>();

            if (schemaAttribute.Type == SCIMSchemaAttributeTypes.COMPLEX)
            {
                if (!jArr.Any())
                {
                    result.Add(new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute));
                }
                else
                {
                    foreach (var jsonProperty in jArr)
                    {
                        var rec = jsonProperty as JObject;
                        if (rec == null)
                        {
                            throw new SCIMSchemaViolatedException(string.Format(Global.NotValidJSON, jsonProperty.ToString()));
                        }

                        CheckRequiredAttributes(schema, schemaAttribute.SubAttributes, rec);
                        var resolutionResult = Resolve(rec, schema, schemaAttribute.SubAttributes);
                        var record           = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute)
                        {
                            Values = BuildRepresentationAttributes(resolutionResult, schemaAttribute.SubAttributes, ignoreUnsupportedCanonicalValues)
                        };

                        foreach (var subAttribute in record.Values)
                        {
                            subAttribute.Parent = record;
                        }

                        result.Add(record);
                    }
                }
            }
            else
            {
                var record = new SCIMRepresentationAttribute(Guid.NewGuid().ToString(), schemaAttribute);
                switch (schemaAttribute.Type)
                {
                case SCIMSchemaAttributeTypes.BOOLEAN:
                    var valuesBooleanResult = Extract <bool>(jArr);
                    if (valuesBooleanResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidBoolean, string.Join(",", valuesBooleanResult.InvalidValues)));
                    }

                    record.ValuesBoolean = valuesBooleanResult.Values;
                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    var valuesIntegerResult = Extract <int>(jArr);
                    if (valuesIntegerResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidInteger, string.Join(",", valuesIntegerResult.InvalidValues)));
                    }

                    record.ValuesInteger = valuesIntegerResult.Values;
                    break;

                case SCIMSchemaAttributeTypes.DATETIME:
                    var valuesDateTimeResult = Extract <DateTime>(jArr);
                    if (valuesDateTimeResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidDateTime, string.Join(",", valuesDateTimeResult.InvalidValues)));
                    }

                    record.ValuesDateTime = valuesDateTimeResult.Values;
                    break;

                case SCIMSchemaAttributeTypes.STRING:
                    record.ValuesString = jArr.Select(j => j.ToString()).ToList();
                    if (schemaAttribute.CanonicalValues != null && schemaAttribute.CanonicalValues.Any() && !ignoreUnsupportedCanonicalValues && !record.ValuesString.All(_ => schemaAttribute.CanonicalValues.Contains(_)))
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidCanonicalValue, schemaAttribute.Name));
                    }

                    break;

                case SCIMSchemaAttributeTypes.REFERENCE:
                    record.ValuesReference = jArr.Select(j => j.ToString()).ToList();
                    break;

                case SCIMSchemaAttributeTypes.DECIMAL:
                    var valuesDecimalResult = Extract <decimal>(jArr);
                    if (valuesDecimalResult.InvalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidDecimal, string.Join(",", valuesDecimalResult.InvalidValues)));
                    }

                    record.ValuesDecimal = valuesDecimalResult.Values;
                    break;

                case SCIMSchemaAttributeTypes.BINARY:
                    var invalidValues = new List <string>();
                    var valuesBinary  = new List <byte[]>();
                    foreach (var rec in jArr)
                    {
                        try
                        {
                            valuesBinary.Add(Convert.FromBase64String(rec.ToString()));
                        }
                        catch (FormatException)
                        {
                            invalidValues.Add(rec.ToString());
                        }
                    }

                    if (invalidValues.Any())
                    {
                        throw new SCIMSchemaViolatedException(string.Format(Global.NotValidBase64, string.Join(",", invalidValues)));
                    }

                    record.ValuesBinary = valuesBinary;
                    break;
                }

                result.Add(record);
            }

            return(result);
        }