role_making 0.0.2.next1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8f7d437e91d318c7c0cba0319e7d5423edefddf7
4
- data.tar.gz: 04b37ad153193005fb9a10a8b7ed995292652f10
2
+ SHA256:
3
+ metadata.gz: d023f18da13dc8704eb3b5824648222f37557cb7ce2621a8e5d78c5197b3fecf
4
+ data.tar.gz: d87dc8b1348d8d4ff34312e292d4658346ee254c441a9b6b1966a0e294e6d8af
5
5
  SHA512:
6
- metadata.gz: ab18b5e6c3bcde0775735f114d54a8e5f2b0d317842e0170cc800ddbb184b0b9548ef7e2b32fc99d044ae92611ec53d0676449cb9d3e86c403bf37c81a5f6b9b
7
- data.tar.gz: 52a48cf43847b5a2f37b1a031e7e3f49ed47d671b9a71f3333e37cd2fef0d6f406a2c61d4a97b8c5b170fbc9b17ca4dccaec903ab0974f986920042b2193dad4
6
+ metadata.gz: 7e7fc6214685e1ac7f66942a22e8eba654563c42cedd95e8fd02206f7b4f03624cfe3748710a85238e0d0f744ca46c11a7dcd8d882eb9006b47f411ed3fd66fc
7
+ data.tar.gz: de0889f852fdc41f69d04d2f9de383749987b5aed2cef9506fa02463408a5d8be31234e3ceee2f360b4bedfb5d9b0d8d1728b031458b9b28ddbde14062cfdeca
data/Gemfile CHANGED
@@ -1,4 +1,4 @@
1
- source 'https://rubygems.org'
1
+ source 'https://gems.ruby-china.com/'
2
2
 
3
3
  # Specify your gem's dependencies in role_making.gemspec
4
4
  gemspec
data/README.md CHANGED
@@ -59,11 +59,43 @@ Or install it yourself as:
59
59
 
60
60
  This will create:
61
61
 
62
- db/migrate/201506250001_role_making_create_roles_create.rb
62
+ db/migrate/201506250001_role_making_create_roles.rb
63
63
  db/migrate/201506250002_role_making_create_role_resources.rb
64
64
  app/models/role.rb
65
65
  app/models/role_resource.rb
66
66
  add "role_making" to user.rb
67
+
68
+ Define resource
69
+ ```ruby
70
+ #with block condition
71
+ group :kefu do
72
+ resource [:read,:update,:destroy], Post do |user,post|
73
+ admin != user
74
+ end
75
+
76
+ resource :create,User
77
+
78
+ end
79
+
80
+
81
+ #with hash condition
82
+ group :kefu do
83
+ resource :edit, Post , type: 3
84
+ end
85
+
86
+ #if the condition need eval,you can do like this
87
+ group :kefu do
88
+ resource :edit, Post , con: {user_id: 'user.id'}
89
+ end
90
+
91
+
92
+ #with customize resource name
93
+
94
+ group :kefu do
95
+ resource :edit, Post , res_name: 'Edit Post'
96
+ end
97
+
98
+
67
99
  ## Contributing
68
100
 
69
101
  1. Fork it ( https://github.com/javyliu/role_making/fork )
@@ -71,3 +103,7 @@ Or install it yourself as:
71
103
  3. Commit your changes (`git commit -am 'Add some feature'`)
72
104
  4. Push to the branch (`git push origin my-new-feature`)
73
105
  5. Create a new Pull Request
106
+ 6.发布 gem
107
+ rake build
108
+ rake release
109
+
data/lib/generators/USAGE CHANGED
@@ -30,7 +30,7 @@ Example:
30
30
  rails generate role_making user
31
31
 
32
32
  This will create:
33
- db/migrate/201506250001_role_making_create_roles_create.rb
33
+ db/migrate/201506250001_role_making_create_roles.rb
34
34
  db/migrate/201506250002_role_making_create_role_resources.rb
35
35
  app/models/role.rb
36
36
  app/models/role_resource.rb
@@ -12,7 +12,7 @@ module RoleMaking
12
12
  def inject_user_class
13
13
  model_path = File.join("app", "models", "#{name.underscore}.rb")
14
14
 
15
- insert_into_file(model_path,after: "ActiveRecord::Base\n") do
15
+ insert_into_file(model_path,after: "ApplicationRecord\n") do
16
16
  " role_making role_cname: '#{role_cname.classify}'\n"
17
17
  end
18
18
 
@@ -35,28 +35,29 @@ module RoleMaking
35
35
  Rails::Generators.invoke "cancan:ability",["-s"],behavior: behavior
36
36
  model_path = File.join("app", "models", "ability.rb")
37
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)
38
+ #可以通过对该用户的每个角色来初始化权限,而不用通过轮循所有角色
39
+ <<-EOF
40
+ ##{role_cname.classify}.all_without_reserved.each do |role|
41
+ # next unless user.has_role?(role.name)
42
+ user.roles.each do |role|
41
43
  role.role_resources.each do |res|
42
- resource = Resource.find_by_name(res.resource_name)
44
+ resource = Resource.find_by_name(res.resource_name) rescue next
43
45
  if block = resource.behavior
44
46
  can resource.verb,resource.object do |obj|
45
47
  block.call(user,obj)
46
48
  end
47
- elsif resource.hash
48
- eval_con = resource.hash.delete(:con).try(:inject,{}) do |r,(k,v)|
49
+ elsif resource.hashs
50
+ eval_con = resource.hashs.delete(:con).try(:inject,{}) do |r,(k,v)|
49
51
  r[k] = eval(v)
50
52
  r
51
- end
52
- can resource[:verb],resource[:object],resource[:hash].merge(eval_con)
53
+ end || {}
54
+ can resource[:verb],resource[:object],resource[:hashs].except(:con).merge(eval_con)
53
55
  else
54
56
  can resource[:verb],resource[:object]
55
57
  end
56
58
  end
57
59
  end \n
58
- EOF
59
-
60
+ EOF
60
61
 
61
62
  end if behavior == :invoke
62
63
 
@@ -74,16 +75,17 @@ module RoleMaking
74
75
  model_path = File.join("app", "models", "#{role_cname.underscore}.rb")
75
76
 
76
77
  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
78
+ join_table = [name.tableize,role_cname.tableize].sort.join("_")
79
+ insert_into_file(model_path,after: "ApplicationRecord\n") do
80
+ <<-EOF
81
+ RESERVED = [:admin,:guest]
82
+ has_and_belongs_to_many :#{name.tableize}, join_table: :#{join_table}
83
+ has_many :role_resources, dependent: :destroy
84
+
85
+ def self.all_without_reserved
86
+ where("name not in (?)",RESERVED)
87
+ end
88
+ EOF
87
89
  end
88
90
 
89
91
  end
@@ -92,63 +94,67 @@ module RoleMaking
92
94
  def inject_role_resource_class
93
95
  model_path = File.join("app", "models", "role_resource.rb")
94
96
 
95
- insert_into_file(model_path,after: "ActiveRecord::Base\n") do
96
- <<-EOF
97
- validates_uniqueness_of :resource_name, scope: :#{role_cname.underscore}_id
97
+ insert_into_file(model_path,after: "ApplicationRecord\n") do
98
+ <<-EOF
99
+ validates_uniqueness_of :resource_name, scope: :#{role_cname.underscore}_id
98
100
 
99
- belongs_to :#{role_cname.underscore}
100
- EOF
101
+ belongs_to :#{role_cname.underscore}
102
+ EOF
101
103
  end
102
104
  end
103
105
 
104
106
 
105
107
  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
108
+ #role_name = role_cname.underscore
109
+ #join_table = "#{name.underscore}_#{role_name}"
110
+ #user_reference = name.underscore
111
+ #role_reference = role_cname.underscore
112
+ #role_file = "db/migrate/201506250001_role_making_create_#{role_table}.rb"
113
+ #role_resource_file = "db/migrate/201506250002_role_making_create_role_resources.rb"
114
+
115
+ # create_file role_file do
116
+ # <<-EOF
117
+ #class RoleMakingCreate#{role_table.camelize} < ActiveRecord::Migration
118
+ # def change
119
+ # create_table(:#{role_table}) do |t|
120
+ # t.string :name
121
+ # t.string :display_name
122
+ #
123
+ # t.timestamps
124
+ # end
125
+ #
126
+ # create_table(:#{join_table}, :id => false) do |t|
127
+ # t.references :#{user_reference}
128
+ # t.references :#{role_reference}
129
+ # end
130
+ #
131
+ # add_index(:#{role_table}, :name)
132
+ # add_index(:#{join_table}, [ :#{user_reference}_id, :#{role_reference}_id ])
133
+ # end
134
+ #end
135
+ # EOF
136
+ #
137
+ # end
138
+
139
+ Rails::Generators.invoke "migration",["create_#{role_cname.tableize}","name:string{20}:index","display_name:string{30}","created_at:datetime","updated_at:datetime"], behavior: behavior
140
+ Rails::Generators.invoke "migration",["create_user_join_table","#{name.tableize}","#{role_cname.tableize}:uniq"], behavior: behavior
141
+ Rails::Generators.invoke "migration",["create_role_resource","#{role_cname.underscore}_id:integer:index","resource_name:string{50}:index","created_at:datetime","updated_at:datetime"], behavior: behavior
142
+ # create_file role_resource_file do
143
+ # <<-EOF
144
+ #class RoleMakingCreateRoleResources < ActiveRecord::Migration
145
+ # def change
146
+ # create_table(:role_resources) do |t|
147
+ # t.references :#{role_reference}
148
+ # t.string :resource_name
149
+ #
150
+ # t.timestamps
151
+ # end
152
+ #
153
+ # add_index(:role_resources, :#{role_reference}_id)
154
+ # end
155
+ #end
156
+ # EOF
157
+ # end
152
158
  end
153
159
 
154
160
 
@@ -15,4 +15,9 @@ class Resource
15
15
  # resource [:update,:destroy,:create],Post
16
16
  #end
17
17
 
18
+ ##with hashs
19
+ #group :post do
20
+ # resource :read,Post
21
+ # resource [:update,:destroy,:create],Post,con: {user_id: 'user.leader_data'}
22
+ #end
18
23
  end
@@ -6,7 +6,16 @@ module RoleMaking
6
6
  @groups = []
7
7
  @current_group = nil
8
8
  @resources = []
9
- Res = Struct.new(:name,:group,:verb,:hash,:object,:behavior)
9
+ Res = Struct.new(:name,:group,:verb,:hashs,:object,:behavior,:action_scope,:res_scope)
10
+ #need set locale yml file
11
+ class Res
12
+ def human_name
13
+ action,res = self.name.split("@")
14
+ ac = I18n.t(action,scope: (self.action_scope || 'actions'),throw: true) rescue action
15
+ _res = I18n.t(res,scope: (self.res_scope || 'activerecord.models'),throw: true) rescue res
16
+ "#{ac}#{_res}"
17
+ end
18
+ end
10
19
  end
11
20
 
12
21
  module ClassMethods
@@ -18,18 +27,23 @@ module RoleMaking
18
27
  block.call
19
28
  end
20
29
 
21
- def resource(verb_or_verbs,object,hash=nil,&block)
30
+
31
+
32
+ def resource(verb_or_verbs,object,hashs=nil,&block)
22
33
  raise "Need define group first" if @current_group.nil?
23
34
  group = @current_group
24
35
  behavior = block
25
36
  Array.wrap(verb_or_verbs).each do |verb|
26
- add_resource(group,verb,object,hash,behavior)
37
+ add_resource(group,verb,object,hashs.try(:dup),behavior)
27
38
  end
28
39
  end
29
40
 
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)
41
+ def add_resource(group,verb,object,hashs,behavior)
42
+ name = "#{verb}@#{hashs.try(:delete,:res_name) || object.to_s.underscore}"
43
+ action_scope = hashs.try(:delete,:action_scope)
44
+ res_scope = hashs.try(:delete,:res_scope)
45
+ hashs = nil if hashs.blank?
46
+ resource = Res.new(name,group,verb,hashs,object,behavior,action_scope,res_scope)
33
47
  @resources << resource
34
48
  end
35
49
 
@@ -13,12 +13,14 @@ module RoleMaking
13
13
  alias_method :grant, :add_role
14
14
 
15
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
16
+ # role_array = if new_record?
17
+ # self.roles.detect { |r| r.name.to_s == role_name.to_s }
18
+ # else
19
+ # #self.respond_to?(:cache_roles) ? self.cache_roles.detect{|it| it.name == role_name.to_s} : self.roles.exists?( name: role_name)
20
+ # self.roles.detect {||}
21
+ # end
22
+ !!self.roles.detect { |r| r.name.to_s == role_name.to_s }
23
+ #!!role_array
22
24
  end
23
25
 
24
26
  def has_all_roles?(*args)
@@ -39,7 +41,8 @@ module RoleMaking
39
41
  alias_method :revoke, :remove_role
40
42
 
41
43
  def roles_name
42
- self.roles.select(:name).map { |r| r.name }
44
+ #self.roles.select(:name).map { |r| r.name }
45
+ self.roles.map { |r| r.display_name }
43
46
  end
44
47
 
45
48
  end
@@ -1,3 +1,3 @@
1
1
  module RoleMaking
2
- VERSION = "0.0.2.next1"
2
+ VERSION = "0.2.3"
3
3
  end
data/lib/role_making.rb CHANGED
@@ -13,7 +13,7 @@ module RoleMaking
13
13
  self.role_cname = options[:role_cname]
14
14
  self.role_table_name = self.role_cname.tableize.gsub(/\//, "_")
15
15
 
16
- default_join_table = "#{self.to_s.tableize.gsub(/\//, "_")}_#{self.role_table_name}"
16
+ default_join_table = [self.to_s.tableize.gsub(/\//, "_"),self.role_table_name].sort.join("_")
17
17
  options.reverse_merge!({:role_join_table_name => default_join_table})
18
18
  self.role_join_table_name = options[:role_join_table_name]
19
19
 
@@ -21,12 +21,9 @@ module RoleMaking
21
21
  rolify_options.merge!({ :join_table => self.role_join_table_name })
22
22
  rolify_options.merge!(options.reject{ |k,v| ![ :before_add, :after_add, :before_remove, :after_remove ].include? k.to_sym })
23
23
 
24
- has_and_belongs_to_many :roles, rolify_options
24
+ has_and_belongs_to_many :roles, **rolify_options
25
+ #has_and_belongs_to_many :roles, class_name: 'role', join_table: 'roles_users'
25
26
 
26
27
  #load_dynamic_methods if RoleMaking.dynamic_shortcuts
27
28
  end
28
-
29
-
30
-
31
-
32
29
  end
data/role_making.gemspec CHANGED
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
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"
21
+ spec.add_dependency 'cancancan'#, '~> 1.10','>= 1.10.1'
22
+ spec.add_development_dependency "bundler", "~> 2.2"
23
+ spec.add_development_dependency "rake", "~> 13.0"
24
24
  end
metadata CHANGED
@@ -1,63 +1,57 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: role_making
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.next1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - javy_liu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-09 00:00:00.000000000 Z
11
+ date: 2021-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancancan
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.10'
20
17
  - - ">="
21
18
  - !ruby/object:Gem::Version
22
- version: 1.10.1
19
+ version: '0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
- - - "~>"
28
- - !ruby/object:Gem::Version
29
- version: '1.10'
30
24
  - - ">="
31
25
  - !ruby/object:Gem::Version
32
- version: 1.10.1
26
+ version: '0'
33
27
  - !ruby/object:Gem::Dependency
34
28
  name: bundler
35
29
  requirement: !ruby/object:Gem::Requirement
36
30
  requirements:
37
31
  - - "~>"
38
32
  - !ruby/object:Gem::Version
39
- version: '1.7'
33
+ version: '2.2'
40
34
  type: :development
41
35
  prerelease: false
42
36
  version_requirements: !ruby/object:Gem::Requirement
43
37
  requirements:
44
38
  - - "~>"
45
39
  - !ruby/object:Gem::Version
46
- version: '1.7'
40
+ version: '2.2'
47
41
  - !ruby/object:Gem::Dependency
48
42
  name: rake
49
43
  requirement: !ruby/object:Gem::Requirement
50
44
  requirements:
51
45
  - - "~>"
52
46
  - !ruby/object:Gem::Version
53
- version: '10.0'
47
+ version: '13.0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
51
  requirements:
58
52
  - - "~>"
59
53
  - !ruby/object:Gem::Version
60
- version: '10.0'
54
+ version: '13.0'
61
55
  description: A gem for resource manage based on role
62
56
  email:
63
57
  - javy_liu@163.com
@@ -84,7 +78,7 @@ homepage: https://github.com/javyliu/role_making
84
78
  licenses:
85
79
  - MIT
86
80
  metadata: {}
87
- post_install_message:
81
+ post_install_message:
88
82
  rdoc_options: []
89
83
  require_paths:
90
84
  - lib
@@ -95,14 +89,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
95
89
  version: '0'
96
90
  required_rubygems_version: !ruby/object:Gem::Requirement
97
91
  requirements:
98
- - - ">"
92
+ - - ">="
99
93
  - !ruby/object:Gem::Version
100
- version: 1.3.1
94
+ version: '0'
101
95
  requirements: []
102
- rubyforge_project:
103
- rubygems_version: 2.4.6
104
- signing_key:
96
+ rubygems_version: 3.2.3
97
+ signing_key:
105
98
  specification_version: 4
106
99
  summary: for custom defined role to manage the resources
107
100
  test_files: []
108
- has_rdoc: