{"id":15608935,"url":"https://github.com/serradura/u-authorization","last_synced_at":"2025-04-28T11:50:40.579Z","repository":{"id":59158446,"uuid":"199061332","full_name":"serradura/u-authorization","owner":"serradura","description":"Simple authorization library and role managment for Ruby","archived":false,"fork":false,"pushed_at":"2020-10-18T16:18:50.000Z","size":86,"stargazers_count":6,"open_issues_count":2,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-16T04:21:48.506Z","etag":null,"topics":["authorization","permissions","role-based-authorization","ruby","ruby-gem"],"latest_commit_sha":null,"homepage":"https://rubygems.org/gems/u-authorization","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/serradura.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-07-26T18:12:45.000Z","updated_at":"2020-10-18T16:18:54.000Z","dependencies_parsed_at":"2022-09-13T20:11:32.500Z","dependency_job_id":null,"html_url":"https://github.com/serradura/u-authorization","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serradura%2Fu-authorization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serradura%2Fu-authorization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serradura%2Fu-authorization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serradura%2Fu-authorization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/serradura","download_url":"https://codeload.github.com/serradura/u-authorization/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251310950,"owners_count":21568999,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authorization","permissions","role-based-authorization","ruby","ruby-gem"],"created_at":"2024-10-03T05:40:32.852Z","updated_at":"2025-04-28T11:50:40.541Z","avatar_url":"https://github.com/serradura.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Ruby](https://img.shields.io/badge/ruby-2.2+-ruby.svg?colorA=99004d\u0026colorB=cc0066)\n[![Gem](https://img.shields.io/gem/v/u-authorization.svg?style=flat-square)](https://rubygems.org/gems/u-authorization)\n[![Build Status](https://travis-ci.com/serradura/u-authorization.svg?branch=master)](https://travis-ci.com/serradura/u-authorization)\n[![Maintainability](https://api.codeclimate.com/v1/badges/19251112cf39afdf8bf6/maintainability)](https://codeclimate.com/github/serradura/u-authorization/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/19251112cf39afdf8bf6/test_coverage)](https://codeclimate.com/github/serradura/u-authorization/test_coverage)\n\n# µ-authorization\n\nSimple authorization library and role managment for Ruby.\n\n## Required Ruby version\n\n\u003e \\\u003e= 2.2.0\n\n## Installation\n\nAdd this line to your application's Gemfile:\n```\ngem 'u-authorization'\n```\n\nAnd then execute:\n```\n$ bundle\n```\n\nOr install it yourself as:\n```\n$ gem install u-authorization\n```\n\n## Usage\n\n```ruby\n  require 'ostruct'\n  require 'u-authorization'\n\n  module Permissions\n    ADMIN = {\n      'visit'  =\u003e { 'any' =\u003e true },\n      'export' =\u003e { 'any' =\u003e true }\n    }\n\n    USER = {\n      'visit'  =\u003e { 'except' =\u003e ['billings'] },\n      'export' =\u003e { 'except' =\u003e ['sales'] }\n    }\n\n    ALL = {\n      'admin' =\u003e ADMIN,\n      'user'  =\u003e USER\n    }\n\n    def self.to(role)\n      ALL.fetch(role, 'user')\n    end\n  end\n\n  user = OpenStruct.new(id: 1, role: 'user')\n\n  class SalesPolicy \u003c Micro::Authorization::Policy\n    def edit?(record)\n      user.id == record.user_id\n    end\n  end\n\n  authorization = Micro::Authorization::Model.build(\n    permissions: Permissions.to(user.role),\n    policies: { default: :sales, sales: SalesPolicy },\n    context: {\n      user: user,\n      to_permit: ['dashboard', 'controllers', 'sales', 'index']\n    }\n  )\n\n  # Info about the `context` data:\n  #   1. :to_permit is a required key\n  #     1.1. :permissions is an alternative of :to_permit key.\n  #   2. :user is an optional key\n  #   3. Any key different of :permissions, will be passed as a policy context.\n\n  # Verifying the permissions for the given context\n  authorization.permissions.to?('visit')  #=\u003e true\n  authorization.permissions.to?('export') #=\u003e false\n\n  # Verifying permission for a given feature in different contexts\n  has_permission_to = authorization.permissions.to('export')\n  has_permission_to.context?('billings') #=\u003e true\n  has_permission_to.context?('sales')    #=\u003e false\n\n  charge = OpenStruct.new(id: 2, user_id: user.id)\n\n  # The #to() method fetch and build a policy.\n  authorization.to(:sales).edit?(charge)   #=\u003e true\n\n  # :default is the only permitted key to receive\n  # another symbol as a value (a policy reference).\n  authorization.to(:default).edit?(charge) #=\u003e true\n\n  # #policy() method has a similar behavior of #to(),\n  # but if there is a policy defined as \":default\", it will be fetched and instantiated by default.\n  authorization.policy.edit?(charge)         #=\u003e true\n  authorization.policy(:sales).edit?(charge) #=\u003e true\n\n  # Cloning the authorization changing only its context.\n  new_authorization = authorization.map(context: [\n    'dashboard', 'controllers', 'billings', 'index'\n  ])\n\n  new_authorization.permissions.to?('visit') #=\u003e false\n\n  authorization.equal?(new_authorization) #=\u003e false\n\n  #========================#\n  # Multi role permissions #\n  #========================#\n\n  authorization = Micro::Authorization::Model.build(\n    permissions: [Permissions::USER, Permissions::ADMIN], # An array of permissions\n    policies: { default: :sales, sales: SalesPolicy },\n    context: {\n      user: user,\n      to_permit: ['dashboard', 'controllers', 'sales', 'index']\n    }\n  )\n\n  authorization.permissions.to?('visit')  #=\u003e true\n  authorization.permissions.to?('export') #=\u003e true\n\n  has_permission_to = authorization.permissions.to('export')\n  has_permission_to.context?('billings') #=\u003e true\n  has_permission_to.context?('sales')    #=\u003e true\n```\n\n## Original implementation\n\nhttps://gist.github.com/serradura/7d51b979b90609d8601d0f416a9aa373\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserradura%2Fu-authorization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserradura%2Fu-authorization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserradura%2Fu-authorization/lists"}