https://github.com/codewars/batcan
Heroic authorizations system for ruby. Both simple and highly flexible.
https://github.com/codewars/batcan
Last synced: 4 months ago
JSON representation
Heroic authorizations system for ruby. Both simple and highly flexible.
- Host: GitHub
- URL: https://github.com/codewars/batcan
- Owner: codewars
- License: mit
- Created: 2014-10-30T05:43:22.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2020-10-29T20:33:59.000Z (over 5 years ago)
- Last Synced: 2025-01-10T05:36:12.684Z (about 1 year ago)
- Language: Ruby
- Homepage:
- Size: 14.6 KB
- Stars: 2
- Watchers: 6
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: changes.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Batcan
## Usage Example
```ruby
# simple example user. You can use PORO if you want.
User = Struct.new(:role) do
include Batcan::Canable
# the default ability if a more specific one is not defined
def default_can?(action, target, options = {})
!!role # by default if a user has any role than they are permitted
end
end
class Team
include Batcan::Permissible
def members
@members ||= []
end
permission :join do |team, user|
# returning a string is like returning false (not allowed) but with a reason
"guest role is not allowed to join" if user.role == :guest
# if nil is returned then default_can? value will be used
end
permission :delete do |team, user|
# only admins are allowed, everyone else will be dissollowed
user.role == :admin
end
# field level permissions
permission :add, :members do |team, user|
# allow members to be added if the user is a member
team.members.include? user
end
end
user = User.new(:admin)
team = Team.new
user.can?(:join, team) # returns true
user.role = :guest
user.can!(:join, team) # raises an error
```
## Storage
There is a Batcan::Storage module that can be utilized along with activerecord/mongoid and the sentient_user gems to provide
secure persistance methods at the model layer. For example:
```ruby
class User
include Mongoid
include SentientUser
include Batcan::Canable
field :role
end
class Team
include Mongoid
include Batcan::Permissible
include Batcan::Storable
field :name
# allow admin users to create/update teams
permission :save do |team, user|
user.role == :admin
end
end
user = User.first.make_current
team = Team.first
team.name = 'foo'
team.store! # will raise an error if user is not an admin
```
## Installation
Add this line to your application's Gemfile:
gem 'batcan'
And then execute:
$ bundle
Or install it yourself as:
$ gem install batcan
## Usage
TODO: Write usage instructions here
## Contributing
1. Fork it ( https://github.com/[my-github-username]/batcan/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