gitea源码

admin_auth.go 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cmd
  4. import (
  5. "context"
  6. "errors"
  7. "fmt"
  8. "os"
  9. "text/tabwriter"
  10. auth_model "code.gitea.io/gitea/models/auth"
  11. "code.gitea.io/gitea/models/db"
  12. auth_service "code.gitea.io/gitea/services/auth"
  13. "github.com/urfave/cli/v3"
  14. )
  15. var (
  16. microcmdAuthDelete = &cli.Command{
  17. Name: "delete",
  18. Usage: "Delete specific auth source",
  19. Flags: []cli.Flag{idFlag()},
  20. Action: runDeleteAuth,
  21. }
  22. microcmdAuthList = &cli.Command{
  23. Name: "list",
  24. Usage: "List auth sources",
  25. Action: runListAuth,
  26. Flags: []cli.Flag{
  27. &cli.IntFlag{
  28. Name: "min-width",
  29. Usage: "Minimal cell width including any padding for the formatted table",
  30. Value: 0,
  31. },
  32. &cli.IntFlag{
  33. Name: "tab-width",
  34. Usage: "width of tab characters in formatted table (equivalent number of spaces)",
  35. Value: 8,
  36. },
  37. &cli.IntFlag{
  38. Name: "padding",
  39. Usage: "padding added to a cell before computing its width",
  40. Value: 1,
  41. },
  42. &cli.StringFlag{
  43. Name: "pad-char",
  44. Usage: `ASCII char used for padding if padchar == '\\t', the Writer will assume that the width of a '\\t' in the formatted output is tabwidth, and cells are left-aligned independent of align_left (for correct-looking results, tabwidth must correspond to the tab width in the viewer displaying the result)`,
  45. Value: "\t",
  46. },
  47. &cli.BoolFlag{
  48. Name: "vertical-bars",
  49. Usage: "Set to true to print vertical bars between columns",
  50. },
  51. },
  52. }
  53. )
  54. func runListAuth(ctx context.Context, c *cli.Command) error {
  55. if err := initDB(ctx); err != nil {
  56. return err
  57. }
  58. authSources, err := db.Find[auth_model.Source](ctx, auth_model.FindSourcesOptions{})
  59. if err != nil {
  60. return err
  61. }
  62. flags := tabwriter.AlignRight
  63. if c.Bool("vertical-bars") {
  64. flags |= tabwriter.Debug
  65. }
  66. padChar := byte('\t')
  67. if len(c.String("pad-char")) > 0 {
  68. padChar = c.String("pad-char")[0]
  69. }
  70. // loop through each source and print
  71. w := tabwriter.NewWriter(os.Stdout, c.Int("min-width"), c.Int("tab-width"), c.Int("padding"), padChar, flags)
  72. fmt.Fprintf(w, "ID\tName\tType\tEnabled\n")
  73. for _, source := range authSources {
  74. fmt.Fprintf(w, "%d\t%s\t%s\t%t\n", source.ID, source.Name, source.Type.String(), source.IsActive)
  75. }
  76. w.Flush()
  77. return nil
  78. }
  79. func runDeleteAuth(ctx context.Context, c *cli.Command) error {
  80. if !c.IsSet("id") {
  81. return errors.New("--id flag is missing")
  82. }
  83. if err := initDB(ctx); err != nil {
  84. return err
  85. }
  86. source, err := auth_model.GetSourceByID(ctx, c.Int64("id"))
  87. if err != nil {
  88. return err
  89. }
  90. return auth_service.DeleteSource(ctx, source)
  91. }