Example #1
0
        public async Task <RepresentationSyncResult> Sync(string resourceType, SCIMRepresentation newSourceScimRepresentation, ICollection <SCIMPatchResult> patchOperations, string location)
        {
            var stopWatch           = new Stopwatch();
            var result              = new RepresentationSyncResult(_resourceTypeResolver);
            var attributeMappingLst = await _scimAttributeMappingQueryRepository.GetBySourceResourceType(resourceType);

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

            foreach (var attributeMapping in attributeMappingLst)
            {
                var existingIds = newSourceScimRepresentation.GetAttributesByAttrSchemaId(attributeMapping.SourceAttributeId).SelectMany(a => newSourceScimRepresentation.GetChildren(a).Where(v => v.SchemaAttribute.Name == "value")).Select(v => v.ValueString);
                var newIds      = patchOperations
                                  .Where(p => p.Operation == DTOs.SCIMPatchOperations.ADD && p.Attr.SchemaAttributeId == attributeMapping.SourceAttributeId)
                                  .SelectMany(p => patchOperations.Where(po => po.Attr.ParentAttributeId == p.Attr.Id && po.Attr.SchemaAttribute.Name == "value").Select(po => po.Attr.ValueString));
                var idsToBeRemoved = patchOperations
                                     .Where(p => p.Operation == DTOs.SCIMPatchOperations.REMOVE && p.Attr.SchemaAttributeId == attributeMapping.SourceAttributeId)
                                     .SelectMany(p => patchOperations.Where(po => po.Attr.ParentAttributeId == p.Attr.Id && po.Attr.SchemaAttribute.Name == "value").Select(po => po.Attr.ValueString));
                var duplicateIds = existingIds.GroupBy(i => i).Where(i => i.Count() > 1);
                if (duplicateIds.Any())
                {
                    throw new SCIMUniquenessAttributeException(string.Format(Global.DuplicateReference, string.Join(",", duplicateIds.Select(_ => _.Key).Distinct())));
                }

                await RemoveReferenceAttributes(result, idsToBeRemoved, attributeMapping, newSourceScimRepresentation, location);
                await UpdateReferenceAttributes(result, newIds, attributeMapping, newSourceScimRepresentation, location);
            }

            return(result);
        }
Example #2
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);
            }
        }
Example #3
0
        protected virtual void UpdateScimRepresentation(SCIMRepresentation scimRepresentation, SCIMRepresentation sourceRepresentation, string attributeId, string resourceType, out bool isAttrUpdated)
        {
            isAttrUpdated = false;
            var attr = scimRepresentation.GetAttributesByAttrSchemaId(attributeId).FirstOrDefault(v => scimRepresentation.GetChildren(v).Any(c => c.ValueString == sourceRepresentation.Id));

            if (attr != null)
            {
                scimRepresentation.RemoveAttributeById(attr);
                isAttrUpdated = true;
            }

            BuildScimRepresentationAttribute(attributeId, scimRepresentation, sourceRepresentation, resourceType);
        }
Example #4
0
        public async virtual Task <RepresentationSyncResult> Sync(string resourceType, SCIMRepresentation oldScimRepresentation, SCIMRepresentation newSourceScimRepresentation, string location, bool updateAllReferences = false, bool isScimRepresentationRemoved = false)
        {
            var stopWatch           = new Stopwatch();
            var result              = new RepresentationSyncResult(_resourceTypeResolver);
            var attributeMappingLst = await _scimAttributeMappingQueryRepository.GetBySourceResourceType(resourceType);

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

            foreach (var attributeMapping in attributeMappingLst)
            {
                var newIds = newSourceScimRepresentation.GetAttributesByAttrSchemaId(attributeMapping.SourceAttributeId).SelectMany(a => newSourceScimRepresentation.GetChildren(a).Where(v => v.SchemaAttribute.Name == "value")).Select(v => v.ValueString);
                if (isScimRepresentationRemoved)
                {
                    await RemoveReferenceAttributes(result, newIds, attributeMapping, newSourceScimRepresentation, location);
                }
                else
                {
                    var oldIds         = oldScimRepresentation.GetAttributesByAttrSchemaId(attributeMapping.SourceAttributeId).SelectMany(a => oldScimRepresentation.GetChildren(a).Where(v => v.SchemaAttribute.Name == "value")).Select(v => v.ValueString);
                    var idsToBeRemoved = oldIds.Except(newIds);
                    var duplicateIds   = newIds.GroupBy(i => i).Where(i => i.Count() > 1);
                    if (duplicateIds.Any())
                    {
                        throw new SCIMUniquenessAttributeException(string.Format(Global.DuplicateReference, string.Join(",", duplicateIds.Select(_ => _.Key).Distinct())));
                    }

                    if (!updateAllReferences)
                    {
                        newIds = newIds.Except(oldIds).ToList();
                    }

                    await RemoveReferenceAttributes(result, idsToBeRemoved, attributeMapping, newSourceScimRepresentation, location);
                    await UpdateReferenceAttributes(result, newIds, attributeMapping, newSourceScimRepresentation, location);
                }
            }

            return(result);
        }