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
tokarchuk/v1.17
ydelafollye 4 years ago committed by GitHub
parent 4fbe64572c
commit cb171dbd56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 89
      cmd/admin.go
  2. 50
      docs/content/doc/usage/command-line.en-us.md
  3. 6
      models/user.go

@ -30,16 +30,38 @@ var (
Name: "admin", Name: "admin",
Usage: "Command line interface to perform common administrative operations", Usage: "Command line interface to perform common administrative operations",
Subcommands: []cli.Command{ Subcommands: []cli.Command{
subcmdCreateUser, subcmdUser,
subcmdChangePassword,
subcmdRepoSyncReleases, subcmdRepoSyncReleases,
subcmdRegenerate, subcmdRegenerate,
subcmdAuth, subcmdAuth,
}, },
} }
subcmdCreateUser = cli.Command{ subcmdUser = cli.Command{
Name: "create-user", 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", Usage: "Create a new user in database",
Action: runCreateUser, Action: runCreateUser,
Flags: []cli.Flag{ Flags: []cli.Flag{
@ -83,7 +105,7 @@ var (
}, },
} }
subcmdChangePassword = cli.Command{ microcmdUserChangePassword = cli.Command{
Name: "change-password", Name: "change-password",
Usage: "Change a user's password", Usage: "Change a user's password",
Action: runChangePassword, 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{ subcmdRepoSyncReleases = cli.Command{
Name: "repo-sync-releases", Name: "repo-sync-releases",
Usage: "Synchronize repository releases with tags", Usage: "Synchronize repository releases with tags",
@ -377,6 +406,56 @@ func runCreateUser(c *cli.Context) error {
return nil 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 { func runRepoSyncReleases(c *cli.Context) error {
if err := initDB(); err != nil { if err := initDB(); err != nil {
return err return err

@ -55,28 +55,40 @@ Starts the server:
Admin operations: Admin operations:
- Commands: - Commands:
- `create-user` - `user`:
- Options: - `list`:
- `--name value`: Username. Required. As of gitea 1.9.0, use the `--username` flag instead. - Options:
- `--username value`: Username. Required. New in gitea 1.9.0. - `--admin`: List only admin users. Optional.
- `--password value`: Password. Required. - Description: lists all users that exist
- `--email value`: Email. Required. - Examples:
- `--admin`: If provided, this makes the user an admin. Optional. - `gitea admin user list`
- `--access-token`: If provided, an access token will be created for the user. Optional. (default: false). - `delete`:
- `--must-change-password`: If provided, the created user will be required to choose a newer password after - Options:
- `--id`: ID of user to be deleted. Required.
- Examples:
- `gitea admin user delete --id 1`
- `create`:
- Options:
- `--name value`: Username. Required. As of gitea 1.9.0, use the `--username` flag instead.
- `--username value`: Username. Required. New in gitea 1.9.0.
- `--password value`: Password. Required.
- `--email value`: Email. Required.
- `--admin`: If provided, this makes the user an admin. Optional.
- `--access-token`: If provided, an access token will be created for the user. Optional. (default: false).
- `--must-change-password`: If provided, the created user will be required to choose a newer password after
the initial login. Optional. (default: true). the initial login. Optional. (default: true).
- ``--random-password``: If provided, a randomly generated password will be used as the password of - ``--random-password``: If provided, a randomly generated password will be used as the password of
the created user. The value of `--password` will be discarded. Optional. the created user. The value of `--password` will be discarded. Optional.
- `--random-password-length`: If provided, it will be used to configure the length of the randomly - `--random-password-length`: If provided, it will be used to configure the length of the randomly
generated password. Optional. (default: 12) generated password. Optional. (default: 12)
- Examples: - Examples:
- `gitea admin create-user --username myname --password asecurepassword --email me@example.com` - `gitea admin create-user --username myname --password asecurepassword --email me@example.com`
- `change-password` - `change-password`:
- Options: - Options:
- `--username value`, `-u value`: Username. Required. - `--username value`, `-u value`: Username. Required.
- `--password value`, `-p value`: New password. Required. - `--password value`, `-p value`: New password. Required.
- Examples: - Examples:
- `gitea admin change-password --username myname --password asecurepassword` - `gitea admin change-password --username myname --password asecurepassword`
- `regenerate` - `regenerate`
- Options: - Options:
- `hooks`: Regenerate git-hooks for all repositories - `hooks`: Regenerate git-hooks for all repositories

@ -235,6 +235,12 @@ func (u *User) GetEmail() string {
return u.Email return u.Email
} }
// GetAllUsers returns a slice of all users found in DB.
func GetAllUsers() ([]*User, error) {
users := make([]*User, 0)
return users, x.OrderBy("id").Find(&users)
}
// APIFormat converts a User to api.User // APIFormat converts a User to api.User
func (u *User) APIFormat() *api.User { func (u *User) APIFormat() *api.User {
if u == nil { if u == nil {

Loading…
Cancel
Save