Module: UsesOpenID::ClassMethods

Defined in:
lib/uses_openid/active_record.rb

Defined Under Namespace

Classes: OpenIDAuthenticationError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/uses_openid/active_record.rb', line 12

def self.extended(klass)
  # Set up the has_many association
  klass.class_eval %Q{
    @identity_url_class_name = '#{klass.name}IdentityURL'
    has_many :identity_urls, :class_name => @identity_url_class_name, :dependent => :delete_all
  }
end

Instance Method Details

#authenticate_with_openid(identity_url, user_args = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/uses_openid/active_record.rb', line 20

def authenticate_with_openid(identity_url, user_args = {})
  # Try to find a user from the identity_url
  # If one cannot be found, use the email to find a user.
  # A user can have multiple identity urls. If an unrecognized one is given, it'll be associated with the user
  user_args.symbolize_keys!
  email = user_args[:email]

  if user = @identity_url_class_name.constantize.find_by_identity_url(identity_url).try(:user)
    # We've already got the identity URL saved
  elsif !email.nil? and user = self.where(user_args).first
    # Save the identity URL for next time
    user.identity_urls.build(:identity_url => identity_url) unless user.identity_urls.where(:identity_url => identity_url).exists?
    user.save(:validate => false) # Don't need to do user validation here
  else
    # We don't have an account for this OpenID
    user = nil
  end

  user
end