Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add the ability to create a user without an invite token #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions pkg/bastion/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -1581,6 +1581,59 @@ GLOBAL OPTIONS:
Usage: "Manages users",
Subcommands: []cli.Command{
{
Name: "create",
ArgsUsage: "<email>",
Usage: "Creates a new user",
Description: "$> user create bob@example.com\n $> user create --name=Robert bob@example.com",
Flags: []cli.Flag{
cli.StringFlag{Name: "name", Usage: "Assigns a name to the user"},
cli.StringFlag{Name: "comment", Usage: "Adds a comment"},
cli.StringSliceFlag{Name: "group, g", Usage: "Names or IDs of `USERGROUPS` (default: \"default\")"},
},
Action: func(c *cli.Context) error {
if c.NArg() != 1 {
return cli.ShowSubcommandHelp(c)
}

if err := myself.CheckRoles([]string{"admin"}); err != nil {
return err
moul marked this conversation as resolved.
Show resolved Hide resolved
}

// FIXME: validate email

email := c.Args().First()
moul marked this conversation as resolved.
Show resolved Hide resolved
name := strings.Split(email, "@")[0]
if c.String("name") != "" {
name = c.String("name")
}

user := dbmodels.User{
Name: name,
Email: email,
Comment: c.String("comment"),
InviteToken: "",
}

if _, err := govalidator.ValidateStruct(user); err != nil {
return err
}

// user group
inputGroups := c.StringSlice("group")
if len(inputGroups) == 0 {
inputGroups = []string{"default"}
}
if err := dbmodels.UserGroupsByIdentifiers(db, inputGroups).Find(&user.Groups).Error; err != nil {
return err
}

// save the user in database
if err := db.Create(&user).Error; err != nil {
return err
}
return nil
},
}, {
Name: "inspect",
Usage: "Shows detailed information on one or more users",
ArgsUsage: "USER...",
Expand Down