controller_policies 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ability.rb +30 -36
- data/lib/controller_policies/enforcement.rb +14 -11
- data/lib/controller_policies/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 85d80d00d5bb31de269ac2190cd44a77e9d2aa1623aa7d6e00cf86de12565baa
|
4
|
+
data.tar.gz: ec23ef9100a962f17386693889903182f84cbb247c9dc275da54cec789acc445
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ec83f851cc09988e4664c404b2e99b2a8c48e954f3804652fb0477f66545d75b12887261e4b72c265da16dd3c8ce277e5fdcf6d170eea535b7dfe07c53aa2e8
|
7
|
+
data.tar.gz: 6afa4ececa6d5c04164eafc5c66a578290d6ff3edfcffff76260f6bb645450000b2177d33b49a9d3105eb8f6463a0cda3902a7caee3baa4acfa665b0395e9f39
|
data/lib/ability.rb
CHANGED
@@ -34,41 +34,28 @@ class Ability
|
|
34
34
|
|
35
35
|
# Filter abilities based on namespace.
|
36
36
|
def where(*queries)
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
results += all.select { |ability| ability.namespace.to_s == "Policies::#{trim(query).camelize}" }
|
46
|
-
end
|
47
|
-
when 'Module', 'Class'
|
48
|
-
results += all.select { |ability| ability.namespace == query }
|
37
|
+
queries.flat_map do |query|
|
38
|
+
case query
|
39
|
+
when String
|
40
|
+
filter_by_string_query(query)
|
41
|
+
when Module, Class
|
42
|
+
all.select { |ability| ability.namespace == query }
|
43
|
+
else
|
44
|
+
[]
|
49
45
|
end
|
50
46
|
end
|
51
|
-
results
|
52
47
|
end
|
53
48
|
|
54
|
-
# Find an ability within a namespace.
|
55
|
-
# def find(query_string)
|
56
|
-
# where(query_string).first
|
57
|
-
# end
|
58
|
-
|
59
49
|
# Match abilities based on a matching string or regex. The matcher is based on the namespace.
|
60
50
|
def match(expression)
|
61
|
-
case expression
|
62
|
-
when
|
63
|
-
|
51
|
+
case expression
|
52
|
+
when String
|
53
|
+
all.select { |ability| ability.namespace.to_s.match?(/#{trim(expression).camelize}/) }
|
54
|
+
when Regexp
|
55
|
+
regex_matcher(expression)
|
64
56
|
end
|
65
57
|
end
|
66
58
|
|
67
|
-
# Find an ability based on a matching string or regex. The matcher is based on the namespace.
|
68
|
-
# def mill(expression)
|
69
|
-
# match(expression).first
|
70
|
-
# end
|
71
|
-
|
72
59
|
# Path to the policy folder.
|
73
60
|
def policy_path
|
74
61
|
@policy_path ||= Rails.root.join('app/policies')
|
@@ -76,6 +63,15 @@ class Ability
|
|
76
63
|
|
77
64
|
private
|
78
65
|
|
66
|
+
def filter_by_string_query(query)
|
67
|
+
if query.empty? || query == '/'
|
68
|
+
all.select { |ability| ability.namespace == Policies }
|
69
|
+
else
|
70
|
+
target_namespace = "Policies::#{trim(query).camelize}"
|
71
|
+
all.select { |ability| ability.namespace.to_s == target_namespace }
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
79
75
|
def definitions
|
80
76
|
@definitions ||= begin
|
81
77
|
data = []
|
@@ -88,19 +84,17 @@ class Ability
|
|
88
84
|
|
89
85
|
def definition_files_post_processing(file_path)
|
90
86
|
namespace_path = convert_namespace(file_path)
|
87
|
+
module_constant = resolve_module_constant(namespace_path)
|
91
88
|
|
92
|
-
|
93
|
-
|
94
|
-
module_constant = Policies
|
95
|
-
else
|
96
|
-
module_constant = "Policies::#{namespace_path}".constantize
|
89
|
+
module_constant::DEFINITIONS.map do |policy_definition|
|
90
|
+
policy_definition.merge(namespace: module_constant)
|
97
91
|
end
|
92
|
+
end
|
98
93
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
end
|
94
|
+
def resolve_module_constant(namespace_path)
|
95
|
+
return Policies if namespace_path.empty?
|
96
|
+
|
97
|
+
"Policies::#{namespace_path}".constantize
|
104
98
|
end
|
105
99
|
|
106
100
|
def regex_matcher(expression)
|
@@ -35,19 +35,22 @@ module ControllerPolicies
|
|
35
35
|
# If it finds a match, it will be an applicable definition to check abilities for.
|
36
36
|
def applicable_abilities
|
37
37
|
@applicable_abilities ||= begin
|
38
|
-
|
39
|
-
namespace_abilities
|
40
|
-
Ability.where('') # This will get root-level policies
|
41
|
-
else
|
42
|
-
Ability.where(controller_namespace)
|
43
|
-
end
|
44
|
-
|
45
|
-
namespace_abilities.select do |ability|
|
46
|
-
matching_actions = ability.actions.select { |action| /#{action}/.match?("#{controller_path}##{action_name}") }
|
47
|
-
matching_actions.present?
|
48
|
-
end
|
38
|
+
namespace_abilities = fetch_namespace_abilities
|
39
|
+
namespace_abilities.select { |ability| ability_matches_current_action?(ability) }
|
49
40
|
end
|
50
41
|
end
|
42
|
+
|
43
|
+
# Get abilities for the current controller namespace, or root if no namespace
|
44
|
+
def fetch_namespace_abilities
|
45
|
+
namespace = controller_namespace.empty? ? '' : controller_namespace
|
46
|
+
Ability.where(namespace)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Check if an ability matches the current controller action
|
50
|
+
def ability_matches_current_action?(ability)
|
51
|
+
current_controller_action = "#{controller_path}##{action_name}"
|
52
|
+
ability.actions.any? { |action| /#{action}/.match?(current_controller_action) }
|
53
|
+
end
|
51
54
|
end
|
52
55
|
end
|
53
56
|
end
|