Commit b1dcc9f2 authored by Haixin HUANG(黄海新)'s avatar Haixin HUANG(黄海新) Committed by GitHub
Browse files

Merge pull request #1364 from WeBankPartners/1363_menu_config_sync

#1363 menu config sync
Showing with 136 additions and 99 deletions
+136 -99
package com.webank.wecube.platform.auth.server.controller;
import static com.webank.wecube.platform.auth.server.dto.CommonResponseDto.okay;
import static com.webank.wecube.platform.auth.server.dto.CommonResponseDto.okayWithData;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -16,46 +20,42 @@ import com.webank.wecube.platform.auth.server.common.ApplicationConstants;
import com.webank.wecube.platform.auth.server.dto.CommonResponseDto;
import com.webank.wecube.platform.auth.server.service.AuthorityRoleRelationshipService;
import static com.webank.wecube.platform.auth.server.dto.CommonResponseDto.okayWithData;
import java.util.List;
import static com.webank.wecube.platform.auth.server.dto.CommonResponseDto.okay;
@RestController
@RequestMapping(ApplicationConstants.ApiInfo.PREFIX_DEFAULT)
public class AuthorityRoleRelationshipController {
private static final Logger log = LoggerFactory.getLogger(AuthorityRoleRelationshipController.class);
@Autowired
AuthorityRoleRelationshipService authorityRoleRelationshipService;
@GetMapping("/authoritys/{authority-id}/roles")
@ResponseBody
public CommonResponseDto getRolesByAuthorityId(@PathVariable(value = "authority-id") Long authorityId) {
return okayWithData(authorityRoleRelationshipService.getRolesByAuthorityId(authorityId));
}
@GetMapping("/roles/{role-id}/authoritys")
@ResponseBody
public CommonResponseDto getAuthoritysByRoleId(@PathVariable(value = "role-id") String roleId) {
return okayWithData(authorityRoleRelationshipService.getAuthoritysByRoleId(roleId));
}
@PostMapping("/roles/{role-id}/authoritys")
@ResponseBody
public CommonResponseDto grantRoleForAuthoritys(@PathVariable(value = "role-id") String roleId,
@RequestBody List<Long> authorityIds) throws Exception {
authorityRoleRelationshipService.grantRoleForAuthoritys(roleId, authorityIds);
return okay();
}
@DeleteMapping("/roles/{role-id}/authoritys")
@ResponseBody
public CommonResponseDto revokeRoleForAuthoritys(@PathVariable(value = "role-id") String roleId,
@RequestBody List<Long> authorityIds) throws Exception {
authorityRoleRelationshipService.revokeRoleForAuthoritys(roleId, authorityIds);
return okay();
}
private static final Logger log = LoggerFactory.getLogger(AuthorityRoleRelationshipController.class);
@Autowired
AuthorityRoleRelationshipService authorityRoleRelationshipService;
@GetMapping("/authoritys/{authority-id}/roles")
@ResponseBody
public CommonResponseDto getRolesByAuthorityId(@PathVariable(value = "authority-id") Long authorityId) {
return okayWithData(authorityRoleRelationshipService.getRolesByAuthorityId(authorityId));
}
@GetMapping("/roles/{role-id}/authorities")
@ResponseBody
public CommonResponseDto getAuthoritysByRoleId(@PathVariable(value = "role-id") String roleId) {
return okayWithData(authorityRoleRelationshipService.getAuthoritysByRoleId(roleId));
}
@PostMapping("/roles/{role-id}/authorities/grant")
@ResponseBody
public CommonResponseDto grantRoleForAuthoritiesByCode(@PathVariable(value = "role-id") String roleId,
@RequestBody List<String> authorityCodes) throws Exception {
log.info("grant authorities to role:roleId={},authorityCodes={}", roleId, authorityCodes);
authorityRoleRelationshipService.grantRoleForAuthoritiesByCode(roleId, authorityCodes);
return okay();
}
@PostMapping("/roles/{role-id}/authorities/revoke")
@ResponseBody
public CommonResponseDto revokeRoleForAuthoritiesByCode(@PathVariable(value = "role-id") String roleId,
@RequestBody List<String> authorityCodes) throws Exception {
log.info("revoke authorities from role:roleId={},authorityCodes={}", roleId, authorityCodes);
authorityRoleRelationshipService.revokeRoleForAuthoritiesByCode(roleId, authorityCodes);
return okay();
}
}
......@@ -8,18 +8,11 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.collect.Lists;
import com.webank.wecube.platform.auth.server.entity.ApiRoleRelationshipEntity;
import com.webank.wecube.platform.auth.server.entity.AuthorityRoleRelationshipEntity;
import com.webank.wecube.platform.auth.server.entity.SysApiEntity;
import com.webank.wecube.platform.auth.server.entity.SysAuthorityEntity;
import com.webank.wecube.platform.auth.server.entity.SysRoleEntity;
import com.webank.wecube.platform.auth.server.entity.SysUserEntity;
import com.webank.wecube.platform.auth.server.entity.UserRoleRelationshipEntity;
import com.webank.wecube.platform.auth.server.repository.ApiRepository;
import com.webank.wecube.platform.auth.server.repository.ApiRoleRelationshipRepository;
import com.webank.wecube.platform.auth.server.repository.AuthorityRepository;
import com.webank.wecube.platform.auth.server.repository.AuthorityRoleRelationshipRepository;
import com.webank.wecube.platform.auth.server.repository.UserRepository;
import com.webank.wecube.platform.auth.server.repository.UserRoleRelationshipRepository;
@Service("authorityRoleRelationshipService")
public class AuthorityRoleRelationshipService {
......@@ -34,6 +27,9 @@ public class AuthorityRoleRelationshipService {
@Autowired
private AuthorityService authorityService;
@Autowired
private AuthorityRepository authorityRepository;
public List<SysAuthorityEntity> getAuthoritysByRoleId(String roleId) {
List<SysAuthorityEntity> authoritys = Lists.newArrayList();
authorityRoleRelationshipRepository.findByRoleId(roleId).forEach(authorityRole -> {
......@@ -50,23 +46,35 @@ public class AuthorityRoleRelationshipService {
return roles;
}
public void grantRoleForAuthoritys(String roleId, List<Long> authorityIds) throws Exception {
public void grantRoleForAuthoritiesByCode(String roleId, List<String> authorityCodes) throws Exception {
SysRoleEntity role = roleService.getRoleByIdIfExisted(roleId);
for (Long authorityId : authorityIds) {
SysAuthorityEntity authorityEntity = authorityService.getAuthorityByIdIfExisted(authorityId);
if (null == authorityRoleRelationshipRepository.findOneByAuthorityIdAndRoleId(authorityId, roleId))
for (String authorityCode : authorityCodes) {
SysAuthorityEntity authorityEntity = authorityService.getAuthorityByCode(authorityCode);
if (authorityEntity == null) {
SysAuthorityEntity authority = new SysAuthorityEntity();
authority.setCode(authorityCode);
authorityEntity = authorityRepository.save(authority);
}
if (authorityRoleRelationshipRepository.findOneByAuthorityIdAndRoleId(authorityEntity.getId(),
roleId) == null)
authorityRoleRelationshipRepository.save(new AuthorityRoleRelationshipEntity(authorityEntity, role));
}
}
public void revokeRoleForAuthoritys(String roleId, List<Long> authorityIds) throws Exception {
public void revokeRoleForAuthoritiesByCode(String roleId, List<String> authorityCodes) throws Exception {
roleService.getRoleByIdIfExisted(roleId);
for (Long authorityId : authorityIds) {
authorityService.getAuthorityByIdIfExisted(authorityId);
for (String authorityCode : authorityCodes) {
SysAuthorityEntity authorityEntity = authorityService.getAuthorityByCode(authorityCode);
if (authorityEntity == null) {
continue;
}
AuthorityRoleRelationshipEntity authorityRoleRelationshipEntity = authorityRoleRelationshipRepository
.findOneByAuthorityIdAndRoleId(authorityId, roleId);
if (null != authorityRoleRelationshipEntity)
.findOneByAuthorityIdAndRoleId(authorityEntity.getId(), roleId);
if (authorityRoleRelationshipEntity != null) {
authorityRoleRelationshipRepository.delete(authorityRoleRelationshipEntity);
}
}
}
......
......@@ -44,6 +44,11 @@ public class AuthorityService {
public void delete(Long id) {
authorityRepository.deleteById(id);
}
public SysAuthorityEntity getAuthorityByCode(String authrityCode){
SysAuthorityEntity entity = authorityRepository.findOneByCode(authrityCode);
return entity;
}
public SysAuthorityEntity getAuthorityByIdIfExisted(Long authorityId) throws Exception {
Optional<SysAuthorityEntity> authorityEntityOptional = authorityRepository.findById(authorityId);
......
......@@ -14,7 +14,7 @@ import java.util.Map;
* @author howechen
*/
@RestController
@RequestMapping("v1/")
@RequestMapping("/v1")
public class RoleManagementController {
private UserManagementServiceImpl userManagementService;
......
......@@ -14,7 +14,7 @@ import java.util.Map;
* @author howechen
*/
@RestController
@RequestMapping("v1/")
@RequestMapping("/v1")
public class UserManagementController {
private UserManagementServiceImpl userManagementService;
private RoleMenuServiceImpl roleMenuService;
......
package com.webank.wecube.platform.core.service.user;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.webank.wecube.platform.core.commons.ApplicationProperties;
import com.webank.wecube.platform.core.commons.WecubeCoreException;
import com.webank.wecube.platform.core.domain.MenuItem;
import com.webank.wecube.platform.core.domain.RoleMenu;
......@@ -7,20 +19,11 @@ import com.webank.wecube.platform.core.domain.plugin.PluginPackageMenu;
import com.webank.wecube.platform.core.dto.MenuItemDto;
import com.webank.wecube.platform.core.dto.user.RoleDto;
import com.webank.wecube.platform.core.dto.user.RoleMenuDto;
import com.webank.wecube.platform.core.http.UserJwtSsoTokenRestTemplate;
import com.webank.wecube.platform.core.jpa.MenuItemRepository;
import com.webank.wecube.platform.core.jpa.PluginPackageMenuRepository;
import com.webank.wecube.platform.core.jpa.user.RoleMenuRepository;
import com.webank.wecube.platform.core.utils.JsonUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author howechen
......@@ -30,21 +33,21 @@ import java.util.stream.Collectors;
public class RoleMenuServiceImpl implements RoleMenuService {
private static final Logger logger = LoggerFactory.getLogger(RoleMenuServiceImpl.class);
@Autowired
private RoleMenuRepository roleMenuRepository;
@Autowired
private MenuItemRepository menuItemRepository;
@Autowired
private PluginPackageMenuRepository pluginPackageMenuRepository;
@Autowired
private UserManagementServiceImpl userManagementService;
@Autowired
public RoleMenuServiceImpl(RoleMenuRepository roleMenuRepository,
MenuItemRepository menuItemRepository,
PluginPackageMenuRepository pluginPackageMenuRepository,
UserManagementServiceImpl userManagementService) {
this.roleMenuRepository = roleMenuRepository;
this.menuItemRepository = menuItemRepository;
this.pluginPackageMenuRepository = pluginPackageMenuRepository;
this.userManagementService = userManagementService;
}
private UserJwtSsoTokenRestTemplate userJwtSsoTokenRestTemplate;
@Autowired
private ApplicationProperties applicationProperties;
/**
* Retrieve role_menu table by given roleId
......@@ -106,8 +109,18 @@ public class RoleMenuServiceImpl implements RoleMenuService {
logger.info(String.format("Deleting menus: [%s]", needToDeleteList));
for (RoleMenu roleMenu : needToDeleteList) {
this.roleMenuRepository.deleteById(roleMenu.getId());
}
}
List<String> menuCodesToRevoke = new ArrayList<>();
for(RoleMenu rm : needToDeleteList){
menuCodesToRevoke.add("MENU_"+rm.getMenuCode());
}
///roles/{role-id}/authorities/revoke
String revokePath = String.format("auth/roles/%s/authorities/revoke", roleId);
userJwtSsoTokenRestTemplate.postForObject(String.format("http://%s/%s", applicationProperties.getGatewayUrl(),revokePath), menuCodesToRevoke, String.class);
// new menuCodeList - current menuCodeList = needToCreateList
List<String> needToCreateList;
......@@ -124,6 +137,14 @@ public class RoleMenuServiceImpl implements RoleMenuService {
logger.error(ex.getMessage());
throw new WecubeCoreException(ex.getMessage());
}
List<String> menuCodesToGrant = new ArrayList<>();
for(RoleMenu rm : batchUpdateList){
menuCodesToGrant.add("MENU_"+rm.getMenuCode());
}
String grantPath = String.format("auth/roles/%s/authorities/grant",roleId);
userJwtSsoTokenRestTemplate.postForObject(String.format("http://%s/%s", applicationProperties.getGatewayUrl(),grantPath), menuCodesToGrant, String.class);
}
}
......
package com.webank.wecube.platform.core.service.user;
import static com.webank.wecube.platform.core.domain.plugin.PluginPackage.Status.RUNNING;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.ExpectedCount;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import com.google.common.collect.Lists;
import com.webank.wecube.platform.core.DatabaseBasedTest;
import com.webank.wecube.platform.core.commons.ApplicationProperties;
......@@ -11,28 +34,8 @@ import com.webank.wecube.platform.core.dto.user.RoleMenuDto;
import com.webank.wecube.platform.core.jpa.MenuItemRepository;
import com.webank.wecube.platform.core.jpa.PluginPackageRepository;
import com.webank.wecube.platform.core.jpa.user.RoleMenuRepository;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.ExpectedCount;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import static com.webank.wecube.platform.core.domain.plugin.PluginPackage.Status.RUNNING;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
@Ignore
public class RoleMenuServiceTest extends DatabaseBasedTest {
static final String ROLE_ONE = "1";
......
Supports Markdown
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