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

     1  package user
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/actors/userprint"
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  	"code.cloudfoundry.org/cli/plugin/models"
    16  )
    17  
    18  type OrgUsers struct {
    19  	ui          terminal.UI
    20  	config      coreconfig.Reader
    21  	orgReq      requirements.OrganizationRequirement
    22  	userRepo    api.UserRepository
    23  	pluginModel *[]plugin_models.GetOrgUsers_Model
    24  	pluginCall  bool
    25  }
    26  
    27  func init() {
    28  	commandregistry.Register(&OrgUsers{})
    29  }
    30  
    31  func (cmd *OrgUsers) MetaData() commandregistry.CommandMetadata {
    32  	fs := make(map[string]flags.FlagSet)
    33  	fs["a"] = &flags.BoolFlag{ShortName: "a", Usage: T("List all users in the org")}
    34  
    35  	return commandregistry.CommandMetadata{
    36  		Name:        "org-users",
    37  		Description: T("Show org users by role"),
    38  		Usage: []string{
    39  			T("CF_NAME org-users ORG"),
    40  		},
    41  		Flags: fs,
    42  	}
    43  }
    44  
    45  func (cmd *OrgUsers) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    46  	if len(fc.Args()) != 1 {
    47  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("org-users"))
    48  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    49  	}
    50  
    51  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[0])
    52  
    53  	reqs := []requirements.Requirement{
    54  		requirementsFactory.NewLoginRequirement(),
    55  		cmd.orgReq,
    56  	}
    57  
    58  	return reqs, nil
    59  }
    60  
    61  func (cmd *OrgUsers) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    62  	cmd.ui = deps.UI
    63  	cmd.config = deps.Config
    64  	cmd.userRepo = deps.RepoLocator.GetUserRepository()
    65  	cmd.pluginCall = pluginCall
    66  	cmd.pluginModel = deps.PluginModels.OrgUsers
    67  	return cmd
    68  }
    69  
    70  func (cmd *OrgUsers) Execute(c flags.FlagContext) error {
    71  	org := cmd.orgReq.GetOrganization()
    72  
    73  	cmd.ui.Say(T("Getting users in org {{.TargetOrg}} as {{.CurrentUser}}...",
    74  		map[string]interface{}{
    75  			"TargetOrg":   terminal.EntityNameColor(org.Name),
    76  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    77  		}))
    78  
    79  	printer := cmd.printer(c)
    80  	printer.PrintUsers(org.GUID, cmd.config.Username())
    81  	return nil
    82  }
    83  
    84  func (cmd *OrgUsers) printer(c flags.FlagContext) userprint.UserPrinter {
    85  	var roles []models.Role
    86  	if c.Bool("a") {
    87  		roles = []models.Role{models.RoleOrgUser}
    88  	} else {
    89  		roles = []models.Role{models.RoleOrgManager, models.RoleBillingManager, models.RoleOrgAuditor}
    90  	}
    91  
    92  	if cmd.pluginCall {
    93  		return userprint.NewOrgUsersPluginPrinter(
    94  			cmd.pluginModel,
    95  			cmd.userRepo.ListUsersInOrgForRoleWithNoUAA,
    96  			roles,
    97  		)
    98  	}
    99  	return &userprint.OrgUsersUIPrinter{
   100  		UI:         cmd.ui,
   101  		UserLister: cmd.userRepo.ListUsersInOrgForRoleWithNoUAA,
   102  		Roles:      roles,
   103  		RoleDisplayNames: map[models.Role]string{
   104  			models.RoleOrgUser:        T("USERS"),
   105  			models.RoleOrgManager:     T("ORG MANAGER"),
   106  			models.RoleBillingManager: T("BILLING MANAGER"),
   107  			models.RoleOrgAuditor:     T("ORG AUDITOR"),
   108  		},
   109  	}
   110  }