github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/org_users_command.go (about)

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