Skip to content

Commit

Permalink
feat: Add the ability to create a user without an invite token
Browse files Browse the repository at this point in the history
For the use case where the invite system is not used to register keys
it should be possible to create a user without an invite token.

Signed-off-by: Jason Wessel <jason.wessel@windriver.com>
  • Loading branch information
jwessel committed Mar 9, 2021
1 parent 762736d commit 632f4cf
Showing 1 changed file with 53 additions and 0 deletions.
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
}

// FIXME: validate email

email := c.Args().First()
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

0 comments on commit 632f4cf

Please sign in to comment.