Skip to content

Instantly share code, notes, and snippets.

@fat4lix
Created November 5, 2019 16:06
Show Gist options
  • Select an option

  • Save fat4lix/b7d371a8b886250452495dee576fdc52 to your computer and use it in GitHub Desktop.

Select an option

Save fat4lix/b7d371a8b886250452495dee576fdc52 to your computer and use it in GitHub Desktop.
Example policies
package permission
import (
"context"
)
var store PolicyStore
func init() {
store = PolicyStore{}
store.RegisterPolicy("user", PolicyMap{
"canEditSomething": func(ctx *context.Context) bool {
// Get permissions from context, and authorize ability
return true
},
})
}
type PolicyStore struct {
policies map[string]PolicyMap
}
type PolicyMap map[string]func(ctx *context.Context) bool
func (s *PolicyStore) RegisterPolicy(name string, p PolicyMap) {
s.policies[name] = p
}
func Check(policy string, ability string, c context.Context) bool {
callback := store.policies[policy][ability]
return callback(&c)
}
/**
Usage:
permission.Check("user", "canEditSomething", ctx)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment