Class: JSONAPI::Authorization::AuthorizingProcessor

Inherits:
Processor
  • Object
show all
Defined in:
lib/jsonapi/authorization/authorizing_processor.rb

Instance Method Summary collapse

Instance Method Details

#authorize_create_resourceObject



127
128
129
130
131
132
133
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 127

def authorize_create_resource
  source_class = resource_klass._model_class
  authorizer.create_resource(
    source_class: source_class,
    related_records_with_context: related_models_with_context
  )
end

#authorize_create_to_many_relationshipsObject



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 170

def authorize_create_to_many_relationships
  source_record = @resource_klass.find_by_key(
    params[:resource_id],
    context: context
  )._model

  relationship_type = params[:relationship_type].to_sym
  related_models = model_class_for_relationship(relationship_type).find(params[:data])

  authorizer.create_to_many_relationship(
    source_record: source_record,
    new_related_records: related_models,
    relationship_type: relationship_type
  )
end

#authorize_findObject



51
52
53
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 51

def authorize_find
  authorizer.find(source_class: @resource_klass._model_class)
end

#authorize_include_directiveObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 36

def authorize_include_directive
  return if result.is_a?(::JSONAPI::ErrorsOperationResult)
  resources = Array.wrap(
    if result.respond_to?(:resources)
      result.resources
    elsif result.respond_to?(:resource)
      result.resource
    end
  )

  resources.each do |resource|
    authorize_model_includes(resource._model)
  end
end

#authorize_remove_resourceObject



135
136
137
138
139
140
141
142
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 135

def authorize_remove_resource
  record = @resource_klass.find_by_key(
    operation_resource_id,
    context: context
  )._model

  authorizer.remove_resource(source_record: record)
end

#authorize_remove_to_many_relationshipsObject



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 203

def authorize_remove_to_many_relationships
  source_resource = @resource_klass.find_by_key(
    params[:resource_id],
    context: context
  )
  source_record = source_resource._model

  relationship_type = params[:relationship_type].to_sym

  related_resources = @resource_klass
    ._relationship(relationship_type)
    .resource_klass
    .find_by_keys(
      params[:associated_keys],
      context: context
    )

  related_records = related_resources.map(&:_model)

  if related_records.size != params[:associated_keys].uniq.size
    fail JSONAPI::Exceptions::RecordNotFound, params[:associated_keys]
  end

  authorizer.remove_to_many_relationship(
    source_record: source_record,
    related_records: related_records,
    relationship_type: relationship_type
  )
end

#authorize_remove_to_one_relationshipObject



233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 233

def authorize_remove_to_one_relationship
  source_record = @resource_klass.find_by_key(
    params[:resource_id],
    context: context
  )._model

  relationship_type = params[:relationship_type].to_sym

  authorizer.remove_to_one_relationship(
    source_record: source_record, relationship_type: relationship_type
  )
end

#authorize_replace_fieldsObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 116

def authorize_replace_fields
  source_record = @resource_klass.find_by_key(
    params[:resource_id],
    context: context
  )._model
  authorizer.replace_fields(
    source_record: source_record,
    related_records_with_context: related_models_with_context
  )
end

#authorize_replace_polymorphic_to_one_relationshipObject



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 246

def authorize_replace_polymorphic_to_one_relationship
  return authorize_remove_to_one_relationship if params[:key_value].nil?

  source_resource = @resource_klass.find_by_key(
    params[:resource_id],
    context: context
  )
  source_record = source_resource._model

  # Fetch the name of the new class based on the incoming polymorphic
  # "type" value. This will fail if there is no associated resource for the
  # incoming "type" value so this shouldn't leak constants
  related_record_class_name = source_resource
    .send(:_model_class_name, params[:key_type])

  # Fetch the underlying Resource class for the new record to-be-associated
  related_resource_klass = @resource_klass.resource_for(related_record_class_name)

  new_related_resource = related_resource_klass
    .find_by_key(
      params[:key_value],
      context: context
    )
  new_related_record = new_related_resource._model unless new_related_resource.nil?

  relationship_type = params[:relationship_type].to_sym
  authorizer.replace_to_one_relationship(
    source_record: source_record,
    new_related_record: new_related_record,
    relationship_type: relationship_type
  )
end

#authorize_replace_to_many_relationshipsObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 186

def authorize_replace_to_many_relationships
  source_resource = @resource_klass.find_by_key(
    params[:resource_id],
    context: context
  )
  source_record = source_resource._model

  relationship_type = params[:relationship_type].to_sym
  new_related_records = model_class_for_relationship(relationship_type).find(params[:data])

  authorizer.replace_to_many_relationship(
    source_record: source_record,
    new_related_records: new_related_records,
    relationship_type: relationship_type
  )
end

#authorize_replace_to_one_relationshipObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 144

def authorize_replace_to_one_relationship
  return authorize_remove_to_one_relationship if params[:key_value].nil?

  source_resource = @resource_klass.find_by_key(
    params[:resource_id],
    context: context
  )
  source_record = source_resource._model

  relationship_type = params[:relationship_type].to_sym
  new_related_resource = @resource_klass
    ._relationship(relationship_type)
    .resource_klass
    .find_by_key(
      params[:key_value],
      context: context
    )
  new_related_record = new_related_resource._model unless new_related_resource.nil?

  authorizer.replace_to_one_relationship(
    source_record: source_record,
    new_related_record: new_related_record,
    relationship_type: relationship_type
  )
end

#authorize_showObject



55
56
57
58
59
60
61
62
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 55

def authorize_show
  record = @resource_klass.find_by_key(
    operation_resource_id,
    context: context
  )._model

  authorizer.show(source_record: record)
end


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 87

def authorize_show_related_resource
  source_klass = params[:source_klass]
  source_id = params[:source_id]
  relationship_type = params[:relationship_type].to_sym

  source_resource = source_klass.find_by_key(source_id, context: context)

  related_resource = source_resource.public_send(relationship_type)

  source_record = source_resource._model
  related_record = related_resource._model unless related_resource.nil?
  authorizer.show_related_resource(
    source_record: source_record, related_record: related_record
  )
end


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 103

def authorize_show_related_resources
  source_resource = params[:source_klass].find_by_key(
    params[:source_id],
    context: context
  )

  source_record = source_resource._model

  authorizer.show_related_resources(
    source_record: source_record, related_record_class: @resource_klass._model_class
  )
end

#authorize_show_relationshipObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 64

def authorize_show_relationship
  parent_resource = @resource_klass.find_by_key(
    params[:parent_key],
    context: context
  )

  relationship = @resource_klass._relationship(params[:relationship_type].to_sym)

  related_resource =
    case relationship
    when JSONAPI::Relationship::ToOne
      parent_resource.public_send(params[:relationship_type].to_sym)
    when JSONAPI::Relationship::ToMany
      # Do nothing — already covered by policy scopes
    else
      raise "Unexpected relationship type: #{relationship.inspect}"
    end

  parent_record = parent_resource._model
  related_record = related_resource._model unless related_resource.nil?
  authorizer.show_relationship(source_record: parent_record, related_record: related_record)
end