rbac.cr

Version, currently 0.1.13 versions

github.com/TPei/rbac.cr

Simple Role Based Access Control for Crystal

0 stars
0 dependents
License: MIT

Nothing has been indexed for 0.1.1 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.

Installation

# Add this to your shard.yml
dependencies:
  rbac.cr:
    github: TPei/rbac.cr
    version: ~> 0.1.1

Then run:

shards install

shard.yml

No shard.yml has been indexed for 0.1.1. You can read it on the repository.

Dependencies

Unknown: the shard.yml for this version has not been read yet.

README

This README is the one indexed from the repository at its latest ref, not from the tag for this version.

rbac.cr

Build Status Release MIT Docs

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 required
  • Rbac::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

  1. Fork it ( https://github.com/tpei/rbac/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • tpei TPei - creator, maintainer