|
1 | 1 | // Copyright (c) SimpleIdServer. All rights reserved. |
2 | 2 | // Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | +using Microsoft.CodeAnalysis; |
3 | 4 | using SimpleIdServer.Scim.Domains; |
4 | 5 | using SimpleIdServer.Scim.DTOs; |
5 | 6 | using SimpleIdServer.Scim.Exceptions; |
|
10 | 11 | using System.Diagnostics; |
11 | 12 | using System.Linq; |
12 | 13 | using System.Threading.Tasks; |
| 14 | +using static MassTransit.ValidationResultExtensions; |
13 | 15 |
|
14 | 16 | namespace SimpleIdServer.Scim.Helpers |
15 | 17 | { |
@@ -96,7 +98,7 @@ public async Task<RepresentationSyncResult> Sync(string resourceType, SCIMRepre |
96 | 98 | if (duplicateIds.Any()) throw new SCIMUniquenessAttributeException(string.Format(Global.DuplicateReference, string.Join(",", duplicateIds.Select(_ => _.Key).Distinct()))); |
97 | 99 | await RemoveReferenceAttributes(result, idsToBeRemoved, attributeMapping, newSourceScimRepresentation, location); |
98 | 100 | await AddReferenceAttributes(result, newIds, attributeMapping, newSourceScimRepresentation, location, targetSchemas.First(s => s.ResourceType == attributeMapping.TargetResourceType)); |
99 | | - await UpdateReferenceAttributes(existingIds, attributeMapping, newSourceScimRepresentation, patchOperations, targetSchemas.First(s => s.ResourceType == attributeMapping.TargetResourceType)); |
| 101 | + await UpdateReferenceAttributes(result, existingIds, attributeMapping, newSourceScimRepresentation, patchOperations, targetSchemas.First(s => s.ResourceType == attributeMapping.TargetResourceType), location: location); |
100 | 102 | } |
101 | 103 |
|
102 | 104 | return result; |
@@ -141,19 +143,21 @@ protected virtual async Task AddReferenceAttributes(RepresentationSyncResult res |
141 | 143 | var sourceSchema = sourceScimRepresentation.Schemas.First(s => s.ResourceType == attributeMapping.SourceResourceType && s.GetAttributeById(attributeMapping.SourceAttributeId) != null); |
142 | 144 | UpdateScimRepresentation(targetRepresentation, sourceScimRepresentation, attributeMapping.TargetAttributeId, attributeMapping.SourceResourceType, targetSchema, out isAttrUpdated); |
143 | 145 | UpdateScimRepresentation(sourceScimRepresentation, targetRepresentation, attributeMapping.SourceAttributeId, attributeMapping.TargetResourceType, sourceSchema, out bool b); |
144 | | - if (!isAttrUpdated && !string.IsNullOrWhiteSpace(attributeMapping.TargetAttributeId)) result.AddReferenceAttr(targetRepresentation, attributeMapping.TargetAttributeId, targetSchema.GetAttributeById(attributeMapping.TargetAttributeId).FullPath, sourceScimRepresentation.Id, location); |
| 146 | + if (!string.IsNullOrWhiteSpace(attributeMapping.TargetAttributeId) && !isAttrUpdated) |
| 147 | + result.AddReferenceAttr(targetRepresentation, attributeMapping.TargetAttributeId, targetSchema.GetAttributeById(attributeMapping.TargetAttributeId).FullPath, sourceScimRepresentation.Id, location); |
145 | 148 | result.AddRepresentation(targetRepresentation); |
146 | 149 | } |
147 | 150 | } |
148 | 151 | } |
149 | 152 |
|
150 | | - protected virtual async Task UpdateReferenceAttributes(IEnumerable<string> ids, SCIMAttributeMapping attributeMapping, SCIMRepresentation sourceScimRepresentation, ICollection<SCIMPatchResult> patchOperations, SCIMSchema targetSchema) |
| 153 | + protected virtual async Task UpdateReferenceAttributes(RepresentationSyncResult result, IEnumerable<string> ids, SCIMAttributeMapping attributeMapping, SCIMRepresentation sourceScimRepresentation, ICollection<SCIMPatchResult> patchOperations, SCIMSchema targetSchema, string location) |
151 | 154 | { |
152 | 155 | if (!patchOperations.Any(p => IsDisplayName(p.Attr.FullPath))) return; |
153 | 156 | var targetRepresentations = await _scimRepresentationCommandRepository.FindSCIMRepresentationByIds(ids, attributeMapping.TargetResourceType); |
154 | 157 | foreach (var targetRepresentation in targetRepresentations) |
155 | 158 | { |
156 | 159 | UpdateScimRepresentation(targetRepresentation, sourceScimRepresentation, attributeMapping.TargetAttributeId, attributeMapping.SourceResourceType, targetSchema, out bool isAttrUpdated); |
| 160 | + result.UpdateReferenceAttr(targetRepresentation, attributeMapping.TargetAttributeId, targetSchema.GetAttributeById(attributeMapping.TargetAttributeId).FullPath, sourceScimRepresentation.Id, location); |
157 | 161 | targetRepresentation.SetUpdated(DateTime.UtcNow); |
158 | 162 | } |
159 | 163 | } |
|
0 commit comments