Update.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <template>
  2. <div>
  3. <page-header :title="$t('system.text_188', [$t('dictionary.policy')])" />
  4. <page-body need-margin-bottom>
  5. <template v-if="loading">
  6. <div class="text-center">
  7. <a-icon type="loading" />
  8. <p>{{$t('system.text_318', [$t('dictionary.policy')])}}</p>
  9. </div>
  10. </template>
  11. <template v-else>
  12. <policy-form
  13. ref="policyForm"
  14. is-update
  15. :policy="policy"
  16. :permissions="permissions"
  17. :edit-type="editType"
  18. :policy-loading="policyLoading"
  19. @edit-type-change="handleEditTypeChange" />
  20. </template>
  21. </page-body>
  22. <page-footer>
  23. <div slot="right">
  24. <a-button v-if="!loading" class="mr-3" type="primary" @click="doUpdata" :loading="submiting">{{ $t('common.ok') }}</a-button>
  25. <a-button @click="handleCancel">{{ $t('common.cancel') }}</a-button>
  26. </div>
  27. </page-footer>
  28. </div>
  29. </template>
  30. <script>
  31. import { DEFAULT_ACTIONS_KEY } from './constants'
  32. import { genPolicyGroups } from './utils'
  33. import PolicyForm from './components/Form'
  34. export default {
  35. name: 'PolicyUpdate',
  36. components: {
  37. PolicyForm,
  38. },
  39. data () {
  40. return {
  41. loading: false,
  42. policy: {},
  43. permissions: {},
  44. submiting: false,
  45. POLICY_GROUPS: genPolicyGroups(),
  46. editType: 'yaml',
  47. policyLoading: false,
  48. }
  49. },
  50. watch: {
  51. editType () {
  52. this.$nextTick(() => {
  53. this.getPolicy().then(policyRes => {
  54. this.policy = policyRes.data
  55. })
  56. })
  57. },
  58. },
  59. created () {
  60. this.getAll()
  61. },
  62. methods: {
  63. getAll () {
  64. this.loading = true
  65. this.getPolicy().then((policyRes) => {
  66. this.policy = policyRes.data
  67. this.getPermissions().then(permissionsRes => {
  68. this.permissions = permissionsRes.data
  69. this.loading = false
  70. }).catch(() => {
  71. this.loading = false
  72. })
  73. }).catch(() => {
  74. this.loading = false
  75. })
  76. },
  77. async getPolicy () {
  78. const params = {}
  79. if (this.editType === 'yaml') {
  80. params.format = 'yaml'
  81. }
  82. this.policyLoading = true
  83. try {
  84. const response = await this.$http.get(`/v1/policies/${this.$route.query.id}`, {
  85. params,
  86. })
  87. this.policyLoading = false
  88. return response
  89. } catch (error) {
  90. throw error
  91. }
  92. },
  93. getPermissions () {
  94. const bodyData = this.genPermissionsBodyData()
  95. return this.$http.post(`/v1/auth/permissions?policy=${this.$route.query.id}`, bodyData)
  96. },
  97. genPermissionsBodyData () {
  98. const ret = {}
  99. const scope = this.policy.scope
  100. for (let i = 0, len = this.POLICY_GROUPS.length; i < len; i++) {
  101. const item = this.POLICY_GROUPS[i]
  102. for (let j = 0, jlen = item.resources.length; j < jlen; j++) {
  103. const resource = item.resources[j]
  104. for (let k = 0, klen = DEFAULT_ACTIONS_KEY.length; k < klen; k++) {
  105. const action = DEFAULT_ACTIONS_KEY[k]
  106. ret[`${resource.resource}_${action}`] = [scope, item.service, resource.resource, action]
  107. }
  108. if (resource.extras && resource.extras) {
  109. resource.extras.forEach(extras => {
  110. ret[`${resource.resource}_${extras.action}_${extras.value}`] = [scope, item.service, resource.resource, extras.action, extras.value]
  111. })
  112. }
  113. }
  114. }
  115. return ret
  116. },
  117. async doUpdata () {
  118. this.submiting = true
  119. try {
  120. const data = await this.$refs.policyForm.getData()
  121. data.tag_update_policy = 'replace'
  122. data.tags_action = 'replace'
  123. await this.$http.patch(`/v1/auth/policies/${this.policy.id}`, data)
  124. this.$store.commit('keepAlive/ADD_DELAY_EVENT', { name: 'ResourceListSingleRefresh', params: this.policy.id })
  125. this.$router.push('/policy')
  126. } catch (error) {
  127. throw error
  128. } finally {
  129. this.submiting = false
  130. }
  131. },
  132. handleCancel () {
  133. this.$router.push('/policy')
  134. },
  135. handleEditTypeChange (type) {
  136. this.editType = type
  137. },
  138. },
  139. }
  140. </script>