Skip to content

Commit b7277b0

Browse files
dragonpooludomikula
authored andcommitted
disable email and providers based on workspace
1 parent 3a95a7f commit b7277b0

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

server/api-service/lowcoder-domain/src/main/java/org/lowcoder/domain/authentication/AuthenticationServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ private Mono<FindAuthConfig> findAuthConfig(String orgId, Function<AbstractAuthC
5353
public Flux<FindAuthConfig> findAllAuthConfigs(String orgId, boolean enableOnly) {
5454

5555
Mono<FindAuthConfig> emailAuthConfigMono = orgMemberService.doesAtleastOneAdminExist()
56-
.map(doesAtleastOneAdminExist -> {
56+
.flatMap(doesAtleastOneAdminExist -> {
5757
boolean shouldEnableRegister;
5858
if(doesAtleastOneAdminExist) {
5959
shouldEnableRegister = authProperties.getEmail().getEnableRegister();
6060
} else {
6161
shouldEnableRegister = Boolean.TRUE;
6262
}
63-
return new FindAuthConfig
64-
(new EmailAuthConfig(AuthSourceConstants.EMAIL, authProperties.getEmail().isEnable(), shouldEnableRegister), null);
63+
if(orgId == null) return Mono.just(new FindAuthConfig(new EmailAuthConfig(AuthSourceConstants.EMAIL, authProperties.getEmail().isEnable(), shouldEnableRegister), null));
64+
else return organizationService.getById(orgId).map(organization -> new FindAuthConfig(new EmailAuthConfig(AuthSourceConstants.EMAIL, !organization.getIsEmailDisabled(), shouldEnableRegister), null));
6565
});
6666

6767

server/api-service/lowcoder-domain/src/main/java/org/lowcoder/domain/organization/model/Organization.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ public class Organization extends HasIdAndAuditing implements BeforeMongodbWrite
5151

5252
private String contactPhoneNumber;
5353

54+
private Boolean isEmailDisabled;
55+
56+
public Boolean getIsEmailDisabled() {
57+
if(isEmailDisabled == null) return false;
58+
else return isEmailDisabled;
59+
}
60+
5461
@JsonIgnore
5562
private String logoAssetId;
5663

server/api-service/lowcoder-server/src/main/java/org/lowcoder/api/authentication/service/AuthenticationApiServiceImpl.java

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,13 @@ public Mono<Boolean> enableAuthConfig(AuthConfigRequest authConfigRequest) {
246246
.then(sessionUserService.getVisitorOrgMemberCache())
247247
.flatMap(orgMember -> organizationService.getById(orgMember.getOrgId()))
248248
.doOnNext(organization -> {
249-
boolean duplicateAuthType = addOrUpdateNewAuthConfig(organization, authConfigFactory.build(authConfigRequest, true));
250-
if(duplicateAuthType) {
251-
deferredError(DUPLICATE_AUTH_CONFIG_ADDITION, "DUPLICATE_AUTH_CONFIG_ADDITION");
249+
if(authConfigRequest.getId().equals("EMAIL")) {
250+
organization.setIsEmailDisabled(false);
251+
} else {
252+
boolean duplicateAuthType = addOrUpdateNewAuthConfig(organization, authConfigFactory.build(authConfigRequest, true));
253+
if (duplicateAuthType) {
254+
deferredError(DUPLICATE_AUTH_CONFIG_ADDITION, "DUPLICATE_AUTH_CONFIG_ADDITION");
255+
}
252256
}
253257
})
254258
.flatMap(organization -> organizationService.update(organization.getId(), organization));
@@ -346,22 +350,15 @@ private Mono<Void> checkIfAdmin() {
346350
* If true, throw an exception to avoid disabling the last effective connection way.
347351
*/
348352
private Mono<Void> checkIfOnlyEffectiveCurrentUserConnections(String authId) {
349-
Mono<List<String>> userConnectionAuthConfigIdListMono = sessionUserService.getVisitor()
350-
.flatMapIterable(User::getConnections)
351-
.filter(connection -> StringUtils.isNotBlank(connection.getAuthId()))
352-
.map(Connection::getAuthId)
353-
.collectList();
354-
Mono<List<String>> orgAuthIdListMono = authenticationService.findAllAuthConfigs(null, true)
355-
.map(FindAuthConfig::authConfig)
356-
.map(AbstractAuthConfig::getId)
357-
.collectList();
358-
return Mono.zip(userConnectionAuthConfigIdListMono, orgAuthIdListMono)
359-
.delayUntil(tuple -> {
360-
List<String> userConnectionAuthConfigIds = tuple.getT1();
361-
List<String> orgAuthConfigIds = tuple.getT2();
362-
userConnectionAuthConfigIds.retainAll(orgAuthConfigIds);
363-
userConnectionAuthConfigIds.remove(authId);
364-
if (CollectionUtils.isEmpty(userConnectionAuthConfigIds)) {
353+
return sessionUserService.getVisitorOrgMemberCache()
354+
.map(OrgMember::getOrgId)
355+
.flatMap(orgId -> authenticationService.findAllAuthConfigs(orgId, true)
356+
.map(FindAuthConfig::authConfig)
357+
.map(AbstractAuthConfig::getId)
358+
.collectList())
359+
.delayUntil(orgAuthConfigIds -> {
360+
orgAuthConfigIds.remove(authId);
361+
if (CollectionUtils.isEmpty(orgAuthConfigIds)) {
365362
return Mono.error(new BizException(DISABLE_AUTH_CONFIG_FORBIDDEN, "DISABLE_AUTH_CONFIG_FORBIDDEN"));
366363
}
367364
return Mono.empty();
@@ -370,26 +367,29 @@ private Mono<Void> checkIfOnlyEffectiveCurrentUserConnections(String authId) {
370367
}
371368

372369
private void disableAuthConfig(Organization organization, String authId, boolean delete) {
373-
374-
Predicate<AbstractAuthConfig> authConfigPredicate = abstractAuthConfig -> Objects.equals(abstractAuthConfig.getId(), authId);
375-
376-
if(delete) {
377-
List<AbstractAuthConfig> abstractAuthConfigs = Optional.of(organization)
378-
.map(Organization::getAuthConfigs)
379-
.orElse(Collections.emptyList());
380-
381-
abstractAuthConfigs.removeIf(authConfigPredicate);
382-
383-
organization.getOrganizationDomain().setConfigs(abstractAuthConfigs);
384-
370+
if(authId.equals("EMAIL")) {
371+
organization.setIsEmailDisabled(true);
385372
} else {
386-
Optional.of(organization)
387-
.map(Organization::getAuthConfigs)
388-
.orElse(Collections.emptyList()).stream()
389-
.filter(authConfigPredicate)
390-
.forEach(abstractAuthConfig -> {
391-
abstractAuthConfig.setEnable(false);
392-
});
373+
Predicate<AbstractAuthConfig> authConfigPredicate = abstractAuthConfig -> Objects.equals(abstractAuthConfig.getId(), authId);
374+
375+
if (delete) {
376+
List<AbstractAuthConfig> abstractAuthConfigs = Optional.of(organization)
377+
.map(Organization::getAuthConfigs)
378+
.orElse(Collections.emptyList());
379+
380+
abstractAuthConfigs.removeIf(authConfigPredicate);
381+
382+
organization.getOrganizationDomain().setConfigs(abstractAuthConfigs);
383+
384+
} else {
385+
Optional.of(organization)
386+
.map(Organization::getAuthConfigs)
387+
.orElse(Collections.emptyList()).stream()
388+
.filter(authConfigPredicate)
389+
.forEach(abstractAuthConfig -> {
390+
abstractAuthConfig.setEnable(false);
391+
});
392+
}
393393
}
394394
}
395395

0 commit comments

Comments
 (0)