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