i_am_i_can 4.3.3 → 4.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/generators/i_am_i_can/templates/initializers/i_am_i_can.erb +2 -0
- data/lib/generators/i_am_i_can/templates/models/permission.erb +2 -0
- data/lib/generators/i_am_i_can/templates/models/role.erb +2 -0
- data/lib/generators/i_am_i_can/templates/models/role_group.erb +2 -0
- data/lib/i_am_i_can/configs/config.rb +2 -0
- data/lib/i_am_i_can/configs/configs.rb +2 -0
- data/lib/i_am_i_can/helpers/dynamic.rb +14 -1
- data/lib/i_am_i_can/helpers/result_of.rb +2 -0
- data/lib/i_am_i_can/permission/assignment.rb +2 -0
- data/lib/i_am_i_can/permission/definition.rb +2 -0
- data/lib/i_am_i_can/permission.rb +2 -0
- data/lib/i_am_i_can/resource.rb +2 -0
- data/lib/i_am_i_can/role/assignment.rb +2 -0
- data/lib/i_am_i_can/role/definition.rb +2 -0
- data/lib/i_am_i_can/role/grouping.rb +2 -0
- data/lib/i_am_i_can/role.rb +7 -0
- data/lib/i_am_i_can/role_group/definition.rb +2 -0
- data/lib/i_am_i_can/subject/permission_querying.rb +5 -3
- data/lib/i_am_i_can/subject/role_querying.rb +25 -15
- data/lib/i_am_i_can/subject.rb +10 -0
- data/lib/i_am_i_can/support/association_class_methods.rb +2 -6
- data/lib/i_am_i_can/support/configurable.rb +2 -0
- data/lib/i_am_i_can/support/reflection.rb +2 -0
- data/lib/i_am_i_can/version.rb +3 -1
- data/lib/i_am_i_can.rb +2 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ab915220af5d95373eaa8b8a57563d311a7f2903
|
4
|
+
data.tar.gz: ea007c8d5b61f511dcbfd799432d5221dd666804
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd645226ade084917ddc64405009d1c7c6f2b8ead5c69544be10e296264a963ab38ab2f851ac46633cbb36c45d052330a1899e3381b1517bc1804c83fb7dbf26
|
7
|
+
data.tar.gz: 0e7ecdb8a56621234a9f52dc9c3f8a6131b55ea0674fe7c78a5c1f84c66305f0ce80a7137d2d3e0a109cae2260cd9cf4f23e8de257906be08aa9f9f843180462
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module IAmICan
|
2
4
|
module Dynamic
|
3
5
|
extend self
|
@@ -8,9 +10,20 @@ module IAmICan
|
|
8
10
|
#
|
9
11
|
# scope :with_stored_roles, -> { includes(:stored_roles) }
|
10
12
|
#
|
13
|
+
# Usage:
|
14
|
+
# User.with_stored_roles(role_id = 1)
|
15
|
+
# User.with_stored_roles?(role_id = 1)
|
11
16
|
proc do |keys|
|
12
17
|
keys.each do |k|
|
13
|
-
scope :"with_#{_reflect_of(k)}", ->
|
18
|
+
scope :"with_#{_reflect_of(k)}", -> (ids = nil) do
|
19
|
+
break includes(_reflect_of(k)) unless ids
|
20
|
+
includes(_reflect_of(k)).where(
|
21
|
+
i_am_i_can.send("#{k}_class").underscore.pluralize => { id: ids })
|
22
|
+
end
|
23
|
+
|
24
|
+
define_singleton_method :"with_#{_reflect_of(k)}?" do |ids|
|
25
|
+
send(:"with_#{_reflect_of(k)}", ids).present?
|
26
|
+
end
|
14
27
|
end
|
15
28
|
end
|
16
29
|
end
|
data/lib/i_am_i_can/resource.rb
CHANGED
data/lib/i_am_i_can/role.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'i_am_i_can/role/definition'
|
2
4
|
require 'i_am_i_can/role/assignment'
|
3
5
|
require 'i_am_i_can/role/grouping'
|
@@ -15,6 +17,11 @@ module IAmICan
|
|
15
17
|
def names
|
16
18
|
self.pluck(:name).map(&:to_sym)
|
17
19
|
end
|
20
|
+
|
21
|
+
def can?(action, obj)
|
22
|
+
send("with_#{_reflect_of('permission')}?",
|
23
|
+
i_am_i_can.permission_model.matched(action, obj).ids)
|
24
|
+
end
|
18
25
|
end
|
19
26
|
|
20
27
|
included do
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module IAmICan
|
2
4
|
module Subject
|
3
5
|
module PermissionQuerying
|
@@ -40,12 +42,12 @@ module IAmICan
|
|
40
42
|
alias can_every! can_each!
|
41
43
|
|
42
44
|
def temporarily_can? action, obj
|
43
|
-
return false if temporary_roles.blank?
|
44
|
-
valid_temporary_roles.
|
45
|
+
return false if try(:temporary_roles).blank?
|
46
|
+
valid_temporary_roles.can?(action, obj)
|
45
47
|
end
|
46
48
|
|
47
49
|
def stored_can? action, obj
|
48
|
-
_roles.
|
50
|
+
_roles.can?(action, obj)
|
49
51
|
end
|
50
52
|
|
51
53
|
def group_can? action, obj, without_group = false
|
@@ -1,48 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module IAmICan
|
2
4
|
module Subject
|
3
5
|
module RoleQuerying
|
4
6
|
|
5
7
|
# === Role Querying ===
|
6
8
|
|
7
|
-
def is? role_name
|
8
|
-
|
9
|
+
def is? role_name, with_tmp: true
|
10
|
+
role_id = i_am_i_can.role_model.find_by(name: role_name)&.id
|
11
|
+
return false unless role_id
|
12
|
+
get_roles(with_tmp: with_tmp).exists?(id: role_id)
|
9
13
|
end
|
10
14
|
|
11
15
|
alias is_role? is?
|
12
16
|
alias has_role? is?
|
13
17
|
|
14
|
-
def isnt?
|
15
|
-
!is?
|
18
|
+
def isnt? role_name, with_tmp: true
|
19
|
+
!is? role_name, with_tmp: with_tmp
|
16
20
|
end
|
17
21
|
|
18
|
-
def is!
|
19
|
-
raise VerificationFailed if isnt?
|
22
|
+
def is! role_name, with_tmp: true
|
23
|
+
raise VerificationFailed if isnt? role_name, with_tmp: with_tmp
|
20
24
|
true
|
21
25
|
end
|
22
26
|
|
23
27
|
alias is_role! is!
|
24
28
|
alias has_role! is!
|
25
29
|
|
26
|
-
def is_one_of? *role_names
|
27
|
-
|
30
|
+
def is_one_of? *role_names, with_tmp: true
|
31
|
+
role_ids = i_am_i_can.role_model.where(name: role_names).ids
|
32
|
+
return false if role_ids.blank?
|
33
|
+
get_roles(with_tmp: with_tmp).exists?(id: role_ids)
|
28
34
|
end
|
29
35
|
|
30
36
|
alias is_one_of_roles? is_one_of?
|
31
37
|
|
32
|
-
def is_one_of! *
|
33
|
-
raise VerificationFailed unless is_one_of? *
|
38
|
+
def is_one_of! *role_names, with_tmp: true
|
39
|
+
raise VerificationFailed unless is_one_of? *role_names, with_tmp: with_tmp
|
40
|
+
true
|
34
41
|
end
|
35
42
|
|
36
43
|
alias is_one_of_roles! is_one_of!
|
37
44
|
|
38
|
-
def is_every? *role_names
|
39
|
-
|
45
|
+
def is_every? *role_names, with_tmp: true
|
46
|
+
role_ids = i_am_i_can.role_model.where(name: role_names).ids
|
47
|
+
return false if role_ids.size != role_names.size
|
48
|
+
get_roles(with_tmp: with_tmp).where(id: role_ids).size == role_names.size
|
40
49
|
end
|
41
50
|
|
42
51
|
alias is_every_role_in? is_every?
|
43
52
|
|
44
|
-
def is_every! *
|
45
|
-
|
53
|
+
def is_every! *role_names, with_tmp: true
|
54
|
+
raise VerificationFailed unless is_every?(*role_names, with_tmp: true)
|
55
|
+
true
|
46
56
|
end
|
47
57
|
|
48
58
|
alias is_every_role_in! is_every!
|
@@ -51,7 +61,7 @@ module IAmICan
|
|
51
61
|
|
52
62
|
def is_in_role_group? name
|
53
63
|
group_members = i_am_i_can.role_group_model.find_by!(name: name)._roles.names
|
54
|
-
(
|
64
|
+
(get_roles.names & group_members).present?
|
55
65
|
end
|
56
66
|
|
57
67
|
alias in_role_group? is_in_role_group?
|
data/lib/i_am_i_can/subject.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'i_am_i_can/subject/role_querying'
|
2
4
|
require 'i_am_i_can/subject/permission_querying'
|
3
5
|
|
@@ -15,6 +17,14 @@ module IAmICan
|
|
15
17
|
Class.new(ActiveRecord::Base)
|
16
18
|
has_many :"assoc_with_#{__roles}", -> { where('expire_at IS NULL OR expire_at > ?', Time.current) },
|
17
19
|
class_name: role_assoc_class
|
20
|
+
|
21
|
+
def get_roles(with_tmp: true)
|
22
|
+
if with_tmp && (tmp_ids = try(:temporary_roles)&.map(&:id)).present?
|
23
|
+
i_am_i_can.role_model.where(id: (tmp_ids + _roles.ids).uniq)
|
24
|
+
else
|
25
|
+
_roles
|
26
|
+
end
|
27
|
+
end
|
18
28
|
end
|
19
29
|
end
|
20
30
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module IAmICan
|
2
4
|
module Association_ClassMethods
|
3
5
|
def has_many_temporary_roles(name: nil)
|
@@ -11,12 +13,6 @@ module IAmICan
|
|
11
13
|
i_am_i_can.role_model.where(id: temporary_roles.map(&:id))
|
12
14
|
end
|
13
15
|
|
14
|
-
raise "Do not set the role association name to `roles` in #{self.class.name} model" if respond_to?(:roles)
|
15
|
-
define_method :roles do
|
16
|
-
ids = (temporary_roles.map(&:id) + _roles.ids).uniq
|
17
|
-
i_am_i_can.role_model.where(id: ids)
|
18
|
-
end
|
19
|
-
|
20
16
|
define_method :temporary_role_names do
|
21
17
|
temporary_roles.map(&:name).map(&:to_sym)
|
22
18
|
end
|
data/lib/i_am_i_can/version.rb
CHANGED
data/lib/i_am_i_can.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: i_am_i_can
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- zhandao
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|