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

     1  package user
     2  
     3  import (
     4  	"github.com/cloudfoundry/cli/cf/api"
     5  	"github.com/cloudfoundry/cli/cf/api/spaces"
     6  	"github.com/cloudfoundry/cli/cf/command_registry"
     7  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
     8  	. "github.com/cloudfoundry/cli/cf/i18n"
     9  	"github.com/cloudfoundry/cli/cf/models"
    10  	"github.com/cloudfoundry/cli/cf/requirements"
    11  	"github.com/cloudfoundry/cli/cf/terminal"
    12  	"github.com/cloudfoundry/cli/flags"
    13  )
    14  
    15  type UnsetSpaceRole struct {
    16  	ui        terminal.UI
    17  	config    core_config.Reader
    18  	spaceRepo spaces.SpaceRepository
    19  	userRepo  api.UserRepository
    20  	userReq   requirements.UserRequirement
    21  	orgReq    requirements.OrganizationRequirement
    22  }
    23  
    24  func init() {
    25  	command_registry.Register(&UnsetSpaceRole{})
    26  }
    27  
    28  func (cmd *UnsetSpaceRole) MetaData() command_registry.CommandMetadata {
    29  	return command_registry.CommandMetadata{
    30  		Name:        "unset-space-role",
    31  		Description: T("Remove a space role from a user"),
    32  		Usage: T("CF_NAME unset-space-role USERNAME ORG SPACE ROLE\n\n") +
    33  			T("ROLES:\n") +
    34  			T("   SpaceManager - Invite and manage users, and enable features for a given space\n") +
    35  			T("   SpaceDeveloper - Create and manage apps and services, and see logs and reports\n") +
    36  			T("   SpaceAuditor - View logs, reports, and settings on this space\n"),
    37  	}
    38  }
    39  
    40  func (cmd *UnsetSpaceRole) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
    41  	if len(fc.Args()) != 4 {
    42  		cmd.ui.Failed(T("Incorrect Usage. Requires USERNAME, ORG, SPACE, ROLE as arguments\n\n") + command_registry.Commands.CommandUsage("unset-space-role"))
    43  	}
    44  
    45  	cmd.userReq = requirementsFactory.NewUserRequirement(fc.Args()[0])
    46  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[1])
    47  
    48  	reqs = []requirements.Requirement{
    49  		requirementsFactory.NewLoginRequirement(),
    50  		cmd.userReq,
    51  		cmd.orgReq,
    52  	}
    53  
    54  	return
    55  }
    56  
    57  func (cmd *UnsetSpaceRole) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
    58  	cmd.ui = deps.Ui
    59  	cmd.config = deps.Config
    60  	cmd.spaceRepo = deps.RepoLocator.GetSpaceRepository()
    61  	cmd.userRepo = deps.RepoLocator.GetUserRepository()
    62  	return cmd
    63  }
    64  
    65  func (cmd *UnsetSpaceRole) Execute(c flags.FlagContext) {
    66  	spaceName := c.Args()[2]
    67  	role := models.UserInputToSpaceRole[c.Args()[3]]
    68  
    69  	user := cmd.userReq.GetUser()
    70  	org := cmd.orgReq.GetOrganization()
    71  	space, apiErr := cmd.spaceRepo.FindByNameInOrg(spaceName, org.Guid)
    72  	if apiErr != nil {
    73  		cmd.ui.Failed(apiErr.Error())
    74  		return
    75  	}
    76  
    77  	cmd.ui.Say(T("Removing role {{.Role}} from user {{.TargetUser}} in org {{.TargetOrg}} / space {{.TargetSpace}} as {{.CurrentUser}}...",
    78  		map[string]interface{}{
    79  			"Role":        terminal.EntityNameColor(role),
    80  			"TargetUser":  terminal.EntityNameColor(user.Username),
    81  			"TargetOrg":   terminal.EntityNameColor(org.Name),
    82  			"TargetSpace": terminal.EntityNameColor(space.Name),
    83  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    84  		}))
    85  
    86  	apiErr = cmd.userRepo.UnsetSpaceRole(user.Guid, space.Guid, role)
    87  
    88  	if apiErr != nil {
    89  		cmd.ui.Failed(apiErr.Error())
    90  		return
    91  	}
    92  
    93  	cmd.ui.Ok()
    94  }