diff --git a/backend/core/src/main/java/org/opencdmp/service/maintenance/KpiProperties.java b/backend/core/src/main/java/org/opencdmp/service/maintenance/KpiProperties.java index 3954fe3c6..dc9a082e5 100644 --- a/backend/core/src/main/java/org/opencdmp/service/maintenance/KpiProperties.java +++ b/backend/core/src/main/java/org/opencdmp/service/maintenance/KpiProperties.java @@ -9,8 +9,6 @@ import java.util.UUID; public class KpiProperties { private UUID id; - private List userIds; - private String label; private String description; @@ -19,6 +17,10 @@ public class KpiProperties { private String code; + private List roles; + + private List tenantRoles; + public UUID getId() { return id; } @@ -27,14 +29,6 @@ public class KpiProperties { this.id = id; } - public List getUserIds() { - return userIds; - } - - public void setUserIds(List userIds) { - this.userIds = userIds; - } - public String getLabel() { return label; } @@ -66,4 +60,20 @@ public class KpiProperties { public void setCode(String code) { this.code = code; } + + public List getRoles() { + return roles; + } + + public void setRoles(List roles) { + this.roles = roles; + } + + public List getTenantRoles() { + return tenantRoles; + } + + public void setTenantRoles(List tenantRoles) { + this.tenantRoles = tenantRoles; + } } diff --git a/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java index 56ccd4d45..e308635ec 100644 --- a/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/maintenance/MaintenanceServiceImpl.java @@ -493,21 +493,31 @@ public class MaintenanceServiceImpl implements MaintenanceService { public void sendIndicatorAccessEntryEvents() throws InvalidApplicationException { try { - for (UUID userId: this.kpiProperties.getUserIds()) { - this.indicatorAccessEventHandler.handle(this.createIndicatorAccessEvent(userId, this.tenantScope.getDefaultTenantCode()), null); - } + List allowedRoles = new ArrayList<>(); + allowedRoles.addAll(this.kpiProperties.getRoles()); + allowedRoles.addAll(this.kpiProperties.getTenantRoles()); + + UserRoleQuery userRoleQuery = this.queryFactory.query(UserRoleQuery.class).disableTracking().roles(allowedRoles); this.tenantEntityManager.disableTenantFilters(); - List tenantUserEntities = this.queryFactory.query(TenantUserQuery.class).disableTracking().userIds(this.kpiProperties.getUserIds()).collect(); - if (tenantUserEntities == null || tenantUserEntities.isEmpty()) throw new MyApplicationException("tenant users not found"); - List tenantEntities = this.queryFactory.query(TenantQuery.class).disableTracking().ids(tenantUserEntities.stream().map(TenantUserEntity::getTenantId).distinct().toList()).collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); - if (tenantEntities != null && !tenantEntities.isEmpty()) { - for (TenantUserEntity user: tenantUserEntities) { - TenantEntity tenant = tenantEntities.stream().filter(x -> x.getId().equals(user.getTenantId())).findFirst().orElse(null); - if (tenant != null){ - this.indicatorAccessEventHandler.handle(this.createIndicatorAccessEvent(user.getUserId(), tenant.getCode()), tenant.getId()); + List users = this.queryFactory.query(UserQuery.class).disableTracking().isActive(IsActive.Active).userRoleSubQuery(userRoleQuery).collectAs(new BaseFieldSet().ensure(User._id).ensure(User._name).ensure(User._createdAt)); + + if (users != null && !users.isEmpty()) { + for (UserEntity user: users) { + this.indicatorAccessEventHandler.handle(this.createIndicatorAccessEvent(user.getId(), this.tenantScope.getDefaultTenantCode()), null); + } + List tenantUserEntities = this.queryFactory.query(TenantUserQuery.class).disableTracking().userIds(users.stream().map(UserEntity::getId).distinct().toList()).isActive(IsActive.Active).collect(); + if (tenantUserEntities == null || tenantUserEntities.isEmpty()) return; + List tenantEntities = this.queryFactory.query(TenantQuery.class).disableTracking().ids(tenantUserEntities.stream().map(TenantUserEntity::getTenantId).distinct().toList()).collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code)); + if (tenantEntities != null && !tenantEntities.isEmpty()) { + for (TenantUserEntity user: tenantUserEntities) { + TenantEntity tenant = tenantEntities.stream().filter(x -> x.getId().equals(user.getTenantId())).findFirst().orElse(null); + if (tenant != null){ + this.indicatorAccessEventHandler.handle(this.createIndicatorAccessEvent(user.getUserId(), tenant.getCode()), tenant.getId()); + } } } } + } finally { this.tenantEntityManager.reloadTenantFilters(); } diff --git a/backend/web/src/main/resources/config/kpi.yml b/backend/web/src/main/resources/config/kpi.yml index 6f56ee211..566597807 100644 --- a/backend/web/src/main/resources/config/kpi.yml +++ b/backend/web/src/main/resources/config/kpi.yml @@ -1,10 +1,12 @@ kpi: user-indicator: id: 97c2d685-d7d2-4bd1-a287-ba329ad45d74 - userIds: - - 8552f758-f196-4a51-a3fd-154c088d85c4 - - e60876ed-87f8-4a8e-8081-e5620ec839cf label: test description: test url: url test - code: test \ No newline at end of file + code: test + roles: + - Admin + - InstallationAdmin + tenant-roles: + - TenantAdmin \ No newline at end of file