Skip to content
This repository was archived by the owner on Jul 24, 2020. It is now read-only.

Commit 771dbbe

Browse files
author
Sydney Young
committed
Create helper method to check ENV variables
Resolves #1422
1 parent c549ec9 commit 771dbbe

18 files changed

+75
-32
lines changed

app/controllers/application_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ def markdown_help
322322
# modify redirect after signing in
323323
def after_sign_in_path_for(user)
324324
# CODE FOR CAS LOGIN --> NEW USER
325-
if ENV['CAS_AUTH'] && current_user && current_user.id.nil? &&
325+
if env?('CAS_AUTH') && current_user && current_user.id.nil? &&
326326
current_user.username
327327
# store username in session since there's a request in between
328328
session[:new_username] = current_user.username

app/controllers/import_users_controller.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ def valid_input_file?(imported_users, file) # rubocop:disable all
5858
end
5959

6060
# make sure we have username data (otherwise all will always fail)
61-
unless imported_users.first.keys.include?(:username) ||
62-
ENV['CAS_AUTH'].nil?
61+
unless imported_users.first.keys.include?(:username) || !env?('CAS_AUTH')
6362
flash[:error] = 'Unable to import CSV file. None of the users will be '\
6463
'able to log in without specifying \'username\' data.'
6564
redirect_to(:back) && return

app/controllers/users_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def set_user
2525
end
2626

2727
def check_cas_auth
28-
@cas_auth = ENV['CAS_AUTH']
28+
@cas_auth = env?('CAS_AUTH')
2929
end
3030

3131
# ------------ end before filter methods ------------ #

app/models/user.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class User < ActiveRecord::Base
1010
# :cas_authenticatable module. If not, we implement password authentcation
1111
# using the :database_authenticatable module and also allow for password
1212
# resets.
13-
if ENV['CAS_AUTH']
13+
if env?('CAS_AUTH')
1414
devise :cas_authenticatable
1515
else
1616
devise :database_authenticatable, :recoverable, :rememberable
@@ -38,7 +38,7 @@ class User < ActiveRecord::Base
3838
presence: true, uniqueness: true,
3939
format: { with: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i }
4040
# validations for CAS authentication
41-
if ENV['CAS_AUTH']
41+
if env?('CAS_AUTH')
4242
validates :cas_login, presence: true, uniqueness: true
4343
# validations for password authentication
4444
else
@@ -92,9 +92,9 @@ def equipment_items
9292
# rubocop:disable CyclomaticComplexity
9393
def self.search_ldap(login)
9494
return nil if login.blank?
95-
return nil unless ENV['USE_LDAP']
95+
return nil unless env?('USE_LDAP')
9696

97-
filter_param = if ENV['CAS_AUTH']
97+
filter_param = if env?('CAS_AUTH')
9898
Rails.application.secrets.ldap_login
9999
else
100100
Rails.application.secrets.ldap_email
@@ -139,7 +139,7 @@ def self.search_ldap(login)
139139
.select { |s| s && !s.empty? }.join(' ')
140140

141141
# define username based on authentication method
142-
out[:username] = if ENV['CAS_AUTH']
142+
out[:username] = if env?('CAS_AUTH')
143143
result[Rails.application.secrets.ldap_login.to_sym][0]
144144
else
145145
out[:email]
@@ -158,7 +158,7 @@ def self.select_options
158158
end
159159

160160
def render_name
161-
ENV['CAS_AUTH'] ? "#{name} #{username}" : name.to_s
161+
env?('CAS_AUTH') ? "#{name} #{username}" : name.to_s
162162
end
163163

164164
# ---- Reservation methods ---- #

app/views/users/_find_user.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<%= label_tag :searched_id, 'Find User' %>
44
<div class="input-group">
55
<%= autocomplete_field_tag 'fake_searched_id', '', autocomplete_user_last_name_users_path,
6-
size: 30, update_elements: {id: '#searched_id'}, placeholder: "Enter a name or #{ENV['CAS_AUTH'] ? 'username' : 'email'}", class: 'submittable form-control' %>
6+
size: 30, update_elements: {id: '#searched_id'}, placeholder: "Enter a name or #{env?('CAS_AUTH') ? 'username' : 'email'}", class: 'submittable form-control' %>
77
<%= hidden_field_tag 'searched_id' %>
88
<span class="input-group-btn">
99
<%= button_tag sanitize("#{content_tag :i, nil, class: "fa fa-search"}"), class: "btn btn-default", rel: 'tooltip', title: 'Search Users' %>

config/environments/production.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22
Rails.application.configure do
3+
ENV_FALSE = [0, '0', false, 'false', nil, ''].freeze
4+
35
# Settings specified here will take precedence over those in
46
# config/application.rb
57

@@ -24,7 +26,8 @@
2426

2527
# Disable serving static files from the `/public` folder by default since
2628
# Apache or NGINX already handles this.
27-
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
29+
config.serve_static_files =
30+
!ENV_FALSE.include?(ENV['RAILS_SERVE_STATIC_FILES'])
2831

2932
# Compress JavaScripts and CSS.
3033
config.assets.js_compressor = :uglifier
@@ -77,7 +80,8 @@
7780
# config.action_controller.asset_host = 'http://assets.example.com'
7881

7982
# Disable e-mails if environment variable is set
80-
config.action_mailer.perform_deliveries = ENV['DISABLE_EMAILS'].nil?
83+
config.action_mailer.perform_deliveries =
84+
ENV_FALSE.include?(ENV['DISABLE_EMAILS'])
8185

8286
# Disable delivery errors, bad email addresses will be ignored
8387
# config.action_mailer.raise_delivery_errors = false
@@ -105,5 +109,7 @@
105109
config.assets.precompile += %w(print.css)
106110

107111
# set up PartyFoul
108-
config.middleware.use('PartyFoul::Middleware') if ENV['PARTY_FOUL_TOKEN']
112+
unless ENV_FALSE.include?(ENV['PARTY_FOUL_TOKEN'])
113+
config.middleware.use('PartyFoul::Middleware')
114+
end
109115
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# frozen_string_literal: true
2+
include EnvironmentHandler

config/initializers/00_devise.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
config.skip_session_storage = [:http_auth]
3232

3333
# ==> Configuration for :database_authenticatable
34-
unless ENV['CAS_AUTH']
34+
unless env?('CAS_AUTH')
3535
# For bcrypt, this is the cost for hashing the password and defaults to 10.
3636
# If using other encryptors, it sets how many times you want the password
3737
# re-encrypted.
@@ -72,7 +72,7 @@
7272
end
7373

7474
# ==> devise_cas_authenticatable configuration
75-
if ENV['CAS_AUTH']
75+
if env?('CAS_AUTH')
7676
# configure the base URL of your CAS server
7777
config.cas_base_url = Rails.application.secrets.cas_base_url
7878

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# frozen_string_literal: true
22
# Check for authentication method and copy data over if necessary (ENV variable
33
# to skip if necessary, skip if migrating from a pre-v4.1.0 DB or no table)
4-
unless ENV['SKIP_AUTH_INIT'] || !User.table_exists? ||
4+
unless env?('SKIP_AUTH_INIT') || !User.table_exists? ||
55
!User.column_names.include?('username')
66

77
user = User.first
88

99
# if we want to use CAS authentication and the username parameter doesn't
1010
# match the cas_login parameter, we need to copy that over
11-
if ENV['CAS_AUTH'] && user && (user.username != user.cas_login)
11+
if env?('CAS_AUTH') && user && (user.username != user.cas_login)
1212
# if there are any users that don't have cas_logins, we can't use CAS
1313
if User.where(cas_login: ['', nil]).count > 0
1414
raise 'There are users missing their CAS logins, you cannot use CAS '\
@@ -18,7 +18,7 @@
1818
end
1919
# if we want to use password authentication all users can reset their
2020
# passwords so it doesn't matter if they already have them or not
21-
elsif ENV['CAS_AUTH'].nil? && user && (user.username != user.email)
21+
elsif env?('CAS_AUTH')
2222
User.update_all 'username = email'
2323
end
2424
end

config/initializers/setup_mail.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
}
1010

1111
# optional server authentication
12-
if ENV['RES_SMTP_AUTH']
12+
if env?('RES_SMTP_AUTH')
1313
ActionMailer::Base.smtp_settings[:authentication] = :login
1414
ActionMailer::Base.smtp_settings[:user_name] =
1515
Rails.application.secrets.smtp_username
@@ -20,7 +20,7 @@
2020
# logging of automatically sent emails
2121
class MailObserver
2222
def self.delivered_email(message)
23-
if ENV['LOG_EMAILS']
23+
if env?('LOG_EMAILS')
2424
Rails.logger.info "Sent #{message.subject} to #{message.to}"
2525
end
2626
end

0 commit comments

Comments
 (0)