-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathuser.go
More file actions
161 lines (144 loc) · 3.98 KB
/
user.go
File metadata and controls
161 lines (144 loc) · 3.98 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cmd
import (
"fmt"
"os"
"bootimus/internal/storage"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var userForce bool
var userCmd = &cobra.Command{
Use: "user",
Short: "Manage local users from the CLI",
Long: `Enable, disable, and toggle admin rights on local users.
Useful for emergency recovery if you've locked yourself out of the admin UI
(e.g. demoted the only admin or disabled their account).`,
}
var userListCmd = &cobra.Command{
Use: "list",
Short: "List all local users",
Run: func(cmd *cobra.Command, args []string) {
store := openStoreOrExit()
users, err := store.ListUsers()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list users: %v\n", err)
os.Exit(1)
}
fmt.Printf("%-20s %-8s %-8s\n", "USERNAME", "ENABLED", "ADMIN")
for _, u := range users {
fmt.Printf("%-20s %-8v %-8v\n", u.Username, u.Enabled, u.IsAdmin)
}
},
}
var userEnableCmd = &cobra.Command{
Use: "enable <username>",
Short: "Enable a user account",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
t := true
setUserFlags(args[0], &t, nil)
},
}
var userDisableCmd = &cobra.Command{
Use: "disable <username>",
Short: "Disable a user account",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
f := false
setUserFlags(args[0], &f, nil)
},
}
var userSetAdminCmd = &cobra.Command{
Use: "set-admin <username>",
Short: "Grant admin rights to a user",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
t := true
setUserFlags(args[0], nil, &t)
},
}
var userUnsetAdminCmd = &cobra.Command{
Use: "unset-admin <username>",
Short: "Revoke admin rights from a user",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
f := false
setUserFlags(args[0], nil, &f)
},
}
func init() {
rootCmd.AddCommand(userCmd)
userCmd.AddCommand(userListCmd)
userCmd.AddCommand(userEnableCmd)
userCmd.AddCommand(userDisableCmd)
userCmd.AddCommand(userSetAdminCmd)
userCmd.AddCommand(userUnsetAdminCmd)
for _, c := range []*cobra.Command{userDisableCmd, userUnsetAdminCmd} {
c.Flags().BoolVar(&userForce, "force", false, "Bypass the last-active-admin guard")
}
}
func openStoreOrExit() storage.Storage {
dataDir := viper.GetString("data_dir")
pgHost := viper.GetString("db.host")
var store storage.Storage
var err error
if pgHost != "" {
store, err = storage.NewPostgresStore(&storage.Config{
Host: pgHost,
Port: viper.GetInt("db.port"),
User: viper.GetString("db.user"),
Password: viper.GetString("db.password"),
DBName: viper.GetString("db.name"),
SSLMode: viper.GetString("db.sslmode"),
})
} else {
store, err = storage.NewSQLiteStore(dataDir)
}
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to open storage: %v\n", err)
os.Exit(1)
}
if err := store.AutoMigrate(); err != nil {
fmt.Fprintf(os.Stderr, "Failed to run migrations: %v\n", err)
os.Exit(1)
}
return store
}
func setUserFlags(username string, enabled, isAdmin *bool) {
store := openStoreOrExit()
user, err := store.GetUser(username)
if err != nil {
fmt.Fprintf(os.Stderr, "User not found: %s\n", username)
os.Exit(1)
}
willBeEnabled := user.Enabled
if enabled != nil {
willBeEnabled = *enabled
}
willBeAdmin := user.IsAdmin
if isAdmin != nil {
willBeAdmin = *isAdmin
}
if user.IsAdmin && user.Enabled && (!willBeAdmin || !willBeEnabled) && !userForce {
all, err := store.ListUsers()
if err == nil {
others := 0
for _, u := range all {
if u.Username != username && u.IsAdmin && u.Enabled {
others++
}
}
if others == 0 {
fmt.Fprintf(os.Stderr, "Refusing: %s is the only active admin. Pass --force to override.\n", username)
os.Exit(1)
}
}
}
user.Enabled = willBeEnabled
user.IsAdmin = willBeAdmin
if err := store.UpdateUser(username, user); err != nil {
fmt.Fprintf(os.Stderr, "Failed to update user: %v\n", err)
os.Exit(1)
}
fmt.Printf("Updated %s: enabled=%v admin=%v\n", username, user.Enabled, user.IsAdmin)
}