From cb171dbd56e3889735115a04e4846f98ec364d65 Mon Sep 17 00:00:00 2001 From: ydelafollye Date: Thu, 15 Oct 2020 22:48:38 -0400 Subject: Improve users management through the CLI (#6001) (#10492) * Fix images in wiki edit preview (#11546) Make sure wiki editor sets wiki to true so gitea renders it as a wiki page. Also change the context data attr for edit form. This looks wrong but everywhere else in our code assumes the urlPrefix to be just the repo url when rendering and manually adds /wiki to the rendered url regardless. Fixes #11540 --- cmd/admin.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 84 insertions(+), 5 deletions(-) (limited to 'cmd/admin.go') diff --git a/cmd/admin.go b/cmd/admin.go index 9f81f5284d..d503657250 100644 --- a/cmd/admin.go +++ b/cmd/admin.go @@ -30,16 +30,38 @@ var ( Name: "admin", Usage: "Command line interface to perform common administrative operations", Subcommands: []cli.Command{ - subcmdCreateUser, - subcmdChangePassword, + subcmdUser, subcmdRepoSyncReleases, subcmdRegenerate, subcmdAuth, }, } - subcmdCreateUser = cli.Command{ - Name: "create-user", + subcmdUser = cli.Command{ + Name: "user", + Usage: "Modify users", + Subcommands: []cli.Command{ + microcmdUserCreate, + microcmdUserList, + microcmdUserChangePassword, + microcmdUserDelete, + }, + } + + microcmdUserList = cli.Command{ + Name: "list", + Usage: "List users", + Action: runListUsers, + Flags: []cli.Flag{ + cli.BoolFlag{ + Name: "admin", + Usage: "List only admin users", + }, + }, + } + + microcmdUserCreate = cli.Command{ + Name: "create", Usage: "Create a new user in database", Action: runCreateUser, Flags: []cli.Flag{ @@ -83,7 +105,7 @@ var ( }, } - subcmdChangePassword = cli.Command{ + microcmdUserChangePassword = cli.Command{ Name: "change-password", Usage: "Change a user's password", Action: runChangePassword, @@ -101,6 +123,13 @@ var ( }, } + microcmdUserDelete = cli.Command{ + Name: "delete", + Usage: "Delete specific user", + Flags: []cli.Flag{idFlag}, + Action: runDeleteUser, + } + subcmdRepoSyncReleases = cli.Command{ Name: "repo-sync-releases", Usage: "Synchronize repository releases with tags", @@ -377,6 +406,56 @@ func runCreateUser(c *cli.Context) error { return nil } +func runListUsers(c *cli.Context) error { + if err := initDB(); err != nil { + return err + } + + users, err := models.GetAllUsers() + + if err != nil { + return err + } + + w := tabwriter.NewWriter(os.Stdout, 5, 0, 1, ' ', 0) + + if c.IsSet("admin") { + fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\n") + for _, u := range users { + if u.IsAdmin { + fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", u.ID, u.Name, u.Email, u.IsActive) + } + } + } else { + fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\n") + for _, u := range users { + fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsActive, u.IsAdmin) + } + + } + + w.Flush() + return nil + +} + +func runDeleteUser(c *cli.Context) error { + if !c.IsSet("id") { + return fmt.Errorf("--id flag is missing") + } + + if err := initDB(); err != nil { + return err + } + + user, err := models.GetUserByID(c.Int64("id")) + if err != nil { + return err + } + + return models.DeleteUser(user) +} + func runRepoSyncReleases(c *cli.Context) error { if err := initDB(); err != nil { return err -- cgit v1.2.3