diff --git a/src/main/java/com/rbkmoney/orgmanager/entity/MemberEntity.java b/src/main/java/com/rbkmoney/orgmanager/entity/MemberEntity.java index cee1d0a..affd8d7 100644 --- a/src/main/java/com/rbkmoney/orgmanager/entity/MemberEntity.java +++ b/src/main/java/com/rbkmoney/orgmanager/entity/MemberEntity.java @@ -1,8 +1,21 @@ package com.rbkmoney.orgmanager.entity; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; +import javax.persistence.Table; -import javax.persistence.*; import java.io.Serializable; import java.util.Set; @@ -28,7 +41,7 @@ public class MemberEntity implements Serializable { @ToString.Exclude @EqualsAndHashCode.Exclude - @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinTable( name = "member_to_organization", joinColumns = @JoinColumn(name = "member_id"), diff --git a/src/main/java/com/rbkmoney/orgmanager/service/AuthContextService.java b/src/main/java/com/rbkmoney/orgmanager/service/AuthContextService.java index 2bf0135..9d749ae 100644 --- a/src/main/java/com/rbkmoney/orgmanager/service/AuthContextService.java +++ b/src/main/java/com/rbkmoney/orgmanager/service/AuthContextService.java @@ -1,5 +1,6 @@ package com.rbkmoney.orgmanager.service; +import com.rbkmoney.bouncer.context.v1.User; import com.rbkmoney.bouncer.ctx.ContextFragment; import com.rbkmoney.bouncer.ctx.ContextFragmentType; import com.rbkmoney.orgmanagement.AuthContextProviderSrv; @@ -11,8 +12,7 @@ import lombok.RequiredArgsConstructor; import org.apache.thrift.TException; import org.apache.thrift.TSerializer; import org.springframework.stereotype.Service; - -import java.util.Optional; +import org.springframework.transaction.annotation.Transactional; @Service @RequiredArgsConstructor @@ -22,16 +22,21 @@ public class AuthContextService implements AuthContextProviderSrv.Iface { private final MemberConverter memberConverter; @Override - public ContextFragment getUserContext(String id) throws UserNotFound, TException { - - Optional entity = memberRepository.findById(id); - if (entity.isEmpty()) { - throw new UserNotFound(); - } - MemberEntity member = entity.get(); + @Transactional(readOnly = true) + public ContextFragment getUserContext(String id) throws TException { + com.rbkmoney.bouncer.context.v1.ContextFragment contextFragment = + new com.rbkmoney.bouncer.context.v1.ContextFragment(); + User user = getUser(id); + contextFragment.setUser(user); TSerializer tSerializer = new TSerializer(); return new ContextFragment() .setType(ContextFragmentType.v1_thrift_binary) - .setContent(tSerializer.serialize(memberConverter.toThrift(member))); + .setContent(tSerializer.serialize(contextFragment)); + } + + private User getUser(String id) throws UserNotFound { + MemberEntity member = memberRepository.findById(id) + .orElseThrow(UserNotFound::new); + return memberConverter.toThrift(member); } }