From 223b4a5ba3a234bf3c01c2cd7fec9531a2bb5767 Mon Sep 17 00:00:00 2001 From: Joel Van Horn Date: Sun, 15 Nov 2015 17:22:19 -0500 Subject: [PATCH 1/2] Specify a custom User superclass to use --- app/models/casino/ticket_granting_ticket.rb | 2 +- app/models/casino/two_factor_authenticator.rb | 2 +- .../casino/ticket_granting_ticket_processor.rb | 2 +- config/cas.yml | 1 + lib/casino.rb | 5 +++++ lib/casino/tasks/user.rake | 6 +++--- .../service_and_proxy_tickets_controller_spec.rb | 4 ++-- spec/controllers/sessions_controller_spec.rb | 10 +++++----- spec/support/factories/user_factory.rb | 2 +- 9 files changed, 20 insertions(+), 14 deletions(-) diff --git a/app/models/casino/ticket_granting_ticket.rb b/app/models/casino/ticket_granting_ticket.rb index 1cab2942..292718f3 100644 --- a/app/models/casino/ticket_granting_ticket.rb +++ b/app/models/casino/ticket_granting_ticket.rb @@ -5,7 +5,7 @@ class CASino::TicketGrantingTicket < ActiveRecord::Base self.ticket_prefix = 'TGC'.freeze - belongs_to :user + belongs_to :user, class_name: CASino.user_class.name has_many :service_tickets, dependent: :destroy scope :active, -> { where(awaiting_two_factor_authentication: false).order('updated_at DESC') } diff --git a/app/models/casino/two_factor_authenticator.rb b/app/models/casino/two_factor_authenticator.rb index ed93c604..97056ea4 100644 --- a/app/models/casino/two_factor_authenticator.rb +++ b/app/models/casino/two_factor_authenticator.rb @@ -1,6 +1,6 @@ class CASino::TwoFactorAuthenticator < ActiveRecord::Base - belongs_to :user + belongs_to :user, class_name: CASino.user_class.name scope :active, -> { where(active: true) } diff --git a/app/processors/casino/ticket_granting_ticket_processor.rb b/app/processors/casino/ticket_granting_ticket_processor.rb index 31a5dcb0..e02e60ef 100644 --- a/app/processors/casino/ticket_granting_ticket_processor.rb +++ b/app/processors/casino/ticket_granting_ticket_processor.rb @@ -38,7 +38,7 @@ def acquire_ticket_granting_ticket(authentication_result, user_agent, user_ip, o end def load_or_initialize_user(authenticator, username, extra_attributes) - user = CASino::User + user = CASino.user_class .where(authenticator: authenticator, username: username) .first_or_initialize user.extra_attributes = extra_attributes diff --git a/config/cas.yml b/config/cas.yml index 26818b50..3ddd407c 100644 --- a/config/cas.yml +++ b/config/cas.yml @@ -1,4 +1,5 @@ defaults: &defaults + user_class_name: 'CASino::User' service_ticket: lifetime_unconsumed: 299 authenticators: diff --git a/lib/casino.rb b/lib/casino.rb index e15bac79..6dcf0138 100644 --- a/lib/casino.rb +++ b/lib/casino.rb @@ -5,6 +5,7 @@ module CASino include ActiveSupport::Configurable defaults = { + user_class_name: 'CASino::User', authenticators: HashWithIndifferentAccess.new, require_service_rules: false, logger: Rails.logger, @@ -51,4 +52,8 @@ module CASino } self.config.merge! defaults.deep_dup + + def self.user_class + config.user_class_name.constantize + end end diff --git a/lib/casino/tasks/user.rake b/lib/casino/tasks/user.rake index e9965af1..196069e7 100644 --- a/lib/casino/tasks/user.rake +++ b/lib/casino/tasks/user.rake @@ -4,7 +4,7 @@ namespace :casino do namespace :user do desc 'Search users by name.' task :search, [:query] => :environment do |task, args| - users = CASino::User.where('username LIKE ?', "%#{args[:query]}%") + users = CASino.user_class.where('username LIKE ?', "%#{args[:query]}%") if users.any? headers = ['User ID', 'Username', 'Authenticator', 'Two-factor authentication enabled?'] table = Terminal::Table.new :headings => headers do |t| @@ -21,8 +21,8 @@ namespace :casino do desc 'Deactivate two-factor authentication for a user.' task :deactivate_two_factor_authentication, [:user_id] => :environment do |task, args| - if CASino::User.find(args[:user_id]).active_two_factor_authenticator - CASino::User.find(args[:user_id]).active_two_factor_authenticator.destroy + if CASino.user_class.find(args[:user_id]).active_two_factor_authenticator + CASino.user_class.find(args[:user_id]).active_two_factor_authenticator.destroy puts "Successfully deactivated two-factor authentication for user ##{args[:user_id]}." else puts "No two-factor authenticator found for user ##{args[:user_id]}." diff --git a/spec/controllers/service_and_proxy_tickets_controller_spec.rb b/spec/controllers/service_and_proxy_tickets_controller_spec.rb index 64cdbbe1..b47c07f3 100644 --- a/spec/controllers/service_and_proxy_tickets_controller_spec.rb +++ b/spec/controllers/service_and_proxy_tickets_controller_spec.rb @@ -30,7 +30,7 @@ context 'with an unconsumed service ticket' do context 'with extra attributes using strings as keys' do before(:each) do - CASino::User.any_instance.stub(:extra_attributes).and_return({ "id" => 1234 }) + CASino.user_class.any_instance.stub(:extra_attributes).and_return({ "id" => 1234 }) end it 'includes the extra attributes' do @@ -41,7 +41,7 @@ context 'with extra attributes using array as value' do before(:each) do - CASino::User.any_instance.stub(:extra_attributes).and_return({ "memberOf" => [ "test", "yolo" ] }) + CASino.user_class.any_instance.stub(:extra_attributes).and_return({ "memberOf" => [ "test", "yolo" ] }) end it 'includes all values' do diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 800ed4ba..b01464ee 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -231,7 +231,7 @@ end context 'with two-factor authentication enabled' do - let!(:user) { CASino::User.create! username: username, authenticator: authenticator } + let!(:user) { CASino.user_class.create! username: username, authenticator: authenticator } let!(:two_factor_authenticator) { FactoryGirl.create :two_factor_authenticator, user: user } it 'renders the validate_otp template' do @@ -283,24 +283,24 @@ it 'generates exactly one user' do lambda do post :create, request_options - end.should change(CASino::User, :count).by(1) + end.should change(CASino.user_class, :count).by(1) end it 'sets the users attributes' do post :create, request_options - user = CASino::User.last + user = CASino.user_class.last user.username.should == username user.authenticator.should == authenticator end end context 'when the user already exists' do - let!(:user) { CASino::User.create! username: username, authenticator: authenticator } + let!(:user) { CASino.user_class.create! username: username, authenticator: authenticator } it 'does not regenerate the user' do lambda do post :create, request_options - end.should_not change(CASino::User, :count) + end.should_not change(CASino.user_class, :count) end it 'updates the extra attributes' do diff --git a/spec/support/factories/user_factory.rb b/spec/support/factories/user_factory.rb index bcc73fe0..d168aac8 100644 --- a/spec/support/factories/user_factory.rb +++ b/spec/support/factories/user_factory.rb @@ -1,7 +1,7 @@ require 'factory_girl' FactoryGirl.define do - factory :user, class: CASino::User do + factory :user, class: CASino.user_class do authenticator 'test' sequence(:username) do |n| "test#{n}" From 9f6b31f4d4fd99ec7fa1e05e498815d7bec7a6ab Mon Sep 17 00:00:00 2001 From: Joel Van Horn Date: Thu, 11 May 2017 19:33:08 -0400 Subject: [PATCH 2/2] Retire Ruby 2.1.0 to support gem dependency `mustermann` --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 327103aa..6d1a8b1f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: ruby rvm: -- 2.1.0 - 2.2.2 +- 2.4.0 notifications: hipchat: rooms: