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