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

Commit

Permalink
Refactored docker-compose and fixed CAS_AUTH
Browse files Browse the repository at this point in the history
  • Loading branch information
dokurnon committed Jul 21, 2020
1 parent 9a1892e commit b39ea18
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Dockerfile.dev
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,4 @@ EXPOSE 3000

# Start the main process.
ENTRYPOINT ["entrypoint.sh"]
CMD ["rails", "server", "-b", "0.0.0.0"]
CMD ["rails", "server", "-b", "0.0.0.0", "-p", "3000", "-e", "development"]
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def markdown_help
# modify redirect after signing in
def after_sign_in_path_for(user)
# CODE FOR CAS LOGIN --> NEW USER
if ENV['CAS_AUTH'] && current_user && current_user.id.nil? &&
if ENV['CAS_AUTH'].present? && current_user && current_user.id.nil? &&
current_user.username
# store username in session since there's a request in between
session[:new_username] = current_user.username
Expand Down
6 changes: 3 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class User < ApplicationRecord
# :cas_authenticatable module. If not, we implement password authentcation
# using the :database_authenticatable module and also allow for password
# resets.
if ENV['CAS_AUTH']
if ENV['CAS_AUTH'].present?
devise :cas_authenticatable
else
devise :database_authenticatable, :recoverable, :rememberable
Expand Down Expand Up @@ -38,7 +38,7 @@ class User < ApplicationRecord
presence: true, uniqueness: { case_sensitive: false },
format: { with: /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i }
# validations for CAS authentication
if ENV['CAS_AUTH']
if ENV['CAS_AUTH'].present?
validates :cas_login, presence: true, uniqueness: { case_sensitive: false }
# validations for password authentication
else
Expand Down Expand Up @@ -105,7 +105,7 @@ def equipment_items
end

def render_name
ENV['CAS_AUTH'] ? "#{name} #{username}" : name.to_s
ENV['CAS_AUTH'].present? ? "#{name} #{username}" : name.to_s
end

# ---- Reservation methods ---- #
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/00_devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
config.skip_session_storage = [:http_auth]

# ==> Configuration for :database_authenticatable
unless ENV['CAS_AUTH']
if ENV['CAS_AUTH'].blank?
# For bcrypt, this is the cost for hashing the password and defaults to 10.
# If using other encryptors, it sets how many times you want the password
# re-encrypted.
Expand Down Expand Up @@ -69,7 +69,7 @@
end

# ==> devise_cas_authenticatable configuration
if ENV['CAS_AUTH']
if ENV['CAS_AUTH'].present?
# configure the base URL of your CAS server
config.cas_base_url = Rails.application.secrets.cas_base_url

Expand Down
9 changes: 5 additions & 4 deletions config/initializers/authentication.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# Check for authentication method and copy data over if necessary (ENV variable
# to skip if necessary, skip if migrating from a pre-v4.1.0 DB or no table)
unless ENV['SKIP_AUTH_INIT'] || !User.table_exists? ||
Expand All @@ -8,17 +9,17 @@

# if we want to use CAS authentication and the username parameter doesn't
# match the cas_login parameter, we need to copy that over
if ENV['CAS_AUTH'] && user && (user.username != user.cas_login)
if ENV['CAS_AUTH'].present? && user && (user.username != user.cas_login)
# if there are any users that don't have cas_logins, we can't use CAS
if User.where(cas_login: ['', nil]).count > 0
if User.where(cas_login: ['', nil]).count.positive?
raise 'There are users missing their CAS logins, you cannot use CAS '\
'authentication.'
else
User.update_all 'username = cas_login'
User.all.each { 'username = cas_login' }
end
# if we want to use password authentication all users can reset their
# passwords so it doesn't matter if they already have them or not
elsif ENV['CAS_AUTH'].nil? && user && (user.username != user.email)
User.update_all 'username = email'
User.all.each { 'username = email' }
end
end
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ services:
container_name: reservations-rails
build:
context: ./
dockerfile: Dockerfile.cron
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'"
dockerfile: Dockerfile.dev
volumes:
- ${APPLICATION_PATH}:/app
ports:
Expand All @@ -33,6 +32,7 @@ services:
RES_DB_NAME_PRDT: ${RES_DB_NAME_PRD}
RES_DB_USERNAME: ${RES_DB_USERNAME}
RES_DB_PASSWORD: ${RES_DB_PASSWORD}
CAS_AUTH: ${CAS_AUTH}
CAS_BASE_URL: ${CAS_BASE_URL}

volumes:
Expand Down
2 changes: 1 addition & 1 deletion lib/csv_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def import_with_search(user_data)
# tries to save using only the csv data. This method will return
# false if the data specified in the csv is invalid on the user model.
def attempt_save_with_csv_data?(user_data)
if ENV['CAS_AUTH']
if ENV['CAS_AUTH'].present?
# set the cas login
user_data[:cas_login] = user_data[:username]
elsif user_data[:username].blank?
Expand Down
8 changes: 4 additions & 4 deletions lib/ldap_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def search

private

ATTRS = %i(login email first_name last_name nickname).freeze
ATTRS = %i[login email first_name last_name nickname].freeze
attr_reader :user_login, :ldap

def user_hash # rubocop:disable Metrics/AbcSize
Expand All @@ -31,10 +31,10 @@ def user_hash # rubocop:disable Metrics/AbcSize

# deal with affiliation
out[:affiliation] = aff_params.map { |param| result[param.to_sym][0] }
.select { |s| s && !s.empty? }.join(' ')
.select(&:present?).join(' ')

# define username based on authentication method
out[:username] = if ENV['CAS_AUTH']
out[:username] = if ENV['CAS_AUTH'].present?
result[secrets.ldap_login.to_sym][0]
else
out[:email]
Expand All @@ -53,7 +53,7 @@ def filter
end

def filter_param
ENV['CAS_AUTH'] ? secrets.ldap_login : secrets.ldap_email
ENV['CAS_AUTH'].present? ? secrets.ldap_login : secrets.ldap_email
end

def attrs
Expand Down
2 changes: 1 addition & 1 deletion lib/people_api_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def send_request
def parse_response
{}.tap do |out|
ATTRS.each { |(attr, str)| out[attr] = extract_data(str) }
out[:username] = ENV['CAS_AUTH'] ? out[:cas_login] : out[:email]
out[:username] = ENV['CAS_AUTH'].present? ? out[:cas_login] : out[:email]
end
end

Expand Down
11 changes: 6 additions & 5 deletions lib/seed/automatic_seed.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

# rubocop:disable Rails/Output
module AutomaticSeed
def automatic_seed
Expand All @@ -15,10 +16,10 @@ def automatic_seed
generate_all_reservation_types
Generator.generate('reservation', 3)
puts "Successfully seeded all records! (#{Time.zone.now - start_time}s)\n\n"
unless ENV['CAS_AUTH']
puts "You can log in using e-mail '[email protected]' and "\
"password 'passw0rd'"
end

return if ENV['CAS_AUTH'].present?
puts "You can log in using e-mail '[email protected]' and "\
"password 'passw0rd'"
end

private
Expand All @@ -30,7 +31,7 @@ def generate_all_reservation_types

def create_superuser
u = Generator.superuser
if ENV['CAS_AUTH']
if ENV['CAS_AUTH'].present?
prompt_field(u, :cas_login)
u.username = u.cas_login
else
Expand Down
2 changes: 1 addition & 1 deletion lib/seed/user_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def self.generate
u.nickname = FFaker::Name.first_name
u.phone = FFaker::PhoneNumber.short_phone_number
u.email = FFaker::Internet.email
u.cas_login = FFaker::Internet.user_name if ENV['CAS_AUTH']
u.cas_login = FFaker::Internet.user_name if ENV['CAS_AUTH'].present?
u.affiliation = 'YC ' + %w[BK BR CC DC ES JE MC PC SM SY TC TD].sample +
' ' + rand(2012..2015).to_s
u.role = %w[normal checkout].sample
Expand Down
5 changes: 3 additions & 2 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# frozen_string_literal: true

# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryGirl.define do
sequence :username do |n|
"abc#{n}"
end

factory :user, aliases: [:reserver, :checkout_handler, :checkin_handler] do
factory :user, aliases: %i[reserver checkout_handler checkin_handler] do
sequence(:cas_login) { |n| "netid#{n}" }
first_name 'First'
last_name 'Last'
Expand All @@ -18,7 +19,7 @@
role 'normal'
view_mode 'normal'

if ENV['CAS_AUTH']
if ENV['CAS_AUTH'].present?
username { cas_login }
else
username { email }
Expand Down

0 comments on commit b39ea18

Please sign in to comment.