apicasso 0.6.5 → 0.6.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d09d0e339bfb17aa767876e5041f749e5154cedb2c327588713a14113d0e00dd
4
- data.tar.gz: dd03efe8e6a7a64e4f67cae8e19f2d8be5a0782f10d1f0524114f3071969bbca
3
+ metadata.gz: 05272b515597bae8632763fc837cc5e18ac0ed42209068b4f88d19b6c7356043
4
+ data.tar.gz: 4e31ec9f33f2d7f1fe91c5154c80992390af1b65dc50e90b7e68f2ed2986472d
5
5
  SHA512:
6
- metadata.gz: a456ef2a8afb8c238f99b693cbd3bf650753fb6c07f6c5e31724cd03e7d5607cb6edfbc67004521e1abcccbf13f80b628ba197fef4876993cfd1c8a4a6f36aae
7
- data.tar.gz: 718d3734733cbeee045546495cd5f26985c799257172ca0434df0a7bd7db64e8a165c537ac17d1fda4f0cf8e3f82343bc9f8aa852e3d70958ffe4e4905ab77da
6
+ metadata.gz: 7f249d297a5ab7abc073f4419acae6f99b7e03859f2afbd170079f9d3832d634ae87b51891939fab83dc6b2fb600e2d01e07a888c645b5e9e3ce59a005980c5e
7
+ data.tar.gz: d33c492ef94b5184acb47b30f621d7fe5bc355868f6c08ae6fcf7bf8a59e81e55ac66c5104a005902e8c3f6c83ccccef8f94f1dc51ffeb3914bdac5663ed2da1
@@ -91,13 +91,6 @@ module Apicasso
91
91
  authorize! action_to_cancancan, @object
92
92
  end
93
93
 
94
- # Used to setup the resource's schema, mapping attributes and it's types
95
- def resource_schema
96
- schemated = {}
97
- resource.columns_hash.each { |key, value| schemated[key] = value.type }
98
- schemated
99
- end
100
-
101
94
  # Used to setup the records from the selected resource that are
102
95
  # going to be rendered, if authorized
103
96
  def set_records
@@ -180,13 +173,6 @@ module Apicasso
180
173
  end
181
174
  end
182
175
 
183
- # Only allow a trusted parameter "white list" through,
184
- # based on resource's schema.
185
- def object_params
186
- params.require(resource.name.underscore.to_sym)
187
- .permit(resource_params)
188
- end
189
-
190
176
  # Common setup to stablish which model is the resource of this request
191
177
  def set_root_resource
192
178
  @root_resource = params[:resource].classify.constantize
@@ -31,36 +31,63 @@ module CrudUtils
31
31
  built
32
32
  end
33
33
 
34
+ # Used to setup the resource's schema, mapping attributes and it's types
35
+ def resource_schema
36
+ schemated = {}
37
+ resource.columns_hash.each { |key, value| schemated[key] = value.type }
38
+ schemated
39
+ end
40
+
34
41
  # A wrapper to has_one relations parameter building
35
42
  def has_one_params
36
43
  resource.reflect_on_all_associations(:has_one).map do |one|
37
- if one.class_name.starts_with?('ActiveStorage')
38
- next if one.class_name.ends_with?('Blob')
39
-
40
- one.name.to_s.gsub(/(_attachment)$/, '').to_sym
41
- else
42
- one.name
43
- end
44
+ relation_param(one)
44
45
  end.compact
45
46
  end
46
47
 
47
48
  # A wrapper to has_many parameter building
48
49
  def has_many_params
49
50
  resource.reflect_on_all_associations(:has_many).map do |many|
50
- if many.class_name.starts_with?('ActiveStorage')
51
- next if many.class_name.ends_with?('Blob')
52
-
53
- { many.name.to_s.gsub(/(_attachments)$/, '').to_sym => [] }
54
- else
55
- { many.name.to_sym => [] }
56
- end
51
+ relation_param(many)
57
52
  end.compact
58
53
  end
59
54
 
55
+ # Extract permitted parameter from relation based on it's type
56
+ # This method proccess ActiveStorage parameters differently,
57
+ # so that it becomes available without further configuration
58
+ def relation_param(relation)
59
+ if relation.class_name.starts_with?('ActiveStorage')
60
+ return if relation.class_name.ends_with?('Blob')
61
+
62
+ active_storage_param(relation)
63
+ else
64
+ common_relation_param(relation)
65
+ end
66
+ end
67
+
68
+ # Non-ActiveStorage relation parameter parsing, receives the
69
+ # relation reflection as parameter
70
+ def common_relation_param(relation)
71
+ if relation.has_one?
72
+ relation.name
73
+ else
74
+ { relation.name.to_sym => [] }
75
+ end
76
+ end
77
+
78
+ # ActiveStorage relation parameter parsing, receives the
79
+ # relation reflection as parameter
80
+ def active_storage_param(relation)
81
+ if relation.has_one?
82
+ relation.name.to_s.gsub(/(_attachment)$/, '').to_sym
83
+ else
84
+ { relation.name.to_s.gsub(/(_attachments)$/, '').to_sym => [] }
85
+ end
86
+ end
87
+
60
88
  # Parse to include options
61
89
  def include_options
62
- { include: parsed_associations || [],
63
- methods: parsed_methods || [] }
90
+ { include: parsed_associations || [], methods: parsed_methods || [] }
64
91
  end
65
92
 
66
93
  # Used to avoid errors parsing the search query, which can be passed as
@@ -75,25 +102,25 @@ module CrudUtils
75
102
  # Used to avoid errors in included associations parsing and to enable a
76
103
  # insertion point for a change on splitting method.
77
104
  def parsed_associations
78
- params[:include].split(',').map do |param|
79
- if @object.respond_to?(param)
80
- param if associations_array.include?(param)
81
- end
82
- end.compact
83
- rescue NoMethodError
84
- []
105
+ parsed_include(:include)
85
106
  end
86
107
 
87
108
  # Used to avoid errors in included associations parsing and to enable a
88
109
  # insertion point for a change on splitting method.
89
110
  def parsed_methods
90
- params[:include].split(',').map do |param|
91
- if @object.respond_to?(param)
111
+ parsed_include(:method)
112
+ end
113
+
114
+ def parsed_include(opts = nil)
115
+ params[:include]&.split(',')&.map do |param|
116
+ next unless @object.respond_to?(param)
117
+
118
+ if opts == :method
92
119
  param unless associations_array.include?(param)
120
+ elsif opts == :include
121
+ param if associations_array.include?(param)
93
122
  end
94
- end.compact
95
- rescue NoMethodError
96
- []
123
+ end&.compact
97
124
  end
98
125
 
99
126
  # Used to avoid errors in fieldset selection parsing and to enable a
@@ -136,4 +163,11 @@ module CrudUtils
136
163
  uri.query = Rack::Utils.build_query(query)
137
164
  uri.to_s
138
165
  end
166
+
167
+ # Only allow a trusted parameter "white list" through,
168
+ # based on resource's schema.
169
+ def object_params
170
+ params.require(resource.name.underscore.to_sym)
171
+ .permit(resource_params)
172
+ end
139
173
  end
@@ -39,7 +39,8 @@ module Orderable
39
39
  attr.match(/\A[+-]/).nil? ? '+': attr.slice!(0)
40
40
  end
41
41
 
42
+ # Gets class of the resource of the current request
42
43
  def model
43
- representative_resource.classify.constantize
44
+ (params[:nested] || params[:resource] || controller_name).classify.constantize
44
45
  end
45
46
  end
@@ -27,17 +27,23 @@ module SqlSecurity
27
27
 
28
28
  # Check if request is a sql injection
29
29
  def sql_injection(klass)
30
- apicasso_parameters.each do |key, value|
31
- if key.to_sym == :group
32
- return false unless group_sql_safe?(klass, value)
33
- else
34
- return false unless parameters_sql_safe?(klass, value)
35
- end
30
+ apicasso_parameters.each do |name, value|
31
+ next if safe_parameter?(klass, name, value)
32
+
33
+ return false
36
34
  end
37
35
  end
38
36
 
39
37
  private
40
38
 
39
+ def safe_parameter?(klass, name, value)
40
+ if name.to_sym == :group
41
+ group_sql_safe?(klass, value)
42
+ else
43
+ parameters_sql_safe?(klass, value)
44
+ end
45
+ end
46
+
41
47
  # Check for SQL injection before requests and
42
48
  # raise a exception when find
43
49
  def bad_request?
@@ -3,5 +3,5 @@
3
3
  # A Module to rule them all...
4
4
  module Apicasso
5
5
  # Current gem version
6
- VERSION = '0.6.5'.freeze
6
+ VERSION = '0.6.6'.freeze
7
7
  end
@@ -29,7 +29,7 @@ module Apicasso
29
29
 
30
30
  # Create an initializer with CORS configuration to Apicasso
31
31
  def copy_initializer
32
- copy_file 'apicasso.rb', 'config/initalizers/apicasso.rb'
32
+ copy_file 'apicasso.rb', 'config/initializers/apicasso.rb'
33
33
  end
34
34
  end
35
35
  end