Skip to content

Commit 4975346

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 fbb1aaf commit 4975346

1 file changed

Lines changed: 34 additions & 27 deletions

File tree

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,6 +212,14 @@ 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: The :managed param is controlled by a check_box in the form
241225
# `app/views/orgs/_profile_form.html.erb`.
@@ -245,6 +229,29 @@ def handle_managed_flag(attrs)
245229
attrs
246230
end
247231

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

0 commit comments

Comments
 (0)