From 76dcdf65decdab957c310de9d2a66d3b7da5a370 Mon Sep 17 00:00:00 2001 From: amentis Date: Wed, 26 Jun 2024 10:53:09 +0300 Subject: [PATCH] implement export users for tenant users page --- .../java/org/opencdmp/service/user/UserService.java | 2 +- .../org/opencdmp/service/user/UserServiceImpl.java | 11 +++++++++-- .../java/org/opencdmp/controllers/UserController.java | 9 +++++---- backend/web/src/main/resources/config/permissions.yml | 1 + .../ui/admin/user/listing/user-listing.component.html | 2 +- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/service/user/UserService.java b/backend/core/src/main/java/org/opencdmp/service/user/UserService.java index 12c1ac5b9..7db7e6526 100644 --- a/backend/core/src/main/java/org/opencdmp/service/user/UserService.java +++ b/backend/core/src/main/java/org/opencdmp/service/user/UserService.java @@ -27,7 +27,7 @@ public interface UserService { void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException; - byte[] exportCsv() throws IOException; + byte[] exportCsv(boolean hasTenantAdminMode) throws IOException, InvalidApplicationException; User patchRoles(UserRolePatchPersist model, FieldSet fields) throws InvalidApplicationException; diff --git a/backend/core/src/main/java/org/opencdmp/service/user/UserServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/user/UserServiceImpl.java index 6712beffe..f754dedf3 100644 --- a/backend/core/src/main/java/org/opencdmp/service/user/UserServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/user/UserServiceImpl.java @@ -296,11 +296,18 @@ public class UserServiceImpl implements UserService { //region export @Override - public byte[] exportCsv() throws IOException { + public byte[] exportCsv(boolean hasTenantAdminMode) throws IOException, InvalidApplicationException { this.authorizationService.authorizeForce(Permission.ExportUsers); FieldSet fieldSet = new BaseFieldSet().ensure(User._id).ensure(User._name).ensure(User._contacts + "." + UserContactInfo._value).ensure(User._contacts + "." + UserContactInfo._type); - List users = this.builderFactory.builder(UserBuilder.class).build(fieldSet, this.queryFactory.query(UserQuery.class).disableTracking().collectAs(fieldSet)); + List users = null; + if (hasTenantAdminMode && !this.tenantScope.getTenantCode().equals(this.tenantScope.getDefaultTenantCode())){ + if (this.tenantScope.getTenant() == null) throw new MyApplicationException("Tenant not found"); + TenantUserQuery tenantUserQuery = this.queryFactory.query(TenantUserQuery.class).disableTracking().authorize(AuthorizationFlags.AllExceptPublic).tenantIds(this.tenantScope.getTenant()).isActive(IsActive.Active); + users = this.builderFactory.builder(UserBuilder.class).build(fieldSet, this.queryFactory.query(UserQuery.class).tenantUserSubQuery(tenantUserQuery).isActive(IsActive.Active).disableTracking().collectAs(fieldSet)); + } else { + users = this.builderFactory.builder(UserBuilder.class).build(fieldSet, this.queryFactory.query(UserQuery.class).disableTracking().isActive(IsActive.Active).collectAs(fieldSet)); + } final ByteArrayOutputStream out = new ByteArrayOutputStream(); final CSVFormat format = CSVFormat.DEFAULT.withHeader("User Id", "User Name", "User Email").withQuoteMode(QuoteMode.NON_NUMERIC); final CSVPrinter csvPrinter = new CSVPrinter(new PrintWriter(out), format); diff --git a/backend/web/src/main/java/org/opencdmp/controllers/UserController.java b/backend/web/src/main/java/org/opencdmp/controllers/UserController.java index 0a39eddcc..6bb91ff1c 100644 --- a/backend/web/src/main/java/org/opencdmp/controllers/UserController.java +++ b/backend/web/src/main/java/org/opencdmp/controllers/UserController.java @@ -162,14 +162,15 @@ public class UserController { return model; } - @GetMapping("/export/csv") - public ResponseEntity exportCsv() throws MyApplicationException, MyForbiddenException, MyNotFoundException, IOException { - logger.debug(new MapLogEntry("export" + User.class.getSimpleName())); + @GetMapping("/export/csv/{hasTenantAdminMode}") + public ResponseEntity exportCsv(@PathVariable("hasTenantAdminMode") Boolean hasTenantAdminMode) throws MyApplicationException, MyForbiddenException, MyNotFoundException, IOException, InvalidApplicationException { + logger.debug(new MapLogEntry("export" + User.class.getSimpleName()).And("hasTenantAdminMode", hasTenantAdminMode)); // this.censorFactory.censor(UserCensor.class).censor(fieldSet, null); - byte[] bytes = this.userTypeService.exportCsv(); + byte[] bytes = this.userTypeService.exportCsv(hasTenantAdminMode); this.auditService.track(AuditableAction.User_ExportCsv, Map.ofEntries( + new AbstractMap.SimpleEntry("hasTenantAdminMode", hasTenantAdminMode) )); return this.responseUtilsService.buildResponseFileFromText(new String(bytes, StandardCharsets.UTF_8), "Users_dump.csv"); diff --git a/backend/web/src/main/resources/config/permissions.yml b/backend/web/src/main/resources/config/permissions.yml index 5b68993ce..2d9a29a9f 100644 --- a/backend/web/src/main/resources/config/permissions.yml +++ b/backend/web/src/main/resources/config/permissions.yml @@ -314,6 +314,7 @@ permissions: roles: - Admin - InstallationAdmin + - TenantAdmin claims: [ ] clients: [ ] allowAnonymous: false diff --git a/dmp-frontend/src/app/ui/admin/user/listing/user-listing.component.html b/dmp-frontend/src/app/ui/admin/user/listing/user-listing.component.html index 1cfe3fb36..370bc1ca6 100644 --- a/dmp-frontend/src/app/ui/admin/user/listing/user-listing.component.html +++ b/dmp-frontend/src/app/ui/admin/user/listing/user-listing.component.html @@ -12,7 +12,7 @@
-