From b634e494fd8ee69999087d6b9a3dbc932eb15465 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Wed, 23 Oct 2024 20:49:33 +0200 Subject: [PATCH] Implement create- and delete-user. --- galenectl/galenectl.go | 95 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) diff --git a/galenectl/galenectl.go b/galenectl/galenectl.go index 4fe891d..c994f56 100644 --- a/galenectl/galenectl.go +++ b/galenectl/galenectl.go @@ -16,6 +16,7 @@ import ( "os" "path/filepath" "slices" + "strings" "golang.org/x/crypto/bcrypt" "golang.org/x/crypto/pbkdf2" @@ -61,6 +62,14 @@ var commands = map[string]command{ command: deleteGroupCmd, description: "delete a group", }, + "create-user": { + command: createUserCmd, + description: "create a user", + }, + "delete-user": { + command: deleteUserCmd, + description: "delete a user", + }, } func main() { @@ -511,3 +520,89 @@ func deleteGroupCmd(cmdname string, args []string) { log.Fatalf("Delete group: %v", err) } } + +func parsePermissions(p string) (any, error) { + p = strings.TrimSpace(p) + if len(p) == 0 { + return nil, errors.New("empty permissions") + } + if p[0] == '[' { + var a []any + err := json.Unmarshal([]byte(p), &a) + if err != nil { + return nil, err + } + return a, nil + } + return p, nil +} + +func createUserCmd(cmdname string, args []string) { + var groupname string + var permissions string + cmd := flag.NewFlagSet(cmdname, flag.ExitOnError) + setUsage(cmd, cmdname, + "%v [option...] %v [option...] username\n", + os.Args[0], cmdname, + ) + cmd.StringVar(&groupname, "group", "", "group") + cmd.StringVar(&permissions, "permissions", "present", "permissions") + cmd.Parse(args) + + if cmd.NArg() != 1 { + cmd.Usage() + os.Exit(1) + } + + perms, err := parsePermissions(permissions) + if err != nil { + log.Fatalf("Parse permissions: %v", err) + } + + url, err := url.JoinPath( + serverURL, "/galene-api/v0/.groups", groupname, + ".users", cmd.Args()[0], + ) + if err != nil { + log.Fatalf("Build URL: %v", err) + } + + dict := map[string]any{"permissions": perms} + err = putJSON(url, dict, false) + if err != nil { + log.Fatalf("Create user: %v", err) + } +} + +func deleteUserCmd(cmdname string, args []string) { + var groupname string + cmd := flag.NewFlagSet(cmdname, flag.ExitOnError) + setUsage(cmd, cmdname, "%v [option...] %v [option...] username\n", + os.Args[0], cmdname, + ) + cmd.StringVar(&groupname, "group", "", "group `name`") + cmd.Parse(args) + + if cmd.NArg() != 1 { + cmd.Usage() + os.Exit(1) + } + + if groupname == "" { + fmt.Fprintf(cmd.Output(), "option \"-group\" is mandatory") + os.Exit(1) + } + + url, err := url.JoinPath( + serverURL, "/galene-api/v0/.groups", groupname, + ".users", cmd.Args()[0], + ) + if err != nil { + log.Fatalf("Build URL: %v", err) + } + + err = deleteValue(url) + if err != nil { + log.Fatalf("Delete user: %v", err) + } +}