1
1
package org .lowcoder .api .datasource ;
2
2
3
- import com .github .f4b6a3 .uuid .UuidCreator ;
4
- import jakarta .annotation .Nullable ;
5
- import lombok .RequiredArgsConstructor ;
3
+ import static org .lowcoder .domain .permission .model .ResourceAction .MANAGE_DATASOURCES ;
4
+ import static org .lowcoder .domain .permission .model .ResourceAction .READ_APPLICATIONS ;
5
+ import static org .lowcoder .domain .permission .model .ResourceAction .USE_DATASOURCES ;
6
+ import static org .lowcoder .sdk .exception .BizError .NOT_AUTHORIZED ;
7
+ import static org .lowcoder .sdk .util .ExceptionUtils .deferredError ;
8
+
9
+ import java .util .ArrayList ;
10
+ import java .util .Collections ;
11
+ import java .util .HashMap ;
12
+ import java .util .List ;
13
+ import java .util .Map ;
14
+ import java .util .Set ;
15
+ import java .util .stream .Collectors ;
16
+
6
17
import org .apache .commons .collections4 .CollectionUtils ;
7
18
import org .apache .commons .lang3 .StringUtils ;
8
19
import org .lowcoder .api .application .ApplicationApiService ;
9
20
import org .lowcoder .api .home .SessionUserService ;
10
21
import org .lowcoder .api .permission .PermissionHelper ;
11
22
import org .lowcoder .api .permission .view .CommonPermissionView ;
12
23
import org .lowcoder .api .permission .view .PermissionItemView ;
24
+ import org .lowcoder .api .usermanagement .GroupApiService ;
25
+ import org .lowcoder .api .usermanagement .OrgApiService ;
13
26
import org .lowcoder .api .usermanagement .OrgDevChecker ;
27
+ import org .lowcoder .api .usermanagement .view .GroupView ;
28
+ import org .lowcoder .api .usermanagement .view .OrgMemberListView ;
14
29
import org .lowcoder .domain .application .service .ApplicationService ;
15
30
import org .lowcoder .domain .datasource .model .Datasource ;
16
31
import org .lowcoder .domain .datasource .model .DatasourceStatus ;
38
53
import org .lowcoder .sdk .models .HasIdAndAuditing ;
39
54
import org .lowcoder .sdk .models .JsDatasourceConnectionConfig ;
40
55
import org .springframework .stereotype .Service ;
41
- import reactor .core .publisher .Flux ;
42
- import reactor .core .publisher .Mono ;
43
56
44
- import java .util .Collections ;
45
- import java .util .List ;
46
- import java .util .Map ;
47
- import java .util .Set ;
48
- import java .util .stream .Collectors ;
57
+ import com .github .f4b6a3 .uuid .UuidCreator ;
49
58
50
- import static org .lowcoder .domain .permission .model .ResourceAction .*;
51
- import static org .lowcoder .sdk .exception .BizError .NOT_AUTHORIZED ;
52
- import static org .lowcoder .sdk .util .ExceptionUtils .deferredError ;
59
+ import jakarta .annotation .Nullable ;
60
+ import lombok .RequiredArgsConstructor ;
61
+ import reactor .core .publisher .Flux ;
62
+ import reactor .core .publisher .Mono ;
53
63
54
64
@ RequiredArgsConstructor
55
65
@ Service
@@ -71,6 +81,8 @@ public class DatasourceApiServiceImpl implements DatasourceApiService {
71
81
private final DatasourcePluginClient datasourcePluginClient ;
72
82
private final DatasourceRepository datasourceRepository ;
73
83
private final ApplicationApiService applicationApiService ;
84
+ private final OrgApiService orgApiService ;
85
+ private final GroupApiService groupApiService ;
74
86
75
87
@ Override
76
88
public Mono <Datasource > create (Datasource datasource ) {
@@ -267,6 +279,66 @@ public Mono<Boolean> grantPermission(String datasourceId, @Nullable Set<String>
267
279
.thenReturn (true );
268
280
}
269
281
282
+ @ Override
283
+ public Mono <List <Object >> getGroupsOrMembersWithoutPermissions (String datasourceId ) {
284
+ return datasourceService .getById (datasourceId )
285
+ .switchIfEmpty (Mono .error (new ServerException ("data source not exist. {}" , datasourceId )))
286
+ .flatMap (datasource -> {
287
+ String orgId = datasource .getOrganizationId ();
288
+ Mono <List <ResourcePermission >> datasourcePermissions = resourcePermissionService .getByDataSourceId (datasource .getId ()).cache ();
289
+
290
+ Mono <List <PermissionItemView >> groupPermissionPairsMono = datasourcePermissions
291
+ .flatMap (permissionHelper ::getGroupPermissions );
292
+
293
+ Mono <List <PermissionItemView >> userPermissionPairsMono = datasourcePermissions
294
+ .flatMap (permissionHelper ::getUserPermissions );
295
+ Mono <OrgMemberListView > orgMemberListViewMono = orgApiService .getOrganizationMembers (orgId , 1 , 0 );
296
+ Mono <List <GroupView >> groupsViewMono = groupApiService .getGroups ();
297
+
298
+ return Mono .zip (groupPermissionPairsMono , userPermissionPairsMono , orgMemberListViewMono , groupsViewMono )
299
+ .map (tuple -> {
300
+ List <PermissionItemView > groupPermissionPairs = tuple .getT1 ();
301
+ List <PermissionItemView > userPermissionPairs = tuple .getT2 ();
302
+ OrgMemberListView orgMemberListViews = tuple .getT3 ();
303
+ List <GroupView > groupListViews = tuple .getT4 ();
304
+
305
+ Set <String > groupIdsWithPerm = groupPermissionPairs .stream ()
306
+ .map (PermissionItemView ::getId )
307
+ .collect (java .util .stream .Collectors .toSet ());
308
+
309
+ List <java .util .Map <String , Object >> filteredGroups = groupListViews .stream ()
310
+ .filter (group -> !groupIdsWithPerm .contains (group .getGroupId ()))
311
+ .map (group -> {
312
+ java .util .Map <String , Object > map = new java .util .HashMap <>();
313
+ map .put ("type" , "Group" );
314
+ map .put ("data" , group );
315
+ return map ;
316
+ })
317
+ .toList ();
318
+
319
+ Set <String > userIdsWithPerm = userPermissionPairs .stream ()
320
+ .map (PermissionItemView ::getId )
321
+ .collect (java .util .stream .Collectors .toSet ());
322
+
323
+ List <Map <String , Object >> filteredMembers = orgMemberListViews .getMembers ().stream ()
324
+ .filter (member -> !userIdsWithPerm .contains (member .getUserId ()))
325
+ .map (member -> {
326
+ Map <String , Object > map = new HashMap <>();
327
+ map .put ("type" , "User" );
328
+ map .put ("data" , member );
329
+ return map ;
330
+ })
331
+ .toList ();
332
+
333
+ List <Object > result = new ArrayList <>();
334
+ result .addAll (filteredGroups );
335
+ result .addAll (filteredMembers );
336
+ return result ;
337
+ });
338
+ });
339
+ }
340
+
341
+
270
342
@ Override
271
343
public Mono <Boolean > updatePermission (String permissionId , ResourceRole role ) {
272
344
return checkBeforePermissionDeleteOrUpdate (permissionId )
0 commit comments