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



130
131
132
133
134
135
136
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 130

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



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

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



54
55
56
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 54

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

#authorize_include_directiveObject



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

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



138
139
140
141
142
143
144
145
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 138

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



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
232
233
234
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 206

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
    raise 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



236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 236

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



119
120
121
122
123
124
125
126
127
128
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 119

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



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
278
279
280
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 249

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



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

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



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

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



58
59
60
61
62
63
64
65
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 58

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

  authorizer.show(source_record: record)
end


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

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


106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/jsonapi/authorization/authorizing_processor.rb', line 106

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



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

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