Input validation on username and improved error handling

This commit is contained in:
Benedikt Kulmann
2020-09-10 17:38:28 +02:00
parent 93ecc5c2e8
commit 5e2130a1e6
3 changed files with 86 additions and 77 deletions
+3 -3
View File
File diff suppressed because one or more lines are too long
+15 -3
View File
@@ -121,14 +121,21 @@ export default {
},
createAccount () {
// note: use bitwise AND because we want all checks to be performed
if (!(this.checkUsername() & this.checkEmail() & this.checkPassword())) {
return
}
this.isRequestInProgress = true
this.createNewAccount(this.formData).finally(() => {
this.cancelForm()
})
this.createNewAccount(this.formData)
.then((success) => {
if (success) {
this.cancelForm()
}
})
.finally(() => {
this.isRequestInProgress = false
})
},
checkUsername () {
@@ -136,6 +143,11 @@ export default {
debounce(this.formValidation.usernameError = this.$gettext('Username cannot be empty'), 500)
return false
}
// hacky check: we want to allow emails and the username part of emails as username
if (!isEmail(this.formData.username) && !isEmail(this.formData.username + '@validate.it')) {
debounce(this.formValidation.usernameError = this.$gettext('Invalid username'), 500)
return false
}
this.formValidation.usernameError = ''
return true
+68 -71
View File
@@ -138,77 +138,77 @@ const actions = {
continue
}
const response = await AccountsService_UpdateAccount({
$domain: rootGetters.configuration.server,
body: {
account: {
id: account.id,
accountEnabled: activated
},
update_mask: {
paths: ['AccountEnabled']
try {
const response = await AccountsService_UpdateAccount({
$domain: rootGetters.configuration.server,
body: {
account: {
id: account.id,
accountEnabled: activated
},
update_mask: {
paths: ['AccountEnabled']
}
}
})
if (response.status === 201) {
commit('UPDATE_ACCOUNT', { ...account, accountEnabled: activated })
} else {
failedAccounts.push({ account: account.username })
}
})
if (response.status === 201) {
commit('UPDATE_ACCOUNT', { ...account, accountEnabled: activated })
} else {
failedAccounts.push({ account: account.displayName, statusText: response.statusText })
} catch (error) {
failedAccounts.push({ account: account.username })
}
}
if (failedAccounts.length === 1) {
const failedMessageTitle = activated ? 'Failed to activate account.' : 'Failed to block account.'
if (failedAccounts.length > 0) {
let errorTitle = ''
if (failedAccounts.length === 1) {
errorTitle = activated ? 'Failed to activate account.' : 'Failed to block account.'
} else {
errorTitle = activated ? 'Failed to activate accounts.' : 'Failed to block accounts.'
}
dispatch('showMessage', {
title: failedMessageTitle,
desc: failedAccounts[0].statusText,
status: 'danger'
}, { root: true })
}
if (failedAccounts.length > 1) {
const failedMessageTitle = activated ? 'Failed to activate accounts.' : 'Failed to block accounts.'
const failedMessageDesc = activated ? 'Could not activate multiple accounts.' : 'Could not block multiple accounts.'
dispatch('showMessage', {
title: failedMessageTitle,
desc: failedMessageDesc,
title: errorTitle,
status: 'danger'
}, { root: true })
return Promise.resolve(false)
}
commit('RESET_ACCOUNTS_SELECTION')
return Promise.resolve(true)
},
async createNewAccount ({ rootGetters, commit, dispatch }, account) {
injectAuthToken(rootGetters.user.token)
const response = await AccountsService_CreateAccount({
$domain: rootGetters.configuration.server,
body: {
account: {
on_premises_sam_account_name: account.username,
preferred_name: account.username,
mail: account.email,
password_profile: {
password: account.password
},
account_enabled: true,
display_name: account.username
try {
const response = await AccountsService_CreateAccount({
$domain: rootGetters.configuration.server,
body: {
account: {
on_premises_sam_account_name: account.username,
preferred_name: account.username,
mail: account.email,
password_profile: {
password: account.password
},
account_enabled: true,
display_name: account.username
}
}
})
if (response.status === 201) {
commit('PUSH_NEW_ACCOUNT', response.data)
return Promise.resolve(true)
}
})
if (response.status === 201) {
commit('PUSH_NEW_ACCOUNT', response.data)
} else {
} catch (error) {
dispatch('showMessage', {
title: 'Failed to create account',
desc: response.statusText,
title: 'Failed to create account.',
status: 'danger'
}, { root: true })
return Promise.reject(error)
}
return Promise.resolve(false)
},
async deleteAccounts ({ rootGetters, state, commit, dispatch }) {
@@ -217,37 +217,34 @@ const actions = {
injectAuthToken(rootGetters.user.token)
for (const account of state.selectedAccounts) {
const response = await AccountsService_DeleteAccount({
$domain: rootGetters.configuration.server,
body: {
id: account.id
try {
const response = await AccountsService_DeleteAccount({
$domain: rootGetters.configuration.server,
body: {
id: account.id
}
})
if (response.status === 201 || response.status === 204) {
commit('DELETE_ACCOUNT', account.id)
} else {
failedAccounts.push({ account: account.username })
}
})
if (response.status === 201 || response.status === 204) {
commit('DELETE_ACCOUNT', account.id)
} else {
failedAccounts.push({ account: account.diisplayName, statusText: response.statusText })
} catch (error) {
failedAccounts.push({ account: account.username })
}
}
if (failedAccounts.length === 1) {
if (failedAccounts.length > 0) {
const errorTitle = failedAccounts.length === 1 ? 'Failed to delete account.' : 'Failed to delete accounts.'
dispatch('showMessage', {
title: 'Failed to delete account',
desc: failedAccounts[0].statusText,
status: 'danger'
}, { root: true })
}
if (failedAccounts.length > 1) {
dispatch('showMessage', {
title: 'Failed to delete accounts',
desc: 'Could not delete multiple accounts',
title: errorTitle,
status: 'danger'
}, { root: true })
return Promise.resolve(false)
}
commit('RESET_ACCOUNTS_SELECTION')
return Promise.resolve(true)
}
}