Commit 4299b799 authored by xiaozhe.chen's avatar xiaozhe.chen

添加role相关的类

parent b088879e
package cn.quantgroup.cashloanflowboss.api.role;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import cn.quantgroup.cashloanflowboss.api.role.entity.RoleUser;
import cn.quantgroup.cashloanflowboss.api.role.repository.RoleRepository;
import cn.quantgroup.cashloanflowboss.api.role.repository.RoleUserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class RoleService {
@Autowired
private RoleRepository roleRepository;
@Autowired
private RoleUserRepository roleUserRepository;
public Role findRoleByUserId(Long userId) {
RoleUser roleUser = roleUserRepository.findByUserId(userId);
Long roleId = roleUser.getRoleId();
Role role = findRoleByRoleId(roleId);
return role;
}
public Role findRoleByRoleId(Long roleId) {
return roleRepository.findOne(roleId);
}
}
package cn.quantgroup.cashloanflowboss.api.role.entity;
import cn.quantgroup.cashloanflowboss.core.persistence.Primary;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.Table;
@Data
@Entity
@Table(name = "role_user")
public class RoleUser extends Primary {
private Long roleId;
private Long userId;
}
package cn.quantgroup.cashloanflowboss.api.role.repository; package cn.quantgroup.cashloanflowboss.api.role.repository;
import cn.quantgroup.cashloanflowboss.api.role.entity.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
* Created by WeiWei on 2019/7/30. * Created by WeiWei on 2019/7/30.
*/ */
@Repository @Repository
public interface RoleRepository { public interface RoleRepository extends JpaRepository<Role, Long> {
} }
package cn.quantgroup.cashloanflowboss.api.role.repository;
import cn.quantgroup.cashloanflowboss.api.role.entity.RoleUser;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface RoleUserRepository extends JpaRepository<RoleUser, Long> {
RoleUser findByUserId(Long userId);
}
package cn.quantgroup.cashloanflowboss.api.test.controller;
import cn.quantgroup.cashloanflowboss.api.user.entity.boss.User;
import cn.quantgroup.cashloanflowboss.api.user.service.UserService;
import cn.quantgroup.cashloanflowboss.core.base.Result;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private UserService userService;
@GetMapping("/user/info")
public Result findUserFromSession() {
User currentUser = userService.findCurrentUser();
return new Result<>(ApplicationStatus.SUCCESS, currentUser);
}
}
package cn.quantgroup.cashloanflowboss.api.user.model;
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class UserSessionInfo {
private String userName;
private String roleName;
private Long channelId;
}
...@@ -4,14 +4,20 @@ import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus; ...@@ -4,14 +4,20 @@ import cn.quantgroup.cashloanflowboss.api.user.dictionary.UserStatus;
import cn.quantgroup.cashloanflowboss.api.user.entity.boss.User; import cn.quantgroup.cashloanflowboss.api.user.entity.boss.User;
import cn.quantgroup.cashloanflowboss.api.user.repository.boss.UserRepository; import cn.quantgroup.cashloanflowboss.api.user.repository.boss.UserRepository;
import cn.quantgroup.cashloanflowboss.core.asserts.Assert; import cn.quantgroup.cashloanflowboss.core.asserts.Assert;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationDictionary;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus; import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationStatus;
import cn.quantgroup.cashloanflowboss.utils.JSONTools;
import cn.quantgroup.cashloanflowboss.utils.MD5Tools; import cn.quantgroup.cashloanflowboss.utils.MD5Tools;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpSession;
import java.util.Objects; import java.util.Objects;
/** /**
...@@ -130,4 +136,13 @@ public class UserService { ...@@ -130,4 +136,13 @@ public class UserService {
} }
public User findCurrentUser() {
HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
Assert.isNull(session.getAttribute(ApplicationDictionary.USER_KEY), ApplicationStatus.INVALID_USER);
User user = JSONTools.deserialize(String.valueOf(session.getAttribute(ApplicationDictionary.USER_KEY)), new TypeReference<User>() {
});
return user;
}
} }
package cn.quantgroup.cashloanflowboss.api.user.service;
import cn.quantgroup.cashloanflowboss.api.user.entity.boss.User;
import cn.quantgroup.cashloanflowboss.api.user.model.UserSessionInfo;
import cn.quantgroup.cashloanflowboss.core.dictionary.ApplicationDictionary;
import cn.quantgroup.cashloanflowboss.utils.JSONTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpSession;
@Slf4j
@Service
public class UserSessionService {
public void setUserSessionInfo(User user) {
HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession();
session.setAttribute(ApplicationDictionary.USER_KEY, JSONTools.serialize(user));
//query user role info
//UserSessionInfo.builder()
// .userName(user.getUsername())
// .roleName()
}
}
...@@ -9,5 +9,7 @@ public final class ApplicationDictionary { ...@@ -9,5 +9,7 @@ public final class ApplicationDictionary {
// 权限Session Key // 权限Session Key
public static final String SECURITY_KEY = "PERMISSION"; public static final String SECURITY_KEY = "PERMISSION";
// user Session key
public static final String USER_KEY = "USER";
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment