github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/user/unset_space_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/api/spaces"
     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  )
    17  
    18  type UnsetSpaceRole struct {
    19  	ui        terminal.UI
    20  	config    coreconfig.Reader
    21  	spaceRepo spaces.SpaceRepository
    22  	userRepo  api.UserRepository
    23  	flagRepo  featureflags.FeatureFlagRepository
    24  	userReq   requirements.UserRequirement
    25  	orgReq    requirements.OrganizationRequirement
    26  }
    27  
    28  func init() {
    29  	commandregistry.Register(&UnsetSpaceRole{})
    30  }
    31  
    32  func (cmd *UnsetSpaceRole) MetaData() commandregistry.CommandMetadata {
    33  	return commandregistry.CommandMetadata{
    34  		Name:        "unset-space-role",
    35  		Description: T("Remove a space role from a user"),
    36  		Usage: []string{
    37  			T("CF_NAME unset-space-role USERNAME ORG SPACE ROLE\n\n"),
    38  			T("ROLES:\n"),
    39  			fmt.Sprintf("   'SpaceManager' - %s", T("Invite and manage users, and enable features for a given space\n")),
    40  			fmt.Sprintf("   'SpaceDeveloper' - %s", T("Create and manage apps and services, and see logs and reports\n")),
    41  			fmt.Sprintf("   'SpaceAuditor' - %s", T("View logs, reports, and settings on this space\n")),
    42  		},
    43  	}
    44  }
    45  
    46  func (cmd *UnsetSpaceRole) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    47  	if len(fc.Args()) != 4 {
    48  		cmd.ui.Failed(T("Incorrect Usage. Requires USERNAME, ORG, SPACE, ROLE as arguments\n\n") + commandregistry.Commands.CommandUsage("unset-space-role"))
    49  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 4)
    50  	}
    51  
    52  	unsetRolesByUsernameFlag, err := cmd.flagRepo.FindByName("unset_roles_by_username")
    53  	wantGUID := (err != nil || !unsetRolesByUsernameFlag.Enabled)
    54  
    55  	cmd.userReq = requirementsFactory.NewUserRequirement(fc.Args()[0], wantGUID)
    56  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[1])
    57  
    58  	reqs := []requirements.Requirement{
    59  		requirementsFactory.NewLoginRequirement(),
    60  		cmd.userReq,
    61  		cmd.orgReq,
    62  	}
    63  
    64  	return reqs, nil
    65  }
    66  
    67  func (cmd *UnsetSpaceRole) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    68  	cmd.ui = deps.UI
    69  	cmd.config = deps.Config
    70  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    71  	cmd.userRepo = deps.RepoLocator.GetUserRepository()
    72  	cmd.flagRepo = deps.RepoLocator.GetFeatureFlagRepository()
    73  	return cmd
    74  }
    75  
    76  func (cmd *UnsetSpaceRole) Execute(c flags.FlagContext) error {
    77  	spaceName := c.Args()[2]
    78  	roleStr := c.Args()[3]
    79  	role, err := models.RoleFromString(roleStr)
    80  	if err != nil {
    81  		return err
    82  	}
    83  	user := cmd.userReq.GetUser()
    84  	org := cmd.orgReq.GetOrganization()
    85  	space, err := cmd.spaceRepo.FindByNameInOrg(spaceName, org.GUID)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	cmd.ui.Say(T("Removing role {{.Role}} from user {{.TargetUser}} in org {{.TargetOrg}} / space {{.TargetSpace}} as {{.CurrentUser}}...",
    91  		map[string]interface{}{
    92  			"Role":        terminal.EntityNameColor(roleStr),
    93  			"TargetUser":  terminal.EntityNameColor(user.Username),
    94  			"TargetOrg":   terminal.EntityNameColor(org.Name),
    95  			"TargetSpace": terminal.EntityNameColor(space.Name),
    96  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    97  		}))
    98  
    99  	if len(user.GUID) > 0 {
   100  		err = cmd.userRepo.UnsetSpaceRoleByGUID(user.GUID, space.GUID, role)
   101  	} else {
   102  		err = cmd.userRepo.UnsetSpaceRoleByUsername(user.Username, space.GUID, role)
   103  	}
   104  	if err != nil {
   105  		return err
   106  	}
   107  
   108  	cmd.ui.Ok()
   109  	return nil
   110  }