Skip to content

Commit dd7c832

Browse files
lvhuichaoQIQI03
authored andcommitted
1. add data source events
2. refactor threshold checker
1 parent 9a12a83 commit dd7c832

File tree

18 files changed

+366
-76
lines changed

18 files changed

+366
-76
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.openblocks.domain.bizthreshold;
2+
3+
import static com.openblocks.sdk.util.ExceptionUtils.deferredError;
4+
5+
import java.util.Map;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
import com.openblocks.domain.application.model.ApplicationStatus;
11+
import com.openblocks.domain.application.service.ApplicationService;
12+
import com.openblocks.domain.group.service.GroupService;
13+
import com.openblocks.domain.organization.model.OrgMember;
14+
import com.openblocks.domain.organization.service.OrgMemberService;
15+
import com.openblocks.sdk.exception.BizError;
16+
17+
import reactor.core.publisher.Mono;
18+
19+
@Component
20+
public abstract class AbstractBizThresholdChecker {
21+
22+
@Autowired
23+
private OrgMemberService orgMemberService;
24+
25+
@Autowired
26+
private GroupService groupService;
27+
28+
@Autowired
29+
private ApplicationService applicationService;
30+
31+
protected abstract int getMaxOrgPerUser();
32+
33+
protected abstract int getMaxOrgMemberCount();
34+
35+
protected abstract int getMaxOrgGroupCount();
36+
37+
protected abstract int getMaxOrgAppCount();
38+
39+
protected abstract Map<String, Integer> getUserOrgCountWhiteList();
40+
41+
protected abstract Map<String, Integer> getOrgMemberCountWhiteList();
42+
43+
protected abstract Map<String, Integer> getOrgAppCountWhiteList();
44+
45+
public Mono<Void> checkMaxOrgCount(String userId) {
46+
return orgMemberService.countAllActiveOrgs(userId)
47+
.filter(userOrgCount -> userOrgCountBelowThreshold(userId, userOrgCount))
48+
.switchIfEmpty(deferredError(BizError.EXCEED_MAX_USER_ORG_COUNT, "EXCEED_MAX_USER_ORG_COUNT"))
49+
.then();
50+
}
51+
52+
private boolean userOrgCountBelowThreshold(String userId, long userOrgCount) {
53+
return userOrgCount < Math.max(getUserOrgCountWhiteList().getOrDefault(userId, 0),
54+
getMaxOrgPerUser());
55+
}
56+
57+
public Mono<Void> checkMaxOrgMemberCount(String orgId) {
58+
return orgMemberService.getOrgMemberCount(orgId)
59+
.filter(orgMemberCount -> orgMemberCountBelowThreshold(orgId, orgMemberCount))
60+
.switchIfEmpty(deferredError(BizError.EXCEED_MAX_ORG_MEMBER_COUNT, "EXCEED_MAX_ORG_MEMBER_COUNT"))
61+
.then();
62+
}
63+
64+
private boolean orgMemberCountBelowThreshold(String orgId, Long orgMemberCount) {
65+
return orgMemberCount < Math.max(getMaxOrgMemberCount(), getOrgMemberCountWhiteList().getOrDefault(orgId, 0));
66+
}
67+
68+
public Mono<Void> checkMaxGroupCount(OrgMember orgMemberMono) {
69+
return groupService.getOrgGroupCount(orgMemberMono.getOrgId())
70+
.filter(it -> it < getMaxOrgGroupCount())
71+
.switchIfEmpty(deferredError(BizError.EXCEED_MAX_GROUP_COUNT, "EXCEED_MAX_GROUP_COUNT"))
72+
.then();
73+
}
74+
75+
public Mono<Void> checkMaxOrgApplicationCount(OrgMember orgMember) {
76+
String orgId = orgMember.getOrgId();
77+
return applicationService.countByOrganizationId(orgId, ApplicationStatus.NORMAL)
78+
.filter(orgAppCount -> orgAppCountBelowThreshold(orgId, orgAppCount))
79+
.switchIfEmpty(deferredError(BizError.EXCEED_MAX_APP_COUNT, "EXCEED_MAX_APP_COUNT"))
80+
.then();
81+
}
82+
83+
private boolean orgAppCountBelowThreshold(String orgId, long orgAppCount) {
84+
return orgAppCount < Math.max(getMaxOrgAppCount(), getOrgAppCountWhiteList().getOrDefault(orgId, 0));
85+
}
86+
87+
}
Lines changed: 24 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,23 @@
11
package com.openblocks.domain.bizthreshold;
22

3-
import static com.openblocks.sdk.util.ExceptionUtils.deferredError;
4-
53
import java.util.Collections;
64
import java.util.Map;
75

86
import javax.annotation.PostConstruct;
97

108
import org.springframework.beans.factory.annotation.Autowired;
11-
import org.springframework.stereotype.Component;
9+
import org.springframework.stereotype.Service;
1210

13-
import com.openblocks.domain.application.model.ApplicationStatus;
14-
import com.openblocks.domain.application.service.ApplicationService;
15-
import com.openblocks.domain.group.service.GroupService;
16-
import com.openblocks.domain.organization.model.OrgMember;
17-
import com.openblocks.domain.organization.service.OrgMemberService;
1811
import com.openblocks.sdk.config.dynamic.Conf;
1912
import com.openblocks.sdk.config.dynamic.ConfigCenter;
2013
import com.openblocks.sdk.config.dynamic.ConfigInstance;
21-
import com.openblocks.sdk.exception.BizError;
22-
23-
import reactor.core.publisher.Mono;
2414

25-
@Component
26-
public class BizThresholdChecker {
27-
28-
@Autowired
29-
private OrgMemberService orgMemberService;
30-
31-
@Autowired
32-
private GroupService groupService;
15+
@Service
16+
public class BizThresholdChecker extends AbstractBizThresholdChecker {
3317

3418
@Autowired
3519
private ConfigCenter configCenter;
3620

37-
@Autowired
38-
private ApplicationService applicationService;
39-
4021
private Conf<Integer> maxOrgPerUser;
4122
private Conf<Integer> maxOrgMemberCount;
4223
private Conf<Integer> maxOrgGroupCount;
@@ -50,58 +31,45 @@ private void init() {
5031
ConfigInstance threshold = configCenter.threshold();
5132
maxOrgPerUser = threshold.ofInteger("maxOrgPerUser", 5);
5233
userOrgCountWhiteList = threshold.ofMap("userOrgCountWhiteList", String.class, Integer.class, Collections.emptyMap());
53-
5434
maxOrgMemberCount = threshold.ofInteger("maxOrgMemberCount", 50);
5535
orgMemberCountWhiteList = threshold.ofMap("orgMemberCountWhiteList", String.class, Integer.class, Collections.emptyMap());
56-
5736
maxOrgGroupCount = threshold.ofInteger("maxOrgGroupCount", 10);
58-
5937
maxOrgAppCount = threshold.ofInteger("maxOrgAppCount", 50);
6038
orgAppCountWhiteList = threshold.ofMap("orgAppCountWhiteList", String.class, Integer.class, Collections.emptyMap());
6139
}
6240

63-
public Mono<Void> checkMaxOrgCount(String userId) {
64-
return orgMemberService.countAllActiveOrgs(userId)
65-
.filter(userOrgCount -> userOrgCountBelowThreshold(userId, userOrgCount))
66-
.switchIfEmpty(deferredError(BizError.EXCEED_MAX_USER_ORG_COUNT, "EXCEED_MAX_USER_ORG_COUNT"))
67-
.then();
41+
@Override
42+
protected int getMaxOrgPerUser() {
43+
return maxOrgPerUser.get();
6844
}
6945

70-
private boolean userOrgCountBelowThreshold(String userId, long userOrgCount) {
71-
return userOrgCount < Math.max(userOrgCountWhiteList.get().getOrDefault(userId, 0),
72-
maxOrgPerUser.get());
46+
@Override
47+
protected int getMaxOrgMemberCount() {
48+
return maxOrgMemberCount.get();
7349
}
7450

75-
public Mono<Void> checkMaxOrgMemberCount(String orgId) {
76-
return orgMemberService.getOrgMemberCount(orgId)
77-
.filter(orgMemberCount -> orgMemberCountBelowThreshold(orgId, orgMemberCount))
78-
.switchIfEmpty(deferredError(BizError.EXCEED_MAX_ORG_MEMBER_COUNT, "EXCEED_MAX_ORG_MEMBER_COUNT"))
79-
.then();
51+
@Override
52+
protected int getMaxOrgGroupCount() {
53+
return maxOrgGroupCount.get();
8054
}
8155

82-
private boolean orgMemberCountBelowThreshold(String orgId, Long orgMemberCount) {
83-
return orgMemberCount < Math.max(maxOrgMemberCount.get(), orgMemberCountWhiteList.get().getOrDefault(orgId, 0));
56+
@Override
57+
protected int getMaxOrgAppCount() {
58+
return maxOrgAppCount.get();
8459
}
8560

86-
public Mono<OrgMember> checkMaxGroupCount(Mono<OrgMember> orgMemberMono) {
87-
return orgMemberMono
88-
.flatMap(it -> groupService.getOrgGroupCount(it.getOrgId()))
89-
.filter(it -> it < maxOrgGroupCount.get())
90-
.switchIfEmpty(deferredError(BizError.EXCEED_MAX_GROUP_COUNT, "EXCEED_MAX_GROUP_COUNT"))
91-
.then(orgMemberMono);
61+
@Override
62+
protected Map<String, Integer> getUserOrgCountWhiteList() {
63+
return userOrgCountWhiteList.get();
9264
}
9365

94-
95-
public Mono<Void> checkMaxOrgApplicationCount(OrgMember orgMember) {
96-
String orgId = orgMember.getOrgId();
97-
return applicationService.countByOrganizationId(orgId, ApplicationStatus.NORMAL)
98-
.filter(orgAppCount -> orgAppCountBelowThreshold(orgId, orgAppCount))
99-
.switchIfEmpty(deferredError(BizError.EXCEED_MAX_APP_COUNT, "EXCEED_MAX_APP_COUNT"))
100-
.then();
66+
@Override
67+
protected Map<String, Integer> getOrgMemberCountWhiteList() {
68+
return orgMemberCountWhiteList.get();
10169
}
10270

103-
private boolean orgAppCountBelowThreshold(String orgId, long orgAppCount) {
104-
return orgAppCount < Math.max(maxOrgAppCount.get(), orgAppCountWhiteList.get().getOrDefault(orgId, 0));
71+
@Override
72+
protected Map<String, Integer> getOrgAppCountWhiteList() {
73+
return orgAppCountWhiteList.get();
10574
}
106-
10775
}

server/openblocks-domain/src/main/java/com/openblocks/domain/invitation/service/InvitationService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.springframework.context.annotation.Lazy;
88
import org.springframework.stereotype.Service;
99

10-
import com.openblocks.domain.bizthreshold.BizThresholdChecker;
10+
import com.openblocks.domain.bizthreshold.AbstractBizThresholdChecker;
1111
import com.openblocks.domain.invitation.model.Invitation;
1212
import com.openblocks.domain.invitation.repository.InvitationRepository;
1313
import com.openblocks.domain.organization.model.MemberRole;
@@ -26,7 +26,7 @@ public class InvitationService {
2626
private OrgMemberService orgMemberService;
2727

2828
@Autowired
29-
private BizThresholdChecker bizThresholdChecker;
29+
private AbstractBizThresholdChecker bizThresholdChecker;
3030

3131
@Autowired
3232
private InvitationRepository repository;

server/openblocks-infra/src/main/java/com/openblocks/infra/event/EventType.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public enum EventType {
3535
GROUP_MEMBER_REMOVE("EVENT_TYPE_GROUP_MEMBER_REMOVE"),
3636
//system
3737
SERVER_START_UP("EVENT_TYPE_SERVER_START_UP"),
38+
39+
// data source
40+
DATA_SOURCE_CREATE("DATA_SOURCE_CREATE"),
41+
DATA_SOURCE_UPDATE("DATA_SOURCE_UPDATE"),
42+
DATA_SOURCE_DELETE("DATA_SOURCE_DELETE"),
43+
DATA_SOURCE_PERMISSION_GRANT("DATA_SOURCE_PERMISSION_GRANT"),
44+
DATA_SOURCE_PERMISSION_UPDATE("DATA_SOURCE_PERMISSION_UPDATE"),
45+
DATA_SOURCE_PERMISSION_DELETE("DATA_SOURCE_PERMISSION_DELETE"),
3846
;
3947

4048
private final String desc;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.openblocks.infra.event.datasource;
2+
3+
import com.openblocks.infra.event.AbstractEvent;
4+
import com.openblocks.infra.event.EventType;
5+
6+
import lombok.Getter;
7+
import lombok.experimental.SuperBuilder;
8+
9+
@Getter
10+
@SuperBuilder
11+
public class DatasourceEvent extends AbstractEvent {
12+
13+
private final String datasourceId;
14+
private final String name;
15+
private final String type;
16+
17+
private final EventType eventType;
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.openblocks.infra.event.datasource;
2+
3+
import java.util.Collection;
4+
5+
import com.openblocks.infra.event.AbstractEvent;
6+
import com.openblocks.infra.event.EventType;
7+
8+
import lombok.Getter;
9+
import lombok.experimental.SuperBuilder;
10+
11+
@Getter
12+
@SuperBuilder
13+
public class DatasourcePermissionEvent extends AbstractEvent {
14+
15+
private final String datasourceId;
16+
private final String name;
17+
private final String type;
18+
19+
private final Collection<String> userIds;
20+
private final Collection<String> groupIds;
21+
private final String role;
22+
23+
private final EventType eventType;
24+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.openblocks.infra.util;
2+
3+
import java.security.Key;
4+
import java.security.KeyFactory;
5+
import java.security.PrivateKey;
6+
import java.security.PublicKey;
7+
import java.security.spec.PKCS8EncodedKeySpec;
8+
import java.security.spec.X509EncodedKeySpec;
9+
10+
import javax.crypto.Cipher;
11+
12+
import lombok.extern.slf4j.Slf4j;
13+
14+
@Slf4j
15+
public class RSAUtils {
16+
17+
private static final String algorithm = "RSA";
18+
19+
public static byte[] encrypt(Key key, byte[] data) throws Exception {
20+
final Cipher cipher = Cipher.getInstance(algorithm);
21+
cipher.init(Cipher.ENCRYPT_MODE, key);
22+
return cipher.doFinal(data);
23+
}
24+
25+
public static byte[] decrypt(Key key, byte[] encryptedData) throws Exception {
26+
final Cipher cipher = Cipher.getInstance(algorithm);
27+
cipher.init(Cipher.DECRYPT_MODE, key);
28+
return cipher.doFinal(encryptedData);
29+
}
30+
31+
public static PublicKey getPublicKey(byte[] encryptedPublicKey) throws Exception {
32+
return KeyFactory.getInstance(algorithm).generatePublic(new X509EncodedKeySpec(encryptedPublicKey));
33+
}
34+
35+
public static PrivateKey getPrivateKey(byte[] encryptedPrivateKey) throws Exception {
36+
return KeyFactory.getInstance(algorithm).generatePrivate(new PKCS8EncodedKeySpec(encryptedPrivateKey));
37+
}
38+
39+
}

server/openblocks-sdk/src/main/resources/locale_en.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,12 @@ EVENT_TYPE_GROUP_MEMBER_ROLE_UPDATE=Group Member Role Update
239239
EVENT_TYPE_GROUP_MEMBER_LEAVE=Group Member Leave
240240
EVENT_TYPE_GROUP_MEMBER_REMOVE=Group Member Remove
241241
EVENT_TYPE_SERVER_START_UP=Server Startup
242+
DATA_SOURCE_CREATE=Create data source
243+
DATA_SOURCE_UPDATE=Update data source
244+
DATA_SOURCE_DELETE=Delete data source
245+
DATA_SOURCE_PERMISSION_GRANT=Grant data source permissions
246+
DATA_SOURCE_PERMISSION_UPDATE=Update data source permissions
247+
DATA_SOURCE_PERMISSION_DELETE=Delete data source permissions
242248
## threshold
243249
EXCEED_MAX_USER_ORG_COUNT=Sorry, you have reached maximum count of workspaces.
244250
EXCEED_MAX_ORG_MEMBER_COUNT=Sorry, this workspace has reached the maximum count of members.

server/openblocks-server/src/main/java/com/openblocks/api/application/ApplicationApiService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import com.openblocks.domain.application.model.ApplicationStatus;
4646
import com.openblocks.domain.application.model.ApplicationType;
4747
import com.openblocks.domain.application.service.ApplicationService;
48-
import com.openblocks.domain.bizthreshold.BizThresholdChecker;
48+
import com.openblocks.domain.bizthreshold.AbstractBizThresholdChecker;
4949
import com.openblocks.domain.datasource.model.Datasource;
5050
import com.openblocks.domain.group.service.GroupService;
5151
import com.openblocks.domain.interaction.UserApplicationInteractionService;
@@ -104,7 +104,7 @@ public class ApplicationApiService {
104104
private UserService userService;
105105

106106
@Autowired
107-
private BizThresholdChecker bizThresholdChecker;
107+
private AbstractBizThresholdChecker bizThresholdChecker;
108108

109109
@Autowired
110110
private TemplateSolution templateSolution;

server/openblocks-server/src/main/java/com/openblocks/api/config/ConfigView.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import java.util.List;
55

6+
import com.fasterxml.jackson.annotation.JsonInclude;
7+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
68
import com.openblocks.sdk.auth.AbstractAuthConfig;
79
import com.openblocks.sdk.constants.WorkspaceMode;
810

@@ -15,6 +17,7 @@ public class ConfigView {
1517

1618
private boolean isCloudHosting;
1719
private List<AbstractAuthConfig> authConfigs;
18-
private boolean needUpdate;
20+
@JsonInclude(Include.NON_EMPTY)
21+
private String warning;
1922
private WorkspaceMode workspaceMode;
2023
}

0 commit comments

Comments
 (0)