Skip to content

Commit 1853056

Browse files
nruthvalscion
authored andcommitted
Default pundit authorizer keyword args (venuu#95)
* add keyword args to a couple of methods in DefaultPunditAuthorizer * refactor authorization_stubs duplication with tap there may be a better way to combine the splat and keyword arguments in the #with call, but I can't see what it is * rubocop linting * set ruby version for rubocop to get rid of ruby 2.0 errors * reduce readability (use ternary op) to make rubocop happy * #find kwarg * #show kwargs * show_related_resource kwargs * #show_related_resources kwargs * #remove_resource kwargs * add all the other methods, but not #new * use kwargs for #new context: too * remove the AuthorizationStubs tap-dance now that positional *args have been removed from the authorizer method signatures
1 parent 868a341 commit 1853056

File tree

10 files changed

+359
-157
lines changed

10 files changed

+359
-157
lines changed

.rubocop.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
AllCops:
2+
TargetRubyVersion: 2.1
23
Exclude:
34
- 'bin/*'
45
- 'spec/dummy/db/schema.rb'

lib/jsonapi/authorization/authorizing_processor.rb

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def authorize_include_directive
4949
end
5050

5151
def authorize_find
52-
authorizer.find(@resource_klass._model_class)
52+
authorizer.find(source_class: @resource_klass._model_class)
5353
end
5454

5555
def authorize_show
@@ -58,7 +58,7 @@ def authorize_show
5858
context: context
5959
)._model
6060

61-
authorizer.show(record)
61+
authorizer.show(source_record: record)
6262
end
6363

6464
def authorize_show_relationship
@@ -81,7 +81,7 @@ def authorize_show_relationship
8181

8282
parent_record = parent_resource._model
8383
related_record = related_resource._model unless related_resource.nil?
84-
authorizer.show_relationship(parent_record, related_record)
84+
authorizer.show_relationship(source_record: parent_record, related_record: related_record)
8585
end
8686

8787
def authorize_show_related_resource
@@ -95,7 +95,9 @@ def authorize_show_related_resource
9595

9696
source_record = source_resource._model
9797
related_record = related_resource._model unless related_resource.nil?
98-
authorizer.show_related_resource(source_record, related_record)
98+
authorizer.show_related_resource(
99+
source_record: source_record, related_record: related_record
100+
)
99101
end
100102

101103
def authorize_show_related_resources
@@ -104,20 +106,26 @@ def authorize_show_related_resources
104106
context: context
105107
)._model
106108

107-
authorizer.show_related_resources(source_record)
109+
authorizer.show_related_resources(source_record: source_record)
108110
end
109111

110112
def authorize_replace_fields
111113
source_record = @resource_klass.find_by_key(
112114
params[:resource_id],
113115
context: context
114116
)._model
115-
authorizer.replace_fields(source_record, related_models_with_context)
117+
authorizer.replace_fields(
118+
source_record: source_record,
119+
related_records_with_context: related_models_with_context
120+
)
116121
end
117122

118123
def authorize_create_resource
119124
source_class = resource_klass._model_class
120-
authorizer.create_resource(source_class, related_models_with_context)
125+
authorizer.create_resource(
126+
source_class: source_class,
127+
related_records_with_context: related_models_with_context
128+
)
121129
end
122130

123131
def authorize_remove_resource
@@ -126,7 +134,7 @@ def authorize_remove_resource
126134
context: context
127135
)._model
128136

129-
authorizer.remove_resource(record)
137+
authorizer.remove_resource(source_record: record)
130138
end
131139

132140
def authorize_replace_to_one_relationship
@@ -149,9 +157,9 @@ def authorize_replace_to_one_relationship
149157
new_related_record = new_related_resource._model unless new_related_resource.nil?
150158

151159
authorizer.replace_to_one_relationship(
152-
source_record,
153-
new_related_record,
154-
relationship_type
160+
source_record: source_record,
161+
new_related_record: new_related_record,
162+
relationship_type: relationship_type
155163
)
156164
end
157165

@@ -164,7 +172,11 @@ def authorize_create_to_many_relationships
164172
relationship_type = params[:relationship_type].to_sym
165173
related_models = model_class_for_relationship(relationship_type).find(params[:data])
166174

167-
authorizer.create_to_many_relationship(source_record, related_models, relationship_type)
175+
authorizer.create_to_many_relationship(
176+
source_record: source_record,
177+
new_related_records: related_models,
178+
relationship_type: relationship_type
179+
)
168180
end
169181

170182
def authorize_replace_to_many_relationships
@@ -178,9 +190,9 @@ def authorize_replace_to_many_relationships
178190
new_related_records = model_class_for_relationship(relationship_type).find(params[:data])
179191

180192
authorizer.replace_to_many_relationship(
181-
source_record,
182-
new_related_records,
183-
relationship_type
193+
source_record: source_record,
194+
new_related_records: new_related_records,
195+
relationship_type: relationship_type
184196
)
185197
end
186198

@@ -204,9 +216,9 @@ def authorize_remove_to_many_relationships
204216
related_records = related_resources.map(&:_model)
205217

206218
authorizer.remove_to_many_relationship(
207-
source_record,
208-
related_records,
209-
relationship_type
219+
source_record: source_record,
220+
related_records: related_records,
221+
relationship_type: relationship_type
210222
)
211223
end
212224

@@ -218,7 +230,9 @@ def authorize_remove_to_one_relationship
218230

219231
relationship_type = params[:relationship_type].to_sym
220232

221-
authorizer.remove_to_one_relationship(source_record, relationship_type)
233+
authorizer.remove_to_one_relationship(
234+
source_record: source_record, relationship_type: relationship_type
235+
)
222236
end
223237

224238
def authorize_replace_polymorphic_to_one_relationship
@@ -248,16 +262,16 @@ def authorize_replace_polymorphic_to_one_relationship
248262

249263
relationship_type = params[:relationship_type].to_sym
250264
authorizer.replace_to_one_relationship(
251-
source_record,
252-
new_related_record,
253-
relationship_type
265+
source_record: source_record,
266+
new_related_record: new_related_record,
267+
relationship_type: relationship_type
254268
)
255269
end
256270

257271
private
258272

259273
def authorizer
260-
@authorizer ||= ::JSONAPI::Authorization.configuration.authorizer.new(context)
274+
@authorizer ||= ::JSONAPI::Authorization.configuration.authorizer.new(context: context)
261275
end
262276

263277
# TODO: Communicate with upstream to fix this nasty hack
@@ -367,11 +381,13 @@ def authorize_include_item(resource_klass, source_record, include_item)
367381
relationship.relation_name(context: context)
368382
)
369383
return if related_record.nil?
370-
authorizer.include_has_one_resource(source_record, related_record)
384+
authorizer.include_has_one_resource(
385+
source_record: source_record, related_record: related_record
386+
)
371387
when JSONAPI::Relationship::ToMany
372388
authorizer.include_has_many_resource(
373-
source_record,
374-
relationship.resource_klass._model_class
389+
source_record: source_record,
390+
record_class: relationship.resource_klass._model_class
375391
)
376392
else
377393
raise "Unexpected relationship type: #{relationship.inspect}"

0 commit comments

Comments
 (0)