model_driven_api 3.20.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7ab2b6a7c7c7384c9decd7ab6491ba6daa6f31a0d3fbfc70c5aa65f61ee609d9
4
- data.tar.gz: 2516e2a385ac48e62dff7e5a4c5d7f9863bbd8fb474ebcc3af98fae1b4d99d6c
3
+ metadata.gz: 0ca2a10c8d05b6fe101faaf4c5e1fa3a22901b93010db36f55e23d0ddee1e2eb
4
+ data.tar.gz: 9581d056a82ed21a8c5f3a66d2a65abba620ab742627a00233359e76454fcfa4
5
5
  SHA512:
6
- metadata.gz: 5bdfab13321344419715fd92b297353803c527b7f99280c461280cf8f97cb25f6e263feda93c3ca40805874fb884d5805f40a55909971c7434424fcbf663c22e
7
- data.tar.gz: a5f9525d01d41c1cec3353bbc651f014a3c4612108a8b27a15da8dc61a43b58a76c177c3a48713e4119f2b0031792d661341eed2e1204fb64b5db5a26c987529
6
+ metadata.gz: 14ea7c336c4d4c2597504807c164c4154ee585bc72f9447b326946c1c4bb53513a499ef1acdcbf68bde0c938a7847ce5c80bcf32f1379388370e743b6513c946
7
+ data.tar.gz: e3fadefce9aef3f6ffd9995a958a3e94fb718039909c625e32cd7e6c6a73decc9e30ef3c8c8aff274a4026ece9043bc3c97ab28e12781c49ea91f39d37fe43be
@@ -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: (status_number.presence || 200) if status == true
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 = @model.ransack(@query.presence || params[:q])
31
- page = (@page.presence || params[:page])
32
- per = (@per.presence || params[: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 = (@count.presence || params[: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: { count: @records_count } if !count.blank?
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, @record_id.presence || @model unless custom_action?
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: (status_number.presence || 200) if status == true
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: (status_number.presence || 200) if status == true
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: (status_number.presence || 200) if status == true
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: (status_number.presence || 200) if status == true
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
- ("Endpoints::#{model_name}".constantize rescue nil)
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 && header.match(pattern)
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
- return klass.is_a?(Class)
198
+ klass.is_a?(Class)
180
199
  rescue NameError
181
- return false
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({ message: @auth_errors })) unless @current_user
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
- record_id ||= (params[:path].split("/").second.to_i rescue nil)
208
- # Keeping this automation can be too dangerous and lead to unpredicted results
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
- @json_attrs.presence || @model.json_attrs.presence || {} rescue {}
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 = (params[@model.model_name.singular].presence || params[@model.model_name.route_key]) rescue params
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: (status_number.presence || 200) if status == true
8
+ return render json: result, status: status_number.presence || 200 if status == true
9
9
 
10
- scope = apply_filters(@model.all)
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: { total: pagy.count }),
17
- status: :ok
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: (status_number.presence || 200) if status == true
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: (status_number.presence || 200) if status == true
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: (status_number.presence || 200) if status == true
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: (status_number.presence || 200) if status == true
60
+ return render json: result, status: status_number.presence || 200 if status == true
61
61
 
62
62
  @record.destroy!
63
63
  head :no_content
@@ -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 StandardError => e
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
  []
@@ -180,7 +180,7 @@ module Api
180
180
  # restore it so neither the next field nor the caller sees a value that was never saved.
181
181
  snapshot = @attributes.deep_dup if is_a?(ActiveRecord::Base)
182
182
  hash[name] = ActiveRecord::Base.while_preventing_writes { send(name) }
183
- rescue StandardError => e
183
+ rescue => e
184
184
  @attributes = snapshot if is_a?(ActiveRecord::Base)
185
185
  RequestJsonAttrs.drop(self.class, name, "raised #{e.class}")
186
186
  end
@@ -1,3 +1,3 @@
1
1
  module ModelDrivenApi
2
- VERSION = "3.20.0".freeze
2
+ VERSION = "3.21.0".freeze
3
3
  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.20.0
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-26 00:00:00.000000000 Z
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