rbac.cr
Version, currently 0.2.03 versions
- 0.2.0latestAug 11, 2017
- 0.1.1not indexedAug 11, 2017
- 0.1.0not indexedAug 11, 2017
github.com/TPei/rbac.cr
Simple Role Based Access Control for Crystal
0 stars
0 dependents
License: MIT
Installation
# Add this to your shard.yml
dependencies:
rbac.cr:
github: TPei/rbac.cr
version: ~> 0.2.0Then run:
shards installshard.yml
- Crystal
0.22.0- License
- MIT
- Author
- TPei
- Target
rbac.crfrom src/rbac.cr
Dependencies
This version declares no dependencies.
README
rbac.cr
rbac.cr provides simple role based access control for crystal programs
Installation
Add to your shard.yml
dependencies:
rbac.cr:
github: tpei/rbac.cr
branch: master
and then install the library into your project with
$ crystal deps
Usage
rbac.cr come with two basic modules
Rbac::Resource: for which access is requiredRbac::Accessor: for instances that can be given access to resources
Consider the following simple example:
# a DataStore that has different access levels
class DataStore
include Rbac::Resource
end
# a User that can also have different access levels
class User
include Rbac::Accessor
end
# create a DataStore and add access levels
ds = DataStore.new
ds.has_roles :add, :edit, :delete
ds.roles # => [:add, :edit, :delete]
ds.has_role? :add # => true
ds.has_role? :read # => false
# create users with different access levels
admin = User.new
admin.has_roles ds.roles
author = User.new
author.has_roles :add, :edit
editor = User.new
editor.has_roles :edit
impotent = User.new
impotent.has_roles :read
# now you can simply check if a user has any of the resource rights
ds.authorized? author # => true
ds.authorized? impotent # => false
# and you can also check if a user has a specific resource right
ds.authorized?(admin, :add, :delete) # => true
# may is a shorthand for the `authorized?(Roleable, *Symbol)` method
ds.may?(editor, :add) # => false
You can also define default roles per model by adding the has_roles
call to the initializer:
class UserWithDefaultRoles
include Rbac::Accessor
def initialize
has_roles :add
end
end
# every instance now has this roles
u = UserWithDefaultRoles.new
u.roles # => [:add]
# roles can of course still be extended for class instances
u.has_roles :edit, :delete
u.roles # => [:add, :edit, :delete]
Contributing
- Fork it ( https://github.com/tpei/rbac/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- tpei TPei - creator, maintainer
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.2.0- Tagged
- Aug 11, 2017
- Commit
fe48aa42a246- Crystal
0.22.0- Indexed
- yes
Dependents
No indexed shard depends on this one yet.
Repository
github.com/TPei/rbac.cr
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 17, 2026
- Synced
- Aug 17, 2026
- Versions
- 3
