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