You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

admin_user_create.go 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "errors"
  6. "fmt"
  7. "os"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. user_model "code.gitea.io/gitea/models/user"
  10. pwd "code.gitea.io/gitea/modules/auth/password"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/util"
  13. "github.com/urfave/cli"
  14. )
  15. var microcmdUserCreate = cli.Command{
  16. Name: "create",
  17. Usage: "Create a new user in database",
  18. Action: runCreateUser,
  19. Flags: []cli.Flag{
  20. cli.StringFlag{
  21. Name: "name",
  22. Usage: "Username. DEPRECATED: use username instead",
  23. },
  24. cli.StringFlag{
  25. Name: "username",
  26. Usage: "Username",
  27. },
  28. cli.StringFlag{
  29. Name: "password",
  30. Usage: "User password",
  31. },
  32. cli.StringFlag{
  33. Name: "email",
  34. Usage: "User email address",
  35. },
  36. cli.BoolFlag{
  37. Name: "admin",
  38. Usage: "User is an admin",
  39. },
  40. cli.BoolFlag{
  41. Name: "random-password",
  42. Usage: "Generate a random password for the user",
  43. },
  44. cli.BoolFlag{
  45. Name: "must-change-password",
  46. Usage: "Set this option to false to prevent forcing the user to change their password after initial login, (Default: true)",
  47. },
  48. cli.IntFlag{
  49. Name: "random-password-length",
  50. Usage: "Length of the random password to be generated",
  51. Value: 12,
  52. },
  53. cli.BoolFlag{
  54. Name: "access-token",
  55. Usage: "Generate access token for the user",
  56. },
  57. cli.BoolFlag{
  58. Name: "restricted",
  59. Usage: "Make a restricted user account",
  60. },
  61. },
  62. }
  63. func runCreateUser(c *cli.Context) error {
  64. if err := argsSet(c, "email"); err != nil {
  65. return err
  66. }
  67. if c.IsSet("name") && c.IsSet("username") {
  68. return errors.New("Cannot set both --name and --username flags")
  69. }
  70. if !c.IsSet("name") && !c.IsSet("username") {
  71. return errors.New("One of --name or --username flags must be set")
  72. }
  73. if c.IsSet("password") && c.IsSet("random-password") {
  74. return errors.New("cannot set both -random-password and -password flags")
  75. }
  76. var username string
  77. if c.IsSet("username") {
  78. username = c.String("username")
  79. } else {
  80. username = c.String("name")
  81. fmt.Fprintf(os.Stderr, "--name flag is deprecated. Use --username instead.\n")
  82. }
  83. ctx, cancel := installSignals()
  84. defer cancel()
  85. if err := initDB(ctx); err != nil {
  86. return err
  87. }
  88. var password string
  89. if c.IsSet("password") {
  90. password = c.String("password")
  91. } else if c.IsSet("random-password") {
  92. var err error
  93. password, err = pwd.Generate(c.Int("random-password-length"))
  94. if err != nil {
  95. return err
  96. }
  97. fmt.Printf("generated random password is '%s'\n", password)
  98. } else {
  99. return errors.New("must set either password or random-password flag")
  100. }
  101. // always default to true
  102. changePassword := true
  103. // If this is the first user being created.
  104. // Take it as the admin and don't force a password update.
  105. if n := user_model.CountUsers(nil); n == 0 {
  106. changePassword = false
  107. }
  108. if c.IsSet("must-change-password") {
  109. changePassword = c.Bool("must-change-password")
  110. }
  111. restricted := util.OptionalBoolNone
  112. if c.IsSet("restricted") {
  113. restricted = util.OptionalBoolOf(c.Bool("restricted"))
  114. }
  115. // default user visibility in app.ini
  116. visibility := setting.Service.DefaultUserVisibilityMode
  117. u := &user_model.User{
  118. Name: username,
  119. Email: c.String("email"),
  120. Passwd: password,
  121. IsAdmin: c.Bool("admin"),
  122. MustChangePassword: changePassword,
  123. Visibility: visibility,
  124. }
  125. overwriteDefault := &user_model.CreateUserOverwriteOptions{
  126. IsActive: util.OptionalBoolTrue,
  127. IsRestricted: restricted,
  128. }
  129. if err := user_model.CreateUser(u, overwriteDefault); err != nil {
  130. return fmt.Errorf("CreateUser: %w", err)
  131. }
  132. if c.Bool("access-token") {
  133. t := &auth_model.AccessToken{
  134. Name: "gitea-admin",
  135. UID: u.ID,
  136. }
  137. if err := auth_model.NewAccessToken(t); err != nil {
  138. return err
  139. }
  140. fmt.Printf("Access token was successfully created... %s\n", t.Token)
  141. }
  142. fmt.Printf("New user '%s' has been successfully created!\n", username)
  143. return nil
  144. }