SetPolicies.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <template>
  2. <a-form
  3. :form="form.fc"
  4. v-bind="formItemLayout">
  5. <a-form-item :label="$t('dictionary.policy')">
  6. <list-select
  7. v-decorator="decorators.policies"
  8. :listProps="policiesSelectProps"
  9. :formatter="row => `${row.name} / ${row.description || ''}`"
  10. :multiple="true"
  11. :dialog-params="{ mask: false, width: 900, title: $t('common.tips.select', [$t('dictionary.policy')]) }" />
  12. </a-form-item>
  13. <a-collapse accordion :border="false">
  14. <a-collapse-panel key="1" :header="$t('common.adv_config')">
  15. <a-form-item :label="$t('system.text_328')">
  16. <template v-slot:extra>
  17. <div>{{$t('system.text_329')}}</div>
  18. <div>{{$t('system.text_331')}}</div>
  19. </template>
  20. <a-input v-decorator="decorators.ips" />
  21. </a-form-item>
  22. <a-form-item :label="$t('res.project')">
  23. <template v-slot:extra>
  24. <div>{{$t('system.text_585')}}</div>
  25. </template>
  26. <base-select
  27. v-decorator="decorators.project"
  28. resource="projects"
  29. version="v1"
  30. remote
  31. :params="projectParams"
  32. :remote-fn="q => ({ filter: `name.contains(${q})` })"
  33. :select-props="{ mode: 'default' }" />
  34. </a-form-item>
  35. <a-form-item :label="$t('iam.role_policy_valid_time_range')">
  36. <a-date-picker :disabled-date="dateDisabledStart" v-decorator="decorators.valid_since" @change="startChange" format="YYYY-MM-DD hh:mm" :showTime="{ format: 'HH:mm' }" />
  37. <span class="ml-2 mr-2">~</span>
  38. <a-date-picker :disabled-date="dateDisabledEnd" v-decorator="decorators.valid_until" @change="endChange" format="YYYY-MM-DD hh:mm" :showTime="{ format: 'HH:mm' }" />
  39. </a-form-item>
  40. </a-collapse-panel>
  41. </a-collapse>
  42. </a-form>
  43. </template>
  44. <script>
  45. import { getNameFilter } from '@/utils/common/tableFilter'
  46. import { REGEXP } from '@/utils/validate'
  47. import ListSelect from '@/sections/ListSelect'
  48. export default {
  49. name: 'SetPoliciesForm',
  50. components: {
  51. ListSelect,
  52. },
  53. props: {
  54. data: {
  55. type: Array,
  56. },
  57. },
  58. data () {
  59. return {
  60. form: {
  61. fc: this.$form.createForm(this),
  62. },
  63. decorators: {
  64. policies: [
  65. 'policies',
  66. {
  67. rules: [
  68. {
  69. required: true,
  70. message: this.$t('common.tips.select', [this.$t('dictionary.policy')]),
  71. },
  72. ],
  73. },
  74. ],
  75. ips: [
  76. 'ips',
  77. {
  78. rules: [
  79. {
  80. validator (rule, value, callback) {
  81. if (value && value.length > 0) {
  82. const ips = value.split(',')
  83. ips.forEach(ip => {
  84. if (!REGEXP.cidr.regexp.test(ip)) {
  85. return callback(new Error(this.$t('system.text_339', [ip])))
  86. }
  87. })
  88. }
  89. callback()
  90. },
  91. },
  92. ],
  93. },
  94. ],
  95. project: [
  96. 'project',
  97. ],
  98. valid_since: [
  99. 'valid_since',
  100. ],
  101. valid_until: [
  102. 'valid_until',
  103. ],
  104. },
  105. formItemLayout: {
  106. wrapperCol: {
  107. span: 20,
  108. },
  109. labelCol: {
  110. span: 4,
  111. },
  112. },
  113. }
  114. },
  115. computed: {
  116. scopeParams () {
  117. const scopeParams = {}
  118. if (!this.data) return scopeParams
  119. if (this.$store.getters.isAdminMode) {
  120. scopeParams.project_domain = this.data[0].domain_id
  121. } else {
  122. scopeParams.scope = this.$store.getters.scope
  123. }
  124. return scopeParams
  125. },
  126. projectParams () {
  127. return { ...this.scopeParams }
  128. },
  129. policiesSelectProps () {
  130. return {
  131. list: this.$list.createList(this, {
  132. resource: 'policies',
  133. apiVersion: 'v1',
  134. genParamsCb: (params) => {
  135. const genParams = { ...params, ...this.scopeParams }
  136. delete genParams.scope
  137. return genParams
  138. },
  139. filterOptions: {
  140. name: getNameFilter(),
  141. },
  142. }),
  143. columns: [
  144. {
  145. field: 'name',
  146. title: this.$t('table.title.name'),
  147. showOverflow: 'title',
  148. },
  149. {
  150. field: 'description',
  151. title: this.$t('table.title.introduction'),
  152. showOverflow: 'title',
  153. },
  154. ],
  155. }
  156. },
  157. },
  158. destroyed () {
  159. this.manager = null
  160. },
  161. created () {
  162. this.manager = new this.$Manager('roles', 'v1')
  163. },
  164. methods: {
  165. async set (rid, action = 'repace') {
  166. try {
  167. const values = await this.form.fc.validateFields()
  168. const policies = []
  169. let ips
  170. if (values.ips) {
  171. ips = values.ips.split(',')
  172. }
  173. if (values.policies && values.policies.length) {
  174. for (let i = 0, len = values.policies.length; i < len; i++) {
  175. const item = {
  176. policy_id: values.policies[i],
  177. }
  178. if (ips) {
  179. item.ips = ips
  180. }
  181. if (values.project) {
  182. item.project_id = values.project
  183. }
  184. if (values.valid_since) {
  185. item.valid_since = values.valid_since
  186. }
  187. if (values.valid_until) {
  188. item.valid_until = values.valid_until
  189. }
  190. policies.push(item)
  191. }
  192. }
  193. const response = await this.manager.performAction({
  194. id: rid,
  195. action: 'set-policies',
  196. data: {
  197. policies,
  198. action,
  199. },
  200. })
  201. return response
  202. } catch (error) {
  203. throw error
  204. }
  205. },
  206. startChange (value) {
  207. this.form.fc.setFieldsValue({
  208. valid_since: value,
  209. })
  210. },
  211. endChange (value) {
  212. this.form.fc.setFieldsValue({
  213. valid_until: value > this.$moment() ? this.$moment() : value,
  214. })
  215. },
  216. dateDisabledStart (value) {
  217. const dateEnd = this.form.fc.getFieldValue('valid_until')
  218. if (dateEnd && value > dateEnd) return true
  219. if (value < this.$moment()) return true
  220. return false
  221. },
  222. dateDisabledEnd (value) {
  223. const dateStart = this.form.fc.getFieldValue('valid_since')
  224. if (dateStart && value < dateStart) return true
  225. if (value < this.$moment().endOf('day')) return true
  226. return false
  227. },
  228. },
  229. }
  230. </script>