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..e93654c 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)