-
Notifications
You must be signed in to change notification settings - Fork 445
/
Copy pathadmin_cmd.go
131 lines (108 loc) · 3.26 KB
/
admin_cmd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package cmd
import (
"github.com/gofrs/uuid"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/supabase/auth/internal/conf"
"github.com/supabase/auth/internal/models"
"github.com/supabase/auth/internal/storage"
)
var autoconfirm, isAdmin bool
var audience string
func getAudience(c *conf.GlobalConfiguration) string {
if audience == "" {
return c.JWT.Aud
}
return audience
}
func adminCmd() *cobra.Command {
var adminCmd = &cobra.Command{
Use: "admin",
}
adminCmd.AddCommand(&adminCreateUserCmd, &adminDeleteUserCmd)
adminCmd.PersistentFlags().StringVarP(&audience, "aud", "a", "", "Set the new user's audience")
adminCreateUserCmd.Flags().BoolVar(&autoconfirm, "confirm", false, "Automatically confirm user without sending an email")
adminCreateUserCmd.Flags().BoolVar(&isAdmin, "admin", false, "Create user with admin privileges")
return adminCmd
}
var adminCreateUserCmd = cobra.Command{
Use: "createuser",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 {
logrus.Fatal("Not enough arguments to createuser command. Expected at least email and password values")
return
}
execWithConfigAndArgs(cmd, adminCreateUser, args)
},
}
var adminDeleteUserCmd = cobra.Command{
Use: "deleteuser",
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
logrus.Fatal("Not enough arguments to deleteuser command. Expected at least ID or email")
return
}
execWithConfigAndArgs(cmd, adminDeleteUser, args)
},
}
func adminCreateUser(config *conf.GlobalConfiguration, args []string) {
db, err := storage.Dial(config)
if err != nil {
logrus.Fatalf("Error opening database: %+v", err)
}
defer db.Close()
aud := getAudience(config)
if user, err := models.IsDuplicatedEmail(db, args[0], aud, nil); user != nil {
logrus.Fatalf("Error creating new user: user already exists")
} else if err != nil {
logrus.Fatalf("Error checking user email: %+v", err)
}
user, err := models.NewUser("", args[0], args[1], aud, nil)
if err != nil {
logrus.Fatalf("Error creating new user: %+v", err)
}
err = db.Transaction(func(tx *storage.Connection) error {
var terr error
if terr = tx.Create(user); terr != nil {
return terr
}
if len(args) > 2 {
if terr = user.SetRole(tx, args[2]); terr != nil {
return terr
}
} else if isAdmin {
if terr = user.SetRole(tx, config.JWT.AdminGroupName); terr != nil {
return terr
}
}
if config.Mailer.Autoconfirm || autoconfirm {
if terr = user.Confirm(tx); terr != nil {
return terr
}
}
return nil
})
if err != nil {
logrus.Fatalf("Unable to create user (%s): %+v", args[0], err)
}
logrus.Infof("Created user: %s", args[0])
}
func adminDeleteUser(config *conf.GlobalConfiguration, args []string) {
db, err := storage.Dial(config)
if err != nil {
logrus.Fatalf("Error opening database: %+v", err)
}
defer db.Close()
user, err := models.FindUserByEmailAndAudience(db, args[0], getAudience(config))
if err != nil {
userID := uuid.Must(uuid.FromString(args[0]))
user, err = models.FindUserByID(db, userID)
if err != nil {
logrus.Fatalf("Error finding user (%s): %+v", userID, err)
}
}
if err = db.Destroy(user); err != nil {
logrus.Fatalf("Error removing user (%s): %+v", args[0], err)
}
logrus.Infof("Removed user: %s", args[0])
}