implement openID authentication
This commit is contained in:
parent
37e026a9fb
commit
600adde6ba
23 changed files with 408 additions and 14 deletions
22
app/models/openid.rb
Normal file
22
app/models/openid.rb
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
class Openid < ActiveRecord::Base
|
||||
belongs_to :user
|
||||
validates :provider, :uid, :presence => true
|
||||
|
||||
# Searches for openid based on provider and uid.
|
||||
# Returns found openid or a new openid.
|
||||
# ====Returns
|
||||
# * Openid::ActiveRecord_Relation -> openid
|
||||
def self.find_for_oauth(auth)
|
||||
openid = Openid.where(provider: auth.provider, uid: auth.uid).first_or_initialize
|
||||
|
||||
if openid.new_record?
|
||||
openid.email = auth.info.email
|
||||
if existing_openid = Openid.where(email: openid.email).first
|
||||
openid.user_id = existing_openid.user_id
|
||||
end
|
||||
end
|
||||
openid
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -4,13 +4,15 @@ class User < ActiveRecord::Base
|
|||
# :token_authenticatable, :confirmable,
|
||||
# :lockable, :timeoutable and :omniauthable
|
||||
devise :database_authenticatable, :registerable,
|
||||
:recoverable, :rememberable, :trackable, :validatable,
|
||||
:confirmable
|
||||
:recoverable, :rememberable, :trackable, :validatable, :confirmable,
|
||||
:omniauthable, omniauth_providers: [:novell, :google, :facebook]
|
||||
|
||||
has_and_belongs_to_many :roles
|
||||
has_one :person, :inverse_of => :user
|
||||
has_many :openids
|
||||
|
||||
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids, :person_attributes
|
||||
attr_accessible :email, :password, :password_confirmation, :remember_me, :role_id, :role_ids,
|
||||
:person_attributes
|
||||
accepts_nested_attributes_for :person
|
||||
accepts_nested_attributes_for :roles
|
||||
|
||||
|
|
@ -19,6 +21,26 @@ class User < ActiveRecord::Base
|
|||
|
||||
delegate :last_name, :first_name, :public_name, to: :person
|
||||
|
||||
# Searches for user based on email. Returns found user or new user.
|
||||
# ====Returns
|
||||
# * +User::ActiveRecord_Relation+ -> user
|
||||
def self.find_for_auth(auth, current_user = nil)
|
||||
|
||||
user = current_user
|
||||
|
||||
if user.nil? # No current user available, user is not already logged in
|
||||
user = User.where(email: auth.info.email).first_or_initialize
|
||||
end
|
||||
|
||||
if user.new_record?
|
||||
user.email = auth.info.email
|
||||
user.password = Devise.friendly_token[0,20]
|
||||
user.skip_confirmation!
|
||||
end
|
||||
|
||||
user
|
||||
end
|
||||
|
||||
def role?(role)
|
||||
Rails.logger.debug("Checking role in user")
|
||||
!!roles.find_by_name(role.to_s.downcase.camelize)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue