From feb6c5dee056fed85f3ca38dcc3b7fa436c051fc Mon Sep 17 00:00:00 2001 From: "CITE\\dtziotzios" Date: Thu, 11 Jul 2024 18:09:13 +0300 Subject: [PATCH] fixes --- .../AuthorizationContentResolverImpl.java | 10 +++---- .../opencdmp/data/TenantEntityManager.java | 2 +- .../service/accounting/AccountingService.java | 26 +++++++++++++++++++ .../description/DescriptionServiceImpl.java | 2 ++ .../service/usagelimit/UsageLimitService.java | 11 ++++++++ .../service/user/UserServiceImpl.java | 4 ++- 6 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java create mode 100644 backend/core/src/main/java/org/opencdmp/service/usagelimit/UsageLimitService.java diff --git a/backend/core/src/main/java/org/opencdmp/authorization/authorizationcontentresolver/AuthorizationContentResolverImpl.java b/backend/core/src/main/java/org/opencdmp/authorization/authorizationcontentresolver/AuthorizationContentResolverImpl.java index e2ff60441..4ab06886c 100644 --- a/backend/core/src/main/java/org/opencdmp/authorization/authorizationcontentresolver/AuthorizationContentResolverImpl.java +++ b/backend/core/src/main/java/org/opencdmp/authorization/authorizationcontentresolver/AuthorizationContentResolverImpl.java @@ -69,7 +69,7 @@ public class AuthorizationContentResolverImpl implements AuthorizationContentRes if (idsToResolve.isEmpty()) return affiliatedResources; List planUsers; try { - this.tenantEntityManager.loadExplictTenantFilters(); + this.tenantEntityManager.loadExplicitTenantFilters(); planUsers = this.queryFactory.query(PlanUserQuery.class).disableTracking().planIds(ids).sectionIsEmpty(true).userIds(userId).isActives(IsActive.Active).collectAs(new BaseFieldSet().ensure(PlanUser._role).ensure(PlanUser._plan)); } catch (InvalidApplicationException e) { log.error(e.getMessage(), e); @@ -109,7 +109,7 @@ public class AuthorizationContentResolverImpl implements AuthorizationContentRes List userDescriptionTemplates; try { - this.tenantEntityManager.loadExplictTenantFilters(); + this.tenantEntityManager.loadExplicitTenantFilters(); userDescriptionTemplates = this.queryFactory.query(UserDescriptionTemplateQuery.class).disableTracking().descriptionTemplateIds(ids).userIds(userId).isActive(IsActive.Active).collectAs(new BaseFieldSet().ensure(UserDescriptionTemplate._role).ensure(UserDescriptionTemplate._descriptionTemplate)); } catch (InvalidApplicationException e) { log.error(e.getMessage(), e); @@ -139,7 +139,7 @@ public class AuthorizationContentResolverImpl implements AuthorizationContentRes //TODO: investigate if we want to use cache boolean hasAny; try { - this.tenantEntityManager.loadExplictTenantFilters(); + this.tenantEntityManager.loadExplicitTenantFilters(); hasAny = this.queryFactory.query(UserDescriptionTemplateQuery.class).disableTracking().userIds(userId).isActive(IsActive.Active).count() > 0; } catch (InvalidApplicationException e) { log.error(e.getMessage(), e); @@ -176,7 +176,7 @@ public class AuthorizationContentResolverImpl implements AuthorizationContentRes List planUsers; List descriptionEntities; try { - this.tenantEntityManager.loadExplictTenantFilters(); + this.tenantEntityManager.loadExplicitTenantFilters(); descriptionEntities = this.queryFactory.query(DescriptionQuery.class).disableTracking().ids(ids).collectAs(new BaseFieldSet().ensure(Description._id).ensure(Description._planDescriptionTemplate).ensure(Description._plan)); planDescriptionTemplateEntities = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking().ids(descriptionEntities.stream().map(DescriptionEntity::getPlanDescriptionTemplateId).distinct().toList()).collectAs(new BaseFieldSet().ensure(PlanDescriptionTemplate._id).ensure(PlanDescriptionTemplate._sectionId)); planUsers = this.queryFactory.query(PlanUserQuery.class).disableTracking().descriptionIds(ids).userIds(userId).isActives(IsActive.Active).collectAs(new BaseFieldSet().ensure(PlanUser._role).ensure(PlanUser._sectionId).ensure(PlanUser._plan)); @@ -232,7 +232,7 @@ public class AuthorizationContentResolverImpl implements AuthorizationContentRes List planUsers; try { - this.tenantEntityManager.loadExplictTenantFilters(); + this.tenantEntityManager.loadExplicitTenantFilters(); planUsers = this.queryFactory.query(PlanUserQuery.class).disableTracking().planIds(planId).userIds(userId).isActives(IsActive.Active).collectAs(new BaseFieldSet().ensure(PlanUser._role).ensure(PlanUser._sectionId).ensure(PlanUser._plan)); } catch (InvalidApplicationException e) { log.error(e.getMessage(), e); diff --git a/backend/core/src/main/java/org/opencdmp/data/TenantEntityManager.java b/backend/core/src/main/java/org/opencdmp/data/TenantEntityManager.java index 93d0979ff..8498d2f45 100644 --- a/backend/core/src/main/java/org/opencdmp/data/TenantEntityManager.java +++ b/backend/core/src/main/java/org/opencdmp/data/TenantEntityManager.java @@ -114,7 +114,7 @@ public class TenantEntityManager { this.tenantFiltersDisabled = false; } - public void loadExplictTenantFilters() throws InvalidApplicationException { + public void loadExplicitTenantFilters() throws InvalidApplicationException { if (!this.entityManager.isOpen()) return; this.disableTenantFilters(); diff --git a/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java new file mode 100644 index 000000000..e1ad947e3 --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/accounting/AccountingService.java @@ -0,0 +1,26 @@ +package org.opencdmp.service.accounting; + +public class AccountingService { + + private Integer getCurrentMetricValue(String metric) { + return 10; + } + + private void set(String metric) { + //Get/Calculate current metric value + //Find metric value from db + // compare these two and throw UsageLimitException when current > metric value + } + + private void increase(String metric) { + //Get/Calculate current metric value + //Find metric value from db + // compare these two and throw UsageLimitException when current > metric value + } + + private void decrease(String metric) { + //Get/Calculate current metric value + //Find metric value from db + // compare these two and throw UsageLimitException when current > metric value + } +} diff --git a/backend/core/src/main/java/org/opencdmp/service/description/DescriptionServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/description/DescriptionServiceImpl.java index 3e3075e95..9c3f68549 100644 --- a/backend/core/src/main/java/org/opencdmp/service/description/DescriptionServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/description/DescriptionServiceImpl.java @@ -222,6 +222,8 @@ public class DescriptionServiceImpl implements DescriptionService { if (!data.getPlanId().equals(model.getPlanId())) throw new MyValidationException(this.errors.getPlanCanNotChange().getCode(), this.errors.getPlanCanNotChange().getMessage()); if (!data.getPlanDescriptionTemplateId().equals(model.getPlanDescriptionTemplateId())) throw new MyValidationException(this.errors.getPlanDescriptionTemplateCanNotChange().getCode(), this.errors.getPlanDescriptionTemplateCanNotChange().getMessage()); } else { + //this.usageLimitService.checkIncrease("description_count"); + PlanEntity planEntity = this.entityManager.find(PlanEntity.class, model.getPlanId(), true); if (planEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getPlanId(), Plan.class.getSimpleName()}, LocaleContextHolder.getLocale())); diff --git a/backend/core/src/main/java/org/opencdmp/service/usagelimit/UsageLimitService.java b/backend/core/src/main/java/org/opencdmp/service/usagelimit/UsageLimitService.java new file mode 100644 index 000000000..e25c49c7a --- /dev/null +++ b/backend/core/src/main/java/org/opencdmp/service/usagelimit/UsageLimitService.java @@ -0,0 +1,11 @@ +package org.opencdmp.service.usagelimit; + +public class UsageLimitService { + + private void checkIncrease(String metric) { + //Get/Calculate current metric value from accountingService + //Find metric value from db + // compare these two and throw UsageLimitException when current > metric value + } + +} 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 7f1d4f648..b900245b2 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 @@ -326,7 +326,9 @@ public class UserServiceImpl implements UserService { @Override public User patchRoles(UserRolePatchPersist model, FieldSet fields) throws InvalidApplicationException { logger.debug(new MapLogEntry("persisting data UserRole").And("model", model).And("fields", fields)); - this.authorizationService.authorizeAtLeastOneForce(this.userScope.getUserId() != null ? List.of(new OwnedResource(this.userScope.getUserId())) : null, Permission.EditUser, Permission.EditTenantUserRole); + + if (!model.getHasTenantAdminMode()) this.authorizationService.authorizeForce(Permission.EditUser); + else this.authorizationService.authorizeForce(Permission.EditTenantUserRole); UserEntity data = this.entityManager.find(UserEntity.class, model.getId(), true); if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), User.class.getSimpleName()}, LocaleContextHolder.getLocale()));