user_allow.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package cmd
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/urfave/cli/v2"
  6. "heckel.io/ntfy/auth"
  7. "heckel.io/ntfy/util"
  8. )
  9. var flagsAllow = append(
  10. userCommandFlags(),
  11. &cli.BoolFlag{Name: "reset", Aliases: []string{"r"}, Usage: "reset access for user (and topic)"},
  12. )
  13. var cmdAllow = &cli.Command{
  14. Name: "allow",
  15. Usage: "Grant a user access to a topic",
  16. UsageText: "ntfy allow USERNAME TOPIC [read-write|read-only|write-only]",
  17. Flags: flagsAllow,
  18. Before: initConfigFileInputSource("config", flagsAllow),
  19. Action: execUserAllow,
  20. Category: categoryServer,
  21. }
  22. func execUserAllow(c *cli.Context) error {
  23. username := c.Args().Get(0)
  24. topic := c.Args().Get(1)
  25. perms := c.Args().Get(2)
  26. reset := c.Bool("reset")
  27. if username == "" {
  28. return errors.New("username expected, type 'ntfy allow --help' for help")
  29. } else if !reset && topic == "" {
  30. return errors.New("topic expected, type 'ntfy allow --help' for help")
  31. } else if !util.InStringList([]string{"", "read-write", "read-only", "read", "ro", "write-only", "write", "wo", "none"}, perms) {
  32. return errors.New("permission must be one of: read-write, read-only, write-only, or none (or the aliases: read, ro, write, wo)")
  33. }
  34. if username == "everyone" {
  35. username = ""
  36. }
  37. read := util.InStringList([]string{"", "read-write", "read-only", "read", "ro"}, perms)
  38. write := util.InStringList([]string{"", "read-write", "write-only", "write", "wo"}, perms)
  39. manager, err := createAuthManager(c)
  40. if err != nil {
  41. return err
  42. }
  43. if reset {
  44. return doAccessReset(c, manager, username, topic)
  45. }
  46. return doAccessAllow(c, manager, username, topic, read, write)
  47. }
  48. func doAccessAllow(c *cli.Context, manager auth.Manager, username string, topic string, read bool, write bool) error {
  49. if err := manager.AllowAccess(username, topic, read, write); err != nil {
  50. return err
  51. }
  52. if username == "" {
  53. if read && write {
  54. fmt.Fprintf(c.App.ErrWriter, "Anonymous users granted full access to topic %s\n", topic)
  55. } else if read {
  56. fmt.Fprintf(c.App.ErrWriter, "Anonymous users granted read-only access to topic %s\n", topic)
  57. } else if write {
  58. fmt.Fprintf(c.App.ErrWriter, "Anonymous users granted write-only access to topic %s\n", topic)
  59. } else {
  60. fmt.Fprintf(c.App.ErrWriter, "Revoked all access to topic %s for all anonymous users\n", topic)
  61. }
  62. } else {
  63. if read && write {
  64. fmt.Fprintf(c.App.ErrWriter, "User %s now has read-write access to topic %s\n", username, topic)
  65. } else if read {
  66. fmt.Fprintf(c.App.ErrWriter, "User %s now has read-only access to topic %s\n", username, topic)
  67. } else if write {
  68. fmt.Fprintf(c.App.ErrWriter, "User %s now has write-only access to topic %s\n", username, topic)
  69. } else {
  70. fmt.Fprintf(c.App.ErrWriter, "Revoked all access to topic %s for user %s\n", topic, username)
  71. }
  72. }
  73. return nil
  74. }
  75. func doAccessReset(c *cli.Context, manager auth.Manager, username, topic string) error {
  76. if err := manager.ResetAccess(username, topic); err != nil {
  77. return err
  78. }
  79. if username == "" {
  80. if topic == "" {
  81. fmt.Fprintln(c.App.ErrWriter, "Reset access for all anonymous users and all topics")
  82. } else {
  83. fmt.Fprintf(c.App.ErrWriter, "Reset access to topic %s for all anonymous users\n", topic)
  84. }
  85. } else {
  86. if topic == "" {
  87. fmt.Fprintf(c.App.ErrWriter, "Reset access for user %s to all topics\n", username)
  88. } else {
  89. fmt.Fprintf(c.App.ErrWriter, "Reset access for user %s and topic %s\n", username, topic)
  90. }
  91. }
  92. return nil
  93. }