Created
November 5, 2019 16:06
-
-
Save fat4lix/b7d371a8b886250452495dee576fdc52 to your computer and use it in GitHub Desktop.
Example policies
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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