用户权限重构
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
package com.zyplayer.doc.data.config.security;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 登录用户信息
|
||||
@@ -13,7 +13,7 @@ public class DocUserDetails {
|
||||
private String username;
|
||||
private String password;
|
||||
private boolean enabled;
|
||||
private Set<String> authorities;
|
||||
private List<UserAuthVo> userAuthList;
|
||||
|
||||
public Long getUserId() {
|
||||
return userId;
|
||||
@@ -47,12 +47,12 @@ public class DocUserDetails {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public Set<String> getAuthorities() {
|
||||
return authorities;
|
||||
public List<UserAuthVo> getUserAuthList() {
|
||||
return userAuthList;
|
||||
}
|
||||
|
||||
public void setAuthorities(Set<String> authorities) {
|
||||
this.authorities = authorities;
|
||||
public void setUserAuthList(List<UserAuthVo> userAuthList) {
|
||||
this.userAuthList = userAuthList;
|
||||
}
|
||||
|
||||
public DocUserDetails(Long userId, String username) {
|
||||
@@ -68,13 +68,13 @@ public class DocUserDetails {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public DocUserDetails(Long userId, String username, String password, boolean enabled, Set<String> authorities) {
|
||||
public DocUserDetails(Long userId, String username, String password, boolean enabled, List<UserAuthVo> userAuthList) {
|
||||
super();
|
||||
this.userId = userId;
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.enabled = enabled;
|
||||
this.authorities = authorities;
|
||||
this.userAuthList = userAuthList;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -84,7 +84,7 @@ public class DocUserDetails {
|
||||
", username='" + username + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
", enabled=" + enabled +
|
||||
", authorities=" + authorities +
|
||||
", userAuthList=" + userAuthList +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,10 @@ package com.zyplayer.doc.data.config.security;
|
||||
import com.zyplayer.doc.data.utils.CachePrefix;
|
||||
import com.zyplayer.doc.data.utils.CacheUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 用户工具类
|
||||
@@ -19,8 +22,17 @@ public class DocUserUtil {
|
||||
DocUserUtil.ACCESS_TOKEN.set(accessToken);
|
||||
}
|
||||
|
||||
public static boolean haveCustomAuth(String authName, String suffix) {
|
||||
return haveAuth(authName + suffix);
|
||||
public static boolean haveCustomAuth(String authName, Integer sysType, Integer sysModuleType, Long sysModuleId) {
|
||||
DocUserDetails currentUser = getCurrentUser();
|
||||
if (currentUser == null) {
|
||||
return false;
|
||||
}
|
||||
return currentUser.getUserAuthList().stream().anyMatch(auth ->
|
||||
Objects.equals(auth.getAuthCode(), authName)
|
||||
&& Objects.equals(auth.getSysType(), sysType)
|
||||
&& Objects.equals(auth.getSysModuleType(), sysModuleType)
|
||||
&& Objects.equals(auth.getSysModuleId(), sysModuleId)
|
||||
);
|
||||
}
|
||||
|
||||
public static boolean haveAuth(String... authNames) {
|
||||
@@ -28,8 +40,9 @@ public class DocUserUtil {
|
||||
if (currentUser == null) {
|
||||
return false;
|
||||
}
|
||||
Set<String> authCodeSet = currentUser.getUserAuthList().stream().map(UserAuthVo::getAuthCode).collect(Collectors.toSet());
|
||||
for (String authName : authNames) {
|
||||
if (!currentUser.getAuthorities().contains(authName)) {
|
||||
if (!authCodeSet.contains(authName)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -64,12 +77,12 @@ public class DocUserUtil {
|
||||
/**
|
||||
* 设置当前用户权限
|
||||
*/
|
||||
public static void setUserAuth(Long userId, Set<String> userAuthSet) {
|
||||
public static void setUserAuth(Long userId, List<UserAuthVo> userAuthList) {
|
||||
String userToken = CacheUtil.get(CachePrefix.LOGIN_USER_ID_TOKEN + userId);
|
||||
if (userToken != null) {
|
||||
DocUserDetails docUser = CacheUtil.get(userToken);
|
||||
if (docUser != null) {
|
||||
docUser.setAuthorities(userAuthSet);
|
||||
docUser.setUserAuthList(userAuthList);
|
||||
CacheUtil.put(userToken, docUser);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package com.zyplayer.doc.data.config.security;
|
||||
|
||||
import com.zyplayer.doc.data.repository.manage.entity.UserAuth;
|
||||
import com.zyplayer.doc.data.repository.support.consts.DocSysModuleType;
|
||||
import com.zyplayer.doc.data.repository.support.consts.DocSysType;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* 用户权限表
|
||||
* </p>
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019-05-31
|
||||
*/
|
||||
public class UserAuthVo implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public UserAuthVo() {
|
||||
|
||||
}
|
||||
|
||||
public UserAuthVo(UserAuth userAuth) {
|
||||
this.authId = userAuth.getAuthId();
|
||||
this.sysType = userAuth.getSysType();
|
||||
this.sysModuleType = userAuth.getSysModuleType();
|
||||
this.sysModuleId = userAuth.getSysModuleId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 权限ID
|
||||
*/
|
||||
private Long authId;
|
||||
|
||||
/**
|
||||
* 权限code
|
||||
*/
|
||||
private String authCode;
|
||||
|
||||
/**
|
||||
* 系统类型,{@link DocSysType}
|
||||
*/
|
||||
private Integer sysType;
|
||||
|
||||
/**
|
||||
* 系统模块类型,{@link DocSysModuleType}
|
||||
*/
|
||||
private Integer sysModuleType;
|
||||
|
||||
/**
|
||||
* 系统模块ID
|
||||
*/
|
||||
private Long sysModuleId;
|
||||
|
||||
public Long getAuthId() {
|
||||
return authId;
|
||||
}
|
||||
|
||||
public void setAuthId(Long authId) {
|
||||
this.authId = authId;
|
||||
}
|
||||
|
||||
public Integer getSysType() {
|
||||
return sysType;
|
||||
}
|
||||
|
||||
public void setSysType(Integer sysType) {
|
||||
this.sysType = sysType;
|
||||
}
|
||||
|
||||
public Integer getSysModuleType() {
|
||||
return sysModuleType;
|
||||
}
|
||||
|
||||
public void setSysModuleType(Integer sysModuleType) {
|
||||
this.sysModuleType = sysModuleType;
|
||||
}
|
||||
|
||||
public Long getSysModuleId() {
|
||||
return sysModuleId;
|
||||
}
|
||||
|
||||
public void setSysModuleId(Long sysModuleId) {
|
||||
this.sysModuleId = sysModuleId;
|
||||
}
|
||||
|
||||
public String getAuthCode() {
|
||||
return authCode;
|
||||
}
|
||||
|
||||
public void setAuthCode(String authCode) {
|
||||
this.authCode = authCode;
|
||||
}
|
||||
}
|
||||
@@ -59,9 +59,19 @@ public class UserAuth implements Serializable {
|
||||
private Date updateTime;
|
||||
|
||||
/**
|
||||
* 自定义权限结尾
|
||||
* 系统类型,DocSysType
|
||||
*/
|
||||
private String authCustomSuffix;
|
||||
private Integer sysType;
|
||||
|
||||
/**
|
||||
* 系统模块类型,DocSysModuleType
|
||||
*/
|
||||
private Integer sysModuleType;
|
||||
|
||||
/**
|
||||
* 系统模块ID
|
||||
*/
|
||||
private Long sysModuleId;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
@@ -119,14 +129,31 @@ public class UserAuth implements Serializable {
|
||||
public void setUpdateTime(Date updateTime) {
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
public String getAuthCustomSuffix() {
|
||||
return authCustomSuffix;
|
||||
|
||||
public Integer getSysType() {
|
||||
return sysType;
|
||||
}
|
||||
|
||||
public void setAuthCustomSuffix(String authCustomSuffix) {
|
||||
this.authCustomSuffix = authCustomSuffix;
|
||||
|
||||
public void setSysType(Integer sysType) {
|
||||
this.sysType = sysType;
|
||||
}
|
||||
|
||||
|
||||
public Integer getSysModuleType() {
|
||||
return sysModuleType;
|
||||
}
|
||||
|
||||
public void setSysModuleType(Integer sysModuleType) {
|
||||
this.sysModuleType = sysModuleType;
|
||||
}
|
||||
|
||||
public Long getSysModuleId() {
|
||||
return sysModuleId;
|
||||
}
|
||||
|
||||
public void setSysModuleId(Long sysModuleId) {
|
||||
this.sysModuleId = sysModuleId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "UserAuth{" +
|
||||
@@ -138,7 +165,6 @@ public class UserAuth implements Serializable {
|
||||
", delFlag=" + delFlag +
|
||||
", creationTime=" + creationTime +
|
||||
", updateTime=" + updateTime +
|
||||
", authCustomSuffix=" + authCustomSuffix +
|
||||
"}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
package com.zyplayer.doc.data.repository.support.consts;
|
||||
|
||||
/**
|
||||
* 系统模块类型枚举
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2020-06-26
|
||||
*/
|
||||
public class DocSysModuleType {
|
||||
public static enum Manage {
|
||||
USER_MANAGE(1, "用户管理权限"),
|
||||
;
|
||||
|
||||
Manage(int type, String desc) {
|
||||
this.type = type;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
private final int type;
|
||||
private final String desc;
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
public static enum Wiki {
|
||||
PAGE(1, "空间"),
|
||||
;
|
||||
|
||||
Wiki(int type, String desc) {
|
||||
this.type = type;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
private final int type;
|
||||
private final String desc;
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
public static enum Db {
|
||||
DATASOURCE(1, "数据源管理"),
|
||||
;
|
||||
|
||||
Db(int type, String desc) {
|
||||
this.type = type;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
private final int type;
|
||||
private final String desc;
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
|
||||
public static enum Api {
|
||||
DOC(1, "api文档管理"),
|
||||
;
|
||||
|
||||
Api(int type, String desc) {
|
||||
this.type = type;
|
||||
this.desc = desc;
|
||||
}
|
||||
|
||||
private final int type;
|
||||
private final String desc;
|
||||
|
||||
public int getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,12 +6,12 @@ package com.zyplayer.doc.data.repository.support.consts;
|
||||
* @author 暮光:城中城
|
||||
* @since 2020-06-26
|
||||
*/
|
||||
public enum UserMsgSysType {
|
||||
public enum DocSysType {
|
||||
// 系统类型 1=manage 2=wiki 3=db
|
||||
MANAGE(1), WIKI(2), DB(2),
|
||||
MANAGE(1), WIKI(2), DB(3),
|
||||
;
|
||||
|
||||
UserMsgSysType(int type) {
|
||||
DocSysType(int type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
package com.zyplayer.doc.data.service.manage;
|
||||
|
||||
import com.zyplayer.doc.data.repository.manage.entity.UserAuth;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.zyplayer.doc.data.config.security.UserAuthVo;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.UserAuth;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
@@ -15,5 +16,5 @@ import java.util.Set;
|
||||
*/
|
||||
public interface UserAuthService extends IService<UserAuth> {
|
||||
|
||||
Set<String> getUserAuthSet(Long id);
|
||||
List<UserAuthVo> getUserAuthSet(Long id);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.zyplayer.doc.data.service.manage;
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.zyplayer.doc.data.config.security.DocUserDetails;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.UserMessage;
|
||||
import com.zyplayer.doc.data.repository.support.consts.UserMsgSysType;
|
||||
import com.zyplayer.doc.data.repository.support.consts.DocSysType;
|
||||
import com.zyplayer.doc.data.repository.support.consts.UserMsgType;
|
||||
|
||||
/**
|
||||
@@ -18,5 +18,5 @@ public interface UserMessageService extends IService<UserMessage> {
|
||||
|
||||
void addWikiMessage(UserMessage userMessage);
|
||||
|
||||
UserMessage createUserMessage(DocUserDetails currentUser, Long pageId, String dataDesc, UserMsgSysType sysType, UserMsgType msgType);
|
||||
UserMessage createUserMessage(DocUserDetails currentUser, Long pageId, String dataDesc, DocSysType sysType, UserMsgType msgType);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,13 @@ package com.zyplayer.doc.data.service.manage.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zyplayer.doc.data.config.security.UserAuthVo;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.AuthInfo;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.UserAuth;
|
||||
import com.zyplayer.doc.data.repository.manage.mapper.UserAuthMapper;
|
||||
import com.zyplayer.doc.data.service.manage.AuthInfoService;
|
||||
import com.zyplayer.doc.data.service.manage.UserAuthService;
|
||||
import org.apache.commons.collections.CollectionUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
@@ -28,20 +30,21 @@ public class UserAuthServiceImpl extends ServiceImpl<UserAuthMapper, UserAuth> i
|
||||
AuthInfoService authInfoService;
|
||||
|
||||
@Override
|
||||
public Set<String> getUserAuthSet(Long id) {
|
||||
public List<UserAuthVo> getUserAuthSet(Long id) {
|
||||
QueryWrapper<UserAuth> authWrapper = new QueryWrapper<>();
|
||||
authWrapper.eq("user_id", id).eq("del_flag", "0");
|
||||
List<UserAuth> userAuthList = this.list(authWrapper);
|
||||
Set<String> userAuthSet = Collections.emptySet();
|
||||
if (userAuthList != null && userAuthList.size() > 0) {
|
||||
List<Long> authIdList = userAuthList.stream().map(UserAuth::getAuthId).collect(Collectors.toList());
|
||||
Collection<AuthInfo> authInfoList = authInfoService.listByIds(authIdList);
|
||||
Map<Long, String> authNameMap = authInfoList.stream().collect(Collectors.toMap(AuthInfo::getId, AuthInfo::getAuthName));
|
||||
userAuthSet = userAuthList.stream().map(val -> {
|
||||
String authName = Optional.ofNullable(authNameMap.get(val.getAuthId())).orElse("");
|
||||
return authName + Optional.ofNullable(val.getAuthCustomSuffix()).orElse("");
|
||||
}).collect(Collectors.toSet());
|
||||
if (CollectionUtils.isEmpty(userAuthList)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return userAuthSet;
|
||||
List<Long> authIdList = userAuthList.stream().map(UserAuth::getAuthId).collect(Collectors.toList());
|
||||
Collection<AuthInfo> authInfoList = authInfoService.listByIds(authIdList);
|
||||
Map<Long, String> authNameMap = authInfoList.stream().collect(Collectors.toMap(AuthInfo::getId, AuthInfo::getAuthName));
|
||||
// 组装
|
||||
List<UserAuthVo> userAuthVoList = userAuthList.stream().map(UserAuthVo::new).collect(Collectors.toList());
|
||||
for (UserAuthVo userAuthVo : userAuthVoList) {
|
||||
userAuthVo.setAuthCode(authNameMap.get(userAuthVo.getAuthId()));
|
||||
}
|
||||
return userAuthVoList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.zyplayer.doc.data.config.security.DocUserDetails;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.UserMessage;
|
||||
import com.zyplayer.doc.data.repository.manage.mapper.UserMessageMapper;
|
||||
import com.zyplayer.doc.data.repository.support.consts.UserMsgSysType;
|
||||
import com.zyplayer.doc.data.repository.support.consts.DocSysType;
|
||||
import com.zyplayer.doc.data.repository.support.consts.UserMsgType;
|
||||
import com.zyplayer.doc.data.service.manage.UserMessageService;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -24,7 +24,7 @@ import java.util.Objects;
|
||||
public class UserMessageServiceImpl extends ServiceImpl<UserMessageMapper, UserMessage> implements UserMessageService {
|
||||
|
||||
@Override
|
||||
public UserMessage createUserMessage(DocUserDetails currentUser, Long dataId, String dataDesc, UserMsgSysType sysType, UserMsgType msgType) {
|
||||
public UserMessage createUserMessage(DocUserDetails currentUser, Long dataId, String dataDesc, DocSysType sysType, UserMsgType msgType) {
|
||||
UserMessage userMessage = new UserMessage();
|
||||
userMessage.setDataId(dataId);
|
||||
userMessage.setDataDesc(dataDesc);
|
||||
|
||||
@@ -8,7 +8,7 @@ import com.zyplayer.doc.data.config.security.DocUserUtil;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.UserMessage;
|
||||
import com.zyplayer.doc.data.repository.manage.entity.WikiPage;
|
||||
import com.zyplayer.doc.data.repository.manage.mapper.WikiPageMapper;
|
||||
import com.zyplayer.doc.data.repository.support.consts.UserMsgSysType;
|
||||
import com.zyplayer.doc.data.repository.support.consts.DocSysType;
|
||||
import com.zyplayer.doc.data.repository.support.consts.UserMsgType;
|
||||
import com.zyplayer.doc.data.service.manage.UserMessageService;
|
||||
import com.zyplayer.doc.data.service.manage.WikiPageService;
|
||||
@@ -54,7 +54,7 @@ public class WikiPageServiceImpl extends ServiceImpl<WikiPageMapper, WikiPage> i
|
||||
// 给相关人发送消息
|
||||
WikiPage wikiPageSel = this.getById(wikiPage.getId());
|
||||
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
|
||||
UserMessage userMessage = userMessageService.createUserMessage(currentUser, wikiPageSel.getId(), wikiPageSel.getName(), UserMsgSysType.WIKI, UserMsgType.WIKI_PAGE_PARENT);
|
||||
UserMessage userMessage = userMessageService.createUserMessage(currentUser, wikiPageSel.getId(), wikiPageSel.getName(), DocSysType.WIKI, UserMsgType.WIKI_PAGE_PARENT);
|
||||
userMessage.setAffectUserId(wikiPageSel.getCreateUserId());
|
||||
userMessage.setAffectUserName(wikiPageSel.getCreateUserName());
|
||||
userMessageService.addWikiMessage(userMessage);
|
||||
@@ -64,7 +64,7 @@ public class WikiPageServiceImpl extends ServiceImpl<WikiPageMapper, WikiPage> i
|
||||
public void deletePage(WikiPage wikiPage) {
|
||||
// 给相关人发送消息
|
||||
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
|
||||
UserMessage userMessage = userMessageService.createUserMessage(currentUser, wikiPage.getId(), wikiPage.getName(), UserMsgSysType.WIKI, UserMsgType.WIKI_PAGE_DELETE);
|
||||
UserMessage userMessage = userMessageService.createUserMessage(currentUser, wikiPage.getId(), wikiPage.getName(), DocSysType.WIKI, UserMsgType.WIKI_PAGE_DELETE);
|
||||
userMessage.setAffectUserId(wikiPage.getCreateUserId());
|
||||
userMessage.setAffectUserName(wikiPage.getCreateUserName());
|
||||
userMessageService.addWikiMessage(userMessage);
|
||||
|
||||
Reference in New Issue
Block a user