Skip to content

Commit eb3b77c

Browse files
committed
Refactor: handle logo and shibboleth w/ helpers
This refactor is meant to serve as a small step in simplifying `OrgsController#admin_update` controller action, which currently contains a lot of rubocop offences. - Moved `attrs[:logo]` handling logic into `handle_logo(attrs)` - Moved Shibboleth identifier handling into `handle_shibboleth_identifier(attrs)`
1 parent 8141bde commit eb3b77c

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

app/controllers/orgs_controller.rb

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,17 @@ def admin_update
3333
@org = Org.find(params[:id])
3434
authorize @org
3535

36-
# If a new logo was supplied then use it, otherwise retain the existing one
37-
attrs[:logo] = attrs[:logo].present? ? attrs[:logo] : @org.logo
38-
# Remove the logo if the user checked the box
39-
attrs[:logo] = nil if attrs[:remove_logo] == '1'
36+
attrs = handle_logo(attrs)
4037

4138
tab = (attrs[:feedback_enabled].present? ? 'feedback' : 'profile')
4239
@org.links = ActiveSupport::JSON.decode(params[:org_links]) if params[:org_links].present?
4340

4441
# Only allow super admins to change the org types and shib info
4542
if current_user.can_super_admin?
46-
identifiers = []
4743
attrs = handle_managed_flag(attrs)
44+
attrs = handle_shibboleth_identifier(attrs)
4845

49-
# Handle Shibboleth identifier if that is enabled
50-
if Rails.configuration.x.shibboleth.use_filtered_discovery_service
51-
shib = IdentifierScheme.by_name('shibboleth').first
52-
53-
if shib.present? && attrs[:identifiers_attributes].present?
54-
key = attrs[:identifiers_attributes].keys.first
55-
entity_id = attrs[:identifiers_attributes][:"#{key}"][:value]
56-
# rubocop:disable Metrics/BlockNesting
57-
if entity_id.present?
58-
identifier = Identifier.find_or_initialize_by(
59-
identifiable: @org, identifier_scheme: shib, value: entity_id
60-
)
61-
@org = process_identifier_change(org: @org, identifier: identifier)
62-
else
63-
# The user blanked out the entityID so delete the record
64-
@org.identifier_for_scheme(scheme: shib)&.destroy
65-
end
66-
# rubocop:enable Metrics/BlockNesting
67-
end
68-
attrs.delete(:identifiers_attributes)
69-
end
70-
46+
identifiers = []
7147
# See if the user selected a new Org via the Org Lookup and
7248
# convert it into an Org
7349
lookup = org_from_params(params_in: attrs)
@@ -236,13 +212,44 @@ def search_params
236212
params.require(:org).permit(:name, :type)
237213
end
238214

215+
def handle_logo(attrs)
216+
# If a new logo was supplied then use it, otherwise retain the existing one
217+
attrs[:logo] = attrs[:logo].present? ? attrs[:logo] : @org.logo
218+
# Remove the logo if the user checked the box
219+
attrs[:logo] = nil if attrs[:remove_logo] == '1'
220+
attrs
221+
end
222+
239223
def handle_managed_flag(attrs)
240224
# NOTE: `:managed` is controlled by a check_box in the form
241225
# `app/views/orgs/_profile_form.html.erb`.
242226
attrs[:managed] = (attrs[:managed] == '1') if attrs.key?(:managed)
243227
attrs
244228
end
245229

230+
# Updates the @org's Shibboleth identifier(s) if the required conditions are met
231+
def handle_shibboleth_identifier(attrs)
232+
return attrs unless Rails.configuration.x.shibboleth.use_filtered_discovery_service
233+
234+
shib = IdentifierScheme.by_name('shibboleth').first
235+
236+
if shib.present? && attrs[:identifiers_attributes].present?
237+
key = attrs[:identifiers_attributes].keys.first
238+
entity_id = attrs[:identifiers_attributes][:"#{key}"][:value]
239+
if entity_id.present?
240+
identifier = Identifier.find_or_initialize_by(
241+
identifiable: @org, identifier_scheme: shib, value: entity_id
242+
)
243+
@org = process_identifier_change(org: @org, identifier: identifier)
244+
else
245+
# The user blanked out the entityID so delete the record
246+
@org.identifier_for_scheme(scheme: shib)&.destroy
247+
end
248+
end
249+
attrs.delete(:identifiers_attributes)
250+
attrs
251+
end
252+
246253
def shib_login_url
247254
shib_login = Rails.configuration.x.shibboleth.login_url
248255
"#{request.base_url.gsub('http:', 'https:')}#{shib_login}"

0 commit comments

Comments
 (0)