github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+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_metadata"
     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/codegangsta/cli"
    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 NewUnsetSpaceRole(ui terminal.UI, config core_config.Reader, spaceRepo spaces.SpaceRepository, userRepo api.UserRepository) (cmd *UnsetSpaceRole) {
    25  	cmd = new(UnsetSpaceRole)
    26  	cmd.ui = ui
    27  	cmd.config = config
    28  	cmd.spaceRepo = spaceRepo
    29  	cmd.userRepo = userRepo
    30  	return
    31  }
    32  
    33  func (cmd *UnsetSpaceRole) Metadata() command_metadata.CommandMetadata {
    34  	return command_metadata.CommandMetadata{
    35  		Name:        "unset-space-role",
    36  		Description: T("Remove a space role from a user"),
    37  		Usage: T("CF_NAME unset-space-role USERNAME ORG SPACE ROLE\n\n") +
    38  			T("ROLES:\n") +
    39  			T("   SpaceManager - Invite and manage users, and enable features for a given space\n") +
    40  			T("   SpaceDeveloper - Create and manage apps and services, and see logs and reports\n") +
    41  			T("   SpaceAuditor - View logs, reports, and settings on this space\n"),
    42  	}
    43  }
    44  
    45  func (cmd *UnsetSpaceRole) GetRequirements(requirementsFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
    46  	if len(c.Args()) != 4 {
    47  		cmd.ui.FailWithUsage(c)
    48  	}
    49  
    50  	cmd.userReq = requirementsFactory.NewUserRequirement(c.Args()[0])
    51  	cmd.orgReq = requirementsFactory.NewOrganizationRequirement(c.Args()[1])
    52  
    53  	reqs = []requirements.Requirement{
    54  		requirementsFactory.NewLoginRequirement(),
    55  		cmd.userReq,
    56  		cmd.orgReq,
    57  	}
    58  
    59  	return
    60  }
    61  
    62  func (cmd *UnsetSpaceRole) Run(c *cli.Context) {
    63  	spaceName := c.Args()[2]
    64  	role := models.UserInputToSpaceRole[c.Args()[3]]
    65  
    66  	user := cmd.userReq.GetUser()
    67  	org := cmd.orgReq.GetOrganization()
    68  	space, apiErr := cmd.spaceRepo.FindByNameInOrg(spaceName, org.Guid)
    69  	if apiErr != nil {
    70  		cmd.ui.Failed(apiErr.Error())
    71  		return
    72  	}
    73  
    74  	cmd.ui.Say(T("Removing role {{.Role}} from user {{.TargetUser}} in org {{.TargetOrg}} / space {{.TargetSpace}} as {{.CurrentUser}}...",
    75  		map[string]interface{}{
    76  			"Role":        terminal.EntityNameColor(role),
    77  			"TargetUser":  terminal.EntityNameColor(user.Username),
    78  			"TargetOrg":   terminal.EntityNameColor(org.Name),
    79  			"TargetSpace": terminal.EntityNameColor(space.Name),
    80  			"CurrentUser": terminal.EntityNameColor(cmd.config.Username()),
    81  		}))
    82  
    83  	apiErr = cmd.userRepo.UnsetSpaceRole(user.Guid, space.Guid, role)
    84  
    85  	if apiErr != nil {
    86  		cmd.ui.Failed(apiErr.Error())
    87  		return
    88  	}
    89  
    90  	cmd.ui.Ok()
    91  }