github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/user/set_org_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/commandregistry" 10 "code.cloudfoundry.org/cli/cf/configuration/coreconfig" 11 "code.cloudfoundry.org/cli/cf/flags" 12 . "code.cloudfoundry.org/cli/cf/i18n" 13 "code.cloudfoundry.org/cli/cf/models" 14 "code.cloudfoundry.org/cli/cf/requirements" 15 "code.cloudfoundry.org/cli/cf/terminal" 16 ) 17 18 //go:generate counterfeiter . OrgRoleSetter 19 20 type OrgRoleSetter interface { 21 commandregistry.Command 22 SetOrgRole(orgGUID string, role models.Role, userGUID, userName string) error 23 } 24 25 type SetOrgRole struct { 26 ui terminal.UI 27 config coreconfig.Reader 28 flagRepo featureflags.FeatureFlagRepository 29 userRepo api.UserRepository 30 userReq requirements.UserRequirement 31 orgReq requirements.OrganizationRequirement 32 } 33 34 func init() { 35 commandregistry.Register(&SetOrgRole{}) 36 } 37 38 func (cmd *SetOrgRole) MetaData() commandregistry.CommandMetadata { 39 return commandregistry.CommandMetadata{ 40 Name: "set-org-role", 41 Description: T("Assign an org role to a user"), 42 Usage: []string{ 43 T("CF_NAME set-org-role USERNAME ORG ROLE\n\n"), 44 T("ROLES:\n"), 45 fmt.Sprintf(" 'OrgManager' - %s", T("Invite and manage users, select and change plans, and set spending limits\n")), 46 fmt.Sprintf(" 'BillingManager' - %s", T("Create and manage the billing account and payment info\n")), 47 fmt.Sprintf(" 'OrgAuditor' - %s", T("Read-only access to org info and reports\n")), 48 }, 49 } 50 } 51 52 func (cmd *SetOrgRole) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) { 53 if len(fc.Args()) != 3 { 54 cmd.ui.Failed(T("Incorrect Usage. Requires USERNAME, ORG, ROLE as arguments\n\n") + commandregistry.Commands.CommandUsage("set-org-role")) 55 return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 3) 56 } 57 58 var wantGUID bool 59 if cmd.config.IsMinAPIVersion(cf.SetRolesByUsernameMinimumAPIVersion) { 60 setRolesByUsernameFlag, err := cmd.flagRepo.FindByName("set_roles_by_username") 61 wantGUID = (err != nil || !setRolesByUsernameFlag.Enabled) 62 } else { 63 wantGUID = true 64 } 65 66 cmd.userReq = requirementsFactory.NewUserRequirement(fc.Args()[0], wantGUID) 67 cmd.orgReq = requirementsFactory.NewOrganizationRequirement(fc.Args()[1]) 68 69 reqs := []requirements.Requirement{ 70 requirementsFactory.NewLoginRequirement(), 71 cmd.userReq, 72 cmd.orgReq, 73 } 74 75 return reqs, nil 76 } 77 78 func (cmd *SetOrgRole) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command { 79 cmd.ui = deps.UI 80 cmd.config = deps.Config 81 cmd.userRepo = deps.RepoLocator.GetUserRepository() 82 cmd.flagRepo = deps.RepoLocator.GetFeatureFlagRepository() 83 return cmd 84 } 85 86 func (cmd *SetOrgRole) Execute(c flags.FlagContext) error { 87 user := cmd.userReq.GetUser() 88 org := cmd.orgReq.GetOrganization() 89 roleStr := c.Args()[2] 90 role, err := models.RoleFromString(roleStr) 91 if err != nil { 92 return err 93 } 94 95 cmd.ui.Say(T("Assigning role {{.Role}} to user {{.TargetUser}} in org {{.TargetOrg}} as {{.CurrentUser}}...", 96 map[string]interface{}{ 97 "Role": terminal.EntityNameColor(roleStr), 98 "TargetUser": terminal.EntityNameColor(user.Username), 99 "TargetOrg": terminal.EntityNameColor(org.Name), 100 "CurrentUser": terminal.EntityNameColor(cmd.config.Username()), 101 })) 102 103 err = cmd.SetOrgRole(org.GUID, role, user.GUID, user.Username) 104 if err != nil { 105 return err 106 } 107 108 cmd.ui.Ok() 109 return nil 110 } 111 112 func (cmd *SetOrgRole) SetOrgRole(orgGUID string, role models.Role, userGUID, userName string) error { 113 if len(userGUID) > 0 { 114 return cmd.userRepo.SetOrgRoleByGUID(userGUID, orgGUID, role) 115 } 116 117 return cmd.userRepo.SetOrgRoleByUsername(userName, orgGUID, role) 118 }