Esempio n. 1
0
        private static void Enrich(SCIMSchema schema, ICollection <SCIMSchemaAttribute> attributes, IDictionary <string, OpenApiSchema> properties)
        {
            foreach (var attr in attributes)
            {
                var sc = new OpenApiSchema
                {
                    Description = attr.Description,
                    Type        = attr.MultiValued ? "array" : MAPPING_ENUM_TO_NAMES[attr.Type],
                    Properties  = new Dictionary <string, OpenApiSchema>()
                };
                if (!attr.MultiValued && MAPPING_ENUM_TO_FORMAT.ContainsKey(attr.Type))
                {
                    sc.Format = MAPPING_ENUM_TO_FORMAT[attr.Type];
                }

                properties.Add(new KeyValuePair <string, OpenApiSchema>(attr.Name, sc));
                if (attr.MultiValued && attr.Type != SCIMSchemaAttributeTypes.COMPLEX)
                {
                    sc.Items = new OpenApiSchema
                    {
                        Type = MAPPING_ENUM_TO_NAMES[attr.Type]
                    };
                    if (MAPPING_ENUM_TO_FORMAT.ContainsKey(attr.Type))
                    {
                        sc.Format = MAPPING_ENUM_TO_FORMAT[attr.Type];
                    }

                    continue;
                }

                var values = schema.GetChildren(attr).ToList();
                if (attr.MultiValued && attr.Type == SCIMSchemaAttributeTypes.COMPLEX)
                {
                    sc.Items = new OpenApiSchema
                    {
                        Type       = "object",
                        Properties = new Dictionary <string, OpenApiSchema>()
                    };
                    Enrich(schema, values, sc.Items.Properties);
                    continue;
                }

                if (attr.Type == SCIMSchemaAttributeTypes.COMPLEX && !attr.MultiValued)
                {
                    Enrich(schema, values, sc.Properties);
                }
            }
        }
Esempio n. 2
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 SCIMRepresentationAttributeBuilder AddAttribute(string name, List <int> valuesInt = null, List <bool> valuesBool = null, List <string> valuesString = null, List <DateTime> valuesDateTime = null, List <decimal> valuesDecimal = null, List <string> valuesBinary = null)
        {
            var id = Guid.NewGuid().ToString();
            SCIMSchemaAttribute schemaAttribute = null;
            var subAttributes = _schema.GetChildren(_scimSchemaAttribute);

            if (subAttributes != null)
            {
                schemaAttribute = subAttributes.FirstOrDefault(a => a.Name == name);
            }

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

            if (valuesInt != null)
            {
                foreach (var attr in valuesInt)
                {
                    _attributes.Add(new SCIMRepresentationAttribute(id, attributeId, schemaAttribute, schemaAttribute.SchemaId, valueInteger: attr)
                    {
                        ParentAttributeId = _parentId
                    });
                }
            }
            else if (valuesBool != null)
            {
                foreach (var attr in valuesBool)
                {
                    _attributes.Add(new SCIMRepresentationAttribute(id, attributeId, schemaAttribute, schemaAttribute.SchemaId, valueBoolean: attr)
                    {
                        ParentAttributeId = _parentId
                    });
                }
            }
            else if (valuesString != null)
            {
                foreach (var attr in valuesString)
                {
                    _attributes.Add(new SCIMRepresentationAttribute(id, attributeId, schemaAttribute, schemaAttribute.SchemaId, valueString: attr)
                    {
                        ParentAttributeId = _parentId
                    });
                }
            }
            else if (valuesDateTime != null)
            {
                foreach (var attr in valuesDateTime)
                {
                    _attributes.Add(new SCIMRepresentationAttribute(id, attributeId, schemaAttribute, schemaAttribute.SchemaId, valueDateTime: attr)
                    {
                        ParentAttributeId = _parentId
                    });
                }
            }
            else if (valuesDecimal != null)
            {
                foreach (var attr in valuesDecimal)
                {
                    _attributes.Add(new SCIMRepresentationAttribute(id, attributeId, schemaAttribute, schemaAttribute.SchemaId, valueDecimal: attr)
                    {
                        ParentAttributeId = _parentId
                    });
                }
            }
            else if (valuesBinary != null)
            {
                foreach (var attr in valuesBinary)
                {
                    _attributes.Add(new SCIMRepresentationAttribute(id, attributeId, schemaAttribute, schemaAttribute.SchemaId, valueBinary: attr)
                    {
                        ParentAttributeId = _parentId
                    });
                }
            }

            return(this);
        }