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