Implements iChain authentication

This commit is contained in:
chrisbr 2014-11-04 17:42:47 +01:00
parent 15c6ed127f
commit 49d565def2
21 changed files with 269 additions and 126 deletions

View file

@ -1,3 +1,6 @@
class IChainRecordNotFound < StandardError
end
class User < ActiveRecord::Base
rolify
include Gravtastic
@ -8,9 +11,17 @@ class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable,
:omniauthable, omniauth_providers: [:suse, :google, :facebook, :github]
devise_modules = []
if CONFIG['authentication']['ichain']['enabled']
devise_modules += [ :ichain_authenticatable, :ichain_registerable ]
else
devise_modules += [:database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable,
:omniauthable, omniauth_providers: [:suse, :google, :facebook, :github] ]
end
devise(*devise_modules)
has_and_belongs_to_many :roles
has_many :openids
@ -31,12 +42,13 @@ class User < ActiveRecord::Base
has_many :subscriptions, dependent: :destroy
accepts_nested_attributes_for :roles
validates :name, presence: true
validates :email, presence: true
validates :username,
:uniqueness => {
:case_sensitive => false
}
uniqueness: {
case_sensitive: false
},
presence: true
# Returns the ticket purchased ticket
# ====Returns
@ -45,10 +57,25 @@ class User < ActiveRecord::Base
ticket_purchases.where(ticket_id: id).first
end
def self.for_ichain_username(username, attributes)
user = find_by(username: username)
if user
user.update_attributes(email: attributes[:email])
else
begin
user = create(username: username, email: attributes[:email])
rescue ActiveRecord::RecordNotUnique
raise IChainRecordNotFound
end
end
user
end
def self.find_for_database_authentication(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
where(conditions).where(["lower(username) = :value OR lower(email) = :value", { :value => login.downcase }]).first
login = conditions.delete(:login)
if login
where(conditions).where(['lower(username) = :value OR lower(email) = :value', { value: login.downcase }]).first
else
where(conditions).first
end