This is an automated email from the ASF dual-hosted git repository.

pearl11594 pushed a commit to branch enh-role-domain-mapping-acc-form
in repository https://gitbox.apache.org/repos/asf/cloudstack.git

commit 6b180f1394baed9bdf91fc17ee930154e408835f
Author: Pearl Dsilva <[email protected]>
AuthorDate: Fri Dec 5 12:37:39 2025 -0500

    UI: Create Account form to set proper domain and role based on route
---
 ui/src/views/iam/AddAccount.vue | 97 +++++++++++++++++++++++++++++++++--------
 1 file changed, 80 insertions(+), 17 deletions(-)

diff --git a/ui/src/views/iam/AddAccount.vue b/ui/src/views/iam/AddAccount.vue
index 9118a61e7fe..488e9853554 100644
--- a/ui/src/views/iam/AddAccount.vue
+++ b/ui/src/views/iam/AddAccount.vue
@@ -114,6 +114,7 @@
             :placeholder="apiParams.domainid.description"
             showSearch
             optionFilterProp="label"
+            @change="onDomainChange"
             :filterOption="(input, option) => {
               return  option.label.toLowerCase().indexOf(input.toLowerCase()) 
>= 0
             }" >
@@ -207,8 +208,9 @@ export default {
     this.fetchTimeZone = debounce(this.fetchTimeZone, 800)
     return {
       loading: false,
-      domain: { loading: false },
+      domain: { id: null, loading: false },
       domainsList: [],
+      dom: null,
       roleLoading: false,
       roles: [],
       timeZoneLoading: false,
@@ -227,14 +229,35 @@ export default {
   computed: {
     samlAllowed () {
       return 'authorizeSamlSso' in this.$store.getters.apis
+    },
+    selectedDomain () {
+      return this.domainsList.find(domain => domain.id === this.form.domainid)
+    },
+    isNonRootDomain () {
+      if (!this.selectedDomain) return false
+      return this.selectedDomain.level > 0 && this.selectedDomain.path !== 
'ROOT'
+    }
+  },
+  watch: {
+    'form.domainid': {
+      handler (newDomainId, oldDomainId) {
+        if (newDomainId && this.roles.length > 0) {
+          this.$nextTick(() => {
+            this.setDefaultRole()
+          })
+        }
+      },
+      immediate: false
     }
   },
   methods: {
     initForm () {
+      var domId = this.$route.query.domainid || 
this.$store.getters.userInfo.domainid
       this.formRef = ref()
       this.form = reactive({
-        domainid: this.$store.getters.userInfo.domainid
+        domainid: domId
       })
+      this.domain.id = domId
       this.rules = reactive({
         roleid: [{ required: true, message: this.$t('message.error.select') }],
         username: [{ required: true, message: 
this.$t('message.error.required.input') }],
@@ -263,9 +286,48 @@ export default {
     isDomainAdmin () {
       return this.$store.getters.userInfo.roletype === 'DomainAdmin'
     },
+    isAdmin () {
+      return this.$store.getters.userInfo.roletype === 'Admin'
+    },
     isValidValueForKey (obj, key) {
       return key in obj && obj[key] != null
     },
+    onDomainChange (newDomainId) {
+      if (newDomainId && this.roles.length > 0) {
+        this.$nextTick(() => {
+          this.setDefaultRole()
+        })
+      }
+    },
+    setDefaultRole () {
+      if (this.isAdmin()) {
+        if (this.isNonRootDomain) {
+          const domainAdminRole = this.roles.find(role => role.type === 
'DomainAdmin')
+          if (domainAdminRole) {
+            this.form.roleid = domainAdminRole.id
+            return
+          }
+        } else {
+          const rootAdminRole = this.roles.find(role => role.type === 'Admin')
+          if (rootAdminRole) {
+            this.form.roleid = rootAdminRole.id
+            return
+          }
+        }
+      }
+
+      if (this.isDomainAdmin()) {
+        const userRole = this.roles.find(role => role.type === 'User')
+        if (userRole) {
+          this.form.roleid = userRole.id
+          return
+        }
+      }
+
+      if (this.roles.length > 0) {
+        this.form.roleid = this.roles[0].id
+      }
+    },
     async validateConfirmPassword (rule, value) {
       if (!value || value.length === 0) {
         return Promise.resolve()
@@ -286,17 +348,22 @@ export default {
       this.loadMore('listDomains', 1, this.domain)
     },
     loadMore (apiToCall, page, sema) {
-      console.log('sema.loading ' + sema.loading)
-      const params = {}
-      params.listAll = true
-      params.details = 'min'
-      params.pagesize = 100
-      params.page = page
+      const params = {
+        listAll: true,
+        details: 'min',
+        pagesize: 100,
+        page: page
+      }
       var count
       getAPI(apiToCall, params).then(json => {
         const listDomains = json.listdomainsresponse.domain
         count = json.listdomainsresponse.count
         this.domainsList = this.domainsList.concat(listDomains)
+        this.dom = this.domainsList.find(domain => domain.id === 
this.domain.id)
+
+        if (this.roles.length > 0) {
+          this.setDefaultRole()
+        }
       }).finally(() => {
         if (count <= this.domainsList.length) {
           sema.loading = false
@@ -307,17 +374,13 @@ export default {
     },
     fetchRoles () {
       this.roleLoading = true
-      const params = {}
-      params.state = 'enabled'
+      const params = {
+        state: 'enabled'
+      }
+
       getAPI('listRoles', params).then(response => {
         this.roles = response.listrolesresponse.role || []
-        this.form.roleid = this.roles[0].id
-        if (this.isDomainAdmin()) {
-          const userRole = this.roles.filter(role => role.type === 'User')
-          if (userRole.length > 0) {
-            this.form.roleid = userRole[0].id
-          }
-        }
+        this.setDefaultRole()
       }).finally(() => {
         this.roleLoading = false
       })

Reply via email to