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.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "github.com/urfave/cli"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. var (
  13. // CmdAdmin represents the available admin sub-command.
  14. CmdAdmin = cli.Command{
  15. Name: "admin",
  16. Usage: "Preform 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. },
  22. }
  23. subcmdCreateUser = cli.Command{
  24. Name: "create-user",
  25. Usage: "Create a new user in database",
  26. Action: runCreateUser,
  27. Flags: []cli.Flag{
  28. cli.StringFlag{
  29. Name: "name",
  30. Value: "",
  31. Usage: "Username",
  32. },
  33. cli.StringFlag{
  34. Name: "password",
  35. Value: "",
  36. Usage: "User password",
  37. },
  38. cli.StringFlag{
  39. Name: "email",
  40. Value: "",
  41. Usage: "User email address",
  42. },
  43. cli.BoolFlag{
  44. Name: "admin",
  45. Usage: "User is an admin",
  46. },
  47. cli.StringFlag{
  48. Name: "config, c",
  49. Value: "custom/conf/app.ini",
  50. Usage: "Custom configuration file path",
  51. },
  52. },
  53. }
  54. )
  55. func runCreateUser(c *cli.Context) error {
  56. if !c.IsSet("name") {
  57. return fmt.Errorf("Username is not specified")
  58. } else if !c.IsSet("password") {
  59. return fmt.Errorf("Password is not specified")
  60. } else if !c.IsSet("email") {
  61. return fmt.Errorf("Email is not specified")
  62. }
  63. if c.IsSet("config") {
  64. setting.CustomConf = c.String("config")
  65. }
  66. setting.NewContext()
  67. models.LoadConfigs()
  68. models.SetEngine()
  69. if err := models.CreateUser(&models.User{
  70. Name: c.String("name"),
  71. Email: c.String("email"),
  72. Passwd: c.String("password"),
  73. IsActive: true,
  74. IsAdmin: c.Bool("admin"),
  75. }); err != nil {
  76. return fmt.Errorf("CreateUser: %v", err)
  77. }
  78. fmt.Printf("New user '%s' has been successfully created!\n", c.String("name"))
  79. return nil
  80. }