Create a user from command line
This commit is contained in:
parent
392aff5956
commit
aef8f1a572
11
main.go
11
main.go
@ -26,6 +26,7 @@ const DB_SCHEMA = 13
|
|||||||
var (
|
var (
|
||||||
flagPort int
|
flagPort int
|
||||||
flagVersion bool
|
flagVersion bool
|
||||||
|
flagCreateUser bool
|
||||||
flagConfig string
|
flagConfig string
|
||||||
|
|
||||||
connectionManager ConnectionManager
|
connectionManager ConnectionManager
|
||||||
@ -44,6 +45,7 @@ func init() { // {{{
|
|||||||
configFilename := os.Getenv("HOME") + "/.config/notes.yaml"
|
configFilename := os.Getenv("HOME") + "/.config/notes.yaml"
|
||||||
flag.IntVar(&flagPort, "port", 1371, "TCP port to listen on")
|
flag.IntVar(&flagPort, "port", 1371, "TCP port to listen on")
|
||||||
flag.BoolVar(&flagVersion, "version", false, "Shows Notes version and exists")
|
flag.BoolVar(&flagVersion, "version", false, "Shows Notes version and exists")
|
||||||
|
flag.BoolVar(&flagCreateUser, "createuser", false, "Create a user and exit")
|
||||||
flag.StringVar(&flagConfig, "config", configFilename, "Filename of configuration file")
|
flag.StringVar(&flagConfig, "config", configFilename, "Filename of configuration file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
@ -72,6 +74,15 @@ func main() { // {{{
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if flagCreateUser {
|
||||||
|
err = createUser()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
connectionManager = NewConnectionManager()
|
connectionManager = NewConnectionManager()
|
||||||
go connectionManager.BroadcastLoop()
|
go connectionManager.BroadcastLoop()
|
||||||
|
|
||||||
|
36
user.go
36
user.go
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
// Standard
|
// Standard
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (session Session) UpdatePassword(currPass, newPass string) (ok bool, err error) {
|
func (session Session) UpdatePassword(currPass, newPass string) (ok bool, err error) {
|
||||||
@ -35,3 +36,38 @@ func (session Session) UpdatePassword(currPass, newPass string) (ok bool, err er
|
|||||||
|
|
||||||
return rowsAffected > 0, nil
|
return rowsAffected > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createUser() (err error) {
|
||||||
|
var username, password string
|
||||||
|
fmt.Printf("Username: ")
|
||||||
|
fmt.Scanln(&username)
|
||||||
|
fmt.Printf("Password: ")
|
||||||
|
fmt.Scanln(&password)
|
||||||
|
|
||||||
|
err = CreateDbUser(username, password)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateDbUser(username string, password string) (err error) {
|
||||||
|
var result sql.Result
|
||||||
|
|
||||||
|
result, err = db.Exec(`
|
||||||
|
INSERT INTO public.user(username, password)
|
||||||
|
VALUES(
|
||||||
|
$1,
|
||||||
|
password_hash(
|
||||||
|
/* salt in hex */
|
||||||
|
ENCODE(gen_random_bytes(16), 'hex'),
|
||||||
|
|
||||||
|
/* password */
|
||||||
|
$2::bytea
|
||||||
|
)
|
||||||
|
)
|
||||||
|
`,
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
)
|
||||||
|
|
||||||
|
_, err = result.RowsAffected()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user