µ-authorization
Simple authorization library and role managment for Ruby.
Prerequisites
Ruby >= 2.2.0
Installation
Add this line to your application's Gemfile:
gem 'u-authorization'
And then execute:
$ bundle
Or install it yourself as:
$ gem install u-
Usage
require 'ostruct'
require 'authorization'
role = OpenStruct.new(
name: 'user',
permissions: {
'visit' => { 'except' => ['billings'] },
'edit_users' => false, # Same as: 'edit_users' => { 'any' => false },
'export_as_csv' => { 'except' => ['sales'] }
}
)
user = OpenStruct.new(id: 1, role: role)
class SalesPolicy < Micro::Authorization::Policy
def edit?(record)
user.id == record.user_id
end
end
= Micro::Authorization::Model.build(
permissions: user.role.,
policies: { default: :sales, sales: SalesPolicy }
context: {
user: user,
permissions: ['dashboard', 'controllers', 'sales', 'index']
}
)
# Verifying the permissions for the given context
..to?('visit') #=> true
..to?('export_as_csv') #=> false
# Verifying permission for a given feature in different contexts
= ..to('export_as_csv')
.context?('billings') #=> true
.context?('sales') #=> false
charge = OpenStruct.new(id: 2, user_id: user.id)
# The #to() method fetch and build a policy.
.to(:sales).edit?(charge) #=> true
# :default is the only permitted key to receive
# another symbol as value (a policy reference).
.to(:default).edit?(charge) #=> true
# #policy() method has a similar behavior of #to(),
# but if there is a policy named as ":default", it will be fetched and instantiated by default.
.policy.edit?(charge) #=> true
.policy(:sales).edit?(charge) #=> true
# Cloning the authorization changing only its context.
= .map(context: [
'dashboard', 'controllers', 'billings', 'index'
])
..to?('visit') #=> false
== #=> false
Original implementation
https://gist.github.com/serradura/7d51b979b90609d8601d0f416a9aa373