can_play 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/can_play/ability.rb +29 -23
- data/lib/can_play/version.rb +1 -1
- data/lib/generators/templates/can_play.rb +3 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 16441a20427ca9e3570f0e3a3cda976916eea535
|
4
|
+
data.tar.gz: 23b9fd796b805e68ab8022ba381920e6b76d1f02
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1275d49864ffd120b58b98a39035f8573defc82ea5b0579e7e01e158c9260f5ba9bce86037838cc881356c75f7d49b2d2566e9a160242cb509dbb6cdd7ad3fec
|
7
|
+
data.tar.gz: 59e3db805478f280d33fd85606dd1cc3e3fe2de01094a3c4157ae5ea0bbdd4200e162235ac08672b9694821dd2e5c6187fae40541252c0b031a55448883ad78d
|
data/lib/can_play/ability.rb
CHANGED
@@ -3,30 +3,36 @@ class Ability
|
|
3
3
|
attr_accessor :user
|
4
4
|
|
5
5
|
def initialize(user)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
6
|
+
CanPlay::Config.tap do |i|
|
7
|
+
self.user = user||i.user_class_name.constantize.new
|
8
|
+
if i.super_roles.is_a?(Array)
|
9
|
+
i.super_roles.each do |role_name|
|
10
|
+
can(:manage, :all) if user.send(i.role_judge_method, role_name)
|
11
|
+
end
|
12
|
+
elsif i.super_roles.is_a?(Proc)
|
13
|
+
can(:manage, :all) if i.super_roles.call(user)
|
14
|
+
end
|
15
|
+
i.role_class_name.constantize.all.each do |role|
|
16
|
+
next unless user.send(i.role_judge_method, role)
|
17
|
+
role.send(i.role_resources_relation_name).each do |role_resource|
|
18
|
+
resource = CanPlay.find_by_name_and_code(role_resource.resource_name, CanPlay.override_code)
|
19
|
+
next unless resource
|
20
|
+
if resource[:type] == 'collection'
|
21
|
+
if resource[:behavior]
|
22
|
+
block = resource[:behavior]
|
23
|
+
can(resource[:verb], resource[:object]) if block.call(user)
|
24
|
+
else
|
25
|
+
can resource[:verb], resource[:object]
|
26
|
+
end
|
27
|
+
elsif resource[:type] == 'member'
|
28
|
+
if resource[:behavior]
|
29
|
+
block = resource[:behavior]
|
30
|
+
can resource[:verb], resource[:object] do |object|
|
31
|
+
block.call(user, object)
|
32
|
+
end
|
33
|
+
else
|
34
|
+
can resource[:verb], resource[:object]
|
27
35
|
end
|
28
|
-
else
|
29
|
-
can resource[:verb], resource[:object]
|
30
36
|
end
|
31
37
|
end
|
32
38
|
end
|
data/lib/can_play/version.rb
CHANGED
@@ -14,6 +14,9 @@ CanPlay::Config.setup do |config|
|
|
14
14
|
# super_roles表示无需分配权限既可拥有所有权限的角色。
|
15
15
|
config.super_roles = ['超级管理员']
|
16
16
|
|
17
|
+
# 也可以传入代码块,若使代码块,则会传入形参user,指代当前用户,代码块返回的结果为true,则当前登录用户的当前角色则有无限权限。。
|
18
|
+
#config.super_roles = ->(role) { 1 == user.current_role.try(:id) }
|
19
|
+
|
17
20
|
# 判断角色是否符合的方法。
|
18
21
|
config.role_judge_method = 'role_is?'
|
19
22
|
end
|