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