github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/user/unset_org_role.go (about)

     1  package user
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api"
     7  	"code.cloudfoundry.org/cli/cf/api/featureflags"
     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  )
    16  
    17  type UnsetOrgRole struct {
    18  	ui       terminal.UI
    19  	config   coreconfig.Reader
    20  	userRepo api.UserRepository
    21  	flagRepo featureflags.FeatureFlagRepository
    22  	userReq  requirements.UserRequirement
    23  	orgReq   requirements.OrganizationRequirement
    24  }
    25  
    26  func init() {
    27  	commandregistry.Register(&UnsetOrgRole{})
    28  }
    29  
    30  func (cmd *UnsetOrgRole) MetaData() commandregistry.CommandMetadata {
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "unset-org-role",
    33  		Description: T("Remove an org role from a user"),
    34  		Usage: []string{
    35  			T("CF_NAME unset-org-role USERNAME ORG ROLE\n\n"),
    36  			T("ROLES:\n"),
    37  			fmt.Sprintf("   'OrgManager' - %s", T("Invite and manage users, select and change plans, and set spending limits\n")),
    38  			fmt.Sprintf("   'BillingManager' - %s", T("Create and manage the billing account and payment info\n")),
    39  			fmt.Sprintf("   'OrgAuditor' - %s", T("Read-only access to org info and reports\n")),
    40  		},
    41  	}
    42  }
    43  
    44  func (cmd *UnsetOrgRole) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    45  	if len(fc.Args()) != 3 {
    46  		cmd.ui.Failed(T("Incorrect Usage. Requires USERNAME, ORG, ROLE as arguments\n\n") + commandregistry.Commands.CommandUsage("unset-org-role"))
    47  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 3)
    48  	}
    49  
    50  	setRolesByUsernameFlag, err := cmd.flagRepo.FindByName("unset_roles_by_username")
    51  	wantGUID := (err != nil || !setRolesByUsernameFlag.Enabled)
    52  
    53  	cmd.userReq = requirementsFactory.NewUserRequirement(fc.Args()[0], wantGUID)
    54  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[1])
    55  
    56  	reqs := []requirements.Requirement{
    57  		requirementsFactory.NewLoginRequirement(),
    58  		cmd.userReq,
    59  		cmd.orgReq,
    60  	}
    61  
    62  	return reqs, nil
    63  }
    64  
    65  func (cmd *UnsetOrgRole) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    66  	cmd.ui = deps.UI
    67  	cmd.config = deps.Config
    68  	cmd.userRepo = deps.RepoLocator.GetUserRepository()
    69  	cmd.flagRepo = deps.RepoLocator.GetFeatureFlagRepository()
    70  	return cmd
    71  }
    72  
    73  func (cmd *UnsetOrgRole) Execute(c flags.FlagContext) error {
    74  	user := cmd.userReq.GetUser()
    75  	org := cmd.orgReq.GetOrganization()
    76  	roleStr := c.Args()[2]
    77  	role, err := models.RoleFromString(roleStr)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	cmd.ui.Say(T("Removing role {{.Role}} from user {{.TargetUser}} in org {{.TargetOrg}} as {{.CurrentUser}}...",
    83  		map[string]interface{}{
    84  			"Role":        terminal.EntityNameColor(roleStr),
    85  			"TargetUser":  terminal.EntityNameColor(c.Args()[0]),
    86  			"TargetOrg":   terminal.EntityNameColor(c.Args()[1]),
    87  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    88  		}))
    89  
    90  	if len(user.GUID) > 0 {
    91  		err = cmd.userRepo.UnsetOrgRoleByGUID(user.GUID, org.GUID, role)
    92  	} else {
    93  		err = cmd.userRepo.UnsetOrgRoleByUsername(user.Username, org.GUID, role)
    94  	}
    95  
    96  	if err != nil {
    97  		return err
    98  	}
    99  
   100  	cmd.ui.Ok()
   101  	return nil
   102  }