forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_user_command.go
135 lines (112 loc) · 4.03 KB
/
create_user_command.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package v7
import (
"strings"
"code.cloudfoundry.org/cli/actor/sharedaction"
"code.cloudfoundry.org/cli/actor/v7action"
"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
"code.cloudfoundry.org/cli/api/uaa"
"code.cloudfoundry.org/cli/command"
"code.cloudfoundry.org/cli/command/flag"
"code.cloudfoundry.org/cli/command/translatableerror"
"code.cloudfoundry.org/cli/command/v7/shared"
"code.cloudfoundry.org/clock"
)
//go:generate counterfeiter . CreateUserActor
type CreateUserActor interface {
CreateUser(username string, password string, origin string) (v7action.User, v7action.Warnings, error)
GetUser(username, origin string) (v7action.User, error)
}
type CreateUserCommand struct {
Args flag.CreateUser `positional-args:"yes"`
Origin string `long:"origin" description:"Origin for mapping a user account to a user in an external identity provider"`
PasswordPrompt bool `long:"password-prompt" description:"Prompt interactively for password"`
usage interface{} `usage:"CF_NAME create-user USERNAME PASSWORD\n CF_NAME create-user USERNAME [--origin ORIGIN]\n CF_NAME create-user USERNAME --password-prompt\n\nEXAMPLES:\n cf create-user j.smith@example.com S3cr3t # internal user\n cf create-user j.smith@example.com --origin ldap # LDAP user\n cf create-user j.smith@example.com --origin provider-alias # SAML or OpenID Connect federated user"`
relatedCommands interface{} `related_commands:"passwd, set-org-role, set-space-role"`
UI command.UI
Config command.Config
SharedActor command.SharedActor
Actor CreateUserActor
}
func (cmd *CreateUserCommand) Setup(config command.Config, ui command.UI) error {
cmd.UI = ui
cmd.Config = config
sharedActor := sharedaction.NewActor(config)
cmd.SharedActor = sharedActor
ccClient, uaaClient, err := shared.GetNewClientsAndConnectToCF(config, ui, "")
if err != nil {
return err
}
cmd.Actor = v7action.NewActor(ccClient, config, sharedActor, uaaClient, clock.NewClock())
return nil
}
func (cmd *CreateUserCommand) Execute(args []string) error {
var password string
var err error
if cmd.passwordRequired() {
return translatableerror.RequiredArgumentError{
ArgumentName: "PASSWORD",
}
}
// Is the admin logged in?
err = cmd.SharedActor.CheckTarget(false, false)
if err != nil {
return err
}
origin := cmd.Origin
if cmd.Origin == "" {
origin = constant.DefaultOriginUaa
}
// Does the new user already exist?
_, err = cmd.Actor.GetUser(cmd.Args.Username, origin)
if err == nil {
// User already exists
cmd.UI.DisplayTextWithFlavor("Creating user {{.TargetUser}}...", map[string]interface{}{
"TargetUser": cmd.Args.Username,
})
cmd.UI.DisplayWarningV7("User '{{.User}}' already exists.", map[string]interface{}{
"User": cmd.Args.Username,
})
cmd.UI.DisplayOK()
return nil
}
// Get the password
if cmd.Args.Password != nil {
password = *cmd.Args.Password
}
if cmd.PasswordPrompt {
password, err = cmd.UI.DisplayPasswordPrompt("Password")
if err != nil {
return err
}
}
// Create the user!
cmd.UI.DisplayTextWithFlavor("Creating user {{.TargetUser}}...", map[string]interface{}{
"TargetUser": cmd.Args.Username,
})
_, warnings, err := cmd.Actor.CreateUser(cmd.Args.Username, password, cmd.Origin)
cmd.UI.DisplayWarningsV7(warnings)
if err != nil {
if _, ok := err.(uaa.ConflictError); ok {
cmd.UI.DisplayWarningV7("User '{{.User}}' already exists.", map[string]interface{}{
"User": cmd.Args.Username,
})
cmd.UI.DisplayOK()
return nil
} else {
return err
}
}
cmd.UI.DisplayOK()
cmd.UI.DisplayText("TIP: Assign roles with '{{.BinaryName}} set-org-role' and '{{.BinaryName}} set-space-role'.", map[string]interface{}{
"BinaryName": cmd.Config.BinaryName(),
})
return nil
}
func (cmd *CreateUserCommand) passwordRequired() bool {
if (cmd.Origin == "" || strings.ToLower(cmd.Origin) == constant.DefaultOriginUaa) && !cmd.PasswordPrompt {
if cmd.Args.Password == nil {
return true
}
}
return false
}