Git Product home page Git Product logo

u-authorization's Introduction

Ruby Gem Build Status Maintainability Test Coverage

µ-authorization

Simple authorization library and role managment for Ruby.

Required Ruby version

>= 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-authorization

Usage

  require 'ostruct'
  require 'u-authorization'

  module Permissions
    ADMIN = {
      'visit'  => { 'any' => true },
      'export' => { 'any' => true }
    }

    USER = {
      'visit'  => { 'except' => ['billings'] },
      'export' => { 'except' => ['sales'] }
    }

    ALL = {
      'admin' => ADMIN,
      'user'  => USER
    }

    def self.to(role)
      ALL.fetch(role, 'user')
    end
  end

  user = OpenStruct.new(id: 1, role: 'user')

  class SalesPolicy < Micro::Authorization::Policy
    def edit?(record)
      user.id == record.user_id
    end
  end

  authorization = Micro::Authorization::Model.build(
    permissions: Permissions.to(user.role),
    policies: { default: :sales, sales: SalesPolicy },
    context: {
      user: user,
      to_permit: ['dashboard', 'controllers', 'sales', 'index']
    }
  )

  # Info about the `context` data:
  #   1. :to_permit is a required key
  #     1.1. :permissions is an alternative of :to_permit key.
  #   2. :user is an optional key
  #   3. Any key different of :permissions, will be passed as a policy context.

  # Verifying the permissions for the given context
  authorization.permissions.to?('visit')  #=> true
  authorization.permissions.to?('export') #=> false

  # Verifying permission for a given feature in different contexts
  has_permission_to = authorization.permissions.to('export')
  has_permission_to.context?('billings') #=> true
  has_permission_to.context?('sales')    #=> false

  charge = OpenStruct.new(id: 2, user_id: user.id)

  # The #to() method fetch and build a policy.
  authorization.to(:sales).edit?(charge)   #=> true

  # :default is the only permitted key to receive
  # another symbol as a value (a policy reference).
  authorization.to(:default).edit?(charge) #=> true

  # #policy() method has a similar behavior of #to(),
  # but if there is a policy defined as ":default", it will be fetched and instantiated by default.
  authorization.policy.edit?(charge)         #=> true
  authorization.policy(:sales).edit?(charge) #=> true

  # Cloning the authorization changing only its context.
  new_authorization = authorization.map(context: [
    'dashboard', 'controllers', 'billings', 'index'
  ])

  new_authorization.permissions.to?('visit') #=> false

  authorization.equal?(new_authorization) #=> false

  #========================#
  # Multi role permissions #
  #========================#

  authorization = Micro::Authorization::Model.build(
    permissions: [Permissions::USER, Permissions::ADMIN], # An array of permissions
    policies: { default: :sales, sales: SalesPolicy },
    context: {
      user: user,
      to_permit: ['dashboard', 'controllers', 'sales', 'index']
    }
  )

  authorization.permissions.to?('visit')  #=> true
  authorization.permissions.to?('export') #=> true

  has_permission_to = authorization.permissions.to('export')
  has_permission_to.context?('billings') #=> true
  has_permission_to.context?('sales')    #=> true

Original implementation

https://gist.github.com/serradura/7d51b979b90609d8601d0f416a9aa373

u-authorization's People

Contributors

serradura avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.