Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Specify a custom User superclass to use #141

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
rvm:
- 2.1.0
- 2.2.2
- 2.4.0
notifications:
hipchat:
rooms:
Expand Down
2 changes: 1 addition & 1 deletion app/models/casino/ticket_granting_ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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') }
Expand Down
2 changes: 1 addition & 1 deletion app/models/casino/two_factor_authenticator.rb
Original file line number Diff line number Diff line change
@@ -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) }

Expand Down
2 changes: 1 addition & 1 deletion app/processors/casino/ticket_granting_ticket_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/cas.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defaults: &defaults
user_class_name: 'CASino::User'
service_ticket:
lifetime_unconsumed: 299
authenticators:
Expand Down
5 changes: 5 additions & 0 deletions lib/casino.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -51,4 +52,8 @@ module CASino
}

self.config.merge! defaults.deep_dup

def self.user_class
config.user_class_name.constantize
end
end
6 changes: 3 additions & 3 deletions lib/casino/tasks/user.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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]}."
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/service_and_proxy_tickets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions spec/controllers/sessions_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,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
Expand Down Expand Up @@ -313,24 +313,24 @@
it 'generates exactly one user' do
lambda do
post :create, params
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, params
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, params
end.should_not change(CASino::User, :count)
end.should_not change(CASino.user_class, :count)
end

it 'updates the extra attributes' do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/factories/user_factory.rb
Original file line number Diff line number Diff line change
@@ -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}"
Expand Down