Skip to content

Commit

Permalink
feat($auth-center): provide remote service for api-portal: get role…
Browse files Browse the repository at this point in the history
… list
  • Loading branch information
johnnymillergh committed May 10, 2020
1 parent 71daf55 commit a472f46
Show file tree
Hide file tree
Showing 18 changed files with 503 additions and 248 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
package com.jmsoftware.apiportal.remoteapi.authcenter.user;
package com.jmsoftware.apiportal.remoteapi;

import com.jmsoftware.common.bean.ResponseBodyBean;
import com.jmsoftware.common.domain.authcenter.role.GetRoleListByUserIdPayload;
import com.jmsoftware.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.common.domain.authcenter.user.GetUserByLoginTokenPayload;
import com.jmsoftware.common.domain.authcenter.user.GetUserByLoginTokenResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
* <h1>UserRemoteApi</h1>
* <h1>AuthCenterRemoteApi</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 5/10/20 4:50 PM
*/
@FeignClient(name = "auth-center")
public interface UserRemoteApi {
public interface AuthCenterRemoteApi {
/**
* Gets user by login token.
*
* @param payload the payload
* @return the user by login token
*/
@PostMapping("/user/get-user-by-login-token")
@PostMapping("/user-remote-api/get-user-by-login-token")
ResponseBodyBean<GetUserByLoginTokenResponse> getUserByLoginToken(@RequestBody GetUserByLoginTokenPayload payload);

/**
* Gets role list by user id.
*
* @param payload the payload
* @return the role list by user id
*/
@PostMapping("/role-remote-api/get-role-list-by-user-id")
ResponseBodyBean<GetRoleListByUserIdResponse> getRoleListByUserId(@RequestBody GetRoleListByUserIdPayload payload);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.jmsoftware.apiportal.remoteapi.authcenter.user.UserRemoteApi;
import com.jmsoftware.apiportal.remoteapi.AuthCenterRemoteApi;
import com.jmsoftware.apiportal.universal.domain.PermissionPO;
import com.jmsoftware.apiportal.universal.domain.RolePO;
import com.jmsoftware.apiportal.universal.domain.UserPO;
import com.jmsoftware.apiportal.universal.domain.UserPrincipal;
import com.jmsoftware.apiportal.universal.mapper.PermissionMapper;
import com.jmsoftware.apiportal.universal.service.RoleService;
import com.jmsoftware.common.constant.HttpStatus;
import com.jmsoftware.common.domain.authcenter.role.GetRoleListByUserIdPayload;
import com.jmsoftware.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import com.jmsoftware.common.domain.authcenter.user.GetUserByLoginTokenPayload;
import com.jmsoftware.common.exception.SecurityException;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,6 +22,7 @@
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -35,31 +37,39 @@
@Service
@RequiredArgsConstructor
public class CustomUserDetailsServiceImpl implements UserDetailsService {
private final RoleService roleService;
private final PermissionMapper permissionMapper;
private final UserRemoteApi userRemoteApi;
private final AuthCenterRemoteApi authCenterRemoteApi;

@Override
public UserDetails loadUserByUsername(String credentials) throws UsernameNotFoundException {
val payload = new GetUserByLoginTokenPayload();
payload.setLoginToken(credentials);
var response = userRemoteApi.getUserByLoginToken(payload);
val response = authCenterRemoteApi.getUserByLoginToken(payload);
val data = response.getData();
if (ObjectUtil.isNull(data)) {
String errorMessage = "User's account not found: " + credentials;
val errorMessage = String.format("User's account not found, credentials: %s", credentials);
log.error(errorMessage);
throw new UsernameNotFoundException(errorMessage);
}
List<RolePO> rolesByUserId = roleService.getRolesByUserId(data.getId());
if (CollUtil.isEmpty(rolesByUserId)) {
val payload1 = new GetRoleListByUserIdPayload();
payload1.setUserId(data.getId());
val response2 = authCenterRemoteApi.getRoleListByUserId(payload1);
val roleList = response2.getData().getRoleList();
if (CollUtil.isEmpty(roleList)) {
throw new SecurityException(HttpStatus.ROLE_NOT_FOUND);
}
List<Long> roleIds = rolesByUserId.stream()
.map(RolePO::getId)
List<Long> roleIdList = roleList.stream()
.map(GetRoleListByUserIdResponse.Role::getId)
.collect(Collectors.toList());
List<PermissionPO> permissionList = permissionMapper.selectByRoleIdList(roleIds);
List<PermissionPO> permissionList = permissionMapper.selectByRoleIdList(roleIdList);
var user = new UserPO();
BeanUtil.copyProperties(data, user);
return UserPrincipal.create(user, rolesByUserId, permissionList);
List<RolePO> rolePOList = new LinkedList<>();
roleList.forEach(role -> {
val rolePO = new RolePO();
BeanUtil.copyProperties(role, rolePO);
rolePOList.add(rolePO);
});
return UserPrincipal.create(user, rolePOList, permissionList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.jmsoftware.authcenter.role.controller;

import com.jmsoftware.authcenter.role.entity.RolePersistence;
import com.jmsoftware.authcenter.role.service.RoleService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
* <h1>RoleController</h1>
* <p>
* Controller implementation of Role.(Role)
*
* @author Johnny Miller (鍾俊)
* @date 2020-05-10 22:39:51
*/
@RestController
@RequestMapping("role")
public class RoleController {
@Resource
private RoleService roleService;

@GetMapping("selectOne")
public RolePersistence selectOne(Long id) {
return this.roleService.queryById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.jmsoftware.authcenter.role.entity;

import lombok.Data;

import java.io.Serializable;
import java.util.Date;

/**
* Role.(Role) Persistence object class
*
* @author Johnny Miller (鍾俊)
* @since 2020-05-10 22:39:45
*/
@Data
public class RolePersistence implements Serializable {
private static final long serialVersionUID = -81197803690669820L;
/**
* Primary key
*/
private Long id;
/**
* Role name
*/
private String name;
/**
* Role description
*/
private String description;
/**
* Created time
*/
private Date createdTime;
/**
* Modified time
*/
private Date modifiedTime;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.jmsoftware.authcenter.role.mapper;

import com.jmsoftware.authcenter.role.entity.RolePersistence;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
* <h1>RoleMapper</h1>
* <p>
* Mapper of Role.(Role)
*
* @author Johnny Miller (鍾俊)
* @date 2020 -05-10 22:39:48
*/
@Mapper
public interface RoleMapper {
/**
* Query by id role.
*
* @param id the id
* @return the role
*/
RolePersistence queryById(Long id);

/**
* Query all by limit list.
*
* @param offset the offset
* @param limit the limit
* @return the list
*/
List<RolePersistence> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);

/**
* Query all list.
*
* @param rolePersistence the role
* @return the list
*/
List<RolePersistence> queryAll(RolePersistence rolePersistence);

/**
* Insert int.
*
* @param rolePersistence the role
* @return the int
*/
int insert(RolePersistence rolePersistence);

/**
* Update int.
*
* @param rolePersistence the role
* @return the int
*/
int update(RolePersistence rolePersistence);

/**
* Delete by id int.
*
* @param id the id
* @return the int
*/
int deleteById(Long id);

/**
* Select role list by user id list.
*
* @param userId the user id
* @return the list
*/
List<RolePersistence> selectRoleListByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.jmsoftware.authcenter.role.remote;

import com.jmsoftware.authcenter.role.service.RoleService;
import com.jmsoftware.common.bean.ResponseBodyBean;
import com.jmsoftware.common.domain.authcenter.role.GetRoleListByUserIdPayload;
import com.jmsoftware.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* <h1>RoleRemoteApiController</h1>
* <p>
* Change description here.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 5/10/20 10:43 PM
**/
@RestController
@RequiredArgsConstructor
@RequestMapping("/role-remote-api")
@Api(tags = {"Role Remote API Controller"})
public class RoleRemoteApiController {
private final RoleService roleService;

@PostMapping("/get-role-list-by-user-id")
public ResponseBodyBean<GetRoleListByUserIdResponse> getRoleListByUserId(@Valid @RequestBody GetRoleListByUserIdPayload payload) {
return ResponseBodyBean.ofSuccess(roleService.getRoleListByUserId(payload));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.jmsoftware.authcenter.role.service;

import com.jmsoftware.authcenter.role.entity.RolePersistence;
import com.jmsoftware.common.domain.authcenter.role.GetRoleListByUserIdPayload;
import com.jmsoftware.common.domain.authcenter.role.GetRoleListByUserIdResponse;
import lombok.NonNull;

import java.util.List;

/**
* <h1>RoleService</h1>
* <p>
* Service of Role.(Role)
*
* @author Johnny Miller (鍾俊)
* @date 2020 -05-10 22:39:49
*/
public interface RoleService {
/**
* Query by ID
*
* @param id the primary key ID
* @return the entity
*/
RolePersistence queryById(Long id);

/**
* Gets role list by user id.
*
* @param payload the payload
* @return the role list by user id
*/
GetRoleListByUserIdResponse getRoleListByUserId(GetRoleListByUserIdPayload payload);

/**
* Gets role list by user id.
*
* @param userId the user id
* @return the role list by user id
*/
List<RolePersistence> getRoleListByUserId(@NonNull Long userId);

/**
* Query all by limit
*
* @param offset the offset
* @param limit the limit
* @return the entity list
*/
List<RolePersistence> queryAllByLimit(int offset, int limit);

/**
* Insert
*
* @param rolePersistence the entity
* @return the entity
*/
RolePersistence insert(RolePersistence rolePersistence);

/**
* Update
*
* @param rolePersistence the entity
* @return the entity
*/
RolePersistence update(RolePersistence rolePersistence);

/**
* Delete by ID
*
* @param id the primary key ID
* @return the boolean
*/
boolean deleteById(Long id);
}
Loading

0 comments on commit a472f46

Please sign in to comment.