pinterface.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Package pinterface provides interface types for the xyproto/simple* and xyproto/permission* packages
  2. package pinterface
  3. import "net/http"
  4. // Stable API within the same version number
  5. const Version = 5.0
  6. // Database interfaces
  7. type IList interface {
  8. Add(value string) error
  9. All() ([]string, error) // GetAll in version 4.0
  10. Last() (string, error) // GetLast in version 4.0
  11. LastN(n int) ([]string, error) // GetLastN in version 4.0
  12. Remove() error
  13. Clear() error
  14. }
  15. type ISet interface {
  16. Add(value string) error
  17. Has(value string) (bool, error)
  18. All() ([]string, error) // GetAll in version 4.0
  19. Del(value string) error
  20. Remove() error
  21. Clear() error
  22. }
  23. type IHashMap interface {
  24. Set(owner, key, value string) error
  25. Get(owner, key string) (string, error)
  26. Has(owner, key string) (bool, error)
  27. Exists(owner string) (bool, error)
  28. All() ([]string, error) // GetAll in version 4.0
  29. Keys(owner string) ([]string, error)
  30. DelKey(owner, key string) error
  31. Del(key string) error
  32. Remove() error
  33. Clear() error
  34. }
  35. type IKeyValue interface {
  36. Set(key, value string) error
  37. Get(key string) (string, error)
  38. Del(key string) error
  39. Inc(key string) (string, error)
  40. Remove() error
  41. Clear() error
  42. }
  43. // Interface for making it possible to depend on different versions of the permission package,
  44. // or other packages that implement userstates.
  45. type IUserState interface {
  46. UserRights(req *http.Request) bool
  47. HasUser(username string) bool
  48. BooleanField(username, fieldname string) bool
  49. SetBooleanField(username, fieldname string, val bool)
  50. IsConfirmed(username string) bool
  51. IsLoggedIn(username string) bool
  52. AdminRights(req *http.Request) bool
  53. IsAdmin(username string) bool
  54. UsernameCookie(req *http.Request) (string, error)
  55. SetUsernameCookie(w http.ResponseWriter, username string) error
  56. AllUsernames() ([]string, error)
  57. Email(username string) (string, error)
  58. PasswordHash(username string) (string, error)
  59. AllUnconfirmedUsernames() ([]string, error)
  60. ConfirmationCode(username string) (string, error)
  61. AddUnconfirmed(username, confirmationCode string)
  62. RemoveUnconfirmed(username string)
  63. MarkConfirmed(username string)
  64. RemoveUser(username string)
  65. SetAdminStatus(username string)
  66. RemoveAdminStatus(username string)
  67. AddUser(username, password, email string)
  68. SetLoggedIn(username string)
  69. SetLoggedOut(username string)
  70. Login(w http.ResponseWriter, username string) error
  71. ClearCookie(w http.ResponseWriter)
  72. Logout(username string)
  73. Username(req *http.Request) string
  74. CookieTimeout(username string) int64
  75. SetCookieTimeout(cookieTime int64)
  76. CookieSecret() string
  77. SetCookieSecret(cookieSecret string)
  78. PasswordAlgo() string
  79. SetPasswordAlgo(algorithm string) error
  80. HashPassword(username, password string) string
  81. SetPassword(username, password string)
  82. CorrectPassword(username, password string) bool
  83. AlreadyHasConfirmationCode(confirmationCode string) bool
  84. FindUserByConfirmationCode(confirmationcode string) (string, error)
  85. Confirm(username string)
  86. ConfirmUserByConfirmationCode(confirmationcode string) error
  87. SetMinimumConfirmationCodeLength(length int)
  88. GenerateUniqueConfirmationCode() (string, error)
  89. Users() IHashMap
  90. Host() IHost
  91. Creator() ICreator
  92. }
  93. // Data structure creator
  94. type ICreator interface {
  95. NewList(id string) (IList, error)
  96. NewSet(id string) (ISet, error)
  97. NewHashMap(id string) (IHashMap, error)
  98. NewKeyValue(id string) (IKeyValue, error)
  99. }
  100. // Database host (or file)
  101. type IHost interface {
  102. Ping() error
  103. Close()
  104. }
  105. // Redis host (implemented structures can also be an IHost, of course)
  106. type IRedisHost interface {
  107. Pool()
  108. DatabaseIndex()
  109. }
  110. // Redis data structure creator
  111. type IRedisCreator interface {
  112. SelectDatabase(dbindex int)
  113. }
  114. // Middleware for permissions
  115. type IPermissions interface {
  116. SetDenyFunction(f http.HandlerFunc)
  117. DenyFunction() http.HandlerFunc
  118. UserState() IUserState
  119. Clear()
  120. AddAdminPath(prefix string)
  121. AddUserPath(prefix string)
  122. AddPublicPath(prefix string)
  123. SetAdminPath(pathPrefixes []string)
  124. SetUserPath(pathPrefixes []string)
  125. SetPublicPath(pathPrefixes []string)
  126. Rejected(w http.ResponseWriter, req *http.Request) bool
  127. ServeHTTP(w http.ResponseWriter, req *http.Request, next http.HandlerFunc)
  128. }