示例#1
0
        public async Task <bool> Update(SCIMRepresentation data, CancellationToken token)
        {
            await Delete(data, token);
            await Add(data, token);

            return(true);
        }
        protected IActionResult BuildHTTPResult(SCIMRepresentation representation, HttpStatusCode status, bool isGetRequest)
        {
            var location = GetLocation(representation);
            var content  = representation.ToResponse(location, isGetRequest);

            return(BuildHTTPResult(status, location, representation.Version, content));
        }
 private SCIMRepresentationBuilder(SCIMRepresentation scimRepresentation, ICollection <SCIMSchema> schemas)
 {
     _id             = scimRepresentation.Id;
     _attributes     = scimRepresentation.FlatAttributes;
     _schemas        = schemas;
     _representation = scimRepresentation;
 }
示例#4
0
        private async Task CheckSCIMRepresentationExistsForGivenUniqueAttributes(IEnumerable <SCIMRepresentationAttribute> attributes, string endpoint = null)
        {
            foreach (var attribute in attributes)
            {
                SCIMRepresentation record = null;
                switch (attribute.SchemaAttribute.Type)
                {
                case SCIMSchemaAttributeTypes.STRING:
                    record = await _scimRepresentationQueryRepository.FindSCIMRepresentationByAttribute(attribute.SchemaAttribute.Id, attribute.ValueString, endpoint);

                    break;

                case SCIMSchemaAttributeTypes.INTEGER:
                    if (attribute.ValueInteger != null)
                    {
                        record = await _scimRepresentationQueryRepository.FindSCIMRepresentationByAttribute(attribute.SchemaAttribute.Id, attribute.ValueInteger.Value, endpoint);
                    }

                    break;
                }

                if (record != null)
                {
                    throw new SCIMUniquenessAttributeException(string.Format(Global.AttributeMustBeUnique, attribute.SchemaAttribute.Name));
                }
            }
        }
        public static ICollection <SCIMRepresentationAttribute> EvaluateAttributes(this SCIMExpression expression, IQueryable <SCIMRepresentationAttribute> attributes, bool isStrictPath)
        {
            var attr = expression as SCIMAttributeExpression;

            if (attr.SchemaAttribute == null || string.IsNullOrWhiteSpace(attr.SchemaAttribute.Id))
            {
                return(new List <SCIMRepresentationAttribute>());
            }
            else
            {
                var treeNodeParameter  = Expression.Parameter(typeof(SCIMRepresentationAttribute), "tn");
                var anyWhereExpression = expression.EvaluateAttributes(treeNodeParameter);
                var enumarableType     = typeof(Queryable);
                var whereMethod        = enumarableType.GetMethods()
                                         .Where(m => m.Name == "Where" && m.IsGenericMethodDefinition)
                                         .Where(m => m.GetParameters().Count() == 2).First().MakeGenericMethod(typeof(SCIMRepresentationAttribute));
                var equalLambda            = Expression.Lambda <Func <SCIMRepresentationAttribute, bool> >(anyWhereExpression, treeNodeParameter);
                var whereExpr              = Expression.Call(whereMethod, Expression.Constant(attributes), equalLambda);
                var finalSelectArg         = Expression.Parameter(typeof(IQueryable <SCIMRepresentationAttribute>), "f");
                var finalSelectRequestBody = Expression.Lambda(whereExpr, new ParameterExpression[] { finalSelectArg });
                var result   = (IQueryable <SCIMRepresentationAttribute>)finalSelectRequestBody.Compile().DynamicInvoke(attributes);
                var fullPath = attr.GetFullPath();
                var res      = SCIMRepresentation.BuildFlatAttributes(result.ToList());
                return(res.Where((a) => a != null && (isStrictPath ? fullPath == a.FullPath : fullPath.StartsWith(a.FullPath) || a.FullPath.StartsWith(fullPath))).ToList());
            }
        }
示例#6
0
        public static SCIMRepresentation BuildRepresentation(JObject json, string externalId, SCIMSchema mainSchema, ICollection <SCIMSchema> extensionSchemas, bool ignoreUnsupportedCanonicalValues)
        {
            var schemas = new List <SCIMSchema>
            {
                mainSchema
            };

            schemas.AddRange(extensionSchemas);
            var result = new SCIMRepresentation
            {
                ExternalId = externalId,
                Schemas    = schemas
            };

            result.Schemas = schemas;
            var resolutionResult = Resolve(json, mainSchema, extensionSchemas);

            result.FlatAttributes = BuildRepresentationAttributes(resolutionResult, resolutionResult.AllSchemaAttributes, ignoreUnsupportedCanonicalValues);
            var attr = result.FlatAttributes.FirstOrDefault(a => a.SchemaAttribute.Name == "displayName");

            if (attr != null)
            {
                result.DisplayName = attr.ValueString;
            }

            return(result);
        }
        public Task <bool> Add(SCIMRepresentation data, CancellationToken token)
        {
            var record = ToModel(data);

            _scimDbContext.SCIMRepresentationLst.Add(record);
            return(Task.FromResult(true));
        }
        public Task <bool> Delete(SCIMRepresentation data, CancellationToken token)
        {
            var result = _scimDbContext.SCIMRepresentationLst
                         .Include(s => s.Attributes).ThenInclude(s => s.Values)
                         .Include(s => s.Attributes).ThenInclude(s => s.SchemaAttribute)
                         .Include(s => s.Attributes).ThenInclude(s => s.Children).ThenInclude(s => s.Values)
                         .Include(s => s.Attributes).ThenInclude(s => s.Children).ThenInclude(s => s.Children)
                         .Include(s => s.Attributes).ThenInclude(s => s.Children).ThenInclude(s => s.Children).ThenInclude(s => s.Values)
                         .Include(s => s.Attributes).ThenInclude(s => s.Children).ThenInclude(s => s.Children).ThenInclude(s => s.SchemaAttribute)
                         .Include(s => s.Schemas).ThenInclude(s => s.Schema).ThenInclude(s => s.Attributes)
                         .Include(s => s.Schemas).ThenInclude(s => s.Schema).ThenInclude(s => s.SchemaExtensions)
                         .FirstOrDefault(r => r.Id == data.Id);

            if (result == null)
            {
                return(Task.FromResult(false));
            }

            var attrs = new List <SCIMRepresentationAttributeModel>();

            GetAllAttributes(result.Attributes, attrs);
            _scimDbContext.SCIMRepresentationAttributeLst.RemoveRange(attrs);
            foreach (var attr in attrs)
            {
                _scimDbContext.SCIMRepresentationAttributeValueLst.RemoveRange(attr.Values);
            }

            _scimDbContext.SCIMRepresentationSchemaLst.RemoveRange(result.Schemas);
            _scimDbContext.SCIMRepresentationLst.Remove(result);
            return(Task.FromResult(false));
        }
        public bool Delete(SCIMRepresentation data)
        {
            var record = _scimDbContext.SCIMRepresentationLst.Find(data.Id);

            _scimDbContext.SCIMRepresentationLst.Remove(record);
            return(true);
        }
示例#10
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);
        }
示例#11
0
        private static JObject ExcludeAttributes(SCIMRepresentation representation, IEnumerable <SCIMAttributeExpression> attributes, string location = null, bool isComplex = false)
        {
            var clone = representation.Clone() as SCIMRepresentation;

            clone.FilterAttributes(new SCIMAttributeExpression[0], attributes);
            clone.AddStandardAttributes(location, attributes.Select(_ => _.GetFullPath()), false, false);
            return(clone.ToResponse(location, false, false));
        }
示例#12
0
        public async Task <bool> Delete(SCIMRepresentation data, CancellationToken token)
        {
            var representations = _scimDbContext.SCIMRepresentationLst;
            var deleteFilter    = Builders <SCIMRepresentationModel> .Filter.Eq("_id", data.Id);

            await representations.DeleteOneAsync(deleteFilter, null, token);

            return(true);
        }
        public void RemoveReferenceAttr(SCIMRepresentation representation, string schemaAttributeId, string fullPath, string value, string location)
        {
            location = $"{location}/{_resourceTypeResolver.ResolveByResourceType(representation.ResourceType).ControllerName}/{representation.Id}";
            var obj    = representation.Duplicate().ToResponse(location, false, addEmptyArray: true);
            var newEvt = new RepresentationReferenceAttributeRemovedEvent(Guid.NewGuid().ToString(), representation.Version, representation.ResourceType, representation.Id, schemaAttributeId, fullPath, obj);

            newEvt.Values.Add(value);
            ProcessReferenceAttr(RemoveAttrEvts, representation, schemaAttributeId, newEvt, value);
        }
示例#14
0
        public static SCIMRepresentation BuildRepresentation(JObject json, IEnumerable <SCIMSchemaAttribute> attrsSchema, string externalId, ICollection <SCIMSchema> schemas, bool ignoreUnsupportedCanonicalValues)
        {
            var result = new SCIMRepresentation();

            result.ExternalId = externalId;
            result.Schemas    = schemas;
            result.Attributes = BuildRepresentationAttributes(json, attrsSchema, ignoreUnsupportedCanonicalValues);
            return(result);
        }
示例#15
0
        public bool Update(SCIMRepresentation data)
        {
            if (!Delete(data))
            {
                return(false);
            }

            Add(data);
            return(true);
        }
 private SCIMRepresentationBuilder(ICollection <SCIMSchema> schemas)
 {
     _id             = Guid.NewGuid().ToString();
     _attributes     = new List <SCIMRepresentationAttribute>();
     _schemas        = schemas;
     _representation = new SCIMRepresentation(_schemas, _attributes)
     {
         Id = _id
     };
 }
        private void ProcessReferenceAttr <T>(ICollection <T> evts, SCIMRepresentation representation, string schemaAttributeId, T newEvt, string value) where T : BaseReferenceAttributeEvent
        {
            var evt = evts.FirstOrDefault(e => e.RepresentationAggregateId == representation.Id && e.SchemaAttributeId == schemaAttributeId);

            if (evt != null)
            {
                evt.Values.Add(value);
                return;
            }

            evts.Add(newEvt);
        }
示例#18
0
 public SCIMRepresentationModel(SCIMRepresentation representation, string schemaCollectionName) : this()
 {
     Id             = representation.Id;
     ExternalId     = representation.ExternalId;
     ResourceType   = representation.ResourceType;
     Version        = representation.Version;
     Created        = representation.Created;
     LastModified   = representation.LastModified;
     DisplayName    = representation.DisplayName;
     FlatAttributes = representation.FlatAttributes;
     SchemaRefs     = representation.Schemas.Select(s => new MongoDBRef(schemaCollectionName, s.Id)).ToList();
 }
示例#19
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);
            }
        }
示例#20
0
        private IActionResult BuildHTTPResult(SCIMRepresentation representation, HttpStatusCode status, bool isGetRequest)
        {
            var location = $"{Request.GetAbsoluteUriWithVirtualPath()}/{_resourceType}/{representation.Id}";

            HttpContext.Response.Headers.Add("Location", location);
            HttpContext.Response.Headers.Add("ETag", representation.Version);
            return(new ContentResult
            {
                StatusCode = (int)status,
                Content = representation.ToResponse(location, isGetRequest).ToString(),
                ContentType = SCIMConstants.STANDARD_SCIM_CONTENT_TYPE
            });
        }
        public async Task <bool> Delete(SCIMRepresentation data, CancellationToken token)
        {
            if (_session != null)
            {
                await _scimDbContext.SCIMRepresentationLst.DeleteOneAsync(_session, d => d.Id == data.Id, null, token);
            }
            else
            {
                await _scimDbContext.SCIMRepresentationLst.DeleteOneAsync(d => d.Id == data.Id, null, token);
            }

            return(true);
        }
示例#22
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);
        }
        public async Task <bool> Update(SCIMRepresentation data, CancellationToken token)
        {
            var record = new SCIMRepresentationModel(data, _options.CollectionSchemas);

            if (_session != null)
            {
                await _scimDbContext.SCIMRepresentationLst.ReplaceOneAsync(_session, s => s.Id == data.Id, record);
            }
            else
            {
                await _scimDbContext.SCIMRepresentationLst.ReplaceOneAsync(s => s.Id == data.Id, record);
            }

            return(true);
        }
        public async Task <bool> Add(SCIMRepresentation representation, CancellationToken token)
        {
            var record = new SCIMRepresentationModel(representation, _options.CollectionSchemas);

            if (_session != null)
            {
                await _scimDbContext.SCIMRepresentationLst.InsertOneAsync(_session, record, null, token);
            }
            else
            {
                await _scimDbContext.SCIMRepresentationLst.InsertOneAsync(record, null, token);
            }

            return(true);
        }
示例#25
0
        public static SCIMRepresentation BuildRepresentation(JObject json, IEnumerable <SCIMSchemaAttribute> attrsSchema, ICollection <SCIMSchema> schemas, bool ignoreUnsupportedCanonicalValues)
        {
            var result     = new SCIMRepresentation();
            var externalId = json.SelectToken(SCIMConstants.StandardSCIMRepresentationAttributes.ExternalId);

            if (externalId != null)
            {
                result.ExternalId = externalId.ToString();
                json.Remove(SCIMConstants.StandardSCIMRepresentationAttributes.ExternalId);
            }

            result.Schemas    = schemas;
            result.Attributes = BuildRepresentationAttributes(json, attrsSchema, ignoreUnsupportedCanonicalValues);
            return(result);
        }
        private static List <SCIMRepresentationAttribute> BuildAttributes(SCIMRepresentation representation, SCIMAttributeMapping attributeMapping, string baseUrl)
        {
            var refLst = new List <SCIMRepresentationAttribute>();

            refLst.Add(new SCIMRepresentationAttribute
            {
                SchemaAttribute = new SCIMSchemaAttribute("value")
                {
                    Name        = "value",
                    MultiValued = false,
                    Type        = SCIMSchemaAttributeTypes.STRING
                },
                ValuesString = new List <string>
                {
                    representation.Id
                }
            });
            refLst.Add(new SCIMRepresentationAttribute
            {
                SchemaAttribute = new SCIMSchemaAttribute("display")
                {
                    Name        = "display",
                    MultiValued = false,
                    Type        = SCIMSchemaAttributeTypes.STRING
                },
                ValuesString = new List <string> {
                    representation.DisplayName
                }
            });
            refLst.Add(new SCIMRepresentationAttribute
            {
                SchemaAttribute = new SCIMSchemaAttribute("$ref")
                {
                    Name        = "$ref",
                    MultiValued = false,
                    Type        = SCIMSchemaAttributeTypes.STRING
                },
                ValuesString = new List <string>
                {
                    $"{baseUrl}/{attributeMapping.TargetResourceType}/{representation.Id}"
                }
            });

            return(refLst);
        }
        public static SCIMRepresentation ToDomain(this SCIMRepresentationModel representation)
        {
            var result = new SCIMRepresentation
            {
                Created      = representation.Created,
                ExternalId   = representation.ExternalId,
                LastModified = representation.LastModified,
                Version      = representation.Version,
                ResourceType = representation.ResourceType,
                Id           = representation.Id,
                Schemas      = representation.Schemas.Select(s => ToDomain(s.Schema)).ToList(),
                Attributes   = representation.Attributes.Where(_ => _.Parent == null).Select(s =>
                {
                    return(ToDomain(s));
                }).ToList()
            };

            return(result);
        }
示例#28
0
        public async Task <bool> Add(SCIMRepresentation data, CancellationToken token)
        {
            var representations = _scimDbContext.SCIMRepresentationLst;
            var record          = new SCIMRepresentationModel
            {
                Created      = data.Created,
                ExternalId   = data.ExternalId,
                Id           = data.Id,
                LastModified = data.LastModified,
                ResourceType = data.ResourceType,
                Version      = data.Version,
                Attributes   = data.Attributes.Select(_ => _.ToModel()).ToList()
            };

            record.SetSchemas(data.Schemas.Select(_ => _.ToModel()).ToList(), _options.CollectionSchemas);
            await representations.InsertOneAsync(record, null, token);

            return(true);
        }
示例#29
0
        public static SCIMRepresentation ToDomain(this SCIMRepresentationModel representation, IMongoDatabase db)
        {
            var result = new SCIMRepresentation
            {
                Created      = representation.Created,
                ExternalId   = representation.ExternalId,
                LastModified = representation.LastModified,
                Version      = representation.Version,
                ResourceType = representation.ResourceType,
                Id           = representation.Id,
                Schemas      = representation.GetSchemas(db).Select(_ => _.ToDomain()).ToList(),
                Attributes   = representation.Attributes.Select(s =>
                {
                    return(ToDomain(s));
                }).ToList()
            };

            return(result);
        }
        public Task <bool> Add(SCIMRepresentation data, CancellationToken token)
        {
            var record = new SCIMRepresentationModel
            {
                Created      = data.Created,
                ExternalId   = data.ExternalId,
                LastModified = data.LastModified,
                Version      = data.Version,
                ResourceType = data.ResourceType,
                Id           = data.Id,
                Attributes   = data.Attributes.Select(a => a.ToModel(data.Id)).ToList(),
                Schemas      = data.Schemas.Select(s => new SCIMRepresentationSchemaModel
                {
                    SCIMRepresentationId = data.Id,
                    SCIMSchemaId         = s.Id
                }).ToList()
            };

            _scimDbContext.SCIMRepresentationLst.Add(record);
            return(Task.FromResult(true));
        }