Skip to content

Commit cce5d1d

Browse files
Merge pull request #139 from usefathom/cli-user-management
rework user cli management
2 parents bbdf67f + fe62c65 commit cce5d1d

File tree

6 files changed

+109
-59
lines changed

6 files changed

+109
-59
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ For getting a development version of Fathom up & running, go through the followi
2626
1. Get code: `git clone https://github.com/usefathom/fathom.git $GOPATH/src/github.com/usefathom/fathom`
2727
1. Compile into binary & prepare assets: `make build`
2828
1. (Optional) Set your [custom configuration values](https://github.com/usefathom/fathom/wiki/Configuration-file).
29-
1. Register your user account: `fathom register --email=<email> --password=<password>`
29+
1. Register your user account: `fathom user add --email=<email> --password=<password>`
3030
1. Start the webserver: `fathom server` and then visit **http://localhost:8080** to access your analytics dashboard.
3131

3232
To install and run Fathom in production, [have a look at the installation instructions](https://github.com/usefathom/fathom/wiki/Installing-&-running-Fathom).

cmd/fathom/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func main() {
3838
app.After = after
3939
app.Commands = []cli.Command{
4040
serverCmd,
41-
registerCmd,
41+
userCmd,
4242
statsCmd,
4343
}
4444

cmd/fathom/register.go

Lines changed: 0 additions & 57 deletions
This file was deleted.

cmd/fathom/user.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
log "github.com/sirupsen/logrus"
8+
"github.com/urfave/cli"
9+
"github.com/usefathom/fathom/pkg/datastore"
10+
"github.com/usefathom/fathom/pkg/models"
11+
)
12+
13+
var userCmd = cli.Command{
14+
Name: "user",
15+
Usage: "manage registered admin users",
16+
Action: userAdd,
17+
Subcommands: []cli.Command{
18+
cli.Command{
19+
Name: "add",
20+
Aliases: []string{"register"},
21+
Action: userAdd,
22+
Flags: []cli.Flag{
23+
cli.StringFlag{
24+
Name: "email, e",
25+
Usage: "user email",
26+
},
27+
cli.StringFlag{
28+
Name: "password, p",
29+
Usage: "user password",
30+
},
31+
cli.BoolFlag{
32+
Name: "skip-bcrypt",
33+
Usage: "store password string as-is, skipping bcrypt",
34+
},
35+
},
36+
},
37+
cli.Command{
38+
Name: "delete",
39+
Action: userDelete,
40+
Flags: []cli.Flag{
41+
cli.StringFlag{
42+
Name: "email, e",
43+
Usage: "user email",
44+
},
45+
},
46+
},
47+
},
48+
}
49+
50+
func userAdd(c *cli.Context) error {
51+
email := c.String("email")
52+
if email == "" {
53+
return errors.New("Invalid arguments: missing email")
54+
}
55+
56+
password := c.String("password")
57+
if password == "" {
58+
return errors.New("Invalid arguments: missing password")
59+
}
60+
61+
user := models.NewUser(email, password)
62+
63+
// set password manually if --skip-bcrypt was given
64+
// this is used to supply an already encrypted password string
65+
if c.Bool("skip-bcrypt") {
66+
user.Password = password
67+
}
68+
69+
if err := app.database.SaveUser(&user); err != nil {
70+
return fmt.Errorf("Error creating user: %s", err)
71+
}
72+
73+
log.Infof("Created user %s", user.Email)
74+
return nil
75+
}
76+
77+
func userDelete(c *cli.Context) error {
78+
email := c.String("email")
79+
if email == "" {
80+
return errors.New("Invalid arguments: missing email")
81+
}
82+
83+
user, err := app.database.GetUserByEmail(email)
84+
if err != nil {
85+
if err == datastore.ErrNoResults {
86+
return fmt.Errorf("No user with email %s", email)
87+
}
88+
89+
return err
90+
}
91+
92+
if err := app.database.DeleteUser(user); err != nil {
93+
return err
94+
}
95+
96+
log.Infof("Deleted user %s", user.Email)
97+
98+
return nil
99+
}

pkg/datastore/datastore.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type Datastore interface {
1616
GetUser(int64) (*models.User, error)
1717
GetUserByEmail(string) (*models.User, error)
1818
SaveUser(*models.User) error
19+
DeleteUser(*models.User) error
1920
CountUsers() (int64, error)
2021

2122
// site stats

pkg/datastore/sqlstore/users.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ func (db *sqlstore) SaveUser(u *models.User) error {
5151
return nil
5252
}
5353

54+
// DeleteUser deletes the user in the datastore
55+
func (db *sqlstore) DeleteUser(user *models.User) error {
56+
query := db.Rebind("DELETE FROM users WHERE id = ?")
57+
_, err := db.Exec(query, user.ID)
58+
return err
59+
}
60+
5461
// CountUsers returns the number of users
5562
func (db *sqlstore) CountUsers() (int64, error) {
5663
var c int64

0 commit comments

Comments
 (0)