From 6d8f15997535fe1cc8e1a571ac60cbb08d501400 Mon Sep 17 00:00:00 2001 From: w940853815 <940853815@qq.com> Date: Mon, 13 Jan 2025 16:15:14 +0800 Subject: [PATCH] perf: Optimize risk action checks and filters --- .../RiskDetect/RiskHandlerFormatter/const.js | 34 ++++++++++--------- .../RiskDetect/RiskHandlerFormatter/index.vue | 23 +++++++++---- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/views/pam/RiskDetect/RiskHandlerFormatter/const.js b/src/views/pam/RiskDetect/RiskHandlerFormatter/const.js index 8208c60e7..16dcc956f 100644 --- a/src/views/pam/RiskDetect/RiskHandlerFormatter/const.js +++ b/src/views/pam/RiskDetect/RiskHandlerFormatter/const.js @@ -1,5 +1,14 @@ import i18n from '@/i18n/i18n' +async function checkUserExist() { + if (!this.row.username) { + return false + } + const url = `/api/v1/accounts/accounts/?username=${encodeURIComponent(this.row.username)}&asset=${this.row.asset.id}` + const data = await this.$axios.get(url) + return data.length > 0 +} + export const riskActions = [ // { // name: 'disable_remote', @@ -21,31 +30,24 @@ export const riskActions = [ label: i18n.t('Add to Account'), has: ['new_found'], disabled: async function() { - if (!this.row.username) { - return false - } - const url = `/api/v1/accounts/accounts/?username=${this.row.username}&asset=${this.row.asset.id}` - const data = await this.$axios.get(url) - return data.length > 0 + return await checkUserExist.call(this) } }, { - name: 'add_account_after_change_password', + name: 'change_password_add', label: i18n.t('Add account after change password'), - has: ['new_found'], - disabled: async function() { - if (!this.row.username) { - return false - } - const url = `/api/v1/accounts/accounts/?username=${this.row.username}&asset=${this.row.asset.id}` - const data = await this.$axios.get(url) - return data.length > 0 + has: async function() { + const risks = ['new_found', 'long_time_password', 'password_expired'] + return risks.includes(this.row.risk.value) && !await checkUserExist.call(this) } }, { name: 'change_password', label: i18n.t('Change Password'), - has: ['long_time_password', 'weak_password', 'password_expired', 'leaked_password', 'repeated_password'] + has: async function() { + const risks = ['long_time_password', 'weak_password', 'password_expired', 'leaked_password', 'repeated_password'] + return risks.includes(this.row.risk.value) && await checkUserExist.call(this) + } }, // { // name: 'addPrivilegedAccount', diff --git a/src/views/pam/RiskDetect/RiskHandlerFormatter/index.vue b/src/views/pam/RiskDetect/RiskHandlerFormatter/index.vue index a6be79421..0a2bdc189 100644 --- a/src/views/pam/RiskDetect/RiskHandlerFormatter/index.vue +++ b/src/views/pam/RiskDetect/RiskHandlerFormatter/index.vue @@ -162,18 +162,27 @@ export default { } return this.actions.length > 0 }, - checkHas(action) { - return action.has.includes(this.row.risk.value) || action.name === 'review' + async checkHas(action) { + const has = action.has === undefined ? false : action.has + if (typeof has === 'function') { + return await action.has.call(this) + } + if (Array.isArray(has)) { + return has.includes(this.row.risk.value) || action.name === 'review' + } + return false }, async getActions() { - let actions = _.cloneDeep(riskActions) - actions = actions.filter((action) => { - return this.checkHas(action) - }) + const actions = _.cloneDeep(riskActions) + const filteredActions = [] for (const action of actions) { action.disabled = await this.checkDisabled(action) + const has = await this.checkHas(action) + if (has) { + filteredActions.push(action) + } } - return actions + return filteredActions } } }