mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-11-04 22:57:12 +08:00
refactor: 优化项目模块命名(简化、分类、统一)
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.common.context;
|
||||
|
||||
import lombok.Data;
|
||||
import top.continew.admin.common.enums.DataScopeEnum;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 角色上下文
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/3/7 22:08
|
||||
*/
|
||||
@Data
|
||||
public class RoleContext implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 角色编码
|
||||
*/
|
||||
private String code;
|
||||
|
||||
/**
|
||||
* 数据权限
|
||||
*/
|
||||
private DataScopeEnum dataScope;
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.common.context;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import top.continew.admin.common.constant.SysConstants;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 用户上下文
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2024/10/9 20:29
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class UserContext implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 用户名
|
||||
*/
|
||||
private String username;
|
||||
|
||||
/**
|
||||
* 部门 ID
|
||||
*/
|
||||
private Long deptId;
|
||||
|
||||
/**
|
||||
* 最后一次修改密码时间
|
||||
*/
|
||||
private LocalDateTime pwdResetTime;
|
||||
|
||||
/**
|
||||
* 登录时系统设置的密码过期天数
|
||||
*/
|
||||
private Integer passwordExpirationDays;
|
||||
|
||||
/**
|
||||
* 权限码集合
|
||||
*/
|
||||
private Set<String> permissions;
|
||||
|
||||
/**
|
||||
* 角色编码集合
|
||||
*/
|
||||
private Set<String> roleCodes;
|
||||
|
||||
/**
|
||||
* 角色集合
|
||||
*/
|
||||
private Set<RoleContext> roles;
|
||||
|
||||
public UserContext(Set<String> permissions, Set<RoleContext> roles, Integer passwordExpirationDays) {
|
||||
this.permissions = permissions;
|
||||
this.setRoles(roles);
|
||||
this.passwordExpirationDays = passwordExpirationDays;
|
||||
}
|
||||
|
||||
public void setRoles(Set<RoleContext> roles) {
|
||||
this.roles = roles;
|
||||
this.roleCodes = roles.stream().map(RoleContext::getCode).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为管理员
|
||||
*
|
||||
* @return true:是;false:否
|
||||
*/
|
||||
public boolean isAdmin() {
|
||||
if (CollUtil.isEmpty(roleCodes)) {
|
||||
return false;
|
||||
}
|
||||
return roleCodes.contains(SysConstants.ADMIN_ROLE_CODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码是否已过期
|
||||
*
|
||||
* @return 是否过期
|
||||
*/
|
||||
public boolean isPasswordExpired() {
|
||||
// 永久有效
|
||||
if (this.passwordExpirationDays == null || this.passwordExpirationDays <= SysConstants.NO) {
|
||||
return false;
|
||||
}
|
||||
// 初始密码(第三方登录用户)暂不提示修改
|
||||
if (this.pwdResetTime == null) {
|
||||
return false;
|
||||
}
|
||||
return this.pwdResetTime.plusDays(this.passwordExpirationDays).isBefore(LocalDateTime.now());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.common.context;
|
||||
|
||||
import cn.dev33.satoken.session.SaSession;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.convert.Convert;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
import top.continew.starter.core.util.ExceptionUtils;
|
||||
import top.continew.starter.extension.crud.service.CommonUserService;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* 用户上下文 Holder
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/24 12:58
|
||||
*/
|
||||
public class UserContextHolder {
|
||||
|
||||
private static final ThreadLocal<UserContext> CONTEXT_HOLDER = new ThreadLocal<>();
|
||||
private static final ThreadLocal<UserExtraContext> EXTRA_CONTEXT_HOLDER = new ThreadLocal<>();
|
||||
|
||||
private UserContextHolder() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上下文
|
||||
*
|
||||
* @param context 上下文
|
||||
*/
|
||||
public static void setContext(UserContext context) {
|
||||
setContext(context, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置上下文
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param isUpdate 是否更新
|
||||
*/
|
||||
public static void setContext(UserContext context, boolean isUpdate) {
|
||||
CONTEXT_HOLDER.set(context);
|
||||
if (isUpdate) {
|
||||
StpUtil.getSessionByLoginId(context.getId()).set(SaSession.USER, context);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上下文
|
||||
*
|
||||
* @return 上下文
|
||||
*/
|
||||
public static UserContext getContext() {
|
||||
UserContext context = CONTEXT_HOLDER.get();
|
||||
if (null == context) {
|
||||
context = StpUtil.getSession().getModel(SaSession.USER, UserContext.class);
|
||||
CONTEXT_HOLDER.set(context);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定用户的上下文
|
||||
*
|
||||
* @param userId 用户 ID
|
||||
* @return 上下文
|
||||
*/
|
||||
public static UserContext getContext(Long userId) {
|
||||
SaSession session = StpUtil.getSessionByLoginId(userId, false);
|
||||
if (null == session) {
|
||||
return null;
|
||||
}
|
||||
return session.getModel(SaSession.USER, UserContext.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置额外上下文
|
||||
*
|
||||
* @param context 额外上下文
|
||||
*/
|
||||
public static void setExtraContext(UserExtraContext context) {
|
||||
EXTRA_CONTEXT_HOLDER.set(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取额外上下文
|
||||
*
|
||||
* @return 额外上下文
|
||||
*/
|
||||
public static UserExtraContext getExtraContext() {
|
||||
UserExtraContext context = EXTRA_CONTEXT_HOLDER.get();
|
||||
if (null == context) {
|
||||
context = getExtraContext(StpUtil.getTokenValue());
|
||||
EXTRA_CONTEXT_HOLDER.set(context);
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取额外上下文
|
||||
*
|
||||
* @param token 令牌
|
||||
* @return 额外上下文
|
||||
*/
|
||||
public static UserExtraContext getExtraContext(String token) {
|
||||
UserExtraContext context = new UserExtraContext();
|
||||
context.setIp(Convert.toStr(StpUtil.getExtra(token, "ip")));
|
||||
context.setAddress(Convert.toStr(StpUtil.getExtra(token, "address")));
|
||||
context.setBrowser(Convert.toStr(StpUtil.getExtra(token, "browser")));
|
||||
context.setOs(Convert.toStr(StpUtil.getExtra(token, "os")));
|
||||
context.setLoginTime(Convert.toLocalDateTime(StpUtil.getExtra(token, "loginTime")));
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除上下文
|
||||
*/
|
||||
public static void clearContext() {
|
||||
CONTEXT_HOLDER.remove();
|
||||
EXTRA_CONTEXT_HOLDER.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户 ID
|
||||
*
|
||||
* @return 用户 ID
|
||||
*/
|
||||
public static Long getUserId() {
|
||||
return Optional.ofNullable(getContext()).map(UserContext::getId).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户名
|
||||
*
|
||||
* @return 用户名
|
||||
*/
|
||||
public static String getUsername() {
|
||||
return Optional.ofNullable(getContext()).map(UserContext::getUsername).orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户昵称
|
||||
*
|
||||
* @return 用户昵称
|
||||
*/
|
||||
public static String getNickname() {
|
||||
return getNickname(getUserId());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户昵称
|
||||
*
|
||||
* @param userId 登录用户 ID
|
||||
* @return 用户昵称
|
||||
*/
|
||||
public static String getNickname(Long userId) {
|
||||
return ExceptionUtils.exToNull(() -> SpringUtil.getBean(CommonUserService.class).getNicknameById(userId));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为管理员
|
||||
*
|
||||
* @return 是否为管理员
|
||||
*/
|
||||
public static Boolean isAdmin() {
|
||||
StpUtil.checkLogin();
|
||||
return getContext().isAdmin();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package top.continew.admin.common.context;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.servlet.JakartaServletUtil;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import top.continew.starter.core.util.ExceptionUtils;
|
||||
import top.continew.starter.core.util.IpUtils;
|
||||
import top.continew.starter.web.util.ServletUtils;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 用户额外上下文
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2024/10/9 20:29
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class UserExtraContext implements Serializable {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* IP
|
||||
*/
|
||||
private String ip;
|
||||
|
||||
/**
|
||||
* IP 归属地
|
||||
*/
|
||||
private String address;
|
||||
|
||||
/**
|
||||
* 浏览器
|
||||
*/
|
||||
private String browser;
|
||||
|
||||
/**
|
||||
* 操作系统
|
||||
*/
|
||||
private String os;
|
||||
|
||||
/**
|
||||
* 登录时间
|
||||
*/
|
||||
private LocalDateTime loginTime;
|
||||
|
||||
public UserExtraContext(HttpServletRequest request) {
|
||||
this.ip = JakartaServletUtil.getClientIP(request);
|
||||
this.address = ExceptionUtils.exToNull(() -> IpUtils.getIpv4Address(this.ip));
|
||||
this.setBrowser(ServletUtils.getBrowser(request));
|
||||
this.setLoginTime(LocalDateTime.now());
|
||||
this.setOs(StrUtil.subBefore(ServletUtils.getOs(request), " or", false));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user