permset.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package permset
  2. import (
  3. "context"
  4. "fmt"
  5. )
  6. // Perm is a permission to do some action.
  7. type Perm int
  8. // Permset represents a set of permissions.
  9. type Permset struct {
  10. permset map[Perm]bool
  11. }
  12. // New receives permissions to contain and returns a permset from it.
  13. func New(perms ...Perm) Permset {
  14. permset := Permset{permset: make(map[Perm]bool)}
  15. permset.Add(perms...)
  16. return permset
  17. }
  18. // Add adds the received perms to the permset.
  19. func (ps *Permset) Add(perms ...Perm) {
  20. for _, perm := range perms {
  21. ps.permset[perm] = true
  22. }
  23. }
  24. // Remove removes the received perms from the permset.
  25. func (ps *Permset) Remove(perms ...Perm) {
  26. for _, perm := range perms {
  27. if _, ok := ps.permset[perm]; ok {
  28. delete(ps.permset, perm)
  29. }
  30. }
  31. }
  32. // Perms returns the permissions contained within the permset.
  33. func (ps *Permset) Perms() []Perm {
  34. var perms []Perm
  35. for k := range ps.permset {
  36. perms = append(perms, k)
  37. }
  38. return perms
  39. }
  40. // Contains receives single Perm and returns true if the permset contains it.
  41. func (ps *Permset) Contains(perm Perm) bool {
  42. if _, ok := ps.permset[perm]; !ok {
  43. return false
  44. }
  45. return true
  46. }
  47. // ContainsAll returns true if the permset contains all received Perms.
  48. func (ps *Permset) ContainsAll(perms ...Perm) bool {
  49. for _, perm := range perms {
  50. if _, ok := ps.permset[perm]; !ok {
  51. return false
  52. }
  53. }
  54. return true
  55. }
  56. // ContainsSome returns true if the permset contains any one or more of the received Perms.
  57. func (ps *Permset) ContainsSome(perms ...Perm) bool {
  58. for _, perm := range perms {
  59. if _, ok := ps.permset[perm]; ok {
  60. return true
  61. }
  62. }
  63. return false
  64. }
  65. // ContainsNone returns true if the permset contains none one of the received Perms.
  66. func (ps *Permset) ContainsNone(perms ...Perm) bool {
  67. for _, perm := range perms {
  68. if _, ok := ps.permset[perm]; ok {
  69. return false
  70. }
  71. }
  72. return true
  73. }
  74. type permsetKeyType int
  75. const permsetKey permsetKeyType = iota
  76. // NewContext receives perms and returns a context with the received perms are the value of the context.
  77. func NewContext(ctx context.Context, permset Permset) context.Context {
  78. return context.WithValue(ctx, permsetKey, permset)
  79. }
  80. // FromContext receives a context and returns the permset in it.
  81. func FromContext(ctx context.Context) (Permset, error) {
  82. permset, ok := ctx.Value(permsetKey).(Permset)
  83. if !ok {
  84. return Permset{}, fmt.Errorf("cannot get context value")
  85. }
  86. return permset, nil
  87. }