github.com/loafoe/cli@v7.1.0+incompatible/command/v7/org_users_command.go (about) 1 package v7 2 3 import ( 4 "code.cloudfoundry.org/cli/actor/v7action" 5 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 6 "code.cloudfoundry.org/cli/command/flag" 7 "code.cloudfoundry.org/cli/resources" 8 ) 9 10 type OrgUsersCommand struct { 11 BaseCommand 12 13 RequiredArgs flag.Organization `positional-args:"yes"` 14 AllUsers bool `long:"all-users" short:"a" description:"List all users with roles in the org or in spaces within the org"` 15 usage interface{} `usage:"CF_NAME org-users ORG"` 16 relatedCommands interface{} `related_commands:"orgs, set-org-role"` 17 } 18 19 func (cmd *OrgUsersCommand) Execute(args []string) error { 20 err := cmd.SharedActor.CheckTarget(false, false) 21 if err != nil { 22 return err 23 } 24 25 user, err := cmd.Config.CurrentUser() 26 if err != nil { 27 return err 28 } 29 30 cmd.UI.DisplayTextWithFlavor("Getting users in org {{.Org}} as {{.CurrentUser}}...", map[string]interface{}{ 31 "Org": cmd.RequiredArgs.Organization, 32 "CurrentUser": user.Name, 33 }) 34 cmd.UI.DisplayNewline() 35 36 org, warnings, err := cmd.Actor.GetOrganizationByName(cmd.RequiredArgs.Organization) 37 cmd.UI.DisplayWarnings(warnings) 38 if err != nil { 39 return err 40 } 41 42 orgUsersByRoleType, warnings, err := cmd.Actor.GetOrgUsersByRoleType(org.GUID) 43 cmd.UI.DisplayWarnings(warnings) 44 if err != nil { 45 return err 46 } 47 48 cmd.displayOrgUsers(orgUsersByRoleType) 49 50 return nil 51 } 52 53 func (cmd OrgUsersCommand) displayOrgUsers(orgUsersByRoleType map[constant.RoleType][]resources.User) { 54 if cmd.AllUsers { 55 cmd.displayRoleGroup(getUniqueUsers(orgUsersByRoleType), "ORG USERS") 56 } else { 57 cmd.displayRoleGroup(orgUsersByRoleType[constant.OrgManagerRole], "ORG MANAGER") 58 cmd.displayRoleGroup(orgUsersByRoleType[constant.OrgBillingManagerRole], "BILLING MANAGER") 59 cmd.displayRoleGroup(orgUsersByRoleType[constant.OrgAuditorRole], "ORG AUDITOR") 60 } 61 } 62 63 func (cmd OrgUsersCommand) displayRoleGroup(usersWithRole []resources.User, roleLabel string) { 64 v7action.SortUsers(usersWithRole) 65 66 cmd.UI.DisplayHeader(roleLabel) 67 if len(usersWithRole) > 0 { 68 for _, userWithRole := range usersWithRole { 69 cmd.UI.DisplayText(" {{.PresentationName}} ({{.Origin}})", map[string]interface{}{ 70 "PresentationName": userWithRole.PresentationName, 71 "Origin": v7action.GetHumanReadableOrigin(userWithRole), 72 }) 73 } 74 } else { 75 cmd.UI.DisplayText(" No {{.RoleLabel}} found", map[string]interface{}{ 76 "RoleLabel": roleLabel, 77 }) 78 } 79 80 cmd.UI.DisplayNewline() 81 } 82 83 func getUniqueUsers(orgUsersByRoleType map[constant.RoleType][]resources.User) []resources.User { 84 var allUsers []resources.User 85 86 usersSet := make(map[string]bool) 87 addUsersWithType := func(roleType constant.RoleType) { 88 for _, user := range orgUsersByRoleType[roleType] { 89 if _, ok := usersSet[user.GUID]; !ok { 90 allUsers = append(allUsers, user) 91 } 92 93 usersSet[user.GUID] = true 94 } 95 } 96 97 addUsersWithType(constant.OrgUserRole) 98 addUsersWithType(constant.OrgManagerRole) 99 addUsersWithType(constant.OrgBillingManagerRole) 100 addUsersWithType(constant.OrgAuditorRole) 101 102 return allUsers 103 }