Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sirscriptalot/permissions
Small authorization library in Ruby.
https://github.com/sirscriptalot/permissions
authorization permissions
Last synced: 12 days ago
JSON representation
Small authorization library in Ruby.
- Host: GitHub
- URL: https://github.com/sirscriptalot/permissions
- Owner: sirscriptalot
- License: mit
- Created: 2017-08-11T15:28:51.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-08-12T02:45:53.000Z (over 7 years ago)
- Last Synced: 2024-04-26T21:04:26.540Z (9 months ago)
- Topics: authorization, permissions
- Language: Ruby
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Permissions
A small library for adding permissions to an application for authorization.
## Installation
`gem install permissions`
## Usage
### API
`for(*keys, &block)`: Creates a permission with the given block for each key.
`authorize?(key, *args)`: Runs permission block for key with args.
`deep_dup`: Copies permissions to a new object.
#### Authorizable
`permissions`: Must be implemented by your application.
`authorize_for?(key, *args)` Based off the implemented permissions, calls block with self and args for the given key.
### Example
```ruby
require 'permissions'class User
include Permissions::Authorizabledef initialize(permissions)
@permissions = permissions
end
endclass Command
attr_reader :userdef initialize(user)
@user = user
end
endpermissions = Permissions.new
permissions.for(Command) { |user, command| user == command.user }
user = User.new(permissions)
foo = Command.new(user)
bar = Command.new(nil)
user.authorize_for?(Command, foo) # true
user.authorize_for?(Command, bar) # false
permissions.authorize?(Command, user, foo) # true
permissions.authorize?(Command, user, bar) # false
```