perf: Optimize risk action checks and filters

This commit is contained in:
w940853815
2025-01-13 16:15:14 +08:00
committed by 老广
parent 7075b5c90a
commit 6d8f159975
2 changed files with 34 additions and 23 deletions

View File

@@ -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',

View File

@@ -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
}
}
}