From b1df71c7172713ace0f336133f5957ec050eb997 Mon Sep 17 00:00:00 2001 From: shiziyuan9527 Date: Fri, 28 Oct 2022 18:09:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E8=B7=AF=E7=94=B1=E8=B7=B3?= =?UTF-8?q?=E8=BD=AC=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gateway/controller/LoginController.java | 9 +- .../frontend/src/business/login/index.vue | 9 +- .../src/components/layout/AsideMenus.vue | 10 +++ .../sdk-parent/frontend/src/router/index.js | 90 +++++-------------- .../frontend/src/router/permission.js | 2 +- .../frontend/src/store/modules/user.js | 13 +-- .../controller/LoginController.java | 4 + 7 files changed, 53 insertions(+), 84 deletions(-) diff --git a/framework/gateway/src/main/java/io/metersphere/gateway/controller/LoginController.java b/framework/gateway/src/main/java/io/metersphere/gateway/controller/LoginController.java index 03a3431d87..1fb1095054 100644 --- a/framework/gateway/src/main/java/io/metersphere/gateway/controller/LoginController.java +++ b/framework/gateway/src/main/java/io/metersphere/gateway/controller/LoginController.java @@ -61,9 +61,14 @@ public class LoginController { .map(r -> { if (r instanceof RsaKey) { return ResultHolder.error(rsaKey.getPublicKey()); - } else { - return ResultHolder.success(r); } + if (r instanceof User) { + // 用户只有工作空间权限 + if (StringUtils.isBlank(((User) r).getLastProjectId())) { + ((User) r).setLastProjectId("no_such_project"); + } + } + return ResultHolder.success(r); }); } else { return Mono.just(ResultHolder.error(rsaKey.getPublicKey())); diff --git a/framework/sdk-parent/frontend/src/business/login/index.vue b/framework/sdk-parent/frontend/src/business/login/index.vue index fcba3915e2..748fd7feb0 100644 --- a/framework/sdk-parent/frontend/src/business/login/index.vue +++ b/framework/sdk-parent/frontend/src/business/login/index.vue @@ -101,7 +101,7 @@ export default { ready: false, openLdap: false, authSources: [], - lastUser: null, + lastUser: sessionStorage.getItem('lastUser'), loginTitle: this.$t('commons.welcome') } }, @@ -238,6 +238,7 @@ export default { }, checkRedirectUrl() { if (this.lastUser === getCurrentUserId()) { + this.$router.push({path: sessionStorage.getItem('redirectUrl') || '/'}); return; } let redirectUrl = '/'; @@ -252,7 +253,8 @@ export default { } sessionStorage.setItem('redirectUrl', redirectUrl); - this.$router.push({path: redirectUrl || '/', query: this.otherQuery}) + sessionStorage.setItem('lastUser', getCurrentUserId()); + this.$router.push({ name: "login_redirect", path: redirectUrl || '/', query: this.otherQuery}); }, doLogin() { const userStore = useUserStore() @@ -282,10 +284,7 @@ export default { .then(response => { language = response.data; localStorage.setItem(DEFAULT_LANGUAGE, language); - window.location.href = "/"; }); - } else { - window.location.href = "/"; } }, redirectAuth(authId) { diff --git a/framework/sdk-parent/frontend/src/components/layout/AsideMenus.vue b/framework/sdk-parent/frontend/src/components/layout/AsideMenus.vue index f1a175f31a..d85841bd37 100644 --- a/framework/sdk-parent/frontend/src/components/layout/AsideMenus.vue +++ b/framework/sdk-parent/frontend/src/components/layout/AsideMenus.vue @@ -116,12 +116,22 @@ export default { this.handleSelect(this.activeIndex); } }, + created() { + this.$EventBus.$on('projectChange', () => { + this.$nextTick(() => { + this.menuKey++; + }) + }) + }, mounted() { if (this.$route.matched.length > 0) { this.activeIndex = this.$route.matched[0].path; } this.registerEvents(); }, + beforeDestroy() { + this.$EventBus.$off('projectChange'); + }, methods: { hasLicense, handleSelect(index) { diff --git a/framework/sdk-parent/frontend/src/router/index.js b/framework/sdk-parent/frontend/src/router/index.js index 718666e9af..6c064c9489 100644 --- a/framework/sdk-parent/frontend/src/router/index.js +++ b/framework/sdk-parent/frontend/src/router/index.js @@ -1,7 +1,6 @@ import Vue from "vue" import Router from "vue-router" import Layout from "../business/app-layout" -import {getCurrentUserId} from "../utils/token"; import {hasPermissions} from "../utils/permission"; import {SECOND_LEVEL_ROUTE_PERMISSION_MAP} from "../utils/constants"; @@ -64,84 +63,35 @@ let store = null; router.beforeEach(async (to, from, next) => { if (store === null) { const {useUserStore} = await import('@/store'); - store = useUserStore() + store = useUserStore(); } - let user = store.currentUser - if (to.path.split('/')[1] !== from.path.split('/')[1]) { + let formModule = from.path.split('/')[1]; + let toModule = to.path.split('/')[1]; + if (to.path !== '/login' && formModule && toModule !== formModule) { try { - user = await store.getIsLogin(); - if (window.location.href.endsWith('/#/login')) { - window.location.replace("/#/setting/personsetting"); - } + await store.getIsLogin(); } catch (e) { - // console.error(e) + // nothing } } - if (user && user.id) { - redirectLoginPath(to.fullPath, next); - } else { + + if (to.name === "login_redirect" || to.path === "/login") { next(); + return; + } + + // 二级菜单权限控制 + let changedPath = getDefaultSecondLevelMenu(to.fullPath); + sessionStorage.setItem('redirectUrl', changedPath); + if (changedPath === to.fullPath) { + // 有权限则放行 + next(); + } else { + // 未通过校验,放行至有权限路由 + next({path: changedPath}); } }); -// 登入后跳转至原路径 -function redirectLoginPath(originPath, next) { - let redirectUrl = sessionStorage.getItem('redirectUrl'); - let loginSuccess = sessionStorage.getItem('loginSuccess'); - - if (!redirectUrl || redirectUrl === '/') { - if (hasPermissions('PROJECT_USER:READ', 'PROJECT_ENVIRONMENT:READ', 'PROJECT_OPERATING_LOG:READ', 'PROJECT_FILE:READ+JAR', 'PROJECT_FILE:READ+FILE', 'PROJECT_CUSTOM_CODE:READ', 'PROJECT_TEMPLATE:READ', 'PROJECT_MESSAGE:READ')) { - redirectUrl = '/project/home'; - } else if (hasPermissions('WORKSPACE_SERVICE:READ', 'PROJECT_MESSAGE:READ', 'WORKSPACE_USER:READ', 'WORKSPACE_PROJECT_MANAGER:READ', 'WORKSPACE_PROJECT_ENVIRONMENT:READ', 'WORKSPACE_OPERATING_LOG:READ')) { - redirectUrl = '/setting/project/:type'; - } else if (hasPermissions('SYSTEM_USER:READ', 'SYSTEM_WORKSPACE:READ', 'SYSTEM_GROUP:READ', 'SYSTEM_TEST_POOL:READ', 'SYSTEM_SETTING:READ', 'SYSTEM_AUTH:READ', 'SYSTEM_QUOTA:READ', 'SYSTEM_OPERATING_LOG:READ')) { - redirectUrl = '/setting'; - } else { - redirectUrl = '/'; - } - } - - sessionStorage.setItem('lastUser', getCurrentUserId()); - sessionStorage.setItem('redirectUrl', originPath); - sessionStorage.removeItem('loginSuccess'); - let defaultMenuRoute = sessionStorage.getItem('defaultMenuRoute'); - - if (redirectUrl && loginSuccess) { - // 登录后只执行一次 - sessionStorage.removeItem('loginSuccess'); - let changedPath = getDefaultSecondLevelMenu(originPath); - if (changedPath === originPath) { - // 通过了权限校验,保留路由相关信息,直接放行 - next(); - } else { - // 未通过校验,放行至有权限路由 - next({path: changedPath}); - } - } else { - if (!defaultMenuRoute) { - // 记录标识,防止死循环 - sessionStorage.setItem('defaultMenuRoute', 'sign'); - let changedPath = getDefaultSecondLevelMenu(originPath); - if (changedPath === originPath) { - // 通过了权限校验,保留路由相关信息,直接放行 - next(); - } else { - // 未通过校验,放行至有权限路由 - next({path: changedPath}); - } - if (router.currentRoute.fullPath === originPath) { - sessionStorage.setItem('redirectUrl', originPath); - // 路径相同时,移除标识 - sessionStorage.removeItem("defaultMenuRoute"); - } - } else { - sessionStorage.setItem('redirectUrl', originPath); - sessionStorage.removeItem("defaultMenuRoute"); - next(); - } - } -} - export function getDefaultSecondLevelMenu(toPath) { let {TRACK: tracks, API: apis, LOAD: loads, UI: ui, REPORT: report} = SECOND_LEVEL_ROUTE_PERMISSION_MAP; if (tracks.map(r => r.router).indexOf(toPath) > -1) { diff --git a/framework/sdk-parent/frontend/src/router/permission.js b/framework/sdk-parent/frontend/src/router/permission.js index 76252b7034..158c6ef785 100644 --- a/framework/sdk-parent/frontend/src/router/permission.js +++ b/framework/sdk-parent/frontend/src/router/permission.js @@ -20,7 +20,7 @@ router.beforeEach(async (to, from, next) => { if (user && user.id) { if (to.path === '/login') { - next({path: '/'}); + next(); NProgress.done(); // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939 } else { // const roles = user.roles.filter(r => r.id); diff --git a/framework/sdk-parent/frontend/src/store/modules/user.js b/framework/sdk-parent/frontend/src/store/modules/user.js index 700b2e97cb..bb1c54a4a6 100644 --- a/framework/sdk-parent/frontend/src/store/modules/user.js +++ b/framework/sdk-parent/frontend/src/store/modules/user.js @@ -109,15 +109,16 @@ export default { }, userLogout() { + clearSessionStorage(); return new Promise((resolve, reject) => { logout().then(() => { - clearSessionStorage() - location.reload() - resolve() + location.href = '/#/login'; + location.reload(); + resolve(); }).catch(error => { - clearSessionStorage() - location.reload() - reject(error) + location.href = '/#/login'; + location.reload(); + reject(error); }) }) }, diff --git a/framework/sdk-parent/sdk/src/main/java/io/metersphere/controller/LoginController.java b/framework/sdk-parent/sdk/src/main/java/io/metersphere/controller/LoginController.java index 1ef66312bd..7acbf986ac 100644 --- a/framework/sdk-parent/sdk/src/main/java/io/metersphere/controller/LoginController.java +++ b/framework/sdk-parent/sdk/src/main/java/io/metersphere/controller/LoginController.java @@ -57,6 +57,10 @@ public class LoginController { baseUserService.autoSwitch(userDTO); SessionUser sessionUser = SessionUser.fromUser(userDTO, SessionUtils.getSessionId()); SessionUtils.putUser(sessionUser); + // 用户只有工作空间权限 + if (StringUtils.isBlank(sessionUser.getLastProjectId())) { + sessionUser.setLastProjectId("no_such_project"); + } return ResultHolder.success(sessionUser); } return ResultHolder.error(rsaKey.getPublicKey());