Skip to content

Commit

Permalink
fix: ensure unique platform account binding per user (#63)
Browse files Browse the repository at this point in the history
### What this PR does?
修复了不同用户无法在同一平台绑定各自账号的问题

/kind bug

Fixes #62 

```release-note
修复了不同用户无法在同一平台绑定各自账号的问题
```
  • Loading branch information
guqing authored Sep 13, 2024
1 parent c6c0c70 commit 54e81a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ test {
}

halo {
version = '2.17'
version = '2.19'
debug = true
}
41 changes: 22 additions & 19 deletions src/main/java/run/halo/oauth/UserConnectionServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,33 @@ public Mono<UserConnection> 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<Boolean> checkExistingBinding(String registrationId, String username) {
return listByRegistrationIdAndUsername(registrationId, username)
.hasElements();
}

private Mono<UserConnection> 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<UserConnection> removeConnection(String registrationId) {
return ReactiveSecurityContextHolder.getContext()
Expand Down Expand Up @@ -90,12 +99,6 @@ Flux<UserConnection> listByRegistrationIdAndUsername(String registrationId, Stri
&& persisted.getSpec().getUsername().equals(username), null);
}

private Mono<UserConnection> findByRegistrationId(String registrationId) {
return client.list(UserConnection.class,
persisted -> persisted.getSpec().getRegistrationId().equals(registrationId), null)
.next();
}

private Mono<UserConnection> fetchUserConnection(String registrationId, String providerUserId) {
return client.list(UserConnection.class, persisted -> persisted.getSpec()
.getProviderUserId().equals(providerUserId)
Expand Down

0 comments on commit 54e81a6

Please sign in to comment.