model_driven_api 3.19.0 → 3.21.0
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/README.md +1 -1
- data/app/controllers/api/v2/application_controller.rb +54 -30
- data/app/controllers/api/v3/application_controller.rb +8 -8
- data/lib/api/hidden_secrets.rb +1 -1
- data/lib/api/v2/request_json_attrs.rb +14 -2
- data/lib/model_driven_api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0ca2a10c8d05b6fe101faaf4c5e1fa3a22901b93010db36f55e23d0ddee1e2eb
|
|
4
|
+
data.tar.gz: 9581d056a82ed21a8c5f3a66d2a65abba620ab742627a00233359e76454fcfa4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 14ea7c336c4d4c2597504807c164c4154ee585bc72f9447b326946c1c4bb53513a499ef1acdcbf68bde0c938a7847ce5c80bcf32f1379388370e743b6513c946
|
|
7
|
+
data.tar.gz: e3fadefce9aef3f6ffd9995a958a3e94fb718039909c625e32cd7e6c6a73decc9e30ef3c8c8aff274a4026ece9043bc3c97ab28e12781c49ea91f39d37fe43be
|
data/README.md
CHANGED
|
@@ -195,7 +195,7 @@ Parameters work identically in query string (GET) and JSON body (POST search).
|
|
|
195
195
|
}
|
|
196
196
|
```
|
|
197
197
|
|
|
198
|
-
The client's options follow conventions (ADR 0006): `include` takes real associations only; secrets the model hides (a secret-named column left out of its default JSON, its `json_attrs[:except]` or `write_only_attributes`, e.g. Devise's `encrypted_password`) never come back, in v2, v3 or Ransack filters and sorts; `methods` serves what the model declares in `json_attrs[:methods]`, columns, associations, `<association>_ids`, and public methods written in the app or a thecore gem (not ending in `!`/`=`, no required arguments), which run read-only. Anything else is dropped from the response and logged; the response stays 200.
|
|
198
|
+
The client's options follow conventions (ADR 0006): `include` takes real associations only; secrets the model hides (a secret-named column left out of its default JSON, its `json_attrs[:except]` or `write_only_attributes`, e.g. Devise's `encrypted_password`) never come back, in v2, v3 or Ransack filters and sorts; `methods` serves what the model declares in `json_attrs[:methods]`, columns, associations, `<association>_ids`, and public methods written in the app or a thecore gem (not a model callback, not ending in `!`/`=`, no required arguments), which run read-only. Anything else is dropped from the response and logged; the response stays 200.
|
|
199
199
|
|
|
200
200
|
#### Ransack filtering (`q`)
|
|
201
201
|
|
|
@@ -20,18 +20,18 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
20
20
|
|
|
21
21
|
# Custom Action
|
|
22
22
|
status, result, status_number = check_for_custom_action
|
|
23
|
-
return render json: result, status:
|
|
23
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
24
24
|
|
|
25
25
|
# Normal Index Action with Ransack querying
|
|
26
26
|
# Keeping this automation can be too dangerous and lead to unpredicted results
|
|
27
27
|
# TODO: Remove it
|
|
28
28
|
# @q = (@model.column_names.include?("user_id") ? @model.where(user_id: current_user.id) : @model).ransack(@query.presence|| params[:q])
|
|
29
29
|
Rails.logger.debug("Querying for #{@model} with #{@query}: #{@query.presence || params[:q]}")
|
|
30
|
-
@q =
|
|
31
|
-
page =
|
|
32
|
-
per =
|
|
30
|
+
@q = authorized_scope(:index).ransack(@query.presence || params[:q])
|
|
31
|
+
page = @page.presence || params[:page]
|
|
32
|
+
per = @per.presence || params[:per]
|
|
33
33
|
# pages_info = (@pages_info.presence || params[:pages_info])
|
|
34
|
-
count =
|
|
34
|
+
count = @count.presence || params[:count]
|
|
35
35
|
@records_count = @q.result.length
|
|
36
36
|
Rails.logger.debug("Found #{@records_count.inspect} records")
|
|
37
37
|
@records_all = @q.result
|
|
@@ -43,24 +43,24 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
43
43
|
response.set_header("Content-Range", "#{@model.table_name} #{range_start}-#{range_end}/#{@records.total_count}")
|
|
44
44
|
|
|
45
45
|
# puts "ALL RECORDS FOUND: #{@records_all.inspect}"
|
|
46
|
-
status = @records_count.zero? && params[:always_ok].blank? ? 404 : 200
|
|
46
|
+
status = (@records_count.zero? && params[:always_ok].blank?) ? 404 : 200
|
|
47
47
|
# puts "If it's asked for page number, then paginate"
|
|
48
48
|
return render json: @records.as_json(json_attrs), status: status if !page.blank? # (@json_attrs || {})
|
|
49
|
-
#puts "if you ask for count, then return a json object with just the number of objects"
|
|
50
|
-
return render json: {
|
|
51
|
-
#puts "Default"
|
|
49
|
+
# puts "if you ask for count, then return a json object with just the number of objects"
|
|
50
|
+
return render json: {count: @records_count} if !count.blank?
|
|
51
|
+
# puts "Default"
|
|
52
52
|
json_out = @records_all.as_json(json_attrs)
|
|
53
|
-
#puts "JSON ATTRS: #{json_attrs}"
|
|
54
|
-
#puts "JSON OUT: #{json_out}"
|
|
55
|
-
render json: json_out, status: status #(@json_attrs || {})
|
|
53
|
+
# puts "JSON ATTRS: #{json_attrs}"
|
|
54
|
+
# puts "JSON OUT: #{json_out}"
|
|
55
|
+
render json: json_out, status: status # (@json_attrs || {})
|
|
56
56
|
end
|
|
57
57
|
|
|
58
58
|
def show
|
|
59
|
-
authorize! :show, @
|
|
59
|
+
authorize! :show, @record unless custom_action?
|
|
60
60
|
|
|
61
61
|
# Custom Show Action
|
|
62
62
|
status, result, status_number = check_for_custom_action
|
|
63
|
-
return render json: result, status:
|
|
63
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
64
64
|
|
|
65
65
|
# Normal Show
|
|
66
66
|
result = @record.to_json(json_attrs)
|
|
@@ -73,7 +73,7 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
73
73
|
authorize! :create, @record.presence || @model unless custom_action?
|
|
74
74
|
# Custom Action
|
|
75
75
|
status, result, status_number = check_for_custom_action
|
|
76
|
-
return render json: result, status:
|
|
76
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
77
77
|
# Keeping this automation can be too dangerous and lead to unpredicted results
|
|
78
78
|
# TODO: Remove it
|
|
79
79
|
# @record.user_id = current_user.id if @model.column_names.include? "user_id"
|
|
@@ -86,7 +86,7 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
86
86
|
|
|
87
87
|
# Custom Action
|
|
88
88
|
status, result, status_number = check_for_custom_action
|
|
89
|
-
return render json: result, status:
|
|
89
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
90
90
|
|
|
91
91
|
# Normal Update Action
|
|
92
92
|
# Use save! to be sure to raise an exception if the record is not valid and to trigger all the callbacks for the model during update
|
|
@@ -102,6 +102,7 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
102
102
|
def update_multi
|
|
103
103
|
authorize! :update, @model
|
|
104
104
|
ids = params[:ids].split(",")
|
|
105
|
+
authorize_each!(:update, ids)
|
|
105
106
|
@model.where(id: ids).update!(@body)
|
|
106
107
|
render json: ids.to_json, status: 200
|
|
107
108
|
end
|
|
@@ -111,7 +112,7 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
111
112
|
|
|
112
113
|
# Custom Action
|
|
113
114
|
status, result, status_number = check_for_custom_action
|
|
114
|
-
return render json: result, status:
|
|
115
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
115
116
|
|
|
116
117
|
# Normal Destroy Action
|
|
117
118
|
# A validation/before_destroy guard (e.g. dependent: :restrict_with_error) blocking this
|
|
@@ -125,12 +126,26 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
125
126
|
|
|
126
127
|
# Normal Destroy Action
|
|
127
128
|
ids = params[:ids].split(",")
|
|
129
|
+
authorize_each!(:destroy, ids)
|
|
128
130
|
@model.where(id: ids).destroy!(@body)
|
|
129
131
|
render json: ids.to_json, status: 200
|
|
130
132
|
end
|
|
131
133
|
|
|
132
134
|
private
|
|
133
135
|
|
|
136
|
+
# The records of @model the caller may `action`. CanCanCan ignores a rule's conditions when it
|
|
137
|
+
# authorizes a class (`authorize! :index, @model` passes for `can :read, Thing, user_id: user.id`),
|
|
138
|
+
# so a query that lists records must be scoped by them too. Rules without conditions (the
|
|
139
|
+
# Role/Permission ones, admins' `can :manage, :all`) match every record and leave it unscoped.
|
|
140
|
+
def authorized_scope(action)
|
|
141
|
+
@model.accessible_by(current_ability, action)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
# Checks every record before touching any, so a refused one leaves all of them unchanged.
|
|
145
|
+
def authorize_each!(action, ids)
|
|
146
|
+
@model.where(id: ids).find_each { |record| authorize! action, record }
|
|
147
|
+
end
|
|
148
|
+
|
|
134
149
|
# Returns true for any custom action request (public or authenticated).
|
|
135
150
|
# Custom actions are self-contained and handle their own authorization logic;
|
|
136
151
|
# the generic CanCan model-level check is not applicable to them.
|
|
@@ -146,7 +161,11 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
146
161
|
model_name = params[:ctrl].to_s.classify
|
|
147
162
|
action_name = params[:action_name].to_s
|
|
148
163
|
# Ensure the endpoint class is loaded so its public_action declarations are registered.
|
|
149
|
-
|
|
164
|
+
begin
|
|
165
|
+
"Endpoints::#{model_name}".constantize
|
|
166
|
+
rescue
|
|
167
|
+
nil
|
|
168
|
+
end
|
|
150
169
|
NonCrudEndpoints.public_action?(model_name, action_name)
|
|
151
170
|
end
|
|
152
171
|
|
|
@@ -171,14 +190,14 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
171
190
|
def bearer_token
|
|
172
191
|
pattern = /^Bearer /
|
|
173
192
|
header = request.headers["Authorization"]
|
|
174
|
-
header.gsub(pattern, "") if header
|
|
193
|
+
header.gsub(pattern, "") if header&.match(pattern)
|
|
175
194
|
end
|
|
176
195
|
|
|
177
196
|
def class_exists?(class_name)
|
|
178
197
|
klass = Module.const_get(class_name)
|
|
179
|
-
|
|
198
|
+
klass.is_a?(Class)
|
|
180
199
|
rescue NameError
|
|
181
|
-
|
|
200
|
+
false
|
|
182
201
|
end
|
|
183
202
|
|
|
184
203
|
def authenticate_request
|
|
@@ -194,7 +213,7 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
194
213
|
Rails.logger.debug("Checking for authorization with AuthorizeApiRequest if current_user not already present -> current_user: #{@current_user}")
|
|
195
214
|
|
|
196
215
|
check_authorization AuthorizeApiRequest.call(request) unless @current_user
|
|
197
|
-
return unauthenticated!(OpenStruct.new({
|
|
216
|
+
return unauthenticated!(OpenStruct.new({message: @auth_errors})) unless @current_user
|
|
198
217
|
|
|
199
218
|
current_user = @current_user
|
|
200
219
|
params[:current_user_id] = @current_user.id
|
|
@@ -203,13 +222,10 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
203
222
|
response.set_header("Token", JsonWebToken.encode(user_id: current_user.id))
|
|
204
223
|
end
|
|
205
224
|
|
|
225
|
+
# The actions using @record check it against the caller's abilities (`authorize! action, @record`).
|
|
206
226
|
def find_record
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
# TODO: Remove it
|
|
210
|
-
# @record = @model.column_names.include?("user_id") ? @model.where(id: (record_id.presence || @record_id.presence || params[:id]), user_id: current_user.id).first : @model.find((@record_id.presence || params[:id]))
|
|
211
|
-
@record = @model.find((@record_id.presence || params[:id]))
|
|
212
|
-
return not_found! if @record.blank?
|
|
227
|
+
@record = @model.find(params[:id])
|
|
228
|
+
not_found! if @record.blank?
|
|
213
229
|
end
|
|
214
230
|
|
|
215
231
|
# A model may declare `self.creation_key`: the columns identifying a record its client
|
|
@@ -277,7 +293,11 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
277
293
|
from_params = nil
|
|
278
294
|
JSON_ATTRS_PARAMS.each { |key| from_params = params[key].deep_symbolize_keys if params[key].present? }
|
|
279
295
|
return Api::V2::RequestJsonAttrs.from_params(from_params) if from_params.present?
|
|
280
|
-
|
|
296
|
+
begin
|
|
297
|
+
@json_attrs.presence || @model.json_attrs.presence || {}
|
|
298
|
+
rescue
|
|
299
|
+
{}
|
|
300
|
+
end
|
|
281
301
|
end
|
|
282
302
|
|
|
283
303
|
def extract_model
|
|
@@ -285,7 +305,11 @@ class Api::V2::ApplicationController < ActionController::API
|
|
|
285
305
|
# A catchall or custom-action path that names no class: nothing can serve it (the CRUD and
|
|
286
306
|
# the dispatcher need a class). Info endpoints resolve no model on purpose and carry neither param.
|
|
287
307
|
return not_found! if @model.nil? && (params[:path].present? || params[:ctrl].present?)
|
|
288
|
-
@body =
|
|
308
|
+
@body = begin
|
|
309
|
+
params[@model.model_name.singular].presence || params[@model.model_name.route_key]
|
|
310
|
+
rescue
|
|
311
|
+
params
|
|
312
|
+
end
|
|
289
313
|
rescue Api::ModelResolver::NotFound
|
|
290
314
|
not_found!
|
|
291
315
|
end
|
|
@@ -5,23 +5,23 @@ class Api::V3::ApplicationController < Api::V2::ApplicationController
|
|
|
5
5
|
authorize! :index, @model unless custom_action?
|
|
6
6
|
|
|
7
7
|
status, result, status_number = check_for_custom_action
|
|
8
|
-
return render json: result, status:
|
|
8
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
9
9
|
|
|
10
|
-
scope = apply_filters(
|
|
10
|
+
scope = apply_filters(authorized_scope(:index))
|
|
11
11
|
scope = apply_sorting(scope)
|
|
12
12
|
|
|
13
13
|
pagy, records = pagy(scope, page: page_number, limit: page_size)
|
|
14
14
|
|
|
15
15
|
serializer = Api::V3::SerializerFactory.serializer_for(@model)
|
|
16
|
-
render json: serializer.new(records, **serializer_opts).serializable_hash.merge(meta: {
|
|
17
|
-
|
|
16
|
+
render json: serializer.new(records, **serializer_opts).serializable_hash.merge(meta: {total: pagy.count}),
|
|
17
|
+
status: :ok
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def show
|
|
21
21
|
authorize! :show, @record unless custom_action?
|
|
22
22
|
|
|
23
23
|
status, result, status_number = check_for_custom_action
|
|
24
|
-
return render json: result, status:
|
|
24
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
25
25
|
|
|
26
26
|
serializer = Api::V3::SerializerFactory.serializer_for(@model)
|
|
27
27
|
render json: serializer.new(@record, **serializer_opts).serializable_hash, status: :ok
|
|
@@ -31,7 +31,7 @@ class Api::V3::ApplicationController < Api::V2::ApplicationController
|
|
|
31
31
|
authorize! :create, @model unless custom_action?
|
|
32
32
|
|
|
33
33
|
status, result, status_number = check_for_custom_action
|
|
34
|
-
return render json: result, status:
|
|
34
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
35
35
|
|
|
36
36
|
record = @model.new(jsonapi_attributes)
|
|
37
37
|
create_once(record) do
|
|
@@ -44,7 +44,7 @@ class Api::V3::ApplicationController < Api::V2::ApplicationController
|
|
|
44
44
|
authorize! :update, @record unless custom_action?
|
|
45
45
|
|
|
46
46
|
status, result, status_number = check_for_custom_action
|
|
47
|
-
return render json: result, status:
|
|
47
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
48
48
|
|
|
49
49
|
@record.update!(jsonapi_attributes)
|
|
50
50
|
serializer = Api::V3::SerializerFactory.serializer_for(@model)
|
|
@@ -57,7 +57,7 @@ class Api::V3::ApplicationController < Api::V2::ApplicationController
|
|
|
57
57
|
authorize! :destroy, @record unless custom_action?
|
|
58
58
|
|
|
59
59
|
status, result, status_number = check_for_custom_action
|
|
60
|
-
return render json: result, status:
|
|
60
|
+
return render json: result, status: status_number.presence || 200 if status == true
|
|
61
61
|
|
|
62
62
|
@record.destroy!
|
|
63
63
|
head :no_content
|
data/lib/api/hidden_secrets.rb
CHANGED
|
@@ -30,7 +30,7 @@ module Api
|
|
|
30
30
|
|
|
31
31
|
def left_out_by_default(klass)
|
|
32
32
|
klass.column_names - klass.new.serializable_hash.keys
|
|
33
|
-
rescue
|
|
33
|
+
rescue => e
|
|
34
34
|
Rails.logger.warn("[model_driven_api] HiddenSecrets: #{klass.name}.new failed (#{e.class}), " \
|
|
35
35
|
"secrets hidden by its serializable_hash are not detected")
|
|
36
36
|
[]
|
|
@@ -9,7 +9,8 @@ module Api
|
|
|
9
9
|
# serializable_hash, e.g. Devise's encrypted_password, LdapServer#admin_password) never come back;
|
|
10
10
|
# - a `methods` name declared in the class's json_attrs[:methods] runs as before (a developer
|
|
11
11
|
# exposed it); a column or association reader (or `<assoc>_ids`) is a plain read; any other
|
|
12
|
-
# name must be public,
|
|
12
|
+
# name must be public, not a callback of the class (host Odoo #6314), end in neither `!` nor
|
|
13
|
+
# `=`, take no required argument and be written in
|
|
13
14
|
# the host or in a thecore gem (a gem named thecore* or depending on one), and it runs under
|
|
14
15
|
# the read-only guard: if it writes, or raises, only that field is dropped.
|
|
15
16
|
#
|
|
@@ -90,6 +91,7 @@ module Api
|
|
|
90
91
|
return :reader if reader?(klass, name)
|
|
91
92
|
return "ends in ! or =" if name.end_with?("!", "=")
|
|
92
93
|
return "not a public method" unless klass.public_method_defined?(name)
|
|
94
|
+
return "a callback" if callback?(klass, name)
|
|
93
95
|
|
|
94
96
|
method = klass.instance_method(name)
|
|
95
97
|
return "takes required arguments" if method.parameters.any? { |type, _| %i[req keyreq].include?(type) }
|
|
@@ -106,6 +108,16 @@ module Api
|
|
|
106
108
|
|
|
107
109
|
def active_record?(klass) = klass < ActiveRecord::Base
|
|
108
110
|
|
|
111
|
+
# Registered as a Symbol filter in any of the class's callback chains (validation, validate,
|
|
112
|
+
# save, create, update, destroy, commit, rollback, initialize, find, touch, and any custom
|
|
113
|
+
# chain). A callback belongs to the record's lifecycle, never to its API, and its side
|
|
114
|
+
# effects may live outside the database, where the read-only guard can't see them.
|
|
115
|
+
def callback?(klass, name)
|
|
116
|
+
return false unless klass.respond_to?(:__callbacks)
|
|
117
|
+
|
|
118
|
+
klass.__callbacks.each_value.any? { |chain| chain.any? { |cb| cb.filter.is_a?(Symbol) && cb.filter.to_s == name } }
|
|
119
|
+
end
|
|
120
|
+
|
|
109
121
|
def reader?(klass, name)
|
|
110
122
|
return false unless active_record?(klass)
|
|
111
123
|
return true if klass.column_names.include?(name)
|
|
@@ -168,7 +180,7 @@ module Api
|
|
|
168
180
|
# restore it so neither the next field nor the caller sees a value that was never saved.
|
|
169
181
|
snapshot = @attributes.deep_dup if is_a?(ActiveRecord::Base)
|
|
170
182
|
hash[name] = ActiveRecord::Base.while_preventing_writes { send(name) }
|
|
171
|
-
rescue
|
|
183
|
+
rescue => e
|
|
172
184
|
@attributes = snapshot if is_a?(ActiveRecord::Base)
|
|
173
185
|
RequestJsonAttrs.drop(self.class, name, "raised #{e.class}")
|
|
174
186
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: model_driven_api
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 3.
|
|
4
|
+
version: 3.21.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gabriele Tassoni
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-12-05 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: thecore_backend_commons
|