2022-04-25 01:08:26 +00:00
package cli
import (
"fmt"
2023-08-11 01:04:35 +00:00
"strings"
2022-04-25 01:08:26 +00:00
"github.com/go-playground/validator/v10"
"golang.org/x/xerrors"
2023-09-07 21:28:22 +00:00
"github.com/coder/pretty"
2023-08-18 18:55:43 +00:00
"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/coder/v2/cryptorand"
2024-03-15 16:24:38 +00:00
"github.com/coder/serpent"
2022-04-25 01:08:26 +00:00
)
2024-03-17 14:45:26 +00:00
func ( r * RootCmd ) userCreate ( ) * serpent . Command {
2022-04-25 01:08:26 +00:00
var (
2023-06-14 17:48:43 +00:00
email string
username string
password string
disableLogin bool
2023-08-11 01:04:35 +00:00
loginType string
2022-04-25 01:08:26 +00:00
)
2023-03-23 22:42:20 +00:00
client := new ( codersdk . Client )
2024-03-17 14:45:26 +00:00
cmd := & serpent . Command {
2022-04-25 01:08:26 +00:00
Use : "create" ,
2024-03-15 16:24:38 +00:00
Middleware : serpent . Chain (
serpent . RequireNArgs ( 0 ) ,
2023-03-23 22:42:20 +00:00
r . InitClient ( client ) ,
) ,
2024-03-15 16:24:38 +00:00
Handler : func ( inv * serpent . Invocation ) error {
2024-02-26 16:03:49 +00:00
organization , err := CurrentOrganization ( r , inv , client )
2022-04-25 01:08:26 +00:00
if err != nil {
return err
}
if username == "" {
2023-03-23 22:42:20 +00:00
username , err = cliui . Prompt ( inv , cliui . PromptOptions {
2022-04-25 01:08:26 +00:00
Text : "Username:" ,
} )
if err != nil {
return err
}
}
if email == "" {
2023-03-23 22:42:20 +00:00
email , err = cliui . Prompt ( inv , cliui . PromptOptions {
2022-04-25 01:08:26 +00: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
}
}
2023-08-11 01:04:35 +00:00
userLoginType := codersdk . LoginTypePassword
if disableLogin && loginType != "" {
return xerrors . New ( "You cannot specify both --disable-login and --login-type" )
}
if disableLogin {
userLoginType = codersdk . LoginTypeNone
} else if loginType != "" {
userLoginType = codersdk . LoginType ( loginType )
}
if password == "" && userLoginType == codersdk . LoginTypePassword {
// Generate a random password
2023-02-08 20:59:07 +00:00
password , err = cryptorand . StringCharset ( cryptorand . Human , 20 )
2022-04-25 01:08:26 +00:00
if err != nil {
return err
}
}
2023-03-23 22:42:20 +00:00
_ , err = client . CreateUser ( inv . Context ( ) , codersdk . CreateUserRequest {
2022-04-25 01:08:26 +00:00
Email : email ,
Username : username ,
Password : password ,
OrganizationID : organization . ID ,
2023-08-11 01:04:35 +00:00
UserLoginType : userLoginType ,
2022-04-25 01:08:26 +00:00
} )
if err != nil {
return err
}
2023-08-11 01:04:35 +00:00
authenticationMethod := ""
switch codersdk . LoginType ( strings . ToLower ( string ( userLoginType ) ) ) {
case codersdk . LoginTypePassword :
2023-09-07 21:28:22 +00:00
authenticationMethod = ` Your password is: ` + pretty . Sprint ( cliui . DefaultStyles . Field , password )
2023-08-11 01:04:35 +00:00
case codersdk . LoginTypeNone :
2023-06-14 17:48:43 +00:00
authenticationMethod = "Login has been disabled for this user. Contact your administrator to authenticate."
2023-08-11 01:04:35 +00: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 17:48:43 +00:00
}
2023-03-23 22:42:20 +00:00
_ , _ = fmt . Fprintln ( inv . Stderr , ` A new user has been created !
2022-04-25 01:08:26 +00:00
Share the instructions below to get them started .
2023-09-07 21:28:22 +00:00
` +pretty.Sprint(cliui.DefaultStyles.Placeholder, "—————————————————————————————————————————————————")+ `
2022-04-25 01:08:26 +00:00
Download the Coder command line for your operating system :
https : //github.com/coder/coder/releases
2023-09-07 21:28:22 +00:00
Run ` +pretty.Sprint(cliui.DefaultStyles.Code, "coder login "+client.URL.String())+ ` to authenticate .
2022-04-25 01:08:26 +00:00
2023-09-07 21:28:22 +00:00
Your email is : ` +pretty.Sprint(cliui.DefaultStyles.Field, email)+ `
2023-06-14 17:48:43 +00:00
` +authenticationMethod+ `
2022-04-25 01:08:26 +00:00
2023-09-07 21:28:22 +00:00
Create a workspace ` +pretty.Sprint(cliui.DefaultStyles.Code, "coder create")+ ` ! ` )
2022-04-25 01:08:26 +00:00
return nil
} ,
}
2024-03-15 16:24:38 +00:00
cmd . Options = serpent . OptionSet {
2023-03-23 22:42:20 +00:00
{
Flag : "email" ,
FlagShorthand : "e" ,
Description : "Specifies an email address for the new user." ,
2024-03-15 16:24:38 +00:00
Value : serpent . StringOf ( & email ) ,
2023-03-23 22:42:20 +00:00
} ,
{
Flag : "username" ,
FlagShorthand : "u" ,
Description : "Specifies a username for the new user." ,
2024-03-15 16:24:38 +00:00
Value : serpent . StringOf ( & username ) ,
2023-03-23 22:42:20 +00:00
} ,
{
Flag : "password" ,
FlagShorthand : "p" ,
Description : "Specifies a password for the new user." ,
2024-03-15 16:24:38 +00:00
Value : serpent . StringOf ( & password ) ,
2023-03-23 22:42:20 +00:00
} ,
2023-06-14 17:48:43 +00:00
{
2023-08-11 01:04:35 +00:00
Flag : "disable-login" ,
Hidden : true ,
Description : "Deprecated: Use '--login-type=none'. \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 17:48:43 +00:00
"Be careful when using this flag as it can lock the user out of their account." ,
2024-03-15 16:24:38 +00:00
Value : serpent . BoolOf ( & disableLogin ) ,
2023-06-14 17:48:43 +00:00
} ,
2023-08-11 01:04:35 +00:00
{
Flag : "login-type" ,
Description : fmt . Sprintf ( "Optionally specify the login type for the user. Valid values are: %s. " +
"Using 'none' prevents the user from authenticating and requires an API key/token to be generated by an admin." ,
strings . Join ( [ ] string {
string ( codersdk . LoginTypePassword ) , string ( codersdk . LoginTypeNone ) , string ( codersdk . LoginTypeGithub ) , string ( codersdk . LoginTypeOIDC ) ,
} , ", " ,
) ) ,
2024-03-15 16:24:38 +00:00
Value : serpent . StringOf ( & loginType ) ,
2023-08-11 01:04:35 +00:00
} ,
2023-03-23 22:42:20 +00:00
}
2022-04-25 01:08:26 +00:00
return cmd
}