From b7dee2e7d329fe8cd98fe2d964ff09d958417aaa Mon Sep 17 00:00:00 2001 From: Gregory <32060161+ggmaleva@users.noreply.github.com> Date: Fri, 9 Sep 2022 14:38:37 +0300 Subject: [PATCH] switch orgId -> party in bouncer context (#37) Co-authored-by: ggmaleva --- .../vality/orgmanager/service/OrganizationService.java | 4 ++-- .../orgmanager/service/ResourceAccessServiceImpl.java | 10 ++++++++-- .../service/ResourceAccessServiceImplTest.java | 10 +++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main/java/dev/vality/orgmanager/service/OrganizationService.java b/src/main/java/dev/vality/orgmanager/service/OrganizationService.java index bf94c66..334ab2c 100644 --- a/src/main/java/dev/vality/orgmanager/service/OrganizationService.java +++ b/src/main/java/dev/vality/orgmanager/service/OrganizationService.java @@ -306,10 +306,10 @@ public class OrganizationService { } @Transactional(readOnly = true) - public String getOrgIdByInvitationToken(String token) { + public String getPartyByInvitationToken(String token) { InvitationEntity invitationEntity = invitationService.findByToken(token); OrganizationEntity organizationEntity = findById(invitationEntity.getOrganizationId()); - return organizationEntity.getId(); + return organizationEntity.getParty(); } public OrganizationEntity findById(String orgId) { diff --git a/src/main/java/dev/vality/orgmanager/service/ResourceAccessServiceImpl.java b/src/main/java/dev/vality/orgmanager/service/ResourceAccessServiceImpl.java index 5fd8a2b..2ca6cc7 100644 --- a/src/main/java/dev/vality/orgmanager/service/ResourceAccessServiceImpl.java +++ b/src/main/java/dev/vality/orgmanager/service/ResourceAccessServiceImpl.java @@ -1,6 +1,7 @@ package dev.vality.orgmanager.service; import dev.vality.orgmanager.config.properties.AccessProperties; +import dev.vality.orgmanager.entity.OrganizationEntity; import dev.vality.orgmanager.exception.AccessDeniedException; import dev.vality.orgmanager.service.dto.BouncerContextDto; import dev.vality.orgmanager.service.dto.InvitationDto; @@ -44,10 +45,15 @@ public class ResourceAccessServiceImpl implements ResourceAccessService { } String callerMethodName = StackUtils.getCallerMethodName(); BouncerContextDto bouncerContext = buildBouncerContextDto(resource, callerMethodName); + if (Objects.nonNull(resource.getOrgId())) { + log.info("Get organization by orgId: {}", resource.getOrgId()); + OrganizationEntity organization = organizationService.findById(resource.getOrgId()); + bouncerContext.setOrganizationId(organization.getParty()); + } if (Objects.nonNull(resource.getInvitationToken())) { log.info("Get organization by invitation token"); - String orgId = organizationService.getOrgIdByInvitationToken(resource.getInvitationToken()); - bouncerContext.setOrganizationId(orgId); + String party = organizationService.getPartyByInvitationToken(resource.getInvitationToken()); + bouncerContext.setOrganizationId(party); } if (Objects.nonNull(resource.getMemberRoleId())) { String memberRoleId = resource.getMemberRoleId(); diff --git a/src/test/java/dev/vality/orgmanager/service/ResourceAccessServiceImplTest.java b/src/test/java/dev/vality/orgmanager/service/ResourceAccessServiceImplTest.java index f529164..5d13e6e 100644 --- a/src/test/java/dev/vality/orgmanager/service/ResourceAccessServiceImplTest.java +++ b/src/test/java/dev/vality/orgmanager/service/ResourceAccessServiceImplTest.java @@ -63,10 +63,12 @@ class ResourceAccessServiceImplTest { @Test void checkOrgRights() { + String orgId = TestObjectFactory.randomString(); ResourceDto resource = ResourceDto.builder() - .orgId(TestObjectFactory.randomString()) + .orgId(orgId) .build(); when(bouncerService.havePrivileges(any(BouncerContextDto.class))).thenReturn(true); + when(organizationService.findById(orgId)).thenReturn(TestObjectFactory.buildOrganization()); assertDoesNotThrow(() -> resourceAccessService.checkRights(resource)); } @@ -77,7 +79,7 @@ class ResourceAccessServiceImplTest { .invitationToken(TestObjectFactory.randomString()) .build(); when(bouncerService.havePrivileges(any(BouncerContextDto.class))).thenReturn(true); - when(organizationService.getOrgIdByInvitationToken(resource.getInvitationToken())) + when(organizationService.getPartyByInvitationToken(resource.getInvitationToken())) .thenThrow(new ResourceNotFoundException()); assertThrows(ResourceNotFoundException.class, @@ -96,11 +98,13 @@ class ResourceAccessServiceImplTest { @Test void checkMemberRights() { + String orgId = TestObjectFactory.randomString(); ResourceDto resource = ResourceDto.builder() - .orgId(TestObjectFactory.randomString()) + .orgId(orgId) .memberId(TestObjectFactory.randomString()) .build(); when(bouncerService.havePrivileges(any(BouncerContextDto.class))).thenReturn(true); + when(organizationService.findById(orgId)).thenReturn(TestObjectFactory.buildOrganization()); assertDoesNotThrow(() -> resourceAccessService.checkRights(resource)); }