CreatePolicyDialog.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('storage.text_238')}}</div>
  4. <div slot="body">
  5. <a-form
  6. :form="form.fc">
  7. <a-form-item :label="$t('storage.text_239')" v-bind="formItemLayout">
  8. <a-radio-group v-decorator="decorators.Effect">
  9. <a-radio-button value="Allow">{{$t('storage.text_217')}}</a-radio-button>
  10. </a-radio-group>
  11. </a-form-item>
  12. <a-form-item :label="$t('storage.text_240')" v-bind="formItemLayout" required>
  13. <a-row>
  14. <a-col :span="11" class="mr-1">
  15. <a-form-item class="mb-0">
  16. <a-select v-decorator="decorators.PrincipalType" @change="handleTypeChange">
  17. <a-select-option value="root">{{$t('storage.text_241')}}</a-select-option>
  18. <a-select-option value="child">{{$t('storage.text_242')}}</a-select-option>
  19. </a-select>
  20. </a-form-item>
  21. </a-col>
  22. <a-col :span="12">
  23. <a-form-item class="mb-0">
  24. <a-input v-if="isRoot" v-decorator="[
  25. 'PrincipalId',
  26. {
  27. rules: [{ required: true, message: $t('storage.text_243_1') }, { validator: this.validateRootId }]
  28. },
  29. ]" :placeholder="$t('storage.text_243_1')" />
  30. <base-select
  31. v-else
  32. v-decorator="decorators.PrincipalId"
  33. class="w-100"
  34. :remote="true"
  35. needParams
  36. resource="cloudusers"
  37. version="v1"
  38. idKey="external_id"
  39. :params="principalParams"
  40. :select-props="{ placeholder: $t('storage.text_243'), allowClear: true, mode: 'multiple' }" />
  41. </a-form-item>
  42. </a-col>
  43. </a-row>
  44. </a-form-item>
  45. <a-form-item :label="$t('storage.text_244')" v-bind="formItemLayout">
  46. <a-radio-group v-decorator="decorators.resource" @change="handleResourceTypeChange">
  47. <a-radio value="entirebucket">{{$t('storage.text_245')}}</a-radio>
  48. <a-radio value="designatedResource">{{$t('storage.text_246')}}</a-radio>
  49. </a-radio-group>
  50. </a-form-item>
  51. <a-form-item :label="$t('storage.text_247')" v-bind="formItemLayout">
  52. <div class="d-flex">
  53. <div>
  54. {{ params.bucketData.name }}/
  55. </div>
  56. <div class="flex-grow-1">
  57. <a-textarea :disabled="isEntirebucket" v-decorator="decorators.ResourcePath" :placeholder="$t('storage.text_250')" :auto-size="{ minRows: 3, maxRows: 5 }" />
  58. </div>
  59. </div>
  60. </a-form-item>
  61. <a-form-item :label="$t('storage.text_251')" v-bind="formItemLayout">
  62. <a-radio-group v-decorator="decorators.CannedAction">
  63. <a-radio-button value="Read">{{$t('storage.text_252')}}</a-radio-button>
  64. <a-radio-button value="ReadWrite">{{$t('storage.text_253')}}</a-radio-button>
  65. <a-radio-button value="FullControl">{{$t('storage.text_254')}}</a-radio-button>
  66. </a-radio-group>
  67. </a-form-item>
  68. </a-form>
  69. </div>
  70. <div slot="footer">
  71. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  72. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  73. </div>
  74. </base-dialog>
  75. </template>
  76. <script>
  77. import { mapGetters } from 'vuex'
  78. import DialogMixin from '@/mixins/dialog'
  79. import WindowsMixin from '@/mixins/windows'
  80. export default {
  81. name: 'CreatePolicyDialog',
  82. mixins: [DialogMixin, WindowsMixin],
  83. data () {
  84. return {
  85. loading: false,
  86. type: 'child',
  87. resourceType: 'entirebucket',
  88. form: {
  89. fc: this.$form.createForm(this),
  90. },
  91. decorators: {
  92. Effect: [
  93. 'Effect',
  94. {
  95. initialValue: 'Allow',
  96. },
  97. ],
  98. PrincipalType: [
  99. 'PrincipalType',
  100. {
  101. initialValue: 'child',
  102. },
  103. ],
  104. PrincipalId: [
  105. 'PrincipalId',
  106. {
  107. rules: [
  108. { required: true, message: this.$t('storage.text_255') },
  109. ],
  110. },
  111. ],
  112. resource: [
  113. 'resource',
  114. {
  115. initialValue: 'entirebucket',
  116. },
  117. ],
  118. ResourcePath: [
  119. 'ResourcePath',
  120. {
  121. initialValue: '*',
  122. rules: [
  123. { required: true, message: this.$t('storage.text_256') },
  124. ],
  125. },
  126. ],
  127. CannedAction: [
  128. 'CannedAction',
  129. {
  130. initialValue: 'Read',
  131. },
  132. ],
  133. },
  134. formItemLayout: {
  135. wrapperCol: {
  136. span: 20,
  137. },
  138. labelCol: {
  139. span: 4,
  140. },
  141. },
  142. principalParams: {
  143. scope: this.$store.getters.scope,
  144. show_fail_reason: true,
  145. details: true,
  146. cloudaccount: this.params.bucketData.account_id,
  147. limit: 20,
  148. },
  149. }
  150. },
  151. computed: {
  152. ...mapGetters(['isAdminMode', 'scope', 'userInfo']),
  153. isRoot () {
  154. return this.type === 'root'
  155. },
  156. isEntirebucket () {
  157. return this.resourceType === 'entirebucket'
  158. },
  159. },
  160. methods: {
  161. handleTypeChange (value) {
  162. this.type = value
  163. this.form.fc.resetFields(['PrincipalId'])
  164. },
  165. handleResourceTypeChange (e) {
  166. if (e.target.value === 'entirebucket') {
  167. this.form.fc.setFieldsValue({ ResourcePath: '*' })
  168. } else {
  169. this.form.fc.setFieldsValue({ ResourcePath: '' })
  170. }
  171. this.resourceType = e.target.value
  172. },
  173. genData (data) {
  174. const { Effect, CannedAction } = data
  175. const ret = {
  176. Effect,
  177. CannedAction,
  178. }
  179. if (data.PrincipalType === 'child') {
  180. ret.PrincipalId = data.PrincipalId.map(item => {
  181. return ':' + item
  182. })
  183. } else {
  184. ret.PrincipalId = data.PrincipalId + ':'
  185. }
  186. ret.ResourcePath = data.ResourcePath.trim().split('\n')
  187. ret.ResourcePath = ret.ResourcePath.map(item => '/' + item)
  188. return ret
  189. },
  190. doCreate (data) {
  191. return new this.$Manager('buckets').performAction({
  192. id: this.params.bucketID,
  193. action: 'set-policy',
  194. data: data,
  195. })
  196. },
  197. async handleConfirm () {
  198. this.loading = true
  199. try {
  200. const values = await this.form.fc.validateFields()
  201. const data = this.genData(values)
  202. await this.doCreate(data)
  203. this.loading = false
  204. this.cancelDialog()
  205. this.params.refresh()
  206. } catch (error) {
  207. this.loading = false
  208. }
  209. },
  210. validateRootId (rule, value, callback) {
  211. if (!value) {
  212. callback()
  213. return
  214. }
  215. if (/^[0-9]*$/.test(value)) {
  216. callback()
  217. } else {
  218. callback(new Error(this.$t('storage.number_validate')))
  219. }
  220. },
  221. },
  222. }
  223. </script>