mirror of
https://github.com/status-im/fathom.git
synced 2025-03-01 11:30:28 +00:00
rework user cli management
This commit is contained in:
parent
bbdf67ff5a
commit
7c3adcb10b
@ -38,7 +38,7 @@ func main() {
|
|||||||
app.After = after
|
app.After = after
|
||||||
app.Commands = []cli.Command{
|
app.Commands = []cli.Command{
|
||||||
serverCmd,
|
serverCmd,
|
||||||
registerCmd,
|
userCmd,
|
||||||
statsCmd,
|
statsCmd,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,57 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"github.com/urfave/cli"
|
|
||||||
"github.com/usefathom/fathom/pkg/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
var registerCmd = cli.Command{
|
|
||||||
Name: "register",
|
|
||||||
Usage: "register a new admin user",
|
|
||||||
Action: register,
|
|
||||||
Flags: []cli.Flag{
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "email, e",
|
|
||||||
Usage: "user email",
|
|
||||||
},
|
|
||||||
cli.StringFlag{
|
|
||||||
Name: "password, p",
|
|
||||||
Usage: "user password",
|
|
||||||
},
|
|
||||||
cli.BoolFlag{
|
|
||||||
Name: "skip-bcrypt",
|
|
||||||
Usage: "store password string as is, skipping bcrypt",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func register(c *cli.Context) error {
|
|
||||||
email := c.String("email")
|
|
||||||
if email == "" {
|
|
||||||
return errors.New("Invalid arguments: missing email")
|
|
||||||
}
|
|
||||||
|
|
||||||
password := c.String("password")
|
|
||||||
if password == "" {
|
|
||||||
return errors.New("Invalid arguments: missing password")
|
|
||||||
}
|
|
||||||
|
|
||||||
user := models.NewUser(email, password)
|
|
||||||
|
|
||||||
// set password manually if --skip-bcrypt was given
|
|
||||||
// this is used to supply an already encrypted password string
|
|
||||||
if c.Bool("skip-bcrypt") {
|
|
||||||
user.Password = password
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := app.database.SaveUser(&user); err != nil {
|
|
||||||
return fmt.Errorf("Error creating user: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("Created user %s", user.Email)
|
|
||||||
return nil
|
|
||||||
}
|
|
99
cmd/fathom/user.go
Normal file
99
cmd/fathom/user.go
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/urfave/cli"
|
||||||
|
"github.com/usefathom/fathom/pkg/datastore"
|
||||||
|
"github.com/usefathom/fathom/pkg/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
var userCmd = cli.Command{
|
||||||
|
Name: "user",
|
||||||
|
Usage: "manage registered admin users",
|
||||||
|
Action: userAdd,
|
||||||
|
Subcommands: []cli.Command{
|
||||||
|
cli.Command{
|
||||||
|
Name: "add",
|
||||||
|
Aliases: []string{"register"},
|
||||||
|
Action: userAdd,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "email, e",
|
||||||
|
Usage: "user email",
|
||||||
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "password, p",
|
||||||
|
Usage: "user password",
|
||||||
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "skip-bcrypt",
|
||||||
|
Usage: "store password string as-is, skipping bcrypt",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
cli.Command{
|
||||||
|
Name: "delete",
|
||||||
|
Action: userDelete,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "email, e",
|
||||||
|
Usage: "user email",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func userAdd(c *cli.Context) error {
|
||||||
|
email := c.String("email")
|
||||||
|
if email == "" {
|
||||||
|
return errors.New("Invalid arguments: missing email")
|
||||||
|
}
|
||||||
|
|
||||||
|
password := c.String("password")
|
||||||
|
if password == "" {
|
||||||
|
return errors.New("Invalid arguments: missing password")
|
||||||
|
}
|
||||||
|
|
||||||
|
user := models.NewUser(email, password)
|
||||||
|
|
||||||
|
// set password manually if --skip-bcrypt was given
|
||||||
|
// this is used to supply an already encrypted password string
|
||||||
|
if c.Bool("skip-bcrypt") {
|
||||||
|
user.Password = password
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := app.database.SaveUser(&user); err != nil {
|
||||||
|
return fmt.Errorf("Error creating user: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("Created user %s", user.Email)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func userDelete(c *cli.Context) error {
|
||||||
|
email := c.String("email")
|
||||||
|
if email == "" {
|
||||||
|
return errors.New("Invalid arguments: missing email")
|
||||||
|
}
|
||||||
|
|
||||||
|
user, err := app.database.GetUserByEmail(email)
|
||||||
|
if err != nil {
|
||||||
|
if err == datastore.ErrNoResults {
|
||||||
|
return fmt.Errorf("No user with email %s", email)
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := app.database.DeleteUser(user); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infof("Deleted user %s", user.Email)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -16,6 +16,7 @@ type Datastore interface {
|
|||||||
GetUser(int64) (*models.User, error)
|
GetUser(int64) (*models.User, error)
|
||||||
GetUserByEmail(string) (*models.User, error)
|
GetUserByEmail(string) (*models.User, error)
|
||||||
SaveUser(*models.User) error
|
SaveUser(*models.User) error
|
||||||
|
DeleteUser(*models.User) error
|
||||||
CountUsers() (int64, error)
|
CountUsers() (int64, error)
|
||||||
|
|
||||||
// site stats
|
// site stats
|
||||||
|
@ -51,6 +51,13 @@ func (db *sqlstore) SaveUser(u *models.User) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DeleteUser deletes the user in the datastore
|
||||||
|
func (db *sqlstore) DeleteUser(user *models.User) error {
|
||||||
|
query := db.Rebind("DELETE FROM users WHERE id = ?")
|
||||||
|
_, err := db.Exec(query, user.ID)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// CountUsers returns the number of users
|
// CountUsers returns the number of users
|
||||||
func (db *sqlstore) CountUsers() (int64, error) {
|
func (db *sqlstore) CountUsers() (int64, error) {
|
||||||
var c int64
|
var c int64
|
||||||
|
Loading…
x
Reference in New Issue
Block a user