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