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 +5 -5
- data/Gemfile +1 -1
- data/README.md +37 -1
- data/lib/generators/USAGE +1 -1
- data/lib/generators/role_making_generator.rb +78 -72
- data/lib/generators/templates/resource.rb +5 -0
- data/lib/role_making/resourcing.rb +20 -6
- data/lib/role_making/role.rb +10 -7
- data/lib/role_making/version.rb +1 -1
- data/lib/role_making.rb +3 -6
- data/role_making.gemspec +3 -3
- metadata +14 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d023f18da13dc8704eb3b5824648222f37557cb7ce2621a8e5d78c5197b3fecf
|
4
|
+
data.tar.gz: d87dc8b1348d8d4ff34312e292d4658346ee254c441a9b6b1966a0e294e6d8af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e7fc6214685e1ac7f66942a22e8eba654563c42cedd95e8fd02206f7b4f03624cfe3748710a85238e0d0f744ca46c11a7dcd8d882eb9006b47f411ed3fd66fc
|
7
|
+
data.tar.gz: de0889f852fdc41f69d04d2f9de383749987b5aed2cef9506fa02463408a5d8be31234e3ceee2f360b4bedfb5d9b0d8d1728b031458b9b28ddbde14062cfdeca
|
data/Gemfile
CHANGED
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/
|
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/
|
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: "
|
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
|
-
|
39
|
-
|
40
|
-
|
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.
|
48
|
-
eval_con = resource.
|
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[:
|
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
|
-
|
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
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
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: "
|
96
|
-
|
97
|
-
|
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
|
-
|
100
|
-
|
101
|
+
belongs_to :#{role_cname.underscore}
|
102
|
+
EOF
|
101
103
|
end
|
102
104
|
end
|
103
105
|
|
104
106
|
|
105
107
|
def create_migration
|
106
|
-
|
107
|
-
join_table = "#{name.
|
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
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
EOF
|
134
|
-
|
135
|
-
end
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
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
|
|
@@ -6,7 +6,16 @@ module RoleMaking
|
|
6
6
|
@groups = []
|
7
7
|
@current_group = nil
|
8
8
|
@resources = []
|
9
|
-
Res = Struct.new(:name,:group,:verb,:
|
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
|
-
|
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,
|
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,
|
31
|
-
name = "#{verb}
|
32
|
-
|
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
|
|
data/lib/role_making/role.rb
CHANGED
@@ -13,12 +13,14 @@ module RoleMaking
|
|
13
13
|
alias_method :grant, :add_role
|
14
14
|
|
15
15
|
def has_role?(role_name)
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
data/lib/role_making/version.rb
CHANGED
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 =
|
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'
|
22
|
-
spec.add_development_dependency "bundler", "~>
|
23
|
-
spec.add_development_dependency "rake", "~>
|
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.
|
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:
|
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:
|
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:
|
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: '
|
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: '
|
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: '
|
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: '
|
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:
|
94
|
+
version: '0'
|
101
95
|
requirements: []
|
102
|
-
|
103
|
-
|
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:
|