public async Task UnsyncUserAsync(Guid userId, Guid scimAppSettingsId) { ScimUserSyncState syncState = await _authDbContext .ScimUserSyncStates .SingleAsync(s => s.SCIMAppSettings.Id == scimAppSettingsId && s.User.Id == userId); Gatekeeper.SCIM.Client.Client scimClient = await GetScimClient(scimAppSettingsId); DeleteUserAction deleteUser = new DeleteUserAction(syncState.ServiceId); DeleteUserResult deleteUserResult = await scimClient.PerformAction <DeleteUserResult>(deleteUser); _authDbContext.Remove(syncState); await _authDbContext.SaveChangesAsync(); }
public async Task SyncUserAsync(Guid userId, Guid scimAppSettingsId) { ScimUserSyncState?syncState = await _authDbContext .ScimUserSyncStates .SingleOrDefaultAsync(s => s.SCIMAppSettings.Id == scimAppSettingsId && s.User.Id == userId); AppUser user = await _authDbContext .Users .SingleAsync(u => u.Id == userId); Gatekeeper.SCIM.Client.Schema.Core20.User scimUser = new Gatekeeper.SCIM.Client.Schema.Core20.User { ExternalId = user.Id.ToString(), UserName = user.UserName, Emails = new List <Gatekeeper.SCIM.Client.Schema.Core20.User.EmailAttribute>() { new Gatekeeper.SCIM.Client.Schema.Core20.User.EmailAttribute { Value = user.Email, Primary = true }, }, DisplayName = user.UserName, Active = true, }; Gatekeeper.SCIM.Client.Client scimClient = await GetScimClient(scimAppSettingsId); if (syncState == null) { CreateAction <Gatekeeper.SCIM.Client.Schema.Core20.User> createUserAction = new CreateAction <Gatekeeper.SCIM.Client.Schema.Core20.User>(scimUser); CreateResult <Gatekeeper.SCIM.Client.Schema.Core20.User> createUserResult = await scimClient.PerformAction <CreateResult <Gatekeeper.SCIM.Client.Schema.Core20.User> >(createUserAction); if (createUserResult.ResultStatus == StateEnum.Success && createUserResult.Resource != null && createUserResult.Resource.Id != null ) { syncState = new ScimUserSyncState { User = user, SCIMAppSettingsId = scimAppSettingsId, ServiceId = createUserResult.Resource.Id, }; _authDbContext.Add(syncState); await _authDbContext.SaveChangesAsync(); } else { throw new Exception("SCIM initial sync failed"); } } else { scimUser.Id = syncState.ServiceId; UpdateUserAction updateUserAction = new UpdateUserAction(scimUser); UpdateUserResult updateUserResult = await scimClient.PerformAction <UpdateUserResult>(updateUserAction); if (updateUserResult.ResultStatus != StateEnum.Success) { throw new Exception("SCIM update failed"); } } }