2022-04-24 20:08:26 -05:00
package cli
import (
"fmt"
2023-08-10 20:04:35 -05:00
"strings"
2022-04-24 20:08:26 -05:00
"github.com/go-playground/validator/v10"
2024-08-23 16:23:51 -05:00
"github.com/google/uuid"
2022-04-24 20:08:26 -05:00
"golang.org/x/xerrors"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/cryptorand"
2026-01-08 15:24:11 +04:00
"github.com/coder/pretty"
2024-03-15 11:24:38 -05:00
"github.com/coder/serpent"
2022-04-24 20:08:26 -05:00
)
2024-03-17 09:45:26 -05:00
func ( r * RootCmd ) userCreate () * serpent . Command {
2022-04-24 20:08:26 -05:00
var (
2026-03-17 14:07:20 -08:00
email string
username string
name string
password string
disableLogin bool
loginType string
serviceAccount bool
orgContext = NewOrganizationContext ()
2022-04-24 20:08:26 -05:00
)
2024-03-17 09:45:26 -05:00
cmd := & serpent . Command {
2025-05-13 16:48:16 -04:00
Use : "create" ,
Short : "Create a new user." ,
2024-03-15 11:24:38 -05:00
Middleware : serpent . Chain (
serpent . RequireNArgs ( 0 ),
2023-03-23 17:42:20 -05:00
),
2024-03-15 11:24:38 -05:00
Handler : func ( inv * serpent . Invocation ) error {
2026-03-17 14:07:20 -08:00
if serviceAccount {
switch {
case loginType != "" :
return xerrors . New ( "You cannot use --login-type with --service-account" )
case password != "" :
return xerrors . New ( "You cannot use --password with --service-account" )
case email != "" :
return xerrors . New ( "You cannot use --email with --service-account" )
case disableLogin :
return xerrors . New ( "You cannot use --disable-login with --service-account" )
}
2026-07-28 21:05:50 +10:00
} else {
switch {
case disableLogin && loginType != "" :
return xerrors . New ( "You cannot specify both --disable-login and --login-type" )
case disableLogin :
return xerrors . New ( "--disable-login is deprecated. Use --service-account for machine-to-machine access." )
case loginType == string ( codersdk . LoginTypeNone ):
return xerrors . New ( "Login type 'none' is deprecated. Use --service-account for machine-to-machine access." )
}
2026-03-17 14:07:20 -08:00
}
2025-09-22 17:14:07 +04:00
client , err := r . InitClient ( inv )
if err != nil {
return err
}
2024-07-01 11:04:45 -10:00
organization , err := orgContext . Selected ( inv , client )
2022-04-24 20:08:26 -05:00
if err != nil {
return err
}
2024-06-26 09:00:42 +01:00
// We only prompt for the full name if both username and email have not
// been set. This is to avoid breaking existing non-interactive usage.
shouldPromptName := username == "" && email == ""
2022-04-24 20:08:26 -05:00
if username == "" {
2023-03-23 17:42:20 -05:00
username , err = cliui . Prompt ( inv , cliui . PromptOptions {
2022-04-24 20:08:26 -05:00
Text : "Username:" ,
2024-09-04 18:34:39 +10:00
Validate : func ( username string ) error {
err = codersdk . NameValid ( username )
if err != nil {
return xerrors . Errorf ( "username %q is invalid: %w" , username , err )
}
return nil
},
2022-04-24 20:08:26 -05:00
})
if err != nil {
return err
}
}
2026-03-17 14:07:20 -08:00
if email == "" && ! serviceAccount {
2023-03-23 17:42:20 -05:00
email , err = cliui . Prompt ( inv , cliui . PromptOptions {
2022-04-24 20:08:26 -05:00
Text : "Email:" ,
Validate : func ( s string ) error {
err := validator . New (). Var ( s , "email" )
if err != nil {
return xerrors . New ( "That's not a valid email address!" )
}
return err
},
})
if err != nil {
return err
}
}
2024-06-26 09:00:42 +01:00
if name == "" && shouldPromptName {
rawName , err := cliui . Prompt ( inv , cliui . PromptOptions {
Text : "Full name (optional):" ,
})
if err != nil {
return err
}
2024-09-04 18:17:53 +10:00
name = codersdk . NormalizeRealUsername ( rawName )
2024-06-26 09:00:42 +01:00
if ! strings . EqualFold ( rawName , name ) {
cliui . Warnf ( inv . Stderr , "Normalized name to %q" , name )
}
}
2023-08-10 20:04:35 -05:00
userLoginType := codersdk . LoginTypePassword
2026-03-17 14:07:20 -08:00
if disableLogin || serviceAccount {
2023-08-10 20:04:35 -05:00
userLoginType = codersdk . LoginTypeNone
} else if loginType != "" {
userLoginType = codersdk . LoginType ( loginType )
}
if password == "" && userLoginType == codersdk . LoginTypePassword {
// Generate a random password
2023-02-08 14:59:07 -06:00
password , err = cryptorand . StringCharset ( cryptorand . Human , 20 )
2022-04-24 20:08:26 -05:00
if err != nil {
return err
}
}
2024-08-23 16:23:51 -05:00
_ , err = client . CreateUserWithOrgs ( inv . Context (), codersdk . CreateUserRequestWithOrgs {
Email : email ,
Username : username ,
Name : name ,
Password : password ,
OrganizationIDs : [] uuid . UUID { organization . ID },
UserLoginType : userLoginType ,
2026-03-17 14:07:20 -08:00
ServiceAccount : serviceAccount ,
2022-04-24 20:08:26 -05:00
})
if err != nil {
return err
}
2023-08-10 20:04:35 -05:00
authenticationMethod := ""
switch codersdk . LoginType ( strings . ToLower ( string ( userLoginType ))) {
case codersdk . LoginTypePassword :
2023-09-07 17:28:22 -04:00
authenticationMethod = `Your password is: ` + pretty . Sprint ( cliui . DefaultStyles . Field , password )
2023-08-10 20:04:35 -05:00
case codersdk . LoginTypeNone :
2023-06-14 12:48:43 -05:00
authenticationMethod = "Login has been disabled for this user. Contact your administrator to authenticate."
2023-08-10 20:04:35 -05:00
case codersdk . LoginTypeGithub :
authenticationMethod = `Login is authenticated through GitHub.`
case codersdk . LoginTypeOIDC :
authenticationMethod = `Login is authenticated through the configured OIDC provider.`
2023-06-14 12:48:43 -05:00
}
2026-03-17 14:07:20 -08:00
if serviceAccount {
email = "n/a"
authenticationMethod = "Service accounts must authenticate with a token and cannot log in."
}
2023-06-14 12:48:43 -05:00
2023-03-23 17:42:20 -05:00
_ , _ = fmt . Fprintln ( inv . Stderr , `A new user has been created!
2022-04-24 20:08:26 -05:00
Share the instructions below to get them started.
2023-09-07 17:28:22 -04:00
` + pretty . Sprint ( cliui . DefaultStyles . Placeholder , "—————————————————————————————————————————————————" ) + `
2022-04-24 20:08:26 -05:00
Download the Coder command line for your operating system:
https://github.com/coder/coder/releases
2023-09-07 17:28:22 -04:00
Run ` + pretty . Sprint ( cliui . DefaultStyles . Code , "coder login " + client . URL . String ()) + ` to authenticate.
2022-04-24 20:08:26 -05:00
2023-09-07 17:28:22 -04:00
Your email is: ` + pretty . Sprint ( cliui . DefaultStyles . Field , email ) + `
2023-06-14 12:48:43 -05:00
` + authenticationMethod + `
2022-04-24 20:08:26 -05:00
2023-09-07 17:28:22 -04:00
Create a workspace ` + pretty . Sprint ( cliui . DefaultStyles . Code , "coder create" ) + `!` )
2022-04-24 20:08:26 -05:00
return nil
},
}
2024-03-15 11:24:38 -05:00
cmd . Options = serpent . OptionSet {
2023-03-23 17:42:20 -05:00
{
Flag : "email" ,
FlagShorthand : "e" ,
Description : "Specifies an email address for the new user." ,
2024-03-15 11:24:38 -05:00
Value : serpent . StringOf ( & email ),
2023-03-23 17:42:20 -05:00
},
{
Flag : "username" ,
FlagShorthand : "u" ,
Description : "Specifies a username for the new user." ,
2024-09-04 18:34:39 +10:00
Value : serpent . Validate ( serpent . StringOf ( & username ), func ( _username * serpent . String ) error {
username := _username . String ()
if username != "" {
err := codersdk . NameValid ( username )
if err != nil {
return xerrors . Errorf ( "username %q is invalid: %w" , username , err )
}
}
return nil
}),
2023-03-23 17:42:20 -05:00
},
2024-06-26 09:00:42 +01:00
{
Flag : "full-name" ,
FlagShorthand : "n" ,
Description : "Specifies an optional human-readable name for the new user." ,
Value : serpent . StringOf ( & name ),
},
2023-03-23 17:42:20 -05:00
{
Flag : "password" ,
FlagShorthand : "p" ,
Description : "Specifies a password for the new user." ,
2024-03-15 11:24:38 -05:00
Value : serpent . StringOf ( & password ),
2023-03-23 17:42:20 -05:00
},
2023-06-14 12:48:43 -05:00
{
2023-08-10 20:04:35 -05:00
Flag : "disable-login" ,
Hidden : true ,
2026-07-28 21:05:50 +10:00
Description : "Deprecated: Use --service-account (requires Premium) for machine-to-machine access. \nDisabling login for a user prevents the user from authenticating via password or IdP login. Authentication requires an API key/token generated by an admin. " +
2023-06-14 12:48:43 -05:00
"Be careful when using this flag as it can lock the user out of their account." ,
2024-03-15 11:24:38 -05:00
Value : serpent . BoolOf ( & disableLogin ),
2023-06-14 12:48:43 -05:00
},
2023-08-10 20:04:35 -05:00
{
Flag : "login-type" ,
Description : fmt . Sprintf ( "Optionally specify the login type for the user. Valid values are: %s. " +
2026-04-27 15:51:01 -07:00
"Using 'none' prevents the user from authenticating and requires an API key/token to be generated by an admin. " +
"Deprecated: 'none' is deprecated. Use service accounts (requires Premium) for machine-to-machine access, " +
"or password/github/oidc login types for regular user accounts." ,
2023-08-10 20:04:35 -05:00
strings . Join ([] string {
string ( codersdk . LoginTypePassword ), string ( codersdk . LoginTypeNone ), string ( codersdk . LoginTypeGithub ), string ( codersdk . LoginTypeOIDC ),
}, ", " ,
)),
2024-03-15 11:24:38 -05:00
Value : serpent . StringOf ( & loginType ),
2023-08-10 20:04:35 -05:00
},
2026-03-17 14:07:20 -08:00
{
Flag : "service-account" ,
Description : "Create a user account intended to be used by a service or as an intermediary rather than by a human." ,
Value : serpent . BoolOf ( & serviceAccount ),
},
2023-03-23 17:42:20 -05:00
}
2024-07-01 11:04:45 -10:00
orgContext . AttachOptions ( cmd )
2022-04-24 20:08:26 -05:00
return cmd
}