Skip to content

Commit 2c55244

Browse files
refactor handleSubmit of user creation form
1 parent 520b9c4 commit 2c55244

File tree

1 file changed

+64
-61
lines changed

1 file changed

+64
-61
lines changed

ui/src/views/iam/AddUser.vue

Lines changed: 64 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -313,76 +313,79 @@ export default {
313313
isValidValueForKey (obj, key) {
314314
return key in obj && obj[key] != null
315315
},
316-
handleSubmit (e) {
316+
async handleSubmit (e) {
317317
e.preventDefault()
318318
if (this.loading) return
319-
this.formRef.value.validate().then(() => {
320-
const values = toRaw(this.form)
321-
this.loading = true
322-
const params = {
323-
username: values.username,
324-
password: values.password,
325-
email: values.email,
326-
firstname: values.firstname,
327-
lastname: values.lastname,
328-
accounttype: 0
329-
}
330-
331-
if (this.account) {
332-
params.account = this.account
333-
} else if (this.accountList[values.account]) {
334-
params.account = this.accountList[values.account].name
335-
}
336319
337-
if (this.domainid) {
338-
params.domainid = this.domainid
339-
} else if (values.domainid) {
340-
params.domainid = values.domainid
341-
}
320+
await this.formRef.value.validate()
321+
.catch(error => this.formRef.value.scrollToField(error.errorFields[0].name))
342322
343-
if (this.isValidValueForKey(values, 'timezone') && values.timezone.length > 0) {
344-
params.timezone = values.timezone
345-
}
323+
this.loading = true
324+
const values = toRaw(this.form)
325+
try {
326+
const userCreationResponse = await this.createUser(values)
327+
this.$notification.success({
328+
message: this.$t('label.create.user'),
329+
description: `${this.$t('message.success.create.user')} ${values.username}`
330+
})
346331
347-
api('createUser', {}, 'POST', params).then(response => {
348-
this.$emit('refresh-data')
332+
const user = userCreationResponse?.createuserresponse?.user
333+
if (values.samlenable && user) {
334+
await api('authorizeSamlSso', {
335+
enable: values.samlenable,
336+
entityid: values.samlentity,
337+
userid: user.id
338+
})
349339
this.$notification.success({
350-
message: this.$t('label.create.user'),
351-
description: `${this.$t('message.success.create.user')} ${params.username}`
340+
message: this.$t('label.samlenable'),
341+
description: this.$t('message.success.enable.saml.auth')
352342
})
353-
const user = response.createuserresponse.user
354-
if (values.samlenable && user) {
355-
api('authorizeSamlSso', {
356-
enable: values.samlenable,
357-
entityid: values.samlentity,
358-
userid: user.id
359-
}).then(response => {
360-
this.$emit('refresh-data')
361-
this.$notification.success({
362-
message: this.$t('label.samlenable'),
363-
description: this.$t('message.success.enable.saml.auth')
364-
})
365-
}).catch(error => {
366-
this.$notification.error({
367-
message: this.$t('message.request.failed'),
368-
description: (error.response && error.response.headers && error.response.headers['x-description']) || error.message,
369-
duration: 0
370-
})
371-
})
372-
}
343+
}
344+
345+
this.closeAction()
346+
this.$emit('refresh-data')
347+
} catch (error) {
348+
if (error?.config?.params?.command === 'authorizeSamlSso') {
373349
this.closeAction()
374-
}).catch(error => {
375-
this.$notification.error({
376-
message: this.$t('message.request.failed'),
377-
description: (error.response && error.response.headers && error.response.headers['x-description']) || error.message,
378-
duration: 0
379-
})
380-
}).finally(() => {
381-
this.loading = false
350+
this.$emit('refresh-data')
351+
}
352+
353+
this.$notification.error({
354+
message: this.$t('message.request.failed'),
355+
description: error?.response?.headers['x-description'] || error.message,
356+
duration: 0
382357
})
383-
}).catch(error => {
384-
this.formRef.value.scrollToField(error.errorFields[0].name)
385-
})
358+
} finally {
359+
this.loading = false
360+
}
361+
},
362+
async createUser (rawParams) {
363+
const params = {
364+
username: rawParams.username,
365+
password: rawParams.password,
366+
email: rawParams.email,
367+
firstname: rawParams.firstname,
368+
lastname: rawParams.lastname,
369+
accounttype: 0
370+
}
371+
372+
if (this.account) {
373+
params.account = this.account
374+
} else if (this.accountList[rawParams.account]) {
375+
params.account = this.accountList[rawParams.account].name
376+
}
377+
378+
if (this.domainid) {
379+
params.domainid = this.domainid
380+
} else if (rawParams.domainid) {
381+
params.domainid = rawParams.domainid
382+
}
383+
384+
if (this.isValidValueForKey(rawParams, 'timezone') && rawParams.timezone.length > 0) {
385+
params.timezone = rawParams.timezone
386+
}
387+
388+
return api('createUser', {}, 'POST', params)
386389
},
387390
async validateConfirmPassword (rule, value) {
388391
if (!value || value.length === 0) {

0 commit comments

Comments
 (0)