Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

admin.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cmd
  6. import (
  7. "fmt"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/setting"
  10. "github.com/urfave/cli"
  11. )
  12. var (
  13. // CmdAdmin represents the available admin sub-command.
  14. CmdAdmin = cli.Command{
  15. Name: "admin",
  16. Usage: "Perform admin operations on command line",
  17. Description: `Allow using internal logic of Gitea without hacking into the source code
  18. to make automatic initialization process more smoothly`,
  19. Subcommands: []cli.Command{
  20. subcmdCreateUser,
  21. subcmdChangePassword,
  22. },
  23. }
  24. subcmdCreateUser = cli.Command{
  25. Name: "create-user",
  26. Usage: "Create a new user in database",
  27. Action: runCreateUser,
  28. Flags: []cli.Flag{
  29. cli.StringFlag{
  30. Name: "name",
  31. Value: "",
  32. Usage: "Username",
  33. },
  34. cli.StringFlag{
  35. Name: "password",
  36. Value: "",
  37. Usage: "User password",
  38. },
  39. cli.StringFlag{
  40. Name: "email",
  41. Value: "",
  42. Usage: "User email address",
  43. },
  44. cli.BoolFlag{
  45. Name: "admin",
  46. Usage: "User is an admin",
  47. },
  48. cli.StringFlag{
  49. Name: "config, c",
  50. Value: "custom/conf/app.ini",
  51. Usage: "Custom configuration file path",
  52. },
  53. },
  54. }
  55. subcmdChangePassword = cli.Command{
  56. Name: "change-password",
  57. Usage: "Change a user's password",
  58. Action: runChangePassword,
  59. Flags: []cli.Flag{
  60. cli.StringFlag{
  61. Name: "username,u",
  62. Value: "",
  63. Usage: "The user to change password for",
  64. },
  65. cli.StringFlag{
  66. Name: "password,p",
  67. Value: "",
  68. Usage: "New password to set for user",
  69. },
  70. },
  71. }
  72. )
  73. func runChangePassword(c *cli.Context) error {
  74. if !c.IsSet("password") {
  75. return fmt.Errorf("Password is not specified")
  76. } else if !c.IsSet("username") {
  77. return fmt.Errorf("Username is not specified")
  78. }
  79. setting.NewContext()
  80. models.LoadConfigs()
  81. setting.NewXORMLogService(false)
  82. if err := models.SetEngine(); err != nil {
  83. return fmt.Errorf("models.SetEngine: %v", err)
  84. }
  85. uname := c.String("username")
  86. user, err := models.GetUserByName(uname)
  87. if err != nil {
  88. return fmt.Errorf("%v", err)
  89. }
  90. user.Passwd = c.String("password")
  91. if user.Salt, err = models.GetUserSalt(); err != nil {
  92. return fmt.Errorf("%v", err)
  93. }
  94. user.EncodePasswd()
  95. if err := models.UpdateUserCols(user, "passwd", "salt"); err != nil {
  96. return fmt.Errorf("%v", err)
  97. }
  98. fmt.Printf("User '%s' password has been successfully updated!\n", uname)
  99. return nil
  100. }
  101. func runCreateUser(c *cli.Context) error {
  102. if !c.IsSet("name") {
  103. return fmt.Errorf("Username is not specified")
  104. } else if !c.IsSet("password") {
  105. return fmt.Errorf("Password is not specified")
  106. } else if !c.IsSet("email") {
  107. return fmt.Errorf("Email is not specified")
  108. }
  109. if c.IsSet("config") {
  110. setting.CustomConf = c.String("config")
  111. }
  112. setting.NewContext()
  113. models.LoadConfigs()
  114. setting.NewXORMLogService(false)
  115. if err := models.SetEngine(); err != nil {
  116. return fmt.Errorf("models.SetEngine: %v", err)
  117. }
  118. if err := models.CreateUser(&models.User{
  119. Name: c.String("name"),
  120. Email: c.String("email"),
  121. Passwd: c.String("password"),
  122. IsActive: true,
  123. IsAdmin: c.Bool("admin"),
  124. }); err != nil {
  125. return fmt.Errorf("CreateUser: %v", err)
  126. }
  127. fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
  128. return nil
  129. }