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