Skip to content

Commit

Permalink
feat: LoginChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
getrebuild committed Nov 15, 2024
1 parent dee861c commit aca04c1
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/main/java/com/rebuild/web/user/signup/LoginAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,10 @@ private void createLoginLog(HttpServletRequest request, ID user) {
OperatingSystem os = UA.getOperatingSystem();
if (os != null) {
uaSimple += " (" + os + ")";
if (os.getDeviceType() != null && os.getDeviceType() == DeviceType.MOBILE) uaSimple += " [Mobile]";
}

uaSimple += " [" + LoginChannel.parse(userAgent).name() + "]";
if (request.getAttribute(SK_TEMP_AUTH) != null) uaSimple += " [TempAuth]";
if (userAgent.contains("DingTalk")) uaSimple += " [DingTalk]";
if (userAgent.contains("wxwork")) uaSimple += " [WeCom]";

} catch (Exception ex) {
log.warn("Unknown User-Agent : {}", userAgent);
Expand All @@ -181,14 +179,14 @@ private void createLoginLog(HttpServletRequest request, ID user) {
final String ipAddr = StringUtils.defaultString(ServletUtils.getRemoteAddr(request), "127.0.0.1");
final String reqUrl = request.getRequestURL() == null ? "" : request.getRequestURL().toString();

final Record llog = EntityHelper.forNew(EntityHelper.LoginLog, UserService.SYSTEM_USER);
llog.setID("user", user);
llog.setString("ipAddr", ipAddr);
llog.setString("userAgent", uaSimple);
llog.setDate("loginTime", CalendarUtils.now());
final Record lgLog = EntityHelper.forNew(EntityHelper.LoginLog, UserService.SYSTEM_USER);
lgLog.setID("user", user);
lgLog.setString("ipAddr", ipAddr);
lgLog.setString("userAgent", uaSimple);
lgLog.setDate("loginTime", CalendarUtils.now());

TaskExecutors.queue(() -> {
Application.getCommonsService().create(llog);
Application.getCommonsService().create(lgLog);

User u = Application.getUserStore().getUser(user);
String uid = StringUtils.defaultString(u.getEmail(), u.getName());
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/com/rebuild/web/user/signup/LoginChannel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*!
Copyright (c) REBUILD <https://getrebuild.com/> and/or its owners. All rights reserved.
rebuild is dual-licensed under commercial and open source licenses (GPLv3).
See LICENSE and COMMERCIAL in the project root for license information.
*/

package com.rebuild.web.user.signup;

import eu.bitwalker.useragentutils.DeviceType;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

/**
* 登录渠道/类型
*
* @author devezhao
* @since 2024/11/15
*/
@Slf4j
public enum LoginChannel {

PC_WEB("PC浏览器"),
PC_DINGTALK("PC钉钉"),
PC_WECOM("PC企业微信"),
PC_WECHAT("PC微信"),
PC_DESKTOP("PC桌面"),

MOB_WEB("手机浏览器"),
MOB_DINGTALK("手机钉钉"),
MOB_WECOM("手机企业微信"),
MOB_WECHAT("手机微信"),
MOB_ANDROID("手机APP"),
MOB_IOS("iOS APP"), // 保留

;

@Getter
private final String name;
LoginChannel(String name) {
this.name = name;
}

/**
* @param userAgent
* @return
*/
public static LoginChannel parse(String userAgent) {
userAgent = userAgent.toUpperCase();
UserAgent UA = UserAgent.parseUserAgentString(userAgent);
OperatingSystem OS = UA.getOperatingSystem();

boolean isDingtalk = userAgent.contains("DINGTALK");
boolean isWecom = userAgent.contains("WXWORK");
boolean isWechat = !isWecom && userAgent.contains("MICROMESSENGER");
boolean isH5PlusApp = userAgent.contains("HTML5PLUS");

if (OS != null && OS.getDeviceType() == DeviceType.MOBILE) {
if (isDingtalk) return MOB_DINGTALK;
else if (isWecom) return MOB_WECOM;
else if (isWechat) return MOB_WECHAT;
else if (isH5PlusApp) return MOB_ANDROID;
return MOB_WEB;
}

boolean isDesktop = userAgent.contains("ELECTRON");

if (isDingtalk) return PC_DINGTALK;
else if (isWecom) return PC_WECOM;
else if (isWechat) return PC_WECHAT;
else if (isDesktop) return PC_DESKTOP;
return PC_WEB;
}
}

0 comments on commit aca04c1

Please sign in to comment.