Skip to content

Commit

Permalink
fix(system-client): fix policy rules CRUD feats;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 6, 2021
1 parent 00cd4bb commit 44a4dbd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 111 deletions.
22 changes: 19 additions & 3 deletions packages/system-client/src/api/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,28 @@ export function updatePolicy(policy_id, data) {
}

/**
* Delete a policy
* Update policy rules
* @param {*} policy_id
* @param {*} rules
* @returns
*/
export function updatePolicyRules(policy_id, rules) {
const appid = store.state.app.appid
return request({
url: `/apps/${appid}/policy/${policy_id}/rules`,
method: 'post',
data: {
rules: rules
}
})
}

/**
* Remove a policy
* @param {*} policy_id
* @param {*} data
* @returns
*/
export function deletePolicy(policy_id) {
export function removePolicy(policy_id) {
const appid = store.state.app.appid
return request({
url: `/apps/${appid}/policy/${policy_id}`,
Expand Down
21 changes: 10 additions & 11 deletions packages/system-client/src/views/database/policies.vue
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@
</el-dialog>

<!-- 部署面板 -->
<!-- <DeployPanel v-model="deployPanelVisible" :policies="list" /> -->
<DeployPanel v-model="deployPanelVisible" :policies="list" />
</div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
// import DeployPanel from '../deploy/components/deploy-panel.vue'
import { createPolicy, deletePolicy, getPolicies, publishPolicies, updatePolicy } from '@/api/policy'
import DeployPanel from '../deploy/components/deploy-panel.vue'
import { createPolicy, removePolicy, getPolicies, publishPolicies, updatePolicy } from '@/api/policy'
import { getFunctions } from '@/api/func'
// 默认化创建表单的值
Expand All @@ -171,8 +171,8 @@ const formRules = {
export default {
name: 'PoliciesListPage',
components: {
Pagination
// DeployPanel
Pagination,
DeployPanel
},
filters: {
statusFilter(status) {
Expand Down Expand Up @@ -215,7 +215,8 @@ export default {
},
methods: {
async getFunctions() {
const r = await getFunctions({ status: 1 }, 1, 999)
// get all functions
const r = await getFunctions({ status: 1 }, 1, 9999)
this.functions = r.data ?? []
},
/**
Expand Down Expand Up @@ -299,8 +300,7 @@ export default {
label: this.form.label,
injector: this.form.injector,
status: this.form.status,
description: this.form.description,
updated_at: Date.now()
description: this.form.description
}
// 执行更新请求
Expand Down Expand Up @@ -330,7 +330,7 @@ export default {
await this.$confirm('确认要删除此数据?', '删除确认')
// 执行删除请求
const r = await deletePolicy(row._id)
const r = await removePolicy(row._id)
if (r.error) {
this.$notify({
Expand Down Expand Up @@ -368,8 +368,7 @@ export default {
},
// 查看详情
async handleShowDetail(row) {
// 跳转到详情页
this.$router.push(`/database/policies/${row._id}`)
this.$router.push(`policies/${row._id}`)
}
}
}
Expand Down
150 changes: 53 additions & 97 deletions packages/system-client/src/views/database/policy.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,18 @@

<script>
import JsonEditor from '@/components/JsonEditor/rule'
import { db } from '../../api/cloud'
import $ from 'lodash'
import { Constants } from '../../api/constants'
import { getPolicyById, updatePolicyRules } from '@/api/policy'
const defaultValue = `
const defaultValue =
{
"read": true,
"update": false,
"remove": false,
"add": false,
"count": true
'read': true,
'count': true,
'update': false,
'remove': false,
'add': false
}
`
const defaultForm = {
collection: ''
}
Expand Down Expand Up @@ -116,11 +115,9 @@ export default {
methods: {
async getPolicy() {
this.loading = true
const r = await db.collection(Constants.cn.policies)
.where({ _id: this.policy_id })
.getOne()
const r = await getPolicyById(this.policy_id)
if (!r.ok) {
if (r.error) {
console.error(r.error)
return
}
Expand All @@ -138,27 +135,19 @@ export default {
this.value = rules[this.collection_name] || defaultValue
},
async updateRule() {
if (this.loading) {
return
}
if (this.validate()) {
return
}
if (this.loading) return
if (this.validate()) return
this.loading = true
const rule_data = this.value
const key = `rules.${this.collection_name}`
const r = await db.collection(Constants.cn.policies)
.where({
_id: this.policy_id
})
.update({
[key]: db.command.set(JSON.parse(rule_data))
})
if (!r.ok) {
this.policy.rules[this.collection_name] = JSON.parse(this.value)
const data = {
...this.policy.rules
}
const r = await updatePolicyRules(this.policy_id, data)
.finally(() => { this.loading = false })
if (r.error) {
console.error(r.error)
this.$message('保存失败!')
this.loading = false
return
}
Expand All @@ -167,98 +156,65 @@ export default {
title: '保存',
message: '保存访问规则成功!'
})
this.loading = false
},
async create() {
if (!this.form.collection) {
this.$message('请正确填写表单!')
return
}
if (this.loading) {
return
}
const coll_name = this.form.collection
if (!coll_name) return this.$message('请正确填写表单!')
if (this.loading) return
this.loading = true
const key = `rules.${this.form.collection}`
const { total } = await db.collection(Constants.cn.policies)
.where({
_id: this.policy_id,
[key]: db.command.exists(true)
}).count()
if (total) {
if (this.collections.includes(coll_name)) {
this.loading = false
this.$message('该集合规则已存在!')
return
}
const r = await db.collection(Constants.cn.policies)
.where({
_id: this.policy_id,
[key]: db.command.exists(false)
})
.update({
[key]: db.command.set(JSON.parse(defaultValue))
})
if (!r.ok) {
const data = {
...this.policy.rules,
[coll_name]: defaultValue
}
const r = await updatePolicyRules(this.policy_id, data)
.finally(() => { this.loading = false })
if (r.error) {
this.$message('创建失败!')
this.loading = false
console.error(r.error)
return
}
await this.getPolicy()
this.collection_name = this.form.collection
this.collection_name = coll_name
this.$notify({
type: 'success',
title: '操作结果',
message: '创建集合成功!'
})
this.form = { ...defaultForm }
this.dialogVisible = false
this.loading = false
},
async removeRule() {
if (!this.collection_name) {
this.$message('请选择要删除的集合规则!')
return
}
if (this.loading) {
return
}
const confirm = await this.$confirm('确定删除该条规则,该操作不可恢复?')
.catch(() => false)
if (!this.collection_name) return this.$message('请选择要删除的集合规则!')
if (this.loading) return
const confirm = await this.$confirm('确定删除该条规则,该操作不可恢复?').catch(() => false)
if (!confirm) return
this.loading = true
const key = `rules.${this.collection_name}`
const r = await db.collection(Constants.cn.policies)
.where({
_id: this.policy_id,
[key]: db.command.exists(true)
})
.update({
[key]: db.command.remove()
})
if (r.ok && r.updated) {
this.$notify({
title: '操作成功',
type: 'success',
message: '删除访问规则成功!'
})
this.collection_name = ''
this.getPolicy()
} else {
this.$message('删除访问规则操作失败 ' + r.error)
const data = { ...this.policy.rules }
delete data[this.collection_name]
const r = await updatePolicyRules(this.policy_id, data)
.finally(() => { this.loading = false })
if (r.error) {
console.error(r.error)
return this.$message('删除访问规则操作失败 ' + r.error)
}
this.loading = false
this.$notify({
title: '操作成功',
type: 'success',
message: '删除访问规则成功!'
})
this.collection_name = ''
this.getPolicy()
},
setTagViewTitle() {
const label = this.policy.name
Expand Down

0 comments on commit 44a4dbd

Please sign in to comment.