role_making 0.0.2.next1

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: 8f7d437e91d318c7c0cba0319e7d5423edefddf7
4
+ data.tar.gz: 04b37ad153193005fb9a10a8b7ed995292652f10
5
+ SHA512:
6
+ metadata.gz: ab18b5e6c3bcde0775735f114d54a8e5f2b0d317842e0170cc800ddbb184b0b9548ef7e2b32fc99d044ae92611ec53d0676449cb9d3e86c403bf37c81a5f6b9b
7
+ data.tar.gz: 52a48cf43847b5a2f37b1a031e7e3f49ed47d671b9a71f3333e37cd2fef0d6f406a2c61d4a97b8c5b170fbc9b17ca4dccaec903ab0974f986920042b2193dad4
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in role_making.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 qmliu
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # RoleMaking
2
+ Used for role-based resource management, based on the gem cancancan,
3
+ like the ability.rb,you need define resource in resource.rb
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'role_making'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install role_making
20
+
21
+ ## Usage
22
+
23
+ ```
24
+ rails generate role_making NAME [ROLE_CNAME] [options]
25
+ ```
26
+
27
+ for user ability you can add following in cancancan ability.rb:
28
+ ```ruby
29
+ user ||= User.new
30
+
31
+ if user.has_role?(:admin)
32
+ can :manager,:all
33
+ end
34
+
35
+ Role.all_without_reserved.each do |role|
36
+ next unless user.has_role?(role.name)
37
+ role.role_resources.each do |role_resource|
38
+ resource = Resource.find_by_name(role_resource.resource_name)
39
+ if resource[:behavior]
40
+ block = resource[:behavior]
41
+ can resource[:verb],resource[:object] do |object|
42
+ block.call(user,object)
43
+ end
44
+ else
45
+ can resource[:verb],resource[:object]
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ ```
52
+
53
+ Example:
54
+ ```
55
+ rails generate role_making user
56
+ or
57
+ rails generate role_making user Role
58
+ ```
59
+
60
+ This will create:
61
+
62
+ db/migrate/201506250001_role_making_create_roles_create.rb
63
+ db/migrate/201506250002_role_making_create_role_resources.rb
64
+ app/models/role.rb
65
+ app/models/role_resource.rb
66
+ add "role_making" to user.rb
67
+ ## Contributing
68
+
69
+ 1. Fork it ( https://github.com/javyliu/role_making/fork )
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,40 @@
1
+ Description:
2
+ generate a role migration and set the user
3
+ Usage:
4
+ rails generate role_making NAME [ROLE_CNAME] [options]
5
+
6
+ in cancancan ability.rb you can add following:
7
+ user ||= User.new
8
+
9
+ if user.has_role?(:admin)
10
+ can :manager,:all
11
+ end
12
+
13
+ Role.all_without_reserved.each do |role|
14
+ next unless user.has_role?(role.name)
15
+ role.role_resources.each do |role_resource|
16
+ resource = Resource.find_by_name(role_resource.resource_name)
17
+ if resource[:behavior]
18
+ block = resource[:behavior]
19
+ can resource[:verb],resource[:object] do |object|
20
+ block.call(user,object)
21
+ end
22
+ else
23
+ can resource[:verb],resource[:object]
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ Example:
30
+ rails generate role_making user
31
+
32
+ This will create:
33
+ db/migrate/201506250001_role_making_create_roles_create.rb
34
+ db/migrate/201506250002_role_making_create_role_resources.rb
35
+ app/models/role.rb
36
+ app/models/role_resource.rb
37
+ add "role_making" to user.rb
38
+
39
+
40
+
@@ -0,0 +1,158 @@
1
+ module RoleMaking
2
+ module Generators
3
+ class RoleMakingGenerator < Rails::Generators::NamedBase
4
+
5
+ source_root File.expand_path('../templates', __FILE__)
6
+ argument :role_cname, :type => :string, :default => "Role"
7
+
8
+ namespace :role_making
9
+
10
+ desc "Generates a model with the given NAME and a migration file."
11
+
12
+ def inject_user_class
13
+ model_path = File.join("app", "models", "#{name.underscore}.rb")
14
+
15
+ insert_into_file(model_path,after: "ActiveRecord::Base\n") do
16
+ " role_making role_cname: '#{role_cname.classify}'\n"
17
+ end
18
+
19
+ end
20
+
21
+ def copy_resource_file
22
+ copy_file "resource.rb",File.join("app","models","resource.rb")
23
+ end
24
+
25
+ def create_role_model
26
+ Rails::Generators.invoke "active_record:model",["RoleResource","--no-migration","--no-fixture"], behavior: behavior
27
+ Rails::Generators.invoke "active_record:model",[role_cname,"--no-migration","--no-fixture"], behavior: behavior
28
+ inject_role_class if behavior == :invoke
29
+ inject_role_resource_class if behavior == :invoke
30
+ create_migration
31
+ end
32
+
33
+ #add ability files
34
+ def create_ability_model
35
+ Rails::Generators.invoke "cancan:ability",["-s"],behavior: behavior
36
+ model_path = File.join("app", "models", "ability.rb")
37
+ insert_into_file(model_path, after: " # end\n") do
38
+ <<-EOF
39
+ #{role_cname.classify}.all_without_reserved.each do |role|
40
+ next unless user.has_role?(role.name)
41
+ role.role_resources.each do |res|
42
+ resource = Resource.find_by_name(res.resource_name)
43
+ if block = resource.behavior
44
+ can resource.verb,resource.object do |obj|
45
+ block.call(user,obj)
46
+ end
47
+ elsif resource.hash
48
+ eval_con = resource.hash.delete(:con).try(:inject,{}) do |r,(k,v)|
49
+ r[k] = eval(v)
50
+ r
51
+ end
52
+ can resource[:verb],resource[:object],resource[:hash].merge(eval_con)
53
+ else
54
+ can resource[:verb],resource[:object]
55
+ end
56
+ end
57
+ end \n
58
+ EOF
59
+
60
+
61
+ end if behavior == :invoke
62
+
63
+ end
64
+
65
+
66
+ def show_readme
67
+ if behavior == :invoke
68
+ readme "../USAGE"
69
+ end
70
+ end
71
+
72
+ private
73
+ def inject_role_class
74
+ model_path = File.join("app", "models", "#{role_cname.underscore}.rb")
75
+
76
+ join_table = "#{name.tableize}_#{role_cname.tableize}"
77
+ insert_into_file(model_path,after: "ActiveRecord::Base\n") do
78
+ <<-EOF
79
+ RESERVED = [:admin,:guest]
80
+ has_and_belongs_to_many :#{name}, join_table: :#{join_table}
81
+ has_many :role_resources, dependent: :destroy
82
+
83
+ def self.all_with_reserved
84
+ self.all.where("name not in ?",RESERVED)
85
+ end
86
+ EOF
87
+ end
88
+
89
+ end
90
+
91
+
92
+ def inject_role_resource_class
93
+ model_path = File.join("app", "models", "role_resource.rb")
94
+
95
+ insert_into_file(model_path,after: "ActiveRecord::Base\n") do
96
+ <<-EOF
97
+ validates_uniqueness_of :resource_name, scope: :#{role_cname.underscore}_id
98
+
99
+ belongs_to :#{role_cname.underscore}
100
+ EOF
101
+ end
102
+ end
103
+
104
+
105
+ def create_migration
106
+ role_table = role_cname.tableize
107
+ join_table = "#{name.tableize}_#{role_table}"
108
+ user_reference = name.underscore
109
+ role_reference = role_cname.underscore
110
+ role_file = "db/migrate/201506250001_role_making_create_#{role_table}.rb"
111
+ role_resource_file = "db/migrate/201506250002_role_making_create_role_resources.rb"
112
+
113
+ create_file role_file do
114
+ <<-EOF
115
+ class RoleMakingCreate#{role_table.camelize} < ActiveRecord::Migration
116
+ def change
117
+ create_table(:#{role_table}) do |t|
118
+ t.string :name
119
+ t.string :display_name
120
+
121
+ t.timestamps
122
+ end
123
+
124
+ create_table(:#{join_table}, :id => false) do |t|
125
+ t.references :#{user_reference}
126
+ t.references :#{role_reference}
127
+ end
128
+
129
+ add_index(:#{role_table}, :name)
130
+ add_index(:#{join_table}, [ :#{user_reference}_id, :#{role_reference}_id ])
131
+ end
132
+ end
133
+ EOF
134
+
135
+ end
136
+ create_file role_resource_file do
137
+ <<-EOF
138
+ class RoleMakingCreateRoleResources < ActiveRecord::Migration
139
+ def change
140
+ create_table(:role_resources) do |t|
141
+ t.references :#{role_reference}
142
+ t.string :resource_name
143
+
144
+ t.timestamps
145
+ end
146
+
147
+ add_index(:role_resources, :#{role_reference}_id)
148
+ end
149
+ end
150
+ EOF
151
+ end
152
+ end
153
+
154
+
155
+
156
+ end
157
+ end
158
+ end
@@ -0,0 +1,18 @@
1
+ class RoleMakingCreateRoles < ActiveRecord::Migration
2
+ def change
3
+ create_table(:roles) do |t|
4
+ t.string :name
5
+ t.string :display_name
6
+
7
+ t.timestamps
8
+ end
9
+
10
+ create_table(:users_roles, :id => false) do |t|
11
+ t.integer :user_id
12
+ t.integer :role_id
13
+ end
14
+
15
+ add_index(:roles, :name)
16
+ add_index(:users_roles, [ :user_id, :role_id ])
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ class Resource
2
+ include RoleMaking::Resourcing
3
+
4
+ #group :user do
5
+ # resource [:read,:update,:destroy], User do |admin,user|
6
+ # admin != user
7
+ # end
8
+
9
+ # resource :create,User
10
+
11
+ #end
12
+
13
+ #group :post do
14
+ # resource :read,Post
15
+ # resource [:update,:destroy,:create],Post
16
+ #end
17
+
18
+ end
@@ -0,0 +1,12 @@
1
+ require 'role_making'
2
+ require 'rails'
3
+
4
+ module RoleMaking
5
+ class Railtie < ::Rails::Railtie
6
+ initializer 'rolify.initialize' do
7
+ ActiveSupport.on_load(:active_record) do
8
+ ActiveRecord::Base.send :extend, RoleMaking
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,60 @@
1
+ module RoleMaking
2
+ module Resourcing
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ @groups = []
7
+ @current_group = nil
8
+ @resources = []
9
+ Res = Struct.new(:name,:group,:verb,:hash,:object,:behavior)
10
+ end
11
+
12
+ module ClassMethods
13
+ attr_reader :resources,:groups
14
+ def group(name,&block)
15
+ @groups << name
16
+ @groups.uniq!
17
+ @current_group = name
18
+ block.call
19
+ end
20
+
21
+ def resource(verb_or_verbs,object,hash=nil,&block)
22
+ raise "Need define group first" if @current_group.nil?
23
+ group = @current_group
24
+ behavior = block
25
+ Array.wrap(verb_or_verbs).each do |verb|
26
+ add_resource(group,verb,object,hash,behavior)
27
+ end
28
+ end
29
+
30
+ def add_resource(group,verb,object,hash,behavior)
31
+ name = "#{verb}_#{object.to_s.underscore}"
32
+ resource = Res.new(name,group,verb,hash,object,behavior)
33
+ @resources << resource
34
+ end
35
+
36
+ def each_group(&block)
37
+ @groups.each do |group|
38
+ block.call(group)
39
+ end
40
+ end
41
+
42
+ def each_resource(&block)
43
+ @resources.group_by(&:group).each(&block)
44
+ end
45
+
46
+ def each_resources_by(group,&block)
47
+ @resources.find_all{|r| r.group == group}.each(&block)
48
+ end
49
+
50
+ def find_by_name(name)
51
+ resource = @resources.detect { |e| e.name == name.to_s }
52
+ raise "not found resources by name: #{name}" unless resource
53
+ resource
54
+
55
+ end
56
+ end
57
+
58
+
59
+ end
60
+ end
@@ -0,0 +1,46 @@
1
+ require 'role_making/resourcing'
2
+ module RoleMaking
3
+ module Role
4
+
5
+ def add_role(role_name)
6
+ role = self.roles.where(name: role_name).first
7
+ unless role
8
+ role = self.class.role_cname.constantize.where(name: role_name.to_s).first_or_initialize
9
+ self.roles << role
10
+ end
11
+ role
12
+ end
13
+ alias_method :grant, :add_role
14
+
15
+ def has_role?(role_name)
16
+ role_array = if new_record?
17
+ self.roles.detect { |r| r.name.to_s == role_name.to_s }
18
+ else
19
+ self.roles.exists?( name: role_name)
20
+ end
21
+ !!role_array
22
+ end
23
+
24
+ def has_all_roles?(*args)
25
+ args.all? do |item|
26
+ self.has_role?(item)
27
+ end
28
+ end
29
+
30
+ def has_any_role?(*args)
31
+ args.any? { |r| self.has_role?(r) }
32
+ end
33
+
34
+
35
+ def remove_role(roles_name)
36
+ self.roles.delete(self.class.role_cname.constantize.where(name: roles_name))
37
+ end
38
+
39
+ alias_method :revoke, :remove_role
40
+
41
+ def roles_name
42
+ self.roles.select(:name).map { |r| r.name }
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ module RoleMaking
2
+ VERSION = "0.0.2.next1"
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'role_making/railtie' if defined?(Rails)
2
+ require 'role_making/role'
3
+
4
+ module RoleMaking
5
+
6
+ attr_accessor :role_cname, :role_join_table_name, :role_table_name
7
+
8
+ def role_making(options = {})
9
+ include Role
10
+ #extend Dynamic if RoleMaking.dynamic_shortcuts
11
+
12
+ options.reverse_merge!({:role_cname => 'Role'})
13
+ self.role_cname = options[:role_cname]
14
+ self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")
15
+
16
+ default_join_table = "#{self.to_s.tableize.gsub(/\//, "_")}_#{self.role_table_name}"
17
+ options.reverse_merge!({:role_join_table_name => default_join_table})
18
+ self.role_join_table_name = options[:role_join_table_name]
19
+
20
+ rolify_options = { :class_name => options[:role_cname].camelize }
21
+ rolify_options.merge!({ :join_table => self.role_join_table_name })
22
+ rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove ].include? k.to_sym })
23
+
24
+ has_and_belongs_to_many :roles, rolify_options
25
+
26
+ #load_dynamic_methods if RoleMaking.dynamic_shortcuts
27
+ end
28
+
29
+
30
+
31
+
32
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'role_making/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "role_making"
8
+ spec.version = RoleMaking::VERSION
9
+ spec.authors = ["javy_liu"]
10
+ spec.email = ["javy_liu@163.com"]
11
+ spec.summary = %q{for custom defined role to manage the resources}
12
+ spec.description = %q{A gem for resource manage based on role}
13
+ spec.homepage = "https://github.com/javyliu/role_making"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'cancancan', '~> 1.10','>= 1.10.1'
22
+ spec.add_development_dependency "bundler", "~> 1.7"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: role_making
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2.next1
5
+ platform: ruby
6
+ authors:
7
+ - javy_liu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cancancan
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.10.1
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.10'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.10.1
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.7'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1.7'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ description: A gem for resource manage based on role
62
+ email:
63
+ - javy_liu@163.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - ".gitignore"
69
+ - Gemfile
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - lib/generators/USAGE
74
+ - lib/generators/role_making_generator.rb
75
+ - lib/generators/templates/migrate.rb
76
+ - lib/generators/templates/resource.rb
77
+ - lib/role_making.rb
78
+ - lib/role_making/railtie.rb
79
+ - lib/role_making/resourcing.rb
80
+ - lib/role_making/role.rb
81
+ - lib/role_making/version.rb
82
+ - role_making.gemspec
83
+ homepage: https://github.com/javyliu/role_making
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">"
99
+ - !ruby/object:Gem::Version
100
+ version: 1.3.1
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.4.6
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: for custom defined role to manage the resources
107
+ test_files: []
108
+ has_rdoc: