mirror of
https://github.com/valitydev/org-manager.git
synced 2024-11-06 00:15:23 +00:00
Merge pull request #57 from rbkmoney/ft/JD-768
fix context for owner user
This commit is contained in:
commit
5cab4a9150
2
pom.xml
2
pom.xml
@ -13,7 +13,7 @@
|
|||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Org manager</name>
|
<name>org-manager</name>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
@ -6,6 +6,7 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import javax.persistence.*;
|
import javax.persistence.*;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@ -24,8 +25,6 @@ public class MemberContextEntity implements Serializable {
|
|||||||
@JoinColumn(name = "organization_id", referencedColumnName = "id")
|
@JoinColumn(name = "organization_id", referencedColumnName = "id")
|
||||||
private OrganizationEntity organizationEntity;
|
private OrganizationEntity organizationEntity;
|
||||||
|
|
||||||
@OneToOne
|
private String memberId;
|
||||||
@JoinColumn(name = "member_id", referencedColumnName = "id")
|
|
||||||
private MemberEntity memberEntity;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,11 @@
|
|||||||
package com.rbkmoney.orgmanager.exception;
|
package com.rbkmoney.orgmanager.exception;
|
||||||
|
|
||||||
public class ResourceNotFoundException extends RuntimeException {
|
public class ResourceNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
public ResourceNotFoundException() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResourceNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,6 @@ import java.util.Optional;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface MemberContextRepository extends JpaRepository<MemberContextEntity, String> {
|
public interface MemberContextRepository extends JpaRepository<MemberContextEntity, String> {
|
||||||
|
|
||||||
Optional<MemberContextEntity> findByMemberEntityId(String memberId);
|
Optional<MemberContextEntity> findByMemberId(String memberId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -32,5 +32,7 @@ public interface MemberRepository extends JpaRepository<MemberEntity, String> {
|
|||||||
" AND m.id = mtmr.member_id ", nativeQuery = true)
|
" AND m.id = mtmr.member_id ", nativeQuery = true)
|
||||||
List<MemberWithRoleDto> getOrgMemberList(String orgId);
|
List<MemberWithRoleDto> getOrgMemberList(String orgId);
|
||||||
|
|
||||||
|
boolean existsById(String id);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ import org.keycloak.representations.AccessToken;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
@ -263,28 +264,32 @@ public class OrganizationService {
|
|||||||
OrganizationEntity organizationEntity = organizationRepository.findById(organizationId)
|
OrganizationEntity organizationEntity = organizationRepository.findById(organizationId)
|
||||||
.orElseThrow(ResourceNotFoundException::new);
|
.orElseThrow(ResourceNotFoundException::new);
|
||||||
Optional<MemberContextEntity> memberContextEntityOptional =
|
Optional<MemberContextEntity> memberContextEntityOptional =
|
||||||
memberContextRepository.findByMemberEntityId(userId);
|
memberContextRepository.findByMemberId(userId);
|
||||||
if (memberContextEntityOptional.isPresent()) {
|
if (memberContextEntityOptional.isPresent()) {
|
||||||
MemberContextEntity memberContextEntity = memberContextEntityOptional.get();
|
MemberContextEntity memberContextEntity = memberContextEntityOptional.get();
|
||||||
memberContextEntity.setOrganizationEntity(organizationEntity);
|
memberContextEntity.setOrganizationEntity(organizationEntity);
|
||||||
memberContextRepository.save(memberContextEntity);
|
memberContextRepository.save(memberContextEntity);
|
||||||
} else {
|
} else {
|
||||||
MemberEntity memberEntity = memberRepository.findById(userId)
|
if (isNotExistUser(userId)) {
|
||||||
.orElseThrow(() -> new IllegalArgumentException("Can't find member. Unknown userId=" + userId));
|
throw new ResourceNotFoundException("Can't find member. Unknown userId=" + userId);
|
||||||
|
}
|
||||||
MemberContextEntity memberContextEntity = new MemberContextEntity();
|
MemberContextEntity memberContextEntity = new MemberContextEntity();
|
||||||
memberContextEntity.setOrganizationEntity(organizationEntity);
|
memberContextEntity.setOrganizationEntity(organizationEntity);
|
||||||
memberContextEntity.setMemberEntity(memberEntity);
|
memberContextEntity.setMemberId(userId);
|
||||||
memberContextRepository.save(memberContextEntity);
|
memberContextRepository.save(memberContextEntity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public MemberContext findMemberContext(String userId) {
|
private boolean isNotExistUser(String userId) {
|
||||||
MemberContextEntity memberContextEntity = memberContextRepository.findByMemberEntityId(userId)
|
return !memberRepository.existsById(userId) &
|
||||||
.orElseThrow(ResourceNotFoundException::new);
|
CollectionUtils.isEmpty(organizationRepository.findAllByOwner(userId));
|
||||||
|
}
|
||||||
|
|
||||||
|
public MemberContext findMemberContext(String userId) {
|
||||||
|
MemberContextEntity memberContextEntity = memberContextRepository.findByMemberId(userId)
|
||||||
|
.orElseThrow(ResourceNotFoundException::new);
|
||||||
MemberContext memberContext = new MemberContext();
|
MemberContext memberContext = new MemberContext();
|
||||||
memberContext.setOrganizationId(memberContextEntity.getOrganizationEntity().getId());
|
memberContext.setOrganizationId(memberContextEntity.getOrganizationEntity().getId());
|
||||||
|
|
||||||
return memberContext;
|
return memberContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
ALTER TABLE org_manager.member_context DROP CONSTRAINT member_context_to_member_fkey;
|
@ -1,6 +1,5 @@
|
|||||||
package com.rbkmoney.orgmanager.controller;
|
package com.rbkmoney.orgmanager.controller;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
||||||
import com.rbkmoney.orgmanager.entity.*;
|
import com.rbkmoney.orgmanager.entity.*;
|
||||||
import com.rbkmoney.orgmanager.exception.AccessDeniedException;
|
import com.rbkmoney.orgmanager.exception.AccessDeniedException;
|
||||||
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
|
import com.rbkmoney.orgmanager.exception.ResourceNotFoundException;
|
||||||
@ -311,6 +310,23 @@ public class UserControllerTest extends AbstractControllerTest {
|
|||||||
assertNull(organizationSearchResultThird.getContinuationToken());
|
assertNull(organizationSearchResultThird.getContinuationToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void switchOrganizationWithNotExistMember() throws Exception {
|
||||||
|
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
|
||||||
|
OrganizationSwitchRequest organizationSwitchRequest = new OrganizationSwitchRequest();
|
||||||
|
organizationSwitchRequest.setOrganizationId(organizationEntity.getId());
|
||||||
|
String jwtToken = generateRbkAdminJwt();
|
||||||
|
|
||||||
|
mockMvc.perform(put("/user/context")
|
||||||
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
.contentType("application/json")
|
||||||
|
.header("Authorization", "Bearer " + jwtToken)
|
||||||
|
.header("X-Request-ID", "testRequestId")
|
||||||
|
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
|
||||||
|
)
|
||||||
|
.andExpect(status().isNotFound());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void switchOrganizationWithNewContextCreation() throws Exception {
|
void switchOrganizationWithNewContextCreation() throws Exception {
|
||||||
String userId = getUserFromToken();
|
String userId = getUserFromToken();
|
||||||
@ -321,18 +337,44 @@ public class UserControllerTest extends AbstractControllerTest {
|
|||||||
organizationSwitchRequest.setOrganizationId(organizationEntity.getId());
|
organizationSwitchRequest.setOrganizationId(organizationEntity.getId());
|
||||||
|
|
||||||
mockMvc.perform(put("/user/context")
|
mockMvc.perform(put("/user/context")
|
||||||
.accept(MediaType.APPLICATION_JSON)
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
.header("Authorization", "Bearer " + jwtToken)
|
.header("Authorization", "Bearer " + jwtToken)
|
||||||
.header("X-Request-ID", "testRequestId")
|
.header("X-Request-ID", "testRequestId")
|
||||||
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
|
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
|
||||||
)
|
)
|
||||||
.andExpect(status().isNoContent());
|
.andExpect(status().isNoContent());
|
||||||
Optional<MemberContextEntity> memberContextEntityOptional =
|
Optional<MemberContextEntity> memberContextEntityOptional =
|
||||||
memberContextRepository.findByMemberEntityId(userId);
|
memberContextRepository.findByMemberId(userId);
|
||||||
|
|
||||||
assertTrue(memberContextEntityOptional.isPresent());
|
assertTrue(memberContextEntityOptional.isPresent());
|
||||||
assertEquals(userId, memberContextEntityOptional.get().getMemberEntity().getId());
|
assertEquals(userId, memberContextEntityOptional.get().getMemberId());
|
||||||
|
assertEquals(organizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void switchOrganizationWithOwnerUser() throws Exception {
|
||||||
|
String userId = getUserFromToken();
|
||||||
|
OrganizationEntity organization = buildOrganization();
|
||||||
|
organization.setOwner(userId);
|
||||||
|
OrganizationEntity organizationEntity = organizationRepository.save(organization);
|
||||||
|
String jwtToken = generateRbkAdminJwt();
|
||||||
|
OrganizationSwitchRequest organizationSwitchRequest = new OrganizationSwitchRequest();
|
||||||
|
organizationSwitchRequest.setOrganizationId(organizationEntity.getId());
|
||||||
|
|
||||||
|
mockMvc.perform(put("/user/context")
|
||||||
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
|
.contentType("application/json")
|
||||||
|
.header("Authorization", "Bearer " + jwtToken)
|
||||||
|
.header("X-Request-ID", "testRequestId")
|
||||||
|
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
|
||||||
|
)
|
||||||
|
.andExpect(status().isNoContent());
|
||||||
|
Optional<MemberContextEntity> memberContextEntityOptional =
|
||||||
|
memberContextRepository.findByMemberId(userId);
|
||||||
|
|
||||||
|
assertTrue(memberContextEntityOptional.isPresent());
|
||||||
|
assertEquals(userId, memberContextEntityOptional.get().getMemberId());
|
||||||
assertEquals(organizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
|
assertEquals(organizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -345,12 +387,12 @@ public class UserControllerTest extends AbstractControllerTest {
|
|||||||
organizationSwitchRequest.setOrganizationId("testOrgId");
|
organizationSwitchRequest.setOrganizationId("testOrgId");
|
||||||
|
|
||||||
mockMvc.perform(put("/user/context")
|
mockMvc.perform(put("/user/context")
|
||||||
.accept(MediaType.APPLICATION_JSON)
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
.header("Authorization", "Bearer " + jwtToken)
|
.header("Authorization", "Bearer " + jwtToken)
|
||||||
.header("X-Request-ID", "testRequestId")
|
.header("X-Request-ID", "testRequestId")
|
||||||
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
|
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
|
||||||
)
|
)
|
||||||
.andExpect(status().isNotFound());
|
.andExpect(status().isNotFound());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -361,7 +403,7 @@ public class UserControllerTest extends AbstractControllerTest {
|
|||||||
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
|
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
|
||||||
MemberContextEntity memberContextEntity = memberContextRepository.save(
|
MemberContextEntity memberContextEntity = memberContextRepository.save(
|
||||||
MemberContextEntity.builder()
|
MemberContextEntity.builder()
|
||||||
.memberEntity(memberEntity)
|
.memberId(userId)
|
||||||
.organizationEntity(organizationEntity)
|
.organizationEntity(organizationEntity)
|
||||||
.build()
|
.build()
|
||||||
);
|
);
|
||||||
@ -371,15 +413,15 @@ public class UserControllerTest extends AbstractControllerTest {
|
|||||||
organizationSwitchRequest.setOrganizationId(newOrganizationEntity.getId());
|
organizationSwitchRequest.setOrganizationId(newOrganizationEntity.getId());
|
||||||
|
|
||||||
mockMvc.perform(put("/user/context")
|
mockMvc.perform(put("/user/context")
|
||||||
.accept(MediaType.APPLICATION_JSON)
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
.header("Authorization", "Bearer " + jwtToken)
|
.header("Authorization", "Bearer " + jwtToken)
|
||||||
.header("X-Request-ID", "testRequestId")
|
.header("X-Request-ID", "testRequestId")
|
||||||
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
|
.content(objectMapper.writeValueAsString(organizationSwitchRequest))
|
||||||
)
|
)
|
||||||
.andExpect(status().isNoContent());
|
.andExpect(status().isNoContent());
|
||||||
Optional<MemberContextEntity> memberContextEntityOptional =
|
Optional<MemberContextEntity> memberContextEntityOptional =
|
||||||
memberContextRepository.findByMemberEntityId(userId);
|
memberContextRepository.findByMemberId(userId);
|
||||||
|
|
||||||
assertTrue(memberContextEntityOptional.isPresent());
|
assertTrue(memberContextEntityOptional.isPresent());
|
||||||
assertEquals(newOrganizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
|
assertEquals(newOrganizationEntity.getId(), memberContextEntityOptional.get().getOrganizationEntity().getId());
|
||||||
@ -392,21 +434,19 @@ public class UserControllerTest extends AbstractControllerTest {
|
|||||||
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
|
OrganizationEntity organizationEntity = organizationRepository.save(buildOrganization());
|
||||||
MemberContextEntity memberContextEntity = memberContextRepository.save(
|
MemberContextEntity memberContextEntity = memberContextRepository.save(
|
||||||
MemberContextEntity.builder()
|
MemberContextEntity.builder()
|
||||||
.memberEntity(memberEntity)
|
.memberId(userId)
|
||||||
.organizationEntity(organizationEntity)
|
.organizationEntity(organizationEntity)
|
||||||
.build()
|
.build()
|
||||||
);
|
);
|
||||||
String jwtToken = generateRbkAdminJwt();
|
String jwtToken = generateRbkAdminJwt();
|
||||||
|
|
||||||
MvcResult mvcResult = mockMvc.perform(get("/user/context")
|
mockMvc.perform(get("/user/context")
|
||||||
.accept(MediaType.APPLICATION_JSON)
|
.accept(MediaType.APPLICATION_JSON)
|
||||||
.header("Authorization", "Bearer " + jwtToken)
|
.header("Authorization", "Bearer " + jwtToken)
|
||||||
.header("X-Request-ID", "testRequestId")
|
.header("X-Request-ID", "testRequestId")
|
||||||
)
|
)
|
||||||
.andExpect(status().isOk())
|
.andExpect(status().isOk())
|
||||||
.andReturn();
|
.andExpect(jsonPath("$.organizationId", equalTo(organizationEntity.getId())));
|
||||||
|
|
||||||
System.out.println(mvcResult);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user