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