role_making 0.0.3 → 0.0.4

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
2
  SHA1:
3
- metadata.gz: bf61b0f58790e176d8bf301c390a8cd980557950
4
- data.tar.gz: a4ec93e882f235453a84922fc897a15cbeadeb7c
3
+ metadata.gz: dd9101daa11496c2da12496ccfdd22d85a6837c2
4
+ data.tar.gz: d937f52f786591f66c289b143e48555aced4cc00
5
5
  SHA512:
6
- metadata.gz: e221d282dec866b9b1262a6888b9f399ff06d929ed553a205aaf8ff27ccfa86b22a30c473ee971668166a469912256dba6c6d811f879dcdba087f9a8801844f8
7
- data.tar.gz: 642148d06df463a1ca139c9c5fd67a9a1d4e3e3bf19ee3ce8f619cb10e56de78b87d7514502f97f0ccabca8a078a0cb07bfa45a059d987f6e0c661da9f18533f
6
+ metadata.gz: 64fd8e1222ae4be4f936203d89862873b16df5976192bad03173861197ebe2f7288435577016c416287e38ae2122b0e4a8a7cb167beca7ad39c85c8ff7a93d00
7
+ data.tar.gz: 8ec78bcdd31367726287854b5a2c6ab810674df26c822593fd6be10e2dd3ea61184dd4415ce55dcc0c1fd8382eaeb81720a527ff58b6d1980f499d0e0d91908e
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 )
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
@@ -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}"
78
+ join_table = [name.tableize,role_cname.tableize].sort.join("_")
77
79
  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
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
@@ -93,62 +95,66 @@ module RoleMaking
93
95
  model_path = File.join("app", "models", "role_resource.rb")
94
96
 
95
97
  insert_into_file(model_path,after: "ActiveRecord::Base\n") do
96
- <<-EOF
97
- validates_uniqueness_of :resource_name, scope: :#{role_cname.underscore}_id
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
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
 
@@ -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,22 @@ 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
+ resource = Res.new(name,group,verb,hashs,object,behavior,action_scope,res_scope)
33
46
  @resources << resource
34
47
  end
35
48
 
@@ -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.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: role_making
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - javy_liu
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-09 00:00:00.000000000 Z
11
+ date: 2015-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cancancan