rbac_core 0.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
+ SHA1:
3
+ metadata.gz: 161e743a58e60da6c8d98560491955bf4ab1fafa
4
+ data.tar.gz: 4daa1438a6b8cbeb205fddc51bbd3542be49341d
5
+ SHA512:
6
+ metadata.gz: 9580bdcdab91ddddfa54bb06d9d5d55b890d34247ffe2ac06daec009f58012cf656d24e6d68f096241c5c552063599007b12100d42f78c9ac9e2958825315016
7
+ data.tar.gz: 600aef20185cea548453aee2046426ed50937098f5ba090148c09c4ea4b2717e08369209eddb091ba6cf4309547b2072f5395c6f97c3c3acae5448c679af910d
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Jun Jiang
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ RBAC Core
2
+ ====
3
+
4
+ A Rails engine providing essential industry of Role-based access control.
5
+
6
+ ## Usage
7
+
8
+ See demo for now.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'rbac_core'
16
+ ```
17
+
18
+ Or you may want to include the gem directly from GitHub:
19
+
20
+ ```ruby
21
+ gem 'rbac_core', github: 'jasl-lab/rbac_core'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ ```sh
27
+ $ bundle
28
+ ```
29
+
30
+ Copy migrations
31
+
32
+ ```sh
33
+ $ bin/rails rbac_core:install:migrations
34
+ ```
35
+
36
+ Then do migrate
37
+
38
+ ```sh
39
+ $ bin/rails db:migrate
40
+ ```
41
+
42
+ ## Demo
43
+
44
+ Clone the repository.
45
+
46
+ ```sh
47
+ $ git clone https://github.com/jasl/rbac_core.git
48
+ ```
49
+
50
+ Change directory
51
+
52
+ ```sh
53
+ $ cd rbac_core
54
+ ```
55
+
56
+ Run bundler
57
+
58
+ ```sh
59
+ $ bundle install
60
+ ```
61
+
62
+ Run yarn
63
+
64
+ ```sh
65
+ $ test/dummy/bin/yarn
66
+ ```
67
+
68
+ Preparing database
69
+
70
+ ```sh
71
+ $ bin/rails db:migrate
72
+ ```
73
+
74
+ Start the Rails server
75
+
76
+ ```sh
77
+ $ bin/rails s
78
+ ```
79
+
80
+ Open your browser, and visit `http://localhost:3000`
81
+
82
+ ## Contributing
83
+
84
+ Bug report or pull request are welcome.
85
+
86
+ ### Make a pull request
87
+
88
+ 1. Fork it
89
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 4. Push to the branch (`git push origin my-new-feature`)
92
+ 5. Create new Pull Request
93
+
94
+ Please write unit test with your code if necessary.
95
+
96
+ ## License
97
+
98
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RbacCore'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'test'
31
+ t.pattern = 'test/**/*_test.rb'
32
+ t.verbose = false
33
+ end
34
+
35
+
36
+ task default: :test
@@ -0,0 +1,5 @@
1
+ module RbacCore
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module RbacCore
2
+ class Role < ApplicationRecord
3
+ include RbacCore::Concerns::Models::Role
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ class CreateRbacCoreRoles < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :rbac_core_roles do |t|
4
+ t.string :name, null: false
5
+ t.text :permissions, null: false, default: ""
6
+
7
+ t.string :type, null: false
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ module RbacCore::Concerns
2
+ module Models
3
+ module Role
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ validates :name,
8
+ presence: true
9
+
10
+ delegate :permitted_permissions, to: :permissions
11
+
12
+ after_initialize do
13
+ self.permissions ||= ::RbacCore.permission_set_class
14
+ end
15
+
16
+ serialize :permissions, RbacCore.permission_set_class
17
+ end
18
+
19
+ def permissions_attributes=(value)
20
+ permissions.update_attributes value
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,5 @@
1
+ module RbacCore
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace RbacCore
4
+ end
5
+ end
@@ -0,0 +1,32 @@
1
+ module RbacCore
2
+ class Mapper
3
+ def initialize(set, **constraints) #:nodoc:
4
+ @constraints = constraints
5
+ @set = set
6
+ end
7
+
8
+ def permission(name, default = false, **options, &block)
9
+ @set.register_permission name, default, @constraints.merge(options), &block
10
+ self
11
+ end
12
+
13
+ def group(name, **constraints, &block)
14
+ raise ArgumentError, "`name` can't be blank" if name.blank?
15
+ raise ArgumentError, "must provide a block" unless block_given?
16
+
17
+ sub_permission_set_class =
18
+ if @set.nested_classes.has_key?(name)
19
+ @set.nested_classes[name]
20
+ else
21
+ klass = PermissionSet.derive "#{name.to_s.classify}"
22
+ @set.embeds_one(name, anonymous_class: klass)
23
+
24
+ klass
25
+ end
26
+
27
+ sub_permission_set_class.draw(@constraints.merge(constraints), &block)
28
+
29
+ self
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,27 @@
1
+ module RbacCore
2
+ class PermissionAdapter
3
+ attr_reader :action, :priority
4
+
5
+ def initialize(action, priority: 0, **options, &block)
6
+ @action = action
7
+ @priority = priority
8
+ end
9
+
10
+ def call(context, *)
11
+ raise NotImplementedError
12
+ end
13
+
14
+ def hash
15
+ instance_values.hash
16
+ end
17
+
18
+ def ==(other)
19
+ unless other.is_a?(RbacCore::PermissionAdapter)
20
+ return false
21
+ end
22
+
23
+ instance_values == other.instance_values
24
+ end
25
+ alias eql? ==
26
+ end
27
+ end
@@ -0,0 +1,43 @@
1
+ module RbacCore
2
+ class PermissionSet < OptionsModel::Base
3
+ def permitted_permission_names
4
+ attributes.select { |_, v| v }.keys
5
+ end
6
+
7
+ def permitted_permissions(include_nested: true)
8
+ permissions = self.class.registered_permissions.slice(*permitted_permission_names).values
9
+ if include_nested && nested_attributes.any?
10
+ permissions + nested_attributes.values.map(&:permitted_permissions).flatten!
11
+ else
12
+ permissions
13
+ end.sort_by(&:priority)
14
+ end
15
+
16
+ class << self
17
+ def i18n_scope
18
+ :rbac_core
19
+ end
20
+
21
+ def draw(**constraints, &block)
22
+ unless block_given?
23
+ raise ArgumentError, "must provide a block"
24
+ end
25
+
26
+ Mapper.new(self, constraints).instance_exec(&block)
27
+
28
+ self
29
+ end
30
+
31
+ def registered_permissions
32
+ @registered_permissions ||= ActiveSupport::HashWithIndifferentAccess.new
33
+ end
34
+
35
+ def register_permission(name, default = false, **options, &block)
36
+ raise ArgumentError, "`name` can't be blank" if name.blank?
37
+
38
+ attribute name, :boolean, default: default
39
+ registered_permissions[name] = RbacCore.permission_adapter_class.new name, options, &block
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module RbacCore
2
+ VERSION = "0.0.1".freeze
3
+ end
data/lib/rbac_core.rb ADDED
@@ -0,0 +1,28 @@
1
+ require "rbac_core/engine"
2
+
3
+ require "options_model"
4
+ require "rbac_core/permission_adapter"
5
+ require "rbac_core/mapper"
6
+ require "rbac_core/permission_set"
7
+
8
+ require "rbac_core/concerns/models/role"
9
+
10
+ module RbacCore
11
+ class << self
12
+ def permission_set_class
13
+ @permission_set_class ||= PermissionSet.derive "Global"
14
+ end
15
+
16
+ def permission_adapter_class
17
+ @permission_adapter_class ||= PermissionAdapter
18
+ end
19
+
20
+ def permission_adapter_class=(klass)
21
+ unless klass && klass < PermissionAdapter
22
+ raise ArgumentError, "#{klass} should be sub-class of #{PermissionAdapter}."
23
+ end
24
+
25
+ @permission_adapter_class = klass
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rbac_core do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbac_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jasl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-07-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: options_model
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A Rails engine providing essential industry of Role-based access control
42
+ email:
43
+ - jasl9187@hotmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - MIT-LICENSE
49
+ - README.md
50
+ - Rakefile
51
+ - app/models/rbac_core/application_record.rb
52
+ - app/models/rbac_core/role.rb
53
+ - db/migrate/20170705174003_create_rbac_core_roles.rb
54
+ - lib/rbac_core.rb
55
+ - lib/rbac_core/concerns/models/role.rb
56
+ - lib/rbac_core/engine.rb
57
+ - lib/rbac_core/mapper.rb
58
+ - lib/rbac_core/permission_adapter.rb
59
+ - lib/rbac_core/permission_set.rb
60
+ - lib/rbac_core/version.rb
61
+ - lib/tasks/rbac_core_tasks.rake
62
+ homepage: https://github.com/jasl-lab/rbac_core
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.6.12
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: A Rails engine providing essential industry of Role-based access control
86
+ test_files: []