From 50e38c68547540b43272e323ec9ca0c67505554a Mon Sep 17 00:00:00 2001 From: Sofia Papacharalampous Date: Fri, 12 Jul 2024 16:52:08 +0300 Subject: [PATCH] 1. clone public plan and description 2. fixed orcid links on public entities --- .../opencdmp/authorization/Permission.java | 3 ++ .../org/opencdmp/model/PublicReference.java | 25 +++++++++++++++ .../model/builder/PublicReferenceBuilder.java | 3 ++ .../service/plan/PlanServiceImpl.java | 31 ++++++++++++++++--- .../src/main/resources/config/permissions.yml | 10 ++++++ .../app/core/common/enum/permission.enum.ts | 1 + .../plan-listing-item.component.ts | 2 +- 7 files changed, 70 insertions(+), 5 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/authorization/Permission.java b/backend/core/src/main/java/org/opencdmp/authorization/Permission.java index 07014bda6..065332053 100644 --- a/backend/core/src/main/java/org/opencdmp/authorization/Permission.java +++ b/backend/core/src/main/java/org/opencdmp/authorization/Permission.java @@ -15,6 +15,9 @@ public final class Permission { public static String PublicBrowseDashboardStatistics = "PublicBrowseDashboardStatistics"; public static String PublicSendContactSupport = "PublicSendContactSupport"; public static String PublicBrowseReferenceType = "PublicBrowseReferenceType"; + public static String PublicClonePlan = "PublicClonePlan"; + public static String PublicCloneDescription = "PublicCloneDescription"; + //Elastic public static String ManageElastic = "ManageElastic"; //Queue Events diff --git a/backend/core/src/main/java/org/opencdmp/model/PublicReference.java b/backend/core/src/main/java/org/opencdmp/model/PublicReference.java index ba10bf426..f50487b21 100644 --- a/backend/core/src/main/java/org/opencdmp/model/PublicReference.java +++ b/backend/core/src/main/java/org/opencdmp/model/PublicReference.java @@ -1,5 +1,7 @@ package org.opencdmp.model; +import org.opencdmp.commons.enums.ReferenceSourceType; + import java.util.UUID; public class PublicReference { @@ -19,6 +21,13 @@ public class PublicReference { private String reference; public static final String _reference = "reference"; + private String source; + public static final String _source = "source"; + + private ReferenceSourceType sourceType; + public static final String _sourceType = "sourceType"; + + public UUID getId() { return id; } @@ -58,6 +67,22 @@ public class PublicReference { public void setReference(String reference) { this.reference = reference; } + + public String getSource() { + return source; + } + + public void setSource(String source) { + this.source = source; + } + + public ReferenceSourceType getSourceType() { + return sourceType; + } + + public void setSourceType(ReferenceSourceType sourceType) { + this.sourceType = sourceType; + } } diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/PublicReferenceBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/PublicReferenceBuilder.java index 9c40c0383..0fade32f4 100644 --- a/backend/core/src/main/java/org/opencdmp/model/builder/PublicReferenceBuilder.java +++ b/backend/core/src/main/java/org/opencdmp/model/builder/PublicReferenceBuilder.java @@ -13,6 +13,7 @@ import org.opencdmp.convention.ConventionService; import org.opencdmp.data.ReferenceEntity; import org.opencdmp.model.PublicReference; import org.opencdmp.model.PublicReferenceType; +import org.opencdmp.model.reference.Reference; import org.opencdmp.query.ReferenceTypeQuery; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -62,6 +63,8 @@ public class PublicReferenceBuilder extends BaseBuilder planDescriptionTemplateRemap, UUID descriptionId, UUID newPlanDescriptionTemplateId) throws InvalidApplicationException, IOException { logger.debug("cloning description: {} with description: {}", descriptionId, planId); - this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(descriptionId)), Permission.CloneDescription); + PlanEntity descriptionPlan = this.queryFactory.query(PlanQuery.class).disableTracking().ids(planId).isActive(IsActive.Active).first(); + + if (!descriptionPlan.getAccessType().equals(PlanAccessType.Public)) this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(descriptionId)), Permission.CloneDescription); + else this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(descriptionId)), Permission.PublicCloneDescription); DescriptionEntity existing = this.queryFactory.query(DescriptionQuery.class).disableTracking().ids(descriptionId).isActive(IsActive.Active).first(); - DescriptionEntity newDescription = new DescriptionEntity(); newDescription.setId(UUID.randomUUID()); newDescription.setLabel(existing.getLabel()); @@ -673,12 +679,15 @@ public class PlanServiceImpl implements PlanService { @Override public Plan buildClone(ClonePlanPersist model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, IOException, InvalidApplicationException { - this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.planAffiliation( model.getId())), Permission.ClonePlan); - PlanEntity existingPlanEntity = this.queryFactory.query(PlanQuery.class).disableTracking().authorize(AuthorizationFlags.AllExceptPublic).ids(model.getId()).firstAs(fields); + PlanEntity existingPlanEntity = this.queryFactory.query(PlanQuery.class).disableTracking().ids(model.getId()).firstAs(fields); + if (!this.conventionService.isValidGuid(model.getId()) || existingPlanEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), Plan.class.getSimpleName()}, LocaleContextHolder.getLocale())); + if (!existingPlanEntity.getAccessType().equals(PlanAccessType.Public)) this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.planAffiliation( model.getId())), Permission.ClonePlan); + else this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.planAffiliation( model.getId())), Permission.PublicClonePlan); + PlanEntity newPlan = new PlanEntity(); newPlan.setId(UUID.randomUUID()); newPlan.setIsActive(IsActive.Active); @@ -711,6 +720,20 @@ public class PlanServiceImpl implements PlanService { .isActive(IsActive.Active) .collect(); + UUID currentUserId = this.userScope.getUserId(); + boolean currentUserIsInPlan = planUsers.stream().anyMatch(u -> u.getUserId() == currentUserId); + if (!currentUserIsInPlan) { + PlanUserEntity newUser = new PlanUserEntity(); + newUser.setId(UUID.randomUUID()); + newUser.setPlanId(newPlan.getId()); + newUser.setUserId(currentUserId); + newUser.setRole(PlanUserRole.Owner); + newUser.setCreatedAt(Instant.now()); + newUser.setUpdatedAt(Instant.now()); + newUser.setIsActive(IsActive.Active); + + this.entityManager.persist(newUser); + } for (PlanUserEntity planUser : planUsers) { PlanUserEntity newUser = new PlanUserEntity(); newUser.setId(UUID.randomUUID()); diff --git a/backend/web/src/main/resources/config/permissions.yml b/backend/web/src/main/resources/config/permissions.yml index 19af56300..bff6926d7 100644 --- a/backend/web/src/main/resources/config/permissions.yml +++ b/backend/web/src/main/resources/config/permissions.yml @@ -69,6 +69,16 @@ permissions: clients: [ ] allowAnonymous: true allowAuthenticated: true + PublicClonePlan: + roles: [ ] + clients: [ ] + allowAnonymous: false + allowAuthenticated: true + PublicCloneDescription: + roles: [ ] + clients: [ ] + allowAnonymous: false + allowAuthenticated: true BrowsePublicStatistics: roles: [ ] clients: [ ] diff --git a/frontend/src/app/core/common/enum/permission.enum.ts b/frontend/src/app/core/common/enum/permission.enum.ts index 4cfeb134e..dc951eb59 100644 --- a/frontend/src/app/core/common/enum/permission.enum.ts +++ b/frontend/src/app/core/common/enum/permission.enum.ts @@ -13,6 +13,7 @@ export enum AppPermission { PublicBrowseDashboardStatistics = "PublicBrowseDashboardStatistics", PublicSendContactSupport = "PublicSendContactSupport", PublicBrowseReferenceType = "PublicBrowseReferenceType", + PublicClonePlan = "PublicClonePlan", //Elastic ManageElastic = "ManageElastic", //Queue Events diff --git a/frontend/src/app/ui/plan/listing/listing-item/plan-listing-item.component.ts b/frontend/src/app/ui/plan/listing/listing-item/plan-listing-item.component.ts index 18e3a31df..51ef6fbb0 100644 --- a/frontend/src/app/ui/plan/listing/listing-item/plan-listing-item.component.ts +++ b/frontend/src/app/ui/plan/listing/listing-item/plan-listing-item.component.ts @@ -248,7 +248,7 @@ export class PlanListingItemComponent extends BaseComponent implements OnInit { } canClonePlan(): boolean { - return this.plan.authorizationFlags?.some(x => x === AppPermission.ClonePlan) || this.authentication.hasPermission(AppPermission.ClonePlan); + return this.plan.authorizationFlags?.some(x => x === AppPermission.ClonePlan) || this.authentication.hasPermission(AppPermission.ClonePlan) || (this.authentication.hasPermission(AppPermission.PublicClonePlan) && this.isPublic); } canFinalizePlan(): boolean {