github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/user/org_users.go (about)

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