refactor(权限管理): 处理冲突track header menu

This commit is contained in:
Captain.B 2021-05-20 16:48:28 +08:00 committed by 刘瑞斌
parent 131ec1ff76
commit c69528cd81
9 changed files with 95 additions and 30 deletions

View File

@ -25,18 +25,18 @@
</el-menu-item>
<el-submenu v-show="isOldVersion"
v-permission="['test_manager','test_user','test_viewer']" index="4">
v-roles="['test_manager','test_user','test_viewer']" index="4">
<template v-slot:title>{{ $t('commons.test') }}</template>
<ms-recent-list ref="testRecent" :options="testRecent"/>
<el-divider class="menu-divider"/>
<ms-show-all :index="'/api/test/list/all'"/>
<el-menu-item :index="apiTestProjectPath" class="blank_item"></el-menu-item>
<ms-create-button v-permission="['test_manager','test_user']" :index="'/api/test/create'"
<ms-create-button v-roles="['test_manager','test_user']" :index="'/api/test/create'"
:title="$t('load_test.create')"/>
</el-submenu>
<el-submenu v-show="isOldVersion"
v-permission="['test_manager','test_user','test_viewer']" index="5">
v-roles="['test_manager','test_user','test_viewer']" index="5">
<template v-slot:title>{{ $t('commons.report') }}</template>
<ms-recent-list ref="reportRecent" :options="reportRecent"/>
<el-divider class="menu-divider"/>
@ -45,7 +45,7 @@
<el-menu-item v-show="isOldVersion"
v-permission="['test_manager','test_user','test_viewer']" :index="'/api/monitor/view'">
v-roles="['test_manager','test_user','test_viewer']" :index="'/api/monitor/view'">
{{ $t('commons.monitor') }}
</el-menu-item>
</el-menu>

View File

@ -1,5 +1,5 @@
<template>
<router-link class="create-test" :to="this.to" v-permission="this.permission">
<router-link class="create-test" :to="this.to" v-roles="this.permission">
<el-button type="primary" size="small">{{ this.title }}</el-button>
</router-link>
</template>

View File

@ -9,19 +9,20 @@
:key="menuKey"
router>
<el-menu-item index="/track" v-if="check('testTrack')" v-permission="['test_manager','test_user','test_viewer']">
<el-menu-item index="/track" v-if="check('testTrack')"
v-permission="['PROJECT_TRACK_CASE:READ','PROJECT_TRACK_PLAN:READ','PROJECT_TRACK_REVIEW:READ']">
{{ $t('test_track.test_track') }}
</el-menu-item>
<el-menu-item index="/api" @click="active()" v-if="check('api')"
v-permission="['test_manager','test_user','test_viewer']">
v-permission="['PROJECT_API_DEFINITION:READ','PROJECT_API_SCENARIO:READ','PROJECT_API_REPORT:READ']">
{{ $t('commons.api') }}
</el-menu-item>
<el-menu-item index="/performance" v-if="check('performance')"
onselectstart="return false"
v-permission="['test_manager','test_user','test_viewer']">
v-permission="['PROJECT_PERFORMANCE_TEST:READ','PROJECT_PERFORMANCE_REPORT:READ']">
{{ $t('commons.performance') }}
</el-menu-item>
<el-menu-item index="/report" v-permission="['test_manager','test_user','test_viewer']"
<el-menu-item index="/report"
v-if="isReport && check('reportStat')">
{{ $t('commons.report_statistics.title') }}
</el-menu-item>

View File

@ -2,7 +2,7 @@
<el-menu class="header-menu" :unique-opened="true" mode="horizontal" default-active="1" router>
<!-- 不激活项目路由-->
<el-menu-item index="1" v-show="false">Placeholder</el-menu-item>
<el-submenu v-permission="['test_manager','test_user','test_viewer']" index="2" popper-class="submenu">
<el-submenu v-roles="['test_manager','test_user','test_viewer']" index="2" popper-class="submenu">
<template v-slot:title>
<span class="project-name" :title="currentProject">
{{ $t('commons.project') }}: {{ currentProject }}
@ -10,7 +10,7 @@
</template>
<search-list :current-project.sync="currentProject"/>
<el-divider/>
<el-menu-item :index="'/setting/project/create'" v-permission="['test_manager','test_user']">
<el-menu-item :index="'/setting/project/create'" v-roles="['test_manager','test_user']">
<font-awesome-icon :icon="['fa', 'plus']"/>
<span style="padding-left: 7px;">{{ $t("project.create") }}</span>
</el-menu-item>

View File

@ -8,17 +8,17 @@
{{ $t("i18n.home") }}
</el-menu-item>
<el-submenu v-permission="['test_manager','test_user','test_viewer']"
<el-submenu v-roles="['test_manager','test_user','test_viewer']"
index="4" popper-class="submenu">
<template v-slot:title>{{ $t('commons.test') }}</template>
<ms-recent-list ref="testRecent" :options="testRecent"/>
<el-divider/>
<ms-show-all :index="'/performance/test/all'"/>
<ms-create-button v-permission="['test_manager','test_user']" :index="'/performance/test/create'"
<ms-create-button v-roles="['test_manager','test_user']" :index="'/performance/test/create'"
:title="$t('load_test.create')"/>
</el-submenu>
<el-submenu v-permission="['test_manager','test_user','test_viewer']"
<el-submenu v-roles="['test_manager','test_user','test_viewer']"
index="5" popper-class="submenu">
<template v-slot:title>{{ $t('commons.report') }}</template>
<ms-recent-list ref="reportRecent" :options="reportRecent"/>

View File

@ -1,6 +1,6 @@
<template>
<el-menu menu-trigger="click" :default-active="$route.path" router class="setting">
<el-submenu index="1" v-permission="['admin']">
<el-submenu index="1" v-roles="['admin']">
<template v-slot:title>
<font-awesome-icon class="icon account" :icon="['far', 'address-card']" size="lg"/>
<span>{{ $t('commons.system') }}</span>
@ -10,7 +10,7 @@
</el-menu-item>
</el-submenu>
<el-submenu index="2" v-permission="['org_admin']" v-if="isCurrentOrganizationAdmin">
<el-submenu index="2" v-roles="['org_admin']" v-if="isCurrentOrganizationAdmin">
<template v-slot:title>
<font-awesome-icon class="icon organization" :icon="['far', 'building']" size="lg"/>
<span>{{ $t('commons.organization') }}</span>
@ -20,7 +20,7 @@
</el-menu-item>
</el-submenu>
<el-submenu index="3" v-permission="['test_manager']" v-if="isCurrentWorkspaceUser">
<el-submenu index="3" v-roles="['test_manager']" v-if="isCurrentWorkspaceUser">
<template v-slot:title>
<font-awesome-icon class="icon workspace" :icon="['far', 'list-alt']" size="lg"/>
<span>{{ $t('commons.workspace') }}</span>
@ -30,13 +30,14 @@
</el-menu-item>
<el-submenu index="3-1">
<template slot="title">模板管理</template>
<el-menu-item v-for="menu in workspaceTemplate" :key="menu.index" :index="menu.index" class="setting-item" v-permission="menu.roles">
<el-menu-item v-for="menu in workspaceTemplate" :key="menu.index" :index="menu.index" class="setting-item"
v-roles="menu.roles">
{{ $t(menu.title) }}
</el-menu-item>
</el-submenu>
</el-submenu>
<el-submenu index="4" v-permission="['test_user','test_manager', 'org_admin', 'admin']">
<el-submenu index="4" v-roles="['test_user','test_manager', 'org_admin', 'admin']">
<template v-slot:title>
<font-awesome-icon class="icon" :icon="['fa', 'bars']" size="lg"/>
<span>{{ $t('commons.project') }}</span>
@ -47,7 +48,7 @@
</el-submenu>
<!-- <el-menu-item v-for="menu in project" :key="menu.index" :index="'/setting/project/all'" class="setting-item"-->
<!-- v-permission="['test_user','test_manager', 'org_admin', 'admin']">-->
<!-- v-roles="['test_user','test_manager', 'org_admin', 'admin']">-->
<!-- <template v-slot:title>-->
<!-- <font-awesome-icon class="icon" :icon="['fa', 'bars']" size="lg"/>-->
<!-- <span>{{ $t(menu.title) }}</span>-->
@ -60,7 +61,7 @@
<span>{{ $t('commons.personal_info') }}</span>
</template>
<el-menu-item v-for="menu in persons" :key="menu.index" :index="menu.index" class="setting-item"
v-permission="menu.roles">
v-roles="menu.roles">
{{ $t(menu.title) }}
</el-menu-item>
</el-submenu>

View File

@ -36,7 +36,7 @@
<el-table-column :label="$t('commons.operating')">
<template v-slot:default="scope">
<ms-table-operator :tip2="$t('commons.remove')" @editClick="edit(scope.row)" @deleteClick="del(scope.row)"
v-permission="['test_manager']"/>
v-roles="['test_manager']"/>
</template>
</el-table-column>
</el-table>

View File

@ -4,7 +4,7 @@ import {
checkoutTestManagerOrTestUser,
enableModules,
hasLicense,
hasRolePermissions,
hasPermissions,
hasRoles
} from "@/common/js/utils";
import NProgress from 'nprogress'; // progress bar
@ -37,7 +37,7 @@ function checkRolePermission(el, binding, type) {
if (type === 'roles') {
hasPermission = hasRoles(...permissionRoles);
} else if (type === 'permission') {
hasPermission = hasRolePermissions(...permissionRoles);
hasPermission = hasPermissions(...permissionRoles);
}
if (!hasPermission) {
el.parentNode && el.parentNode.removeChild(el);

View File

@ -52,12 +52,66 @@ export function hasRolePermission(role) {
return false;
}
export function hasPermission(permission) {
let user = getCurrentUser();
user.groupPermissions.forEach(gp => {
for (let userGroup of user.userGroups) {
if (gp.group.id === userGroup.id) {
gp.sourceId = userGroup.sourceId;
}
}
});
// todo 权限验证
let currentProjectPermissions = user.groupPermissions.filter(gp => gp.group.type === 'PROJECT')
.filter(gp => gp.group.scopeId === getCurrentProjectID())[0]?.userGroupPermissions
.map(g => g.permissionId) || [];
for (const p of currentProjectPermissions) {
if (p === permission) {
return true;
}
}
let currentWorkspacePermissions = user.groupPermissions.filter(gp => gp.group.type === 'WORKSPACE')
.filter(gp => gp.group.scopeId === getCurrentWorkspaceId())[0]?.userGroupPermissions
.map(g => g.permissionId) || [];
for (const p of currentWorkspacePermissions) {
if (p === permission) {
return true;
}
}
let currentOrganizationPermissions = user.groupPermissions.filter(gp => gp.group.type === 'ORGANIZATION')
.filter(gp => gp.group.scopeId === getCurrentOrganizationId())[0]?.userGroupPermissions
.map(g => g.permissionId) || [];
for (const p of currentOrganizationPermissions) {
if (p === permission) {
return true;
}
}
let systemPermissions = user.groupPermissions.filter(gp => gp.group.type === 'SYSTEM')
.filter(gp => gp.group.scopeId === 'global')[0]?.userGroupPermissions
.map(g => g.permissionId) || [];
for (const p of systemPermissions) {
if (p === permission) {
return true;
}
}
return false;
}
export function hasLicense() {
let v = localStorage.getItem(LicenseKey);
return v === 'valid';
}
//是否含有对应组织或工作空间的角色
export function hasRolePermissions(...roles) {
for (let role of roles) {
if (hasRolePermission(role)) {
@ -67,6 +121,15 @@ export function hasRolePermissions(...roles) {
return false;
}
export function hasPermissions(...permissions) {
for (let p of permissions) {
if (hasPermission(p)) {
return true;
}
}
return false;
}
export function checkoutCurrentOrganization() {
// 查看当前用户是否是 lastOrganizationId 的组织管理员
return hasRolePermissions(ROLE_ORG_ADMIN);