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,36 @@ 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 .roles .length === 0 ) return
304+
305+ let targetRoleType = null
306+
307+ if (this .isAdmin ()) {
308+ targetRoleType = this .isNonRootDomain ? ' DomainAdmin' : ' Admin'
309+ } else if (this .isDomainAdmin ()) {
310+ targetRoleType = ' User'
311+ }
312+
313+ const targetRole = targetRoleType
314+ ? this .roles .find (role => role .type === targetRoleType)
315+ : this .roles [0 ]
316+
317+ this .form .roleid = (targetRole || this .roles [0 ]).id
318+ },
269319 async validateConfirmPassword (rule , value ) {
270320 if (! value || value .length === 0 ) {
271321 return Promise .resolve ()
@@ -286,17 +336,22 @@ export default {
286336 this .loadMore (' listDomains' , 1 , this .domain )
287337 },
288338 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
339+ const params = {
340+ listAll : true ,
341+ details : ' min ' ,
342+ pagesize : 100 ,
343+ page : page
344+ }
295345 var count
296346 getAPI (apiToCall, params).then (json => {
297347 const listDomains = json .listdomainsresponse .domain
298348 count = json .listdomainsresponse .count
299349 this .domainsList = this .domainsList .concat (listDomains)
350+ this .dom = this .domainsList .find (domain => domain .id === this .domain .id )
351+
352+ if (this .roles .length > 0 ) {
353+ this .setDefaultRole ()
354+ }
300355 }).finally (() => {
301356 if (count <= this .domainsList .length ) {
302357 sema .loading = false
@@ -307,17 +362,13 @@ export default {
307362 },
308363 fetchRoles () {
309364 this .roleLoading = true
310- const params = {}
311- params .state = ' enabled'
365+ const params = {
366+ state: ' enabled'
367+ }
368+
312369 getAPI (' listRoles' , params).then (response => {
313370 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- }
371+ this .setDefaultRole ()
321372 }).finally (() => {
322373 this .roleLoading = false
323374 })
0 commit comments