github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/commands/user/unset_org_role.go (about)

     1  package user
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api"
     5  	"github.com/cloudfoundry/cli/cf/command_registry"
     6  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     7  	. "github.com/cloudfoundry/cli/cf/i18n"
     8  	"github.com/cloudfoundry/cli/cf/models"
     9  	"github.com/cloudfoundry/cli/cf/requirements"
    10  	"github.com/cloudfoundry/cli/cf/terminal"
    11  	"github.com/cloudfoundry/cli/flags"
    12  )
    13  
    14  type UnsetOrgRole struct {
    15  	ui       terminal.UI
    16  	config   core_config.Reader
    17  	userRepo api.UserRepository
    18  	userReq  requirements.UserRequirement
    19  	orgReq   requirements.OrganizationRequirement
    20  }
    21  
    22  func init() {
    23  	command_registry.Register(&UnsetOrgRole{})
    24  }
    25  
    26  func (cmd *UnsetOrgRole) MetaData() command_registry.CommandMetadata {
    27  	return command_registry.CommandMetadata{
    28  		Name:        "unset-org-role",
    29  		Description: T("Remove an org role from a user"),
    30  		Usage: T("CF_NAME unset-org-role USERNAME ORG ROLE\n\n") +
    31  			T("ROLES:\n") +
    32  			T("   OrgManager - Invite and manage users, select and change plans, and set spending limits\n") +
    33  			T("   BillingManager - Create and manage the billing account and payment info\n") +
    34  			T("   OrgAuditor - Read-only access to org info and reports\n"),
    35  	}
    36  }
    37  
    38  func (cmd *UnsetOrgRole) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    39  	if len(fc.Args()) != 3 {
    40  		cmd.ui.Failed(T("Incorrect Usage. Requires USERNAME, ORG, ROLE as arguments\n\n") + command_registry.Commands.CommandUsage("unset-org-role"))
    41  	}
    42  
    43  	cmd.userReq = requirementsFactory.NewUserRequirement(fc.Args()[0])
    44  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[1])
    45  
    46  	reqs = []requirements.Requirement{
    47  		requirementsFactory.NewLoginRequirement(),
    48  		cmd.userReq,
    49  		cmd.orgReq,
    50  	}
    51  
    52  	return
    53  }
    54  
    55  func (cmd *UnsetOrgRole) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    56  	cmd.ui = deps.Ui
    57  	cmd.config = deps.Config
    58  	cmd.userRepo = deps.RepoLocator.GetUserRepository()
    59  	return cmd
    60  }
    61  
    62  func (cmd *UnsetOrgRole) Execute(c flags.FlagContext) {
    63  	role := models.UserInputToOrgRole[c.Args()[2]]
    64  	user := cmd.userReq.GetUser()
    65  	org := cmd.orgReq.GetOrganization()
    66  
    67  	cmd.ui.Say(T("Removing role {{.Role}} from user {{.TargetUser}} in org {{.TargetOrg}} as {{.CurrentUser}}...",
    68  		map[string]interface{}{
    69  			"Role":        terminal.EntityNameColor(role),
    70  			"TargetUser":  terminal.EntityNameColor(c.Args()[0]),
    71  			"TargetOrg":   terminal.EntityNameColor(c.Args()[1]),
    72  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    73  		}))
    74  
    75  	apiErr := cmd.userRepo.UnsetOrgRole(user.Guid, org.Guid, role)
    76  
    77  	if apiErr != nil {
    78  		cmd.ui.Failed(apiErr.Error())
    79  		return
    80  	}
    81  
    82  	cmd.ui.Ok()
    83  }