SamluserCreate.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{ $t('user.create_samluser') }}</div>
  4. <div slot="body">
  5. <a-form
  6. :form="form.fc"
  7. v-bind="formItemLayout">
  8. <a-form-item :label="$t('cloudenv.clouduser_list_t4')">
  9. <span>{{ params.user.name }}</span>
  10. </a-form-item>
  11. <a-form-item :label="$t('dictionary.project')">
  12. <base-select
  13. v-decorator="decorators.project_id"
  14. filterable
  15. :item.sync="form.fi.project"
  16. :options="projects">
  17. <template #optionTemplate="{ options }">
  18. <a-select-option v-for="item in options" :key="item.id" :value="item.id">
  19. {{ item.name }}<span class="text-color-secondary ml-2">{{ $t('common_624', [$t('dictionary.domain')]) }}: </span>{{ item.project_domain }}
  20. </a-select-option>
  21. </template>
  22. </base-select>
  23. </a-form-item>
  24. <a-form-item :label="$t('dictionary.cloudaccount')">
  25. <a-row :gutter="8">
  26. <a-col :span="12">
  27. <a-select v-model="form.fi.provider">
  28. <template v-for="item of providerOptions">
  29. <a-select-option :key="item[0]" :value="item[1].provider">
  30. <span class="text-color-secondary">{{ $t('common.brand') }}: </span>{{ item[1].label }}
  31. </a-select-option>
  32. </template>
  33. </a-select>
  34. </a-col>
  35. <a-col :span="12">
  36. <base-select
  37. v-decorator="decorators.cloudaccount_id"
  38. resource="cloudaccounts"
  39. filterable
  40. :item.sync="form.fi.cloudaccount"
  41. :params="cloudaccountParams"
  42. :mapper="cloudaccountMapper">
  43. <template #optionTemplate="{ options }">
  44. <a-select-option v-for="item in options" :key="item.id" :value="item.id">
  45. <span class="text-color-secondary">{{ $t('dictionary.cloudaccount') }}: </span>{{ item.name }}
  46. </a-select-option>
  47. </template>
  48. </base-select>
  49. </a-col>
  50. </a-row>
  51. </a-form-item>
  52. <template v-if="isGoogle">
  53. <a-form-item :label="$t('dictionary.cloudprovider')">
  54. <base-select
  55. v-decorator="decorators.cloudprovider_id"
  56. resource="cloudproviders"
  57. filterable
  58. :params="cloudproviderParams" />
  59. </a-form-item>
  60. </template>
  61. <a-form-item :label="$t('dictionary.cloudgroup')" :extra="$t('cloudenv.clouduser_text2')">
  62. <list-select
  63. v-decorator="decorators.cloudgroup_id"
  64. :listProps="cloudgroupListSelectProps"
  65. :formatter="formatterLabel"
  66. :dialog-params="{ mask: false }"
  67. :multiple="false" />
  68. </a-form-item>
  69. </a-form>
  70. </div>
  71. <div slot="footer">
  72. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  73. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  74. </div>
  75. </base-dialog>
  76. </template>
  77. <script>
  78. import get from 'lodash/get'
  79. import * as R from 'ramda'
  80. import DialogMixin from '@/mixins/dialog'
  81. import WindowsMixin from '@/mixins/windows'
  82. import ListSelect from '@/sections/ListSelect'
  83. import { getNameFilter } from '@/utils/common/tableFilter'
  84. import { HYPERVISORS_MAP } from '@/constants'
  85. export default {
  86. name: 'SamluserCreateForUserDialog',
  87. components: {
  88. ListSelect,
  89. },
  90. mixins: [DialogMixin, WindowsMixin],
  91. data () {
  92. const providerOptions = Object.entries(HYPERVISORS_MAP).filter(item => {
  93. return (this.$store.getters.capability.saml_auth_brands || []).includes(item[1].provider) && this.$store.getters.capability.brands.includes(item[1].provider)
  94. })
  95. const firstProvider = get(providerOptions, '[0][1].provider')
  96. return {
  97. loading: false,
  98. form: {
  99. fc: this.$form.createForm(this),
  100. fi: {
  101. provider: firstProvider,
  102. cloudaccount: {},
  103. project: {},
  104. },
  105. },
  106. projects: [],
  107. decorators: {
  108. project_id: [
  109. 'project_id',
  110. {
  111. rules: [
  112. { required: true, message: this.$t('common.select') },
  113. ],
  114. },
  115. ],
  116. owner_id: [
  117. 'owner_id',
  118. {
  119. rules: [
  120. { required: true, message: this.$t('common.select') },
  121. ],
  122. },
  123. ],
  124. cloudaccount_id: [
  125. 'cloudaccount_id',
  126. {
  127. rules: [
  128. { required: true, message: this.$t('common.select') },
  129. ],
  130. },
  131. ],
  132. cloudprovider_id: [
  133. 'cloudprovider_id',
  134. {
  135. rules: [
  136. { required: true, message: this.$t('common.select') },
  137. ],
  138. },
  139. ],
  140. cloudgroup_id: [
  141. 'cloudgroup_id',
  142. {
  143. rules: [
  144. { required: true, message: this.$t('common.select') },
  145. // { required: firstProvider === 'Google', message: this.$t('common.select') },
  146. ],
  147. },
  148. ],
  149. },
  150. providerOptions,
  151. cloudgroupListSelectProps: {
  152. list: this.$list.createList(this, {
  153. id: this.id,
  154. resource: 'cloudgroups',
  155. apiVersion: 'v1',
  156. getParams: () => {
  157. return {
  158. scope: this.$store.getters.scope,
  159. provider: this.form.fi.provider,
  160. }
  161. },
  162. filterOptions: {
  163. name: getNameFilter(),
  164. },
  165. }),
  166. columns: [
  167. {
  168. field: 'name',
  169. title: this.$t('table.title.name'),
  170. showOverflow: 'title',
  171. },
  172. {
  173. field: 'description',
  174. title: this.$t('system.text_193'),
  175. showOverflow: 'title',
  176. },
  177. {
  178. field: 'cloudpolicies',
  179. title: this.$t('cloudenv.text_329'),
  180. type: 'expand',
  181. slots: {
  182. default: ({ row }) => {
  183. return [this.$t('cloudenv.text_245', [(row.cloudpolicies && row.cloudpolicies.length) || 0])]
  184. },
  185. content: ({ row }) => {
  186. if (R.isNil(row.cloudpolicies) || R.isEmpty(row.cloudpolicies)) return this.$t('cloudenv.text_330')
  187. return [
  188. <vxe-grid
  189. showOverflow='title'
  190. data={ row.cloudpolicies }
  191. columns={[
  192. {
  193. field: 'name',
  194. title: this.$t('common.name'),
  195. },
  196. {
  197. field: 'description',
  198. title: this.$t('table.title.desc'),
  199. formatter: ({ cellValue }) => cellValue || '-',
  200. },
  201. ]} />,
  202. ]
  203. },
  204. },
  205. },
  206. ],
  207. },
  208. formItemLayout: {
  209. wrapperCol: {
  210. span: 19,
  211. },
  212. labelCol: {
  213. span: 5,
  214. },
  215. },
  216. }
  217. },
  218. computed: {
  219. cloudaccountParams () {
  220. const params = {
  221. scope: this.$store.getters.scope,
  222. provider: this.form.fi.provider,
  223. enabled: true,
  224. health_status: 'normal',
  225. status: 'connected',
  226. tenant_id: this.form.fi.project.id,
  227. }
  228. if (this.$store.getters.isAdminMode) {
  229. params.project_domain = this.form.fi.project.domain_id
  230. }
  231. if (this.$store.getters.isDomainMode) {
  232. params.project_domains = [this.form.fi.project.domain_id]
  233. }
  234. return params
  235. },
  236. cloudproviderParams () {
  237. return {
  238. scope: this.$store.getters.scope,
  239. cloudaccount_id: this.form.fi.cloudaccount.id || '',
  240. tenant_id: this.form.fi.project.id,
  241. }
  242. },
  243. isGoogle () {
  244. return this.form.fi.provider === 'Google'
  245. },
  246. namePlaceholder () {
  247. return this.isGoogle ? this.$t('system.text_496') : this.params.user.name
  248. },
  249. nameExtra () {
  250. return this.isGoogle ? null : this.$t('cloudenv.clouduser_text1')
  251. },
  252. },
  253. watch: {
  254. isGoogle (val) {
  255. this.decorators.generate_name[1].rules = val ? [
  256. { required: true, message: this.$t('common.text00042') },
  257. { validator: this.$validate('email'), message: this.$t('system.text_497') },
  258. ] : []
  259. if (val) {
  260. this.decorators.cloudgroup_id[1].rules[0].required = true
  261. } else {
  262. this.decorators.cloudgroup_id[1].rules[0].required = false
  263. }
  264. this.form.fc.resetFields(['generate_name', 'cloudgroup_id'])
  265. },
  266. 'form.fi.cloudaccount.provider' (val) {
  267. this.form.fc.resetFields(['cloudgroup_id', 'cloudprovider_id'])
  268. },
  269. 'form.fi.project.id' (val) {
  270. this.form.fc.resetFields(['cloudgroup_id', 'cloudprovider_id'])
  271. },
  272. },
  273. destroyed () {
  274. this.um = null
  275. },
  276. created () {
  277. this.um = new this.$Manager('users', 'v1')
  278. this.fetchProjects()
  279. },
  280. methods: {
  281. async fetchProjects () {
  282. try {
  283. const response = await this.um.getSpecific({
  284. id: this.params.user.id,
  285. spec: 'projects',
  286. params: {
  287. scope: this.$store.getters.scope,
  288. },
  289. })
  290. this.projects = response.data.data || []
  291. this.form.fc.setFieldsValue({
  292. project_id: this.projects[0].id,
  293. })
  294. } catch (error) {
  295. throw error
  296. }
  297. },
  298. async handleConfirm () {
  299. this.loading = true
  300. try {
  301. const { ...values } = await this.form.fc.validateFields()
  302. values.owner_id = this.params.user.id
  303. values.project_domain = this.form.fi.project.project_domain
  304. await this.params.onManager('create', {
  305. managerArgs: {
  306. data: values,
  307. },
  308. })
  309. this.cancelDialog()
  310. } finally {
  311. this.loading = false
  312. }
  313. },
  314. formatterLabel (row) {
  315. return row.description ? `${row.name} / ${row.description}` : row.name
  316. },
  317. cloudaccountMapper (data) {
  318. let _data = [...data]
  319. _data = _data.filter(item => {
  320. if (this.form.fi.provider !== 'Google' && item.share_mode === 'provider_domain' && item.project_domain !== this.form.fi.project.project_domain) {
  321. return false
  322. }
  323. return true
  324. })
  325. return _data
  326. },
  327. },
  328. }
  329. </script>