user.rb 1.2 KB
require_dependency 'user'

class User

  has_many :oauth_user_providers, :class_name => 'OauthClientPlugin::UserProvider'
  has_many :oauth_providers, :through => :oauth_user_providers, :source => :provider

  def password_required_with_oauth?
    password_required_without_oauth? && oauth_providers.empty?
  end

  alias_method_chain :password_required?, :oauth

  after_create :activate_oauth_user

  def activate_oauth_user
    # user creation through api does not set oauth_providers
    if oauth_providers.empty?
      #check if is oauth user, reading oauth_data recorded at cache store
      oauth_data = OauthClientPlugin.read_cache_for(self.email)
      if oauth_data
        oauth_providers = [OauthClientPlugin::Provider.find(oauth_data[:provider])]
        OauthClientPlugin.delete_cache_for(self.email)
      end
    end

    unless oauth_providers.empty?
      activate
      oauth_providers.each do |provider|
        OauthClientPlugin::UserProvider.create!(:user => self, :provider => provider, :enabled => true)
      end
    end
  end

  def make_activation_code_with_oauth
    oauth_providers.blank? ? make_activation_code_without_oauth : nil
  end

  alias_method_chain :make_activation_code, :oauth

end