role_core 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea0cbcc5a32c18136f5916bf1e58639590304226
4
+ data.tar.gz: a66fd20532ea7c6504b279da9d3d540cf5860f11
5
+ SHA512:
6
+ metadata.gz: f4115f302c5cfb3c96db4c2c0b136c62b99b3cd9e6bf024dc043edab1964ae6325f7c5fb9afcb4c5a03945c100fe70126cafb9a1c89170c1b83b6b58c2c662f4
7
+ data.tar.gz: fe55fc4129c60e27a5bf793ea8cc62ee497de1c50e727b350faa3cec1347d49b68df59609c01625d2bf3ded089e99cf69c3b8e1bab2968ff0e235f99a2253c9c
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,92 @@
1
+ RoleCore
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 'role_core'
16
+ ```
17
+
18
+ Or you may want to include the gem directly from GitHub:
19
+
20
+ ```ruby
21
+ gem 'role_core', github: 'jasl-lab/role_core'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ ```sh
27
+ $ bundle
28
+ ```
29
+
30
+ Copy migrations
31
+
32
+ ```sh
33
+ $ bin/rails role_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/role_core.git
48
+ ```
49
+
50
+ Change directory
51
+
52
+ ```sh
53
+ $ cd role_core
54
+ ```
55
+
56
+ Run bundler
57
+
58
+ ```sh
59
+ $ bundle install
60
+ ```
61
+
62
+ Preparing database
63
+
64
+ ```sh
65
+ $ bin/rails db:migrate
66
+ ```
67
+
68
+ Start the Rails server
69
+
70
+ ```sh
71
+ $ bin/rails s
72
+ ```
73
+
74
+ Open your browser, and visit `http://localhost:3000`
75
+
76
+ ## Contributing
77
+
78
+ Bug report or pull request are welcome.
79
+
80
+ ### Make a pull request
81
+
82
+ 1. Fork it
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
87
+
88
+ Please write unit test with your code if necessary.
89
+
90
+ ## License
91
+
92
+ 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,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require "bundler/setup"
5
+ rescue LoadError
6
+ puts "You must `gem install bundler` and `bundle install` to run rake tasks"
7
+ end
8
+
9
+ require "rdoc/task"
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = "rdoc"
13
+ rdoc.title = "RoleCore"
14
+ rdoc.options << "--line-numbers"
15
+ rdoc.rdoc_files.include("README.md")
16
+ rdoc.rdoc_files.include("lib/**/*.rb")
17
+ end
18
+
19
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
20
+ load "rails/tasks/engine.rake"
21
+
22
+ load "rails/tasks/statistics.rake"
23
+
24
+ require "bundler/gem_tasks"
25
+
26
+ require "rake/testtask"
27
+
28
+ Rake::TestTask.new(:test) do |t|
29
+ t.libs << "test"
30
+ t.pattern = "test/**/*_test.rb"
31
+ t.verbose = false
32
+ end
33
+
34
+ task default: :test
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RoleCore
4
+ class ApplicationRecord < ActiveRecord::Base
5
+ self.abstract_class = true
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RoleCore
4
+ class Role < ApplicationRecord
5
+ include RoleCore::Concerns::Models::Role
6
+
7
+ self.table_name = "roles"
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ class CreateRoles < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :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,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RoleCore
4
+ class ComputedPermissions
5
+ delegate :each, :map, :to_a, :to_ary, to: :@permissions
6
+
7
+ def initialize(permissions = [])
8
+ @permissions = [].concat permissions.to_a
9
+ regroup!
10
+ end
11
+
12
+ def concat(permissions)
13
+ @permissions.concat permissions
14
+ regroup!
15
+
16
+ self
17
+ end
18
+
19
+ def call(context, *args)
20
+ @permissions.each do |permission|
21
+ permission.call(context, *args)
22
+ end
23
+
24
+ self
25
+ end
26
+
27
+ private
28
+
29
+ def regroup!
30
+ @permissions.uniq!
31
+ @permissions.sort_by!(&:priority)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RoleCore::Concerns
4
+ module Models
5
+ module Role
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ validates :name,
10
+ presence: true
11
+
12
+ delegate :permitted_permissions, to: :permissions
13
+
14
+ serialize :permissions, RoleCore.permission_set_class
15
+ end
16
+
17
+ def permissions_attributes=(value)
18
+ permissions.update_attributes value
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RoleCore
4
+ class Engine < ::Rails::Engine
5
+ isolate_namespace RoleCore
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RoleCore
4
+ class Mapper
5
+ def initialize(set, **constraints) #:nodoc:
6
+ @constraints = constraints
7
+ @set = set
8
+ end
9
+
10
+ def permission(name, default: false, **options, &block)
11
+ @set.register_permission name, default, @constraints.merge(options), &block
12
+ self
13
+ end
14
+
15
+ def group(name, **constraints, &block)
16
+ raise ArgumentError, "`name` can't be blank" if name.blank?
17
+ raise ArgumentError, "must provide a block" unless block_given?
18
+
19
+ sub_permission_set_class =
20
+ if @set.nested_classes.has_key?(name)
21
+ @set.nested_classes[name]
22
+ else
23
+ klass = PermissionSet.derive "#{name.to_s.classify}"
24
+ @set.embeds_one(name, anonymous_class: klass)
25
+
26
+ klass
27
+ end
28
+
29
+ sub_permission_set_class.draw(@constraints.merge(constraints), &block)
30
+
31
+ self
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RoleCore
4
+ class Permission
5
+ attr_reader :name, :priority
6
+
7
+ def initialize(name, priority: 0, **options, &block)
8
+ @name = name
9
+ @priority = priority
10
+ end
11
+
12
+ def call(context, *)
13
+ raise NotImplementedError
14
+ end
15
+
16
+ def hash
17
+ instance_values.hash
18
+ end
19
+
20
+ def ==(other)
21
+ unless other.is_a?(RoleCore::Permission)
22
+ return false
23
+ end
24
+
25
+ instance_values == other.instance_values
26
+ end
27
+ alias eql? ==
28
+ end
29
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RoleCore
4
+ class PermissionSet < OptionsModel::Base
5
+ def permitted_permission_names
6
+ attributes.select { |_, v| v }.keys
7
+ end
8
+
9
+ def permitted_permissions(include_nested: true)
10
+ permissions = self.class.registered_permissions.slice(*permitted_permission_names).values
11
+ if include_nested && nested_attributes.any?
12
+ permissions.concat nested_attributes.values.map(&:permitted_permissions).flatten!
13
+ end
14
+
15
+ ComputedPermissions.new(permissions)
16
+ end
17
+
18
+ class << self
19
+ def i18n_scope
20
+ :role_core
21
+ end
22
+
23
+ def permission_class
24
+ @permission_class || RoleCore.permission_class
25
+ end
26
+
27
+ def permission_class=(klass)
28
+ unless klass && klass < Permission
29
+ raise ArgumentError, "#{klass} should be sub-class of #{Permission}."
30
+ end
31
+
32
+ @permission_class = klass
33
+ end
34
+
35
+ def draw(**constraints, &block)
36
+ unless block_given?
37
+ raise ArgumentError, "must provide a block"
38
+ end
39
+
40
+ Mapper.new(self, constraints).instance_exec(&block)
41
+
42
+ self
43
+ end
44
+
45
+ def registered_permissions
46
+ @registered_permissions ||= ActiveSupport::HashWithIndifferentAccess.new
47
+ end
48
+
49
+ def register_permission(name, default = false, **options, &block)
50
+ raise ArgumentError, "`name` can't be blank" if name.blank?
51
+
52
+ attribute name, :boolean, default: default
53
+ registered_permissions[name] = permission_class.new name, options, &block
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RoleCore
4
+ VERSION = "0.0.11"
5
+ end
data/lib/role_core.rb ADDED
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "role_core/engine"
4
+
5
+ require "options_model"
6
+ require "role_core/permission"
7
+ require "role_core/mapper"
8
+ require "role_core/permission_set"
9
+ require "role_core/computed_permissions"
10
+
11
+ require "role_core/concerns/models/role"
12
+
13
+ module RoleCore
14
+ class << self
15
+ def permission_set_class
16
+ @permission_set_class ||= PermissionSet.derive "Global"
17
+ end
18
+
19
+ def permission_set_class=(klass)
20
+ unless klass && klass < PermissionSet
21
+ raise ArgumentError, "#{klass} should be sub-class of #{PermissionSet}."
22
+ end
23
+
24
+ @permission_set_class = klass
25
+ end
26
+
27
+ def permission_class
28
+ @permission_class ||= Permission
29
+ end
30
+
31
+ def permission_class=(klass)
32
+ unless klass && klass < Permission
33
+ raise ArgumentError, "#{klass} should be sub-class of #{Permission}."
34
+ end
35
+
36
+ @permission_class = klass
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # desc "Explaining what the task does"
3
+ # task :role_core do
4
+ # # Task goes here
5
+ # end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: role_core
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.11
5
+ platform: ruby
6
+ authors:
7
+ - jasl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-10 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: '4.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: options_model
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: A Rails engine providing essential industry of Role-based access control
48
+ email:
49
+ - jasl9187@hotmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - MIT-LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - app/models/role_core/application_record.rb
58
+ - app/models/role_core/role.rb
59
+ - db/migrate/20170705174003_create_roles.rb
60
+ - lib/role_core.rb
61
+ - lib/role_core/computed_permissions.rb
62
+ - lib/role_core/concerns/models/role.rb
63
+ - lib/role_core/engine.rb
64
+ - lib/role_core/mapper.rb
65
+ - lib/role_core/permission.rb
66
+ - lib/role_core/permission_set.rb
67
+ - lib/role_core/version.rb
68
+ - lib/tasks/role_core_tasks.rake
69
+ homepage: https://github.com/jasl-lab/role_core
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.6.12
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A Rails engine providing essential industry of Role-based access control
93
+ test_files: []