From 0269eabcfeafe639564f0c9ee8dcd61149cb5a1f Mon Sep 17 00:00:00 2001 From: fit2cloud-chenyw Date: Wed, 7 Sep 2022 14:46:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E7=B3=BB=E7=BB=9F=E7=AE=A1=E7=90=86-?= =?UTF-8?q?=E7=B3=BB=E7=BB=9F=E5=8F=82=E6=95=B0):=20=E9=A3=9E=E4=B9=A6?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/dataease/auth/api/AuthApi.java | 4 +- .../io/dataease/auth/server/AuthServer.java | 4 +- .../auth/service/AuthUserService.java | 2 +- .../service/impl/AuthUserServiceImpl.java | 9 +- .../auth/service/impl/ShiroServiceImpl.java | 6 +- .../dataease/plugins/server/XLarkServer.java | 134 ++++++++++++++++++ .../dataease/service/sys/SysUserService.java | 23 +++ frontend/src/api/user.js | 4 +- frontend/src/assets/{fark.png => lark.png} | Bin frontend/src/views/login/index.vue | 12 +- frontend/src/views/system/SysParam/index.vue | 37 ++--- 11 files changed, 192 insertions(+), 43 deletions(-) create mode 100644 backend/src/main/java/io/dataease/plugins/server/XLarkServer.java rename frontend/src/assets/{fark.png => lark.png} (100%) diff --git a/backend/src/main/java/io/dataease/auth/api/AuthApi.java b/backend/src/main/java/io/dataease/auth/api/AuthApi.java index c4d6230f8c..e4b2df95b5 100644 --- a/backend/src/main/java/io/dataease/auth/api/AuthApi.java +++ b/backend/src/main/java/io/dataease/auth/api/AuthApi.java @@ -72,8 +72,8 @@ public interface AuthApi { boolean isOpenDingtalk(); @ApiOperation("是否开启飞书") - @PostMapping("/isOpenFark") - boolean isOpenFark(); + @PostMapping("/isOpenLark") + boolean isOpenLark(); @ApiIgnore @PostMapping("/isPluginLoaded") diff --git a/backend/src/main/java/io/dataease/auth/server/AuthServer.java b/backend/src/main/java/io/dataease/auth/server/AuthServer.java index bd7334851b..046015a14f 100644 --- a/backend/src/main/java/io/dataease/auth/server/AuthServer.java +++ b/backend/src/main/java/io/dataease/auth/server/AuthServer.java @@ -305,12 +305,12 @@ public class AuthServer implements AuthApi { } @Override - public boolean isOpenFark() { + public boolean isOpenLark() { Boolean licValid = PluginUtils.licValid(); if (!licValid) return false; - return authUserService.supportFark(); + return authUserService.supportLark(); } @Override diff --git a/backend/src/main/java/io/dataease/auth/service/AuthUserService.java b/backend/src/main/java/io/dataease/auth/service/AuthUserService.java index 483bab6553..d6a885b960 100644 --- a/backend/src/main/java/io/dataease/auth/service/AuthUserService.java +++ b/backend/src/main/java/io/dataease/auth/service/AuthUserService.java @@ -36,7 +36,7 @@ public interface AuthUserService { Boolean supportDingtalk(); - Boolean supportFark(); + Boolean supportLark(); Boolean pluginLoaded(); diff --git a/backend/src/main/java/io/dataease/auth/service/impl/AuthUserServiceImpl.java b/backend/src/main/java/io/dataease/auth/service/impl/AuthUserServiceImpl.java index 9fdb6c807c..825dd7ff03 100644 --- a/backend/src/main/java/io/dataease/auth/service/impl/AuthUserServiceImpl.java +++ b/backend/src/main/java/io/dataease/auth/service/impl/AuthUserServiceImpl.java @@ -16,6 +16,7 @@ import io.dataease.plugins.config.SpringContextUtil; import io.dataease.plugins.util.PluginUtils; import io.dataease.plugins.xpack.cas.service.CasXpackService; import io.dataease.plugins.xpack.dingtalk.service.DingtalkXpackService; +import io.dataease.plugins.xpack.lark.service.LarkXpackService; import io.dataease.plugins.xpack.ldap.service.LdapXpackService; import io.dataease.plugins.xpack.oidc.service.OidcXpackService; @@ -187,8 +188,12 @@ public class AuthUserServiceImpl implements AuthUserService { } @Override - public Boolean supportFark() { - return false; + public Boolean supportLark() { + Map beansOfType = SpringContextUtil.getApplicationContext().getBeansOfType((LarkXpackService.class)); + if (beansOfType.keySet().size() == 0) return false; + LarkXpackService larkXpackService = SpringContextUtil.getBean(LarkXpackService.class); + if (ObjectUtils.isEmpty(larkXpackService)) return false; + return larkXpackService.isOpen(); } @Override diff --git a/backend/src/main/java/io/dataease/auth/service/impl/ShiroServiceImpl.java b/backend/src/main/java/io/dataease/auth/service/impl/ShiroServiceImpl.java index 5cf3f896ba..4df99ceb75 100644 --- a/backend/src/main/java/io/dataease/auth/service/impl/ShiroServiceImpl.java +++ b/backend/src/main/java/io/dataease/auth/service/impl/ShiroServiceImpl.java @@ -83,7 +83,7 @@ public class ShiroServiceImpl implements ShiroService { filterChainDefinitionMap.put("/api/auth/isOpenOidc", ANON); filterChainDefinitionMap.put("/api/auth/isOpenWecom", ANON); filterChainDefinitionMap.put("/api/auth/isOpenDingtalk", ANON); - filterChainDefinitionMap.put("/api/auth/isOpenFark", ANON); + filterChainDefinitionMap.put("/api/auth/isOpenLark", ANON); filterChainDefinitionMap.put("/api/auth/getPublicKey", ANON); filterChainDefinitionMap.put("/api/pluginCommon/component/*", ANON); filterChainDefinitionMap.put("/api/pluginCommon/staticInfo/**", ANON); @@ -94,8 +94,8 @@ public class ShiroServiceImpl implements ShiroService { filterChainDefinitionMap.put("/plugin/wecom/getQrParam", ANON); filterChainDefinitionMap.put("/plugin/dingtalk/callBack*", ANON); filterChainDefinitionMap.put("/plugin/dingtalk/getQrParam", ANON); - filterChainDefinitionMap.put("/plugin/fark/callBack*", ANON); - filterChainDefinitionMap.put("/plugin/fark/getQrParam", ANON); + filterChainDefinitionMap.put("/plugin/lark/callBack*", ANON); + filterChainDefinitionMap.put("/plugin/lark/getQrParam", ANON); filterChainDefinitionMap.put("/cas/reset/**", ANON); filterChainDefinitionMap.put("/unauth", ANON); diff --git a/backend/src/main/java/io/dataease/plugins/server/XLarkServer.java b/backend/src/main/java/io/dataease/plugins/server/XLarkServer.java new file mode 100644 index 0000000000..c6ce08472d --- /dev/null +++ b/backend/src/main/java/io/dataease/plugins/server/XLarkServer.java @@ -0,0 +1,134 @@ +package io.dataease.plugins.server; + +import io.dataease.auth.entity.SysUserEntity; +import io.dataease.auth.entity.TokenInfo; +import io.dataease.auth.service.AuthUserService; +import io.dataease.auth.util.JWTUtils; +import io.dataease.commons.constants.SysLogConstants; +import io.dataease.commons.exception.DEException; +import io.dataease.commons.utils.DeLogUtils; +import io.dataease.commons.utils.LogUtil; +import io.dataease.commons.utils.ServletUtils; +import io.dataease.plugins.config.SpringContextUtil; + +import io.dataease.plugins.xpack.display.dto.response.SysSettingDto; +import io.dataease.plugins.xpack.lark.dto.entity.LarkQrResult; +import io.dataease.plugins.xpack.lark.dto.entity.LarkUserInfo; +import io.dataease.plugins.xpack.lark.dto.response.LarkInfo; +import io.dataease.plugins.xpack.lark.service.LarkXpackService; +import io.dataease.service.sys.SysUserService; +import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; +import springfox.documentation.annotations.ApiIgnore; + +import javax.annotation.Resource; +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.List; +import java.util.Map; + +@ApiIgnore +@RequestMapping("/plugin/lark") +@Controller +public class XLarkServer { + + + @Resource + private AuthUserService authUserService; + @Resource + private SysUserService sysUserService; + + @ResponseBody + @GetMapping("/info") + public LarkInfo getLarkInfo() { + LarkXpackService larkXpackService = SpringContextUtil.getBean(LarkXpackService.class); + return larkXpackService.info(); + } + + @ResponseBody + @RequiresPermissions("sysparam:read") + @PostMapping("/save") + public void save(@RequestBody List settings) { + LarkXpackService larkXpackService = SpringContextUtil.getBean(LarkXpackService.class); + larkXpackService.save(settings); + } + + @ResponseBody + @PostMapping("/testConn") + public void testConn(@RequestBody LarkInfo larkInfo) { + LarkXpackService larkXpackService = SpringContextUtil.getBean(LarkXpackService.class); + try { + larkXpackService.testConn(larkInfo); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @ResponseBody + @PostMapping("/getQrParam") + public LarkQrResult getQrParam() { + LarkXpackService larkXpackService = SpringContextUtil.getBean(LarkXpackService.class); + return larkXpackService.getQrParam(); + } + + @GetMapping("/callBack") + public ModelAndView callBack(@RequestParam("code") String code, @RequestParam("state") String state) { + ModelAndView modelAndView = new ModelAndView("redirect:/"); + HttpServletResponse response = ServletUtils.response(); + LarkXpackService larkXpackService = null; + try { + Map beansOfType = SpringContextUtil.getApplicationContext().getBeansOfType((LarkXpackService.class)); + if (beansOfType.keySet().size() == 0) { + DEException.throwException("缺少飞书插件"); + } + larkXpackService = SpringContextUtil.getBean(LarkXpackService.class); + Boolean isOpen = larkXpackService.isOpen(); + if (!isOpen) { + DEException.throwException("未开启飞书"); + } + LarkUserInfo larkUserInfo = larkXpackService.userInfo(code, state); + String username = larkUserInfo.getUser_id(); + String sub = larkUserInfo.getSub(); + SysUserEntity sysUserEntity = authUserService.getUserBySub(sub, 6); + if (null == sysUserEntity) { + String email = StringUtils.isNotBlank(larkUserInfo.getEmail()) ? larkUserInfo.getEmail() : "demo@lark.work"; + sysUserService.validateExistUser(username, larkUserInfo.getName(), email); + sysUserService.saveLarkCUser(larkUserInfo, email); + sysUserEntity = authUserService.getUserBySub(sub, 6); + } + TokenInfo tokenInfo = TokenInfo.builder().userId(sysUserEntity.getUserId()).username(sysUserEntity.getUsername()).build(); + String realPwd = sysUserEntity.getPassword(); + String token = JWTUtils.sign(tokenInfo, realPwd); + ServletUtils.setToken(token); + + DeLogUtils.save(SysLogConstants.OPERATE_TYPE.LOGIN, SysLogConstants.SOURCE_TYPE.USER, sysUserEntity.getUserId(), null, null, null); + + Cookie cookie_token = new Cookie("Authorization", token); + cookie_token.setPath("/"); + + response.addCookie(cookie_token); + } catch (Exception e) { + + String msg = e.getMessage(); + if (null != e.getCause()) { + msg = e.getCause().getMessage(); + } + try { + msg = URLEncoder.encode(msg, "UTF-8"); + LogUtil.error(e); + Cookie cookie_error = new Cookie("LarkError", msg); + cookie_error.setPath("/"); + + return modelAndView; + } catch (UnsupportedEncodingException e1) { + e.printStackTrace(); + } + } + return modelAndView; + } +} diff --git a/backend/src/main/java/io/dataease/service/sys/SysUserService.java b/backend/src/main/java/io/dataease/service/sys/SysUserService.java index 3d55955005..f9189d80f0 100644 --- a/backend/src/main/java/io/dataease/service/sys/SysUserService.java +++ b/backend/src/main/java/io/dataease/service/sys/SysUserService.java @@ -20,6 +20,7 @@ import io.dataease.plugins.common.base.mapper.SysUserMapper; import io.dataease.plugins.common.base.mapper.SysUsersRolesMapper; import io.dataease.plugins.common.entity.XpackLdapUserEntity; import io.dataease.plugins.xpack.dingtalk.dto.response.DingUserEntity; +import io.dataease.plugins.xpack.lark.dto.entity.LarkUserInfo; import io.dataease.plugins.xpack.oidc.dto.SSOUserInfo; import org.apache.commons.collections4.CollectionUtils; @@ -172,6 +173,28 @@ public class SysUserService { } + @Transactional + public void saveLarkCUser(LarkUserInfo larkUserInfo, String email) { + long now = System.currentTimeMillis(); + SysUser sysUser = new SysUser(); + + sysUser.setUsername(larkUserInfo.getUser_id()); + sysUser.setNickName(larkUserInfo.getName()); + sysUser.setEmail(email); + sysUser.setPassword(CodingUtil.md5(DEFAULT_PWD)); + sysUser.setCreateTime(now); + sysUser.setUpdateTime(now); + + sysUser.setEnabled(1L); + sysUser.setLanguage("zh_CN"); + sysUser.setFrom(6); + sysUser.setIsAdmin(false); + sysUser.setSub(larkUserInfo.getSub()); + sysUser.setPhone(larkUserInfo.getMobile()); + sysUserMapper.insert(sysUser); + + } + @Transactional public void saveCASUser(String name, String email) { long now = System.currentTimeMillis(); diff --git a/frontend/src/api/user.js b/frontend/src/api/user.js index b8f1680b90..6899c6726d 100644 --- a/frontend/src/api/user.js +++ b/frontend/src/api/user.js @@ -114,9 +114,9 @@ export function dingtalkStatus() { }) } -export function farkStatus() { +export function larkStatus() { return request({ - url: '/api/auth/isOpenFark', + url: '/api/auth/isOpenLark', method: 'post' }) } diff --git a/frontend/src/assets/fark.png b/frontend/src/assets/lark.png similarity index 100% rename from frontend/src/assets/fark.png rename to frontend/src/assets/lark.png diff --git a/frontend/src/views/login/index.vue b/frontend/src/views/login/index.vue index 15fd78dc67..7681a713f3 100644 --- a/frontend/src/views/login/index.vue +++ b/frontend/src/views/login/index.vue @@ -61,13 +61,13 @@ - + @@ -89,7 +89,7 @@