Skip to content

Commit 8c40725

Browse files
committed
Fix to allow first user(admin) to sign up
1 parent a5270c4 commit 8c40725

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.lowcoder.domain.authentication;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import org.lowcoder.domain.organization.service.OrgMemberService;
45
import org.lowcoder.domain.organization.service.OrganizationService;
56
import org.lowcoder.sdk.auth.AbstractAuthConfig;
67
import org.lowcoder.sdk.auth.EmailAuthConfig;
@@ -26,6 +27,10 @@ public class AuthenticationServiceImpl implements AuthenticationService {
2627

2728
@Autowired
2829
private OrganizationService organizationService;
30+
31+
@Autowired
32+
private OrgMemberService orgMemberService;
33+
2934
@Autowired
3035
private CommonConfig commonConfig;
3136
@Autowired
@@ -51,18 +56,27 @@ private Mono<FindAuthConfig> findAuthConfig(String orgId, Function<AbstractAuthC
5156

5257
@Override
5358
public Flux<FindAuthConfig> findAllAuthConfigs(String orgId, boolean enableOnly) {
54-
return findAllAuthConfigsByDomain()
59+
60+
Mono<FindAuthConfig> emailAuthConfigMono = orgMemberService.doesAtleastOneAdminExist()
61+
.map(doesAtleastOneAdminExist -> {
62+
boolean shouldEnableRegister = !doesAtleastOneAdminExist && authProperties.getEmail().isEnableRegister();
63+
return new FindAuthConfig
64+
(new EmailAuthConfig(AuthSourceConstants.EMAIL, authProperties.getEmail().isEnable(), shouldEnableRegister), null);
65+
});
66+
67+
68+
Flux<FindAuthConfig> findAuthConfigFlux = findAllAuthConfigsByDomain()
5569
.switchIfEmpty(findAllAuthConfigsForEnterpriseMode())
5670
.switchIfEmpty(findAllAuthConfigsForSaasMode(orgId))
5771
.filter(findAuthConfig -> {
5872
if (enableOnly) {
5973
return findAuthConfig.authConfig().isEnable();
6074
}
6175
return true;
62-
})
63-
.concatWithValues(new FindAuthConfig
64-
(new EmailAuthConfig(AuthSourceConstants.EMAIL, authProperties.getEmail().isEnable(),
65-
authProperties.getEmail().isEnableRegister()), null));
76+
});
77+
78+
return Flux.concat(findAuthConfigFlux, emailAuthConfigMono);
79+
6680
}
6781

6882
private Flux<FindAuthConfig> findAllAuthConfigsByDomain() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public interface OrgMemberService {
2424

2525
Mono<Long> getOrgMemberCount(String orgId);
2626

27+
Mono<Boolean> doesAtleastOneAdminExist();
28+
2729
Mono<Long> countAllActiveOrgs(String userId);
2830

2931
Mono<OrgMember> getOrgMember(String orgId, String userId);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ public Mono<Long> getOrgMemberCount(String orgId) {
125125
return biRelationService.countBySourceId(ORG_MEMBER, orgId);
126126
}
127127

128+
@Override
129+
public Mono<Boolean> doesAtleastOneAdminExist() {
130+
return biRelationService.countByRelation(ORG_MEMBER, MemberRole.ADMIN.getValue())
131+
.single()
132+
.map(count -> count != 0);
133+
}
134+
128135
@Override
129136
public Mono<Boolean> addMember(String orgId, String userId, MemberRole memberRole) {
130137
return biRelationService.addBiRelation(ORG_MEMBER, orgId,

server/api-service/lowcoder-infra/src/main/java/org/lowcoder/infra/birelation/BiRelationRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public interface BiRelationRepository extends ReactiveMongoRepository<BiRelation
2626

2727
Flux<BiRelation> findByBizTypeAndSourceIdAndRelation(BiRelationBizType bizType, String sourceId, String relation);
2828

29+
Mono<Long> countByBizTypeAndRelation(BiRelationBizType bizType, String relation);
30+
2931
Mono<Long> countByBizTypeAndSourceId(BiRelationBizType bizType, String sourceId);
3032

3133
Mono<Long> countByBizTypeAndTargetId(BiRelationBizType bizType, String targetId);

server/api-service/lowcoder-infra/src/main/java/org/lowcoder/infra/birelation/BiRelationService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ public Flux<BiRelation> getBySourceIdAndRelation(BiRelationBizType bizType, Stri
166166
return biRelationRepository.findByBizTypeAndSourceIdAndRelation(bizType, sourceId, relation);
167167
}
168168

169+
public Mono<Long> countByRelation(BiRelationBizType bizType, String relation) {
170+
return biRelationRepository.countByBizTypeAndRelation(bizType, relation);
171+
}
172+
169173
public Mono<Long> countBySourceId(BiRelationBizType bizType, String sourceId) {
170174
return biRelationRepository.countByBizTypeAndSourceId(bizType, sourceId);
171175
}

0 commit comments

Comments
 (0)