rbac-ruby 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b9600e5634e01bd76377e231fc3854ed30695b284fbf5cc7dc408b502179797d
4
+ data.tar.gz: df125b25b7deb799119bd9a5fcca3492939fd0504411381936975a2342933629
5
+ SHA512:
6
+ metadata.gz: 7bd4b6562cc69b1a7031f8bb358cdaf05f2678c43fc0daada334f89601c6daa313243f2b8ad5056b6c87abf11a9ec6b81129e006e4b08f9554681153fdeabf7d
7
+ data.tar.gz: 1756b4083d7b9d8cef60a886fd946d790554b35805900dcabdfba61fb47ac902f37a626b8347784b64a815844ae4e63b5776091d306c4e6905fe95b3a5814cb9
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-07-01
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ahrechushkin@ibagroup.eu. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in rbac.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
13
+
14
+ gem "yaml", "~> 0.2.0"
15
+
16
+ gem "activesupport", "~> 7.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,71 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rbac (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ activesupport (7.0.3)
10
+ concurrent-ruby (~> 1.0, >= 1.0.2)
11
+ i18n (>= 1.6, < 2)
12
+ minitest (>= 5.1)
13
+ tzinfo (~> 2.0)
14
+ ast (2.4.2)
15
+ concurrent-ruby (1.1.10)
16
+ diff-lcs (1.5.0)
17
+ i18n (1.10.0)
18
+ concurrent-ruby (~> 1.0)
19
+ json (2.6.2)
20
+ minitest (5.16.2)
21
+ parallel (1.22.1)
22
+ parser (3.1.2.0)
23
+ ast (~> 2.4.1)
24
+ rainbow (3.1.1)
25
+ rake (13.0.6)
26
+ regexp_parser (2.5.0)
27
+ rexml (3.2.5)
28
+ rspec (3.11.0)
29
+ rspec-core (~> 3.11.0)
30
+ rspec-expectations (~> 3.11.0)
31
+ rspec-mocks (~> 3.11.0)
32
+ rspec-core (3.11.0)
33
+ rspec-support (~> 3.11.0)
34
+ rspec-expectations (3.11.0)
35
+ diff-lcs (>= 1.2.0, < 2.0)
36
+ rspec-support (~> 3.11.0)
37
+ rspec-mocks (3.11.1)
38
+ diff-lcs (>= 1.2.0, < 2.0)
39
+ rspec-support (~> 3.11.0)
40
+ rspec-support (3.11.0)
41
+ rubocop (1.31.2)
42
+ json (~> 2.3)
43
+ parallel (~> 1.10)
44
+ parser (>= 3.1.0.0)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8, < 3.0)
47
+ rexml (>= 3.2.5, < 4.0)
48
+ rubocop-ast (>= 1.18.0, < 2.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 3.0)
51
+ rubocop-ast (1.18.0)
52
+ parser (>= 3.1.1.0)
53
+ ruby-progressbar (1.11.0)
54
+ tzinfo (2.0.4)
55
+ concurrent-ruby (~> 1.0)
56
+ unicode-display_width (2.2.0)
57
+ yaml (0.2.0)
58
+
59
+ PLATFORMS
60
+ x86_64-linux
61
+
62
+ DEPENDENCIES
63
+ activesupport (~> 7.0)
64
+ rake (~> 13.0)
65
+ rbac!
66
+ rspec (~> 3.0)
67
+ rubocop (~> 1.21)
68
+ yaml (~> 0.2.0)
69
+
70
+ BUNDLED WITH
71
+ 2.2.33
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # RBAC Ruby
2
+
3
+ A simple way to manage access and visibillity scopes to objects in your Rails applications.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rbac'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rbac
20
+
21
+ ## Requirements
22
+ Your application should contain model User with methods `current_user` which will return attributes to filtering your objects.
23
+
24
+
25
+ ## Usage
26
+
27
+ Write config file with buissiness logic (see examples). Include Rbac modules into your application. And have fun :)
28
+
29
+ EXAMPLE:
30
+
31
+ Models:
32
+ - HelpdeskSystem
33
+ - Project
34
+ - Request
35
+ - User (can be abstract model)
36
+
37
+ Relations:
38
+ - HelpdeskSystem 1-M Project
39
+ - Project 1-M Request
40
+
41
+ Roles:
42
+ - admin (can see and modify each object in application)
43
+ - supported (can see and modify each object in region)
44
+ - guest (can see and modify own request into projects which available in region)
45
+
46
+
47
+
48
+ ```ruby
49
+ # For example class User.
50
+ # Cause User.current_user IS REQUIRED for this gem
51
+ class User
52
+ attr_reader :userid, :region, :role
53
+ thread_mattr_accessor :current_user
54
+
55
+ def initialize(opts)
56
+ @userid = opts[:userid]
57
+ @region = opts[:region]
58
+ @role = opts[:role]
59
+ end
60
+ end
61
+ ```
62
+
63
+ Define a [RBAC Routes file](https://github.com/ahrechushkin/rbac-ruby/blob/master/rbac_routes.example.yml) which configure access to controllers for autheticated users and include to your controllers and write custom buissiness logic.
64
+
65
+ Define a [RBAC Scopes file](https://github.com/ahrechushkin/rbac-ruby/blob/master/rbac_scopes.example.yml) which containe logic how our application will filter records and include Rbac::Filterer into app/models/application.rb (to include filtering method to all models) OR into specific model to use filtation only here. Now you can call Model.filtered (e.g. HelpdeskSystem.filtered, Project.filtered, Request.filtered)
66
+
67
+
68
+
69
+
70
+
71
+
72
+
73
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "rbac"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module Rbac
6
+ # Authorizer class is responsible for checking
7
+ # if user has access to perform some action.
8
+ class Authorizer
9
+ attr_reader :config
10
+
11
+ # Load configuration from a YAML file.
12
+ def initialize(config_file)
13
+ @config = YAML.load_file(config_file)
14
+ end
15
+
16
+ def role_allows?(request)
17
+ controller, action = fetch_params_from_request(request)
18
+ config["features"][controller][action].include?(User.current_user.role)
19
+ end
20
+
21
+ private
22
+
23
+ def fetch_params_from_request(request)
24
+ controller = request.params[:controller]
25
+ action = request.params[:action]
26
+ [controller, action]
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,34 @@
1
+ module Rbac::Filterer
2
+ require "active_support/concern"
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ def self.filtered
7
+ require 'yaml'
8
+
9
+ scopes_map = YAML.load File.open ENV['SCOPES_MAP_FILE']
10
+
11
+ filters_config = scopes_map[self.name][User.current_user.role]
12
+
13
+ filters = filters_config['filters']
14
+ scope = filters_config['scope']
15
+ parent = filters_config['parent']
16
+
17
+ # TODO: refactor it and remove complexity of this code
18
+ if parent
19
+ if filters
20
+ additional_filters = filters.map { |field_name, attribute| { field_name => User.current_user.send(attribute) } }.reduce Hash.new, :merge
21
+ where(additional_filters.merge(parent.downcase.to_sym => parent.constantize.filtered))
22
+ else
23
+ where(parent.downcase.to_sym => parent.constantize.filtered)
24
+ end
25
+ elsif filters
26
+ where(filters.map { |field_name, attribute| { field_name => User.current_user.send(attribute) } }.reduce Hash.new, :merge)
27
+ elsif scope
28
+ self.send(scope)
29
+ else
30
+ []
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rbac
4
+ VERSION = "1.0.1"
5
+ end
data/lib/rbac.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "rbac/version"
4
+ require_relative "rbac/authorizer"
5
+ require_relative "rbac/filterer"
6
+
7
+ module Rbac
8
+ class Error < StandardError; end
9
+ # Your code goes here...
10
+ end
data/rbac.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rbac/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rbac-ruby"
7
+ spec.version = Rbac::VERSION
8
+ spec.authors = ["Aliaksei Hrechushkin"]
9
+ spec.email = ["ahrechushkin@ibagroup.eu"]
10
+
11
+ spec.summary = "Role-based access control gem."
12
+ spec.description = "Use dynamicly configurable RBAC system to control access to your application."
13
+ spec.required_ruby_version = ">= 2.6.0"
14
+ spec.homepage = "https://icdc.io"
15
+ spec.licenses = ["Apache-2.0"]
16
+ spec.metadata["source_code_uri"] = "https://github.com/icdc-io/rbac-ruby"
17
+ spec.metadata["changelog_uri"] = "https://github.com/icdc-io/rbac-ruby/blob/master/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject do |f|
23
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
24
+ end
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ # Uncomment to register a new dependency of your gem
31
+ # spec.add_dependency "example-gem", "~> 1.0"
32
+
33
+ # For more information and examples about making a new gem, checkout our
34
+ # guide at: https://bundler.io/guides/creating_gem.html
35
+ end
@@ -0,0 +1,57 @@
1
+ ---
2
+ features:
3
+ api/v1/helpdesk_systems:
4
+ index:
5
+ - admin
6
+ - supporter
7
+ - guest
8
+ show:
9
+ - admin
10
+ - supporter
11
+ - guest
12
+ create:
13
+ - admin
14
+ destroy:
15
+ - admin
16
+ update:
17
+ - admin
18
+ - supporter
19
+ api/v1/projects:
20
+ index:
21
+ - admin
22
+ - supporter
23
+ - guest
24
+ show:
25
+ - admin
26
+ - supporter
27
+ - guest
28
+ create:
29
+ - admin
30
+ - supporter
31
+ update:
32
+ - admin
33
+ - supporter
34
+ destroy:
35
+ - admin
36
+ - supporter
37
+ api/v1/requests:
38
+ index:
39
+ - admin
40
+ - supporter
41
+ - guest
42
+ show:
43
+ - admin
44
+ - supporter
45
+ - guest
46
+ create:
47
+ - admin
48
+ - supporter
49
+ - guest
50
+ update:
51
+ - admin
52
+ - supporter
53
+ - guest
54
+ destroy:
55
+ - admin
56
+ - supporter
57
+ - guest
@@ -0,0 +1,29 @@
1
+ ---
2
+ HelpdeskSystem:
3
+ admin:
4
+ scope:
5
+ all
6
+ supporter:
7
+ filters:
8
+ region: region
9
+ guest:
10
+ filters:
11
+ region: region
12
+ Project:
13
+ admin:
14
+ scope:
15
+ all
16
+ supporter:
17
+ parent: HelpdeskSystem
18
+ guest:
19
+ parent: HelpdeskSystem
20
+ Request:
21
+ admin:
22
+ scope:
23
+ all
24
+ supporter:
25
+ parent: Project
26
+ guest:
27
+ parent: Project
28
+ filters:
29
+ requester: userid
data/sig/rbac.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module Rbac
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbac-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Aliaksei Hrechushkin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-08-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Use dynamicly configurable RBAC system to control access to your application.
14
+ email:
15
+ - ahrechushkin@ibagroup.eu
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - README.md
27
+ - Rakefile
28
+ - bin/console
29
+ - bin/setup
30
+ - lib/rbac.rb
31
+ - lib/rbac/authorizer.rb
32
+ - lib/rbac/filterer.rb
33
+ - lib/rbac/version.rb
34
+ - rbac.gemspec
35
+ - rbac_routes.example.yml
36
+ - rbac_scopes.example.yml
37
+ - sig/rbac.rbs
38
+ homepage: https://icdc.io
39
+ licenses:
40
+ - Apache-2.0
41
+ metadata:
42
+ source_code_uri: https://github.com/icdc-io/rbac-ruby
43
+ changelog_uri: https://github.com/icdc-io/rbac-ruby/blob/master/CHANGELOG.md
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.6.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.2.3
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Role-based access control gem.
63
+ test_files: []