Skip to content

Commit 6b180f1

Browse files
committed
UI: Create Account form to set proper domain and role based on route
1 parent b0d74fe commit 6b180f1

File tree

1 file changed

+80
-17
lines changed

1 file changed

+80
-17
lines changed

ui/src/views/iam/AddAccount.vue

Lines changed: 80 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
:placeholder="apiParams.domainid.description"
115115
showSearch
116116
optionFilterProp="label"
117+
@change="onDomainChange"
117118
:filterOption="(input, option) => {
118119
return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0
119120
}" >
@@ -207,8 +208,9 @@ export default {
207208
this.fetchTimeZone = debounce(this.fetchTimeZone, 800)
208209
return {
209210
loading: false,
210-
domain: { loading: false },
211+
domain: { id: null, loading: false },
211212
domainsList: [],
213+
dom: null,
212214
roleLoading: false,
213215
roles: [],
214216
timeZoneLoading: false,
@@ -227,14 +229,35 @@ export default {
227229
computed: {
228230
samlAllowed () {
229231
return 'authorizeSamlSso' in this.$store.getters.apis
232+
},
233+
selectedDomain () {
234+
return this.domainsList.find(domain => domain.id === this.form.domainid)
235+
},
236+
isNonRootDomain () {
237+
if (!this.selectedDomain) return false
238+
return this.selectedDomain.level > 0 && this.selectedDomain.path !== 'ROOT'
239+
}
240+
},
241+
watch: {
242+
'form.domainid': {
243+
handler (newDomainId, oldDomainId) {
244+
if (newDomainId && this.roles.length > 0) {
245+
this.$nextTick(() => {
246+
this.setDefaultRole()
247+
})
248+
}
249+
},
250+
immediate: false
230251
}
231252
},
232253
methods: {
233254
initForm () {
255+
var domId = this.$route.query.domainid || this.$store.getters.userInfo.domainid
234256
this.formRef = ref()
235257
this.form = reactive({
236-
domainid: this.$store.getters.userInfo.domainid
258+
domainid: domId
237259
})
260+
this.domain.id = domId
238261
this.rules = reactive({
239262
roleid: [{ required: true, message: this.$t('message.error.select') }],
240263
username: [{ required: true, message: this.$t('message.error.required.input') }],
@@ -263,9 +286,48 @@ export default {
263286
isDomainAdmin () {
264287
return this.$store.getters.userInfo.roletype === 'DomainAdmin'
265288
},
289+
isAdmin () {
290+
return this.$store.getters.userInfo.roletype === 'Admin'
291+
},
266292
isValidValueForKey (obj, key) {
267293
return key in obj && obj[key] != null
268294
},
295+
onDomainChange (newDomainId) {
296+
if (newDomainId && this.roles.length > 0) {
297+
this.$nextTick(() => {
298+
this.setDefaultRole()
299+
})
300+
}
301+
},
302+
setDefaultRole () {
303+
if (this.isAdmin()) {
304+
if (this.isNonRootDomain) {
305+
const domainAdminRole = this.roles.find(role => role.type === 'DomainAdmin')
306+
if (domainAdminRole) {
307+
this.form.roleid = domainAdminRole.id
308+
return
309+
}
310+
} else {
311+
const rootAdminRole = this.roles.find(role => role.type === 'Admin')
312+
if (rootAdminRole) {
313+
this.form.roleid = rootAdminRole.id
314+
return
315+
}
316+
}
317+
}
318+
319+
if (this.isDomainAdmin()) {
320+
const userRole = this.roles.find(role => role.type === 'User')
321+
if (userRole) {
322+
this.form.roleid = userRole.id
323+
return
324+
}
325+
}
326+
327+
if (this.roles.length > 0) {
328+
this.form.roleid = this.roles[0].id
329+
}
330+
},
269331
async validateConfirmPassword (rule, value) {
270332
if (!value || value.length === 0) {
271333
return Promise.resolve()
@@ -286,17 +348,22 @@ export default {
286348
this.loadMore('listDomains', 1, this.domain)
287349
},
288350
loadMore (apiToCall, page, sema) {
289-
console.log('sema.loading ' + sema.loading)
290-
const params = {}
291-
params.listAll = true
292-
params.details = 'min'
293-
params.pagesize = 100
294-
params.page = page
351+
const params = {
352+
listAll: true,
353+
details: 'min',
354+
pagesize: 100,
355+
page: page
356+
}
295357
var count
296358
getAPI(apiToCall, params).then(json => {
297359
const listDomains = json.listdomainsresponse.domain
298360
count = json.listdomainsresponse.count
299361
this.domainsList = this.domainsList.concat(listDomains)
362+
this.dom = this.domainsList.find(domain => domain.id === this.domain.id)
363+
364+
if (this.roles.length > 0) {
365+
this.setDefaultRole()
366+
}
300367
}).finally(() => {
301368
if (count <= this.domainsList.length) {
302369
sema.loading = false
@@ -307,17 +374,13 @@ export default {
307374
},
308375
fetchRoles () {
309376
this.roleLoading = true
310-
const params = {}
311-
params.state = 'enabled'
377+
const params = {
378+
state: 'enabled'
379+
}
380+
312381
getAPI('listRoles', params).then(response => {
313382
this.roles = response.listrolesresponse.role || []
314-
this.form.roleid = this.roles[0].id
315-
if (this.isDomainAdmin()) {
316-
const userRole = this.roles.filter(role => role.type === 'User')
317-
if (userRole.length > 0) {
318-
this.form.roleid = userRole[0].id
319-
}
320-
}
383+
this.setDefaultRole()
321384
}).finally(() => {
322385
this.roleLoading = false
323386
})

0 commit comments

Comments
 (0)