From 87f3b853be778226e36712bff10a0036012bc2ab Mon Sep 17 00:00:00 2001 From: guqing Date: Fri, 13 Sep 2024 17:34:40 +0800 Subject: [PATCH] fix: ensure unique platform account binding per user --- build.gradle | 2 +- .../halo/oauth/UserConnectionServiceImpl.java | 41 ++++++++++--------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/build.gradle b/build.gradle index 9bed6f9..8fb0b0e 100644 --- a/build.gradle +++ b/build.gradle @@ -29,6 +29,6 @@ test { } halo { - version = '2.17' + version = '2.19' debug = true } diff --git a/src/main/java/run/halo/oauth/UserConnectionServiceImpl.java b/src/main/java/run/halo/oauth/UserConnectionServiceImpl.java index 380897e..2e935b0 100644 --- a/src/main/java/run/halo/oauth/UserConnectionServiceImpl.java +++ b/src/main/java/run/halo/oauth/UserConnectionServiceImpl.java @@ -40,24 +40,33 @@ public Mono createConnection(String username, UserConnection connection = convert(username, authentication); String providerUserId = authentication.getPrincipal().getName(); - return findByRegistrationId(connection.getSpec().getRegistrationId()) - .hasElement() - .flatMap(exists -> { - if (exists) { + return checkExistingBinding(connection.getSpec().getRegistrationId(), username) + .flatMap(existing -> { + if (existing) { return Mono.error(new ServerWebInputException( - "已经绑定过 " + connection.getSpec().getRegistrationId() + " 账号,请先解绑")); + "已经绑定过其他 " + connection.getSpec().getRegistrationId() + + " 账号,请先解绑后重试")); } - return fetchUserConnection(connection.getSpec().getRegistrationId(), providerUserId) - .flatMap(persisted -> { - connection.getMetadata().setName(persisted.getMetadata().getName()); - connection.getMetadata() - .setVersion(persisted.getMetadata().getVersion()); - return client.update(connection); - }) - .switchIfEmpty(Mono.defer(() -> client.create(connection))); + return upsertUserConnection(connection, providerUserId); }); } + private Mono checkExistingBinding(String registrationId, String username) { + return listByRegistrationIdAndUsername(registrationId, username) + .hasElements(); + } + + private Mono upsertUserConnection(UserConnection connection, + String providerUserId) { + return fetchUserConnection(connection.getSpec().getRegistrationId(), providerUserId) + .flatMap(persisted -> { + connection.getMetadata().setName(persisted.getMetadata().getName()); + connection.getMetadata().setVersion(persisted.getMetadata().getVersion()); + return client.update(connection); + }) + .switchIfEmpty(Mono.defer(() -> client.create(connection))); + } + @Override public Flux removeConnection(String registrationId) { return ReactiveSecurityContextHolder.getContext() @@ -90,12 +99,6 @@ Flux listByRegistrationIdAndUsername(String registrationId, Stri && persisted.getSpec().getUsername().equals(username), null); } - private Mono findByRegistrationId(String registrationId) { - return client.list(UserConnection.class, - persisted -> persisted.getSpec().getRegistrationId().equals(registrationId), null) - .next(); - } - private Mono fetchUserConnection(String registrationId, String providerUserId) { return client.list(UserConnection.class, persisted -> persisted.getSpec() .getProviderUserId().equals(providerUserId)