项目简介

TL;DR;

acl is a lightweight acl manager for go.

Features

  • Design simple & reusable roles to empower your application.
  • Acquire the rights of other roles to build a powerful set of permissions.
  • Resolve possible roles by examine them in an unified way.

Example

  1. type User struct {
  2. isAdmin bool
  3. }
  4. func main() {
  5. // first of all: create a new manager instance to register all your roles in one place
  6. manager := acl.NewManager()
  7. // now you can use `Ensure` to guarantee that the role with the passed identifier is present
  8. user := manager.Ensure("user").Grant("profile.edit")
  9. // use `Grant`, `Revoke` and `AcquireFrom` to extend the right stack
  10. editor := manager.Ensure("editor").Grant("news.list", "news.create", "news.edit").AcquireFrom(user)
  11. // you can also use NewRole to create a Role manually
  12. admin := acl.NewRole("admin").Grant("news.delete").AcquireFrom(editor)
  13. // note, that you have to register the role by yourself
  14. manager.Register(admin)
  15. // to check if a right was granted to a role you can use:
  16. var hasAccess bool
  17. hasAccess = admin.Has("some.right")
  18. // to check if at least one of the expected rights is present:
  19. hasAccess = admin.HasOneOf("news.list", "news.create")
  20. // ... and finally, to check that all the expected rights are present, use:
  21. hasAccess = admin.HasAllOf("news.delete", "news.list")
  22. // a role can be extended with an examiner to determine whether a role can be added
  23. // to a `ResultSet`
  24. admin.SetExaminer(func (payload interface{}) bool {
  25. user := payload.(User)
  26. return user.isAdmin
  27. })
  28. // to get a result set you can use the managers `Examine` function
  29. rs := manager.Examine(User{isAdmin: true})
  30. // a result set contains "Has", "HasOneOf" and "HasAllOf" as described above and...
  31. // `GetRole` to grab specific roles from the result set
  32. expectedRole := rs.GetRole("admin")
  33. // you can also check if a role was added to a result set using:
  34. if rs.HasRole("admin") {
  35. // ...
  36. }
  37. }

Blueprint