Skip to content

Commit cb171db

Browse files
author
ydelafollye
authored
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
1 parent 4fbe645 commit cb171db

File tree

3 files changed

+121
-24
lines changed

3 files changed

+121
-24
lines changed

cmd/admin.go

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,38 @@ var (
3030
Name: "admin",
3131
Usage: "Command line interface to perform common administrative operations",
3232
Subcommands: []cli.Command{
33-
subcmdCreateUser,
34-
subcmdChangePassword,
33+
subcmdUser,
3534
subcmdRepoSyncReleases,
3635
subcmdRegenerate,
3736
subcmdAuth,
3837
},
3938
}
4039

41-
subcmdCreateUser = cli.Command{
42-
Name: "create-user",
40+
subcmdUser = cli.Command{
41+
Name: "user",
42+
Usage: "Modify users",
43+
Subcommands: []cli.Command{
44+
microcmdUserCreate,
45+
microcmdUserList,
46+
microcmdUserChangePassword,
47+
microcmdUserDelete,
48+
},
49+
}
50+
51+
microcmdUserList = cli.Command{
52+
Name: "list",
53+
Usage: "List users",
54+
Action: runListUsers,
55+
Flags: []cli.Flag{
56+
cli.BoolFlag{
57+
Name: "admin",
58+
Usage: "List only admin users",
59+
},
60+
},
61+
}
62+
63+
microcmdUserCreate = cli.Command{
64+
Name: "create",
4365
Usage: "Create a new user in database",
4466
Action: runCreateUser,
4567
Flags: []cli.Flag{
@@ -83,7 +105,7 @@ var (
83105
},
84106
}
85107

86-
subcmdChangePassword = cli.Command{
108+
microcmdUserChangePassword = cli.Command{
87109
Name: "change-password",
88110
Usage: "Change a user's password",
89111
Action: runChangePassword,
@@ -101,6 +123,13 @@ var (
101123
},
102124
}
103125

126+
microcmdUserDelete = cli.Command{
127+
Name: "delete",
128+
Usage: "Delete specific user",
129+
Flags: []cli.Flag{idFlag},
130+
Action: runDeleteUser,
131+
}
132+
104133
subcmdRepoSyncReleases = cli.Command{
105134
Name: "repo-sync-releases",
106135
Usage: "Synchronize repository releases with tags",
@@ -377,6 +406,56 @@ func runCreateUser(c *cli.Context) error {
377406
return nil
378407
}
379408

409+
func runListUsers(c *cli.Context) error {
410+
if err := initDB(); err != nil {
411+
return err
412+
}
413+
414+
users, err := models.GetAllUsers()
415+
416+
if err != nil {
417+
return err
418+
}
419+
420+
w := tabwriter.NewWriter(os.Stdout, 5, 0, 1, ' ', 0)
421+
422+
if c.IsSet("admin") {
423+
fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\n")
424+
for _, u := range users {
425+
if u.IsAdmin {
426+
fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", u.ID, u.Name, u.Email, u.IsActive)
427+
}
428+
}
429+
} else {
430+
fmt.Fprintf(w, "ID\tUsername\tEmail\tIsActive\tIsAdmin\n")
431+
for _, u := range users {
432+
fmt.Fprintf(w, "%d\t%s\t%s\t%t\t%t\n", u.ID, u.Name, u.Email, u.IsActive, u.IsAdmin)
433+
}
434+
435+
}
436+
437+
w.Flush()
438+
return nil
439+
440+
}
441+
442+
func runDeleteUser(c *cli.Context) error {
443+
if !c.IsSet("id") {
444+
return fmt.Errorf("--id flag is missing")
445+
}
446+
447+
if err := initDB(); err != nil {
448+
return err
449+
}
450+
451+
user, err := models.GetUserByID(c.Int64("id"))
452+
if err != nil {
453+
return err
454+
}
455+
456+
return models.DeleteUser(user)
457+
}
458+
380459
func runRepoSyncReleases(c *cli.Context) error {
381460
if err := initDB(); err != nil {
382461
return err

docs/content/doc/usage/command-line.en-us.md

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,40 @@ Starts the server:
5555
Admin operations:
5656

5757
- Commands:
58-
- `create-user`
59-
- Options:
60-
- `--name value`: Username. Required. As of gitea 1.9.0, use the `--username` flag instead.
61-
- `--username value`: Username. Required. New in gitea 1.9.0.
62-
- `--password value`: Password. Required.
63-
- `--email value`: Email. Required.
64-
- `--admin`: If provided, this makes the user an admin. Optional.
65-
- `--access-token`: If provided, an access token will be created for the user. Optional. (default: false).
66-
- `--must-change-password`: If provided, the created user will be required to choose a newer password after
58+
- `user`:
59+
- `list`:
60+
- Options:
61+
- `--admin`: List only admin users. Optional.
62+
- Description: lists all users that exist
63+
- Examples:
64+
- `gitea admin user list`
65+
- `delete`:
66+
- Options:
67+
- `--id`: ID of user to be deleted. Required.
68+
- Examples:
69+
- `gitea admin user delete --id 1`
70+
- `create`:
71+
- Options:
72+
- `--name value`: Username. Required. As of gitea 1.9.0, use the `--username` flag instead.
73+
- `--username value`: Username. Required. New in gitea 1.9.0.
74+
- `--password value`: Password. Required.
75+
- `--email value`: Email. Required.
76+
- `--admin`: If provided, this makes the user an admin. Optional.
77+
- `--access-token`: If provided, an access token will be created for the user. Optional. (default: false).
78+
- `--must-change-password`: If provided, the created user will be required to choose a newer password after
6779
the initial login. Optional. (default: true).
68-
- ``--random-password``: If provided, a randomly generated password will be used as the password of
80+
- ``--random-password``: If provided, a randomly generated password will be used as the password of
6981
the created user. The value of `--password` will be discarded. Optional.
70-
- `--random-password-length`: If provided, it will be used to configure the length of the randomly
82+
- `--random-password-length`: If provided, it will be used to configure the length of the randomly
7183
generated password. Optional. (default: 12)
72-
- Examples:
73-
- `gitea admin create-user --username myname --password asecurepassword --email [email protected]`
74-
- `change-password`
75-
- Options:
76-
- `--username value`, `-u value`: Username. Required.
77-
- `--password value`, `-p value`: New password. Required.
78-
- Examples:
79-
- `gitea admin change-password --username myname --password asecurepassword`
84+
- Examples:
85+
- `gitea admin create-user --username myname --password asecurepassword --email [email protected]`
86+
- `change-password`:
87+
- Options:
88+
- `--username value`, `-u value`: Username. Required.
89+
- `--password value`, `-p value`: New password. Required.
90+
- Examples:
91+
- `gitea admin change-password --username myname --password asecurepassword`
8092
- `regenerate`
8193
- Options:
8294
- `hooks`: Regenerate git-hooks for all repositories

models/user.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ func (u *User) GetEmail() string {
235235
return u.Email
236236
}
237237

238+
// GetAllUsers returns a slice of all users found in DB.
239+
func GetAllUsers() ([]*User, error) {
240+
users := make([]*User, 0)
241+
return users, x.OrderBy("id").Find(&users)
242+
}
243+
238244
// APIFormat converts a User to api.User
239245
func (u *User) APIFormat() *api.User {
240246
if u == nil {

0 commit comments

Comments
 (0)