From 74683e1fb1c068e1ae3c0a832f708e7727682b62 Mon Sep 17 00:00:00 2001 From: tanghc Date: Wed, 27 Mar 2019 20:19:03 +0800 Subject: [PATCH] =?UTF-8?q?ISV=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sop-admin/sop-admin-server/sop.sql | 79 ++++++++++++++++++ .../gitee/sop/adminserver/api/isv/IsvApi.java | 81 ++++++++++++++++--- .../api/isv/param/IsvInfoForm.java | 6 +- .../sop/adminserver/api/isv/result/IsvVO.java | 2 + .../adminserver/api/isv/result/RoleVO.java | 16 ++++ .../sop/adminserver/entity/PermIsvRole.java | 40 +++++++++ .../sop/adminserver/entity/PermRole.java | 40 +++++++++ .../entity/PermRolePermission.java | 40 +++++++++ .../sop/adminserver/mapper/IsvInfoMapper.java | 1 + .../adminserver/mapper/PermIsvRoleMapper.java | 12 +++ .../adminserver/mapper/PermRoleMapper.java | 12 +++ .../mapper/PermRolePermissionMapper.java | 12 +++ .../sop/adminserver/service/PermService.java | 34 ++++++++ 13 files changed, 360 insertions(+), 15 deletions(-) create mode 100644 sop-admin/sop-admin-server/sop.sql create mode 100644 sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/result/RoleVO.java create mode 100644 sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermIsvRole.java create mode 100644 sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermRole.java create mode 100644 sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermRolePermission.java create mode 100644 sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermIsvRoleMapper.java create mode 100644 sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermRoleMapper.java create mode 100644 sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermRolePermissionMapper.java create mode 100644 sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/PermService.java diff --git a/sop-admin/sop-admin-server/sop.sql b/sop-admin/sop-admin-server/sop.sql new file mode 100644 index 00000000..341f589b --- /dev/null +++ b/sop-admin/sop-admin-server/sop.sql @@ -0,0 +1,79 @@ +/* + Navicat Premium Data Transfer + + Source Server : mysql-localhost + Source Server Type : MySQL + Source Server Version : 50724 + Source Host : localhost:3306 + Source Schema : sop + + Target Server Type : MySQL + Target Server Version : 50724 + File Encoding : 65001 + + Date: 27/03/2019 20:16:41 +*/ + +SET NAMES utf8mb4; +SET FOREIGN_KEY_CHECKS = 0; + +-- ---------------------------- +-- Table structure for isv_info +-- ---------------------------- +DROP TABLE IF EXISTS `isv_info`; +CREATE TABLE `isv_info` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `app_key` varchar(100) NOT NULL COMMENT 'appKey', + `secret` varchar(200) NOT NULL COMMENT 'secret', + `pub_key` text COMMENT '公钥', + `pri_key` text COMMENT '私钥', + `status` tinyint(4) unsigned NOT NULL DEFAULT '0' COMMENT '0启用,1禁用', + `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_app_key` (`app_key`) +) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='isv信息表'; + +-- ---------------------------- +-- Table structure for perm_isv_role +-- ---------------------------- +DROP TABLE IF EXISTS `perm_isv_role`; +CREATE TABLE `perm_isv_role` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `isv_info_id` bigint(20) NOT NULL COMMENT 'isv_info.id', + `role_code` varchar(50) NOT NULL COMMENT '角色code', + `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_user_role` (`isv_info_id`,`role_code`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='isv角色'; + +-- ---------------------------- +-- Table structure for perm_role +-- ---------------------------- +DROP TABLE IF EXISTS `perm_role`; +CREATE TABLE `perm_role` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `role_code` varchar(50) NOT NULL COMMENT '角色代码', + `description` varchar(50) NOT NULL COMMENT '角色描述', + `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_code` (`role_code`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色表'; + +-- ---------------------------- +-- Table structure for perm_role_permission +-- ---------------------------- +DROP TABLE IF EXISTS `perm_role_permission`; +CREATE TABLE `perm_role_permission` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `role_code` varchar(50) NOT NULL COMMENT '角色表code', + `route_id` bigint(20) NOT NULL COMMENT 'api_id', + `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `uk_role_perm` (`role_code`,`route_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='角色权限表'; + +SET FOREIGN_KEY_CHECKS = 1; diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/IsvApi.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/IsvApi.java index e513c96d..095f2144 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/IsvApi.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/IsvApi.java @@ -11,23 +11,29 @@ import com.gitee.fastmybatis.core.PageInfo; import com.gitee.fastmybatis.core.query.Query; import com.gitee.fastmybatis.core.support.PageEasyui; import com.gitee.fastmybatis.core.util.MapperUtil; -import com.gitee.sop.adminserver.api.isv.param.IsvInfoForm; import com.gitee.sop.adminserver.api.isv.param.IsvInfoFormAdd; import com.gitee.sop.adminserver.api.isv.param.IsvInfoFormUpdate; import com.gitee.sop.adminserver.api.isv.param.IsvPageParam; import com.gitee.sop.adminserver.api.isv.result.AppKeySecretVo; import com.gitee.sop.adminserver.api.isv.result.IsvVO; import com.gitee.sop.adminserver.api.isv.result.PubPriVo; +import com.gitee.sop.adminserver.api.isv.result.RoleVO; import com.gitee.sop.adminserver.common.IdGen; import com.gitee.sop.adminserver.entity.IsvInfo; +import com.gitee.sop.adminserver.entity.PermIsvRole; +import com.gitee.sop.adminserver.entity.PermRole; import com.gitee.sop.adminserver.mapper.IsvInfoMapper; -import com.google.common.collect.Sets; +import com.gitee.sop.adminserver.mapper.PermIsvRoleMapper; +import com.gitee.sop.adminserver.mapper.PermRoleMapper; +import com.gitee.sop.adminserver.service.PermService; +import org.apache.commons.collections.CollectionUtils; import org.springframework.beans.factory.annotation.Autowired; import java.text.SimpleDateFormat; -import java.util.ArrayList; +import java.util.Collections; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; /** * @author tanghc @@ -39,6 +45,15 @@ public class IsvApi { @Autowired IsvInfoMapper isvInfoMapper; + @Autowired + PermIsvRoleMapper permIsvRoleMapper; + + @Autowired + PermRoleMapper permRoleMapper; + + @Autowired + PermService permService; + @Api(name = "isv.info.page") @ApiDocMethod(description = "接入方列表") PageEasyui pageIsv(IsvPageParam param) { @@ -46,14 +61,14 @@ public class IsvApi { PageInfo pageInfo = MapperUtil.query(isvInfoMapper, query); List list = pageInfo.getList(); - List retList = new ArrayList<>(list.size()); - - for (IsvInfo permClient : list) { - IsvVO vo = new IsvVO(); - CopyUtil.copyProperties(permClient, vo); - //vo.setRoleList(this.buildClientRole(permClient)); - retList.add(vo); - } + List retList = list.stream() + .map(isvInfo -> { + IsvVO vo = new IsvVO(); + CopyUtil.copyProperties(isvInfo, vo); + vo.setRoleList(this.buildIsvRole(isvInfo)); + return vo; + }) + .collect(Collectors.toList()); PageEasyui pageInfoRet = new PageEasyui<>(); pageInfoRet.setTotal(pageInfo.getTotal()); @@ -62,6 +77,28 @@ public class IsvApi { return pageInfoRet; } + /** + * 构建ISV拥有的角色 + * + * @param permClient + * @return + */ + List buildIsvRole(IsvInfo permClient) { + List roleCodeList = permService.listClientRoleCode(permClient.getId()); + if (CollectionUtils.isEmpty(roleCodeList)) { + return Collections.emptyList(); + } + List list = permRoleMapper.list(new Query().in("role_code", roleCodeList)); + + return list.stream() + .map(permRole -> { + RoleVO vo = new RoleVO(); + CopyUtil.copyProperties(permRole, vo); + return vo; + }) + .collect(Collectors.toList()); + } + @Api(name = "isv.info.add") @ApiDocMethod(description = "添加isv") void addIsv(IsvInfoFormAdd param) { @@ -69,7 +106,7 @@ public class IsvApi { CopyUtil.copyPropertiesIgnoreNull(param, rec); isvInfoMapper.saveIgnoreNull(rec); -// this.saveClientRole(rec, param.getRoleCode()); + this.saveClientRole(rec, param.getRoleCode()); // TODO:发送消息队列到zookeeper } @@ -80,7 +117,7 @@ public class IsvApi { CopyUtil.copyPropertiesIgnoreNull(param, rec); isvInfoMapper.updateIgnoreNull(rec); -// this.saveClientRole(rec, param.getRoleCode()); + this.saveClientRole(rec, param.getRoleCode()); // syncService.syncAppSecretConfig(Sets.newHashSet(param.getApp())); // TODO:发送消息队列到zookeeper @@ -106,4 +143,22 @@ public class IsvApi { vo.setSecret(secret); return vo; } + + void saveClientRole(IsvInfo isvInfo, List roleCodeList) { + Query query = new Query(); + long isvInfoId = isvInfo.getId(); + query.eq("isv_info_id", isvInfoId); + permIsvRoleMapper.deleteByQuery(query); + + List tobeSaveList = roleCodeList.stream() + .map(roleCode -> { + PermIsvRole rec = new PermIsvRole(); + rec.setIsvInfoId(isvInfoId); + rec.setRoleCode(roleCode); + return rec; + }) + .collect(Collectors.toList()); + + permIsvRoleMapper.saveBatch(tobeSaveList); + } } diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/param/IsvInfoForm.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/param/IsvInfoForm.java index 74da1b7b..eb34c528 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/param/IsvInfoForm.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/param/IsvInfoForm.java @@ -5,6 +5,8 @@ import lombok.Data; import org.hibernate.validator.constraints.Length; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotEmpty; +import java.util.List; /** * @author tanghc @@ -31,6 +33,6 @@ public class IsvInfoForm { @ApiDocField(description = "状态:0:启用,1:禁用") private Byte status = 0; -// @NotEmpty(message = "角色不能为空") -// private List roleCode; + @NotEmpty(message = "角色不能为空") + private List roleCode; } diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/result/IsvVO.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/result/IsvVO.java index fab238cc..1de27271 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/result/IsvVO.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/result/IsvVO.java @@ -33,4 +33,6 @@ public class IsvVO { /** 数据库字段:gmt_modified */ private Date gmtModified; + + private List roleList; } diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/result/RoleVO.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/result/RoleVO.java new file mode 100644 index 00000000..2e26096f --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/api/isv/result/RoleVO.java @@ -0,0 +1,16 @@ +package com.gitee.sop.adminserver.api.isv.result; + +import lombok.Data; + +import java.util.Date; + +/** + * @author thc + */ +@Data +public class RoleVO { + private Long id; + private String roleCode; + private String description; + private Date gmtCreate; +} diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermIsvRole.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermIsvRole.java new file mode 100644 index 00000000..3bbe767d --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermIsvRole.java @@ -0,0 +1,40 @@ +package com.gitee.sop.adminserver.entity; + +import lombok.Data; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + + +/** + * 表名:perm_isv_role + * 备注:isv角色 + * + * @author tanghc + */ +@Table(name = "perm_isv_role") +@Data +public class PermIsvRole { + @Id + @Column(name = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + /** 数据库字段:id */ + private Long id; + + /** isv_info.id, 数据库字段:isv_info_id */ + private Long isvInfoId; + + /** 角色code, 数据库字段:role_code */ + private String roleCode; + + /** 数据库字段:gmt_create */ + private Date gmtCreate; + + /** 数据库字段:gmt_modified */ + private Date gmtModified; +} diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermRole.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermRole.java new file mode 100644 index 00000000..f8d502b3 --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermRole.java @@ -0,0 +1,40 @@ +package com.gitee.sop.adminserver.entity; + +import lombok.Data; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + + +/** + * 表名:perm_role + * 备注:角色表 + * + * @author tanghc + */ +@Table(name = "perm_role") +@Data +public class PermRole { + @Id + @Column(name = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + /** 数据库字段:id */ + private Long id; + + /** 角色代码, 数据库字段:role_code */ + private String roleCode; + + /** 角色描述, 数据库字段:description */ + private String description; + + /** 数据库字段:gmt_create */ + private Date gmtCreate; + + /** 数据库字段:gmt_modified */ + private Date gmtModified; +} diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermRolePermission.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermRolePermission.java new file mode 100644 index 00000000..587f3efb --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/entity/PermRolePermission.java @@ -0,0 +1,40 @@ +package com.gitee.sop.adminserver.entity; + +import lombok.Data; + +import java.util.Date; + +import javax.persistence.Column; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + + +/** + * 表名:perm_role_permission + * 备注:角色权限表 + * + * @author tanghc + */ +@Table(name = "perm_role_permission") +@Data +public class PermRolePermission { + @Id + @Column(name = "id") + @GeneratedValue(strategy = GenerationType.IDENTITY) + /** 数据库字段:id */ + private Long id; + + /** 角色表code, 数据库字段:role_code */ + private String roleCode; + + /** api_id, 数据库字段:route_id */ + private Long routeId; + + /** 数据库字段:gmt_create */ + private Date gmtCreate; + + /** 数据库字段:gmt_modified */ + private Date gmtModified; +} diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/IsvInfoMapper.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/IsvInfoMapper.java index 1f99cd2e..9747701c 100644 --- a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/IsvInfoMapper.java +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/IsvInfoMapper.java @@ -4,6 +4,7 @@ import com.gitee.fastmybatis.core.mapper.CrudMapper; import com.gitee.sop.adminserver.entity.IsvInfo; + /** * @author tanghc */ diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermIsvRoleMapper.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermIsvRoleMapper.java new file mode 100644 index 00000000..e5d4ac72 --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermIsvRoleMapper.java @@ -0,0 +1,12 @@ +package com.gitee.sop.adminserver.mapper; + +import com.gitee.fastmybatis.core.mapper.CrudMapper; + +import com.gitee.sop.adminserver.entity.PermIsvRole; + + +/** + * @author tanghc + */ +public interface PermIsvRoleMapper extends CrudMapper { +} diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermRoleMapper.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermRoleMapper.java new file mode 100644 index 00000000..754b1994 --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermRoleMapper.java @@ -0,0 +1,12 @@ +package com.gitee.sop.adminserver.mapper; + +import com.gitee.fastmybatis.core.mapper.CrudMapper; + +import com.gitee.sop.adminserver.entity.PermRole; + + +/** + * @author tanghc + */ +public interface PermRoleMapper extends CrudMapper { +} diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermRolePermissionMapper.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermRolePermissionMapper.java new file mode 100644 index 00000000..c40777a3 --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/mapper/PermRolePermissionMapper.java @@ -0,0 +1,12 @@ +package com.gitee.sop.adminserver.mapper; + +import com.gitee.fastmybatis.core.mapper.CrudMapper; + +import com.gitee.sop.adminserver.entity.PermRolePermission; + + +/** + * @author tanghc + */ +public interface PermRolePermissionMapper extends CrudMapper { +} diff --git a/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/PermService.java b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/PermService.java new file mode 100644 index 00000000..0ed8332b --- /dev/null +++ b/sop-admin/sop-admin-server/src/main/java/com/gitee/sop/adminserver/service/PermService.java @@ -0,0 +1,34 @@ +package com.gitee.sop.adminserver.service; + +import com.gitee.sop.adminserver.entity.PermIsvRole; +import com.gitee.sop.adminserver.mapper.PermIsvRoleMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author thc + */ +@Service +public class PermService { + @Autowired + PermIsvRoleMapper permClientRoleMapper; + + /** + * 获取客户端角色码列表 + * + * @param clientId + * @return + */ + public List listClientRoleCode(Long clientId) { + List list = permClientRoleMapper.listByColumn("client_id", clientId); + List retList = new ArrayList<>(list.size()); + for (PermIsvRole permClientRole : list) { + retList.add(permClientRole.getRoleCode()); + } + return retList; + } + +}