|
|
|
package com.qs.serve.common.util;
|
|
|
|
|
|
|
|
import com.qs.serve.common.framework.security.model.LoginUser;
|
|
|
|
import com.qs.serve.common.framework.security.model.LoginUserType;
|
|
|
|
import com.qs.serve.common.model.consts.GySysConst;
|
|
|
|
import com.qs.serve.common.model.enums.HttpCode;
|
|
|
|
import com.qs.serve.modules.bms.entity.BmsSupplier;
|
|
|
|
import com.qs.serve.modules.bms.service.BmsSupplierService;
|
|
|
|
import com.qs.serve.modules.wx.entity.WxUser;
|
|
|
|
import com.qs.serve.modules.wx.service.WxUserService;
|
|
|
|
import lombok.experimental.UtilityClass;
|
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
|
import org.springframework.security.core.context.SecurityContext;
|
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author YenHex
|
|
|
|
* @since 2022/3/1
|
|
|
|
*/
|
|
|
|
@UtilityClass
|
|
|
|
public class AuthContextUtils {
|
|
|
|
|
|
|
|
private final static ThreadLocal<String> THREAD_TENANT = new ThreadLocal<>();
|
|
|
|
private final static ThreadLocal<Long> THREAD_SUPPLIER = new ThreadLocal<>();
|
|
|
|
|
|
|
|
public static String getAppId(){
|
|
|
|
return getAppId(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getAppId(boolean throwEx){
|
|
|
|
String appId = ServletUtils.getHeader(GySysConst.APP_ID_PROP);
|
|
|
|
if(throwEx&&StringUtils.isEmpty(appId)){
|
|
|
|
Assert.throwEx(HttpCode.LOGIN_ERR_4002);
|
|
|
|
}
|
|
|
|
return appId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取用户登录信息
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static LoginUser getLoginUser(){
|
|
|
|
return getLoginUser(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 系统用户ID
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static String getSysUserId(){
|
|
|
|
LoginUser loginUser = getLoginUser();
|
|
|
|
return loginUser.getUserId();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 判断是否超级管理员
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public static boolean isSupAdmin(){
|
|
|
|
return Objects.requireNonNull(getLoginUser(false))
|
|
|
|
.getTypeFlag().equals(LoginUserType.SYS_SUP_USER);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void setTenant(String value){THREAD_TENANT.set(value);}
|
|
|
|
|
|
|
|
public static String getTenant(){return THREAD_TENANT.get();}
|
|
|
|
|
|
|
|
public static void setSupplierId(String value){
|
|
|
|
if(value!=null){
|
|
|
|
try {
|
|
|
|
THREAD_SUPPLIER.set(Long.parseLong(value));
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static BmsSupplier getCurrentSupplier(){
|
|
|
|
Long supplierId = AuthContextUtils.getSupplierId();
|
|
|
|
BmsSupplierService supplierService = SpringUtils.getBean(BmsSupplierService.class);
|
|
|
|
WxUserService wxUserService = SpringUtils.getBean(WxUserService.class);
|
|
|
|
WxUser wxUser = wxUserService.getCurrentWxUser();
|
|
|
|
BmsSupplier supplier = supplierService.getById(supplierId);
|
|
|
|
boolean isOther = false;
|
|
|
|
if(CollectionUtil.isNotEmpty(supplier.getOtherUserIds())){
|
|
|
|
for (String otherUserId : supplier.getOtherUserIds()) {
|
|
|
|
if(wxUser.getSysUserId().equals(otherUserId)){
|
|
|
|
isOther = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(isOther && !supplier.getUserId().equals(wxUser.getSysUserId())){
|
|
|
|
Assert.throwEx("供应商无效或未授权");
|
|
|
|
}
|
|
|
|
if(supplier.getStopFlag().equals(1)){
|
|
|
|
Assert.throwEx("供应商已停用");
|
|
|
|
}
|
|
|
|
return supplier;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Long getSupplierId(){return THREAD_SUPPLIER.get();}
|
|
|
|
|
|
|
|
public static void removeTenant(){THREAD_TENANT.remove();}
|
|
|
|
|
|
|
|
public static LoginUser getLoginUser(boolean throwEx){
|
|
|
|
UserDetails details = getUserDetail(throwEx);
|
|
|
|
if(details instanceof LoginUser){
|
|
|
|
return (LoginUser) details;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static UserDetails getUserDetail(boolean throwEx){
|
|
|
|
SecurityContext ctx = SecurityContextHolder.getContext();
|
|
|
|
Authentication auth = ctx.getAuthentication();
|
|
|
|
if (auth != null) {
|
|
|
|
Object authPri = auth.getPrincipal();
|
|
|
|
if (authPri instanceof UserDetails) {
|
|
|
|
UserDetails details = (UserDetails) authPri;
|
|
|
|
return details;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(throwEx){
|
|
|
|
throw new UsernameNotFoundException("未登录或被登出");
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|