Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module Oauthable
- extend ActiveSupport::Concern
- included do |base|
- def self.find_or_create_with_oauth(oauth_data, user_data={})
- find_with_oauth(oauth_data) || create_with_oauth(oauth_data)
- end
- def self.find_with_oauth(oauth_data)
- find_through_authorization(oauth_data.provider, oauth_data.uid)
- end
- def self.find_through_authorization(service_name, uid)
- social_profile = SocialProfile.where(service_name: service_name, uid: uid).first
- social_profile && social_profile.user
- end
- def self.create_with_oauth(oauth_data)
- User.create(password: SecureRandom.base64(6), profile_attributes: { name: oauth_data.extra.raw_info.name })
- end
- def self.find_or_create_by_social_profiles(social_profiles, user_data)
- social_profiles.each do |profile|
- user = find_with_oauth(profile)
- return user if user.present?
- end
- create_with_oauth(user_data)
- end
- def register_social_profile(data)
- profile = SocialProfile.where(service_name: data[:service_name], uid: data[:uid]).first_or_create
- if profile.user_id.present? && profile.user_id != id
- return false
- else
- profile.update_attributes(access_token: data[:access_token], secret_key: data[:secret_key], active: true, user_id: id)
- end
- profile.persisted? ? profile : false
- end
- def password_required?
- encrypted_password.nil? && social_profiles.to_a.count < 1
- end
- def email_required?
- social_profiles.to_a.count < 1
- end
- def remember_me
- true
- end
- end
- end
Add Comment
Please, Sign In to add comment