Sha256: c531f0a1af7b2d81647dff27b313f3995d16c96e94008b0e7c3fcaf2fb322d5d

Contents?: true

Size: 1.86 KB

Versions: 6

Compression:

Stored size: 1.86 KB

Contents

require 'devise_password_sharing_extension/hooks/password_sharing'

module Devise
  module Models
    module PasswordSharing
      extend ActiveSupport::Concern

      module ClassMethods
        Devise::Models.config(self, :enable_banning)
        Devise::Models.config(self, :geoip_database)
        Devise::Models.config(self, :time_frame)
        Devise::Models.config(self, :number_of_cities)
        Devise::Models.config(self, :white_listed_ips)
      end

      included do
        has_many :login_events

        @@white_listed_ips = YAML::load(File.read(Rails.root.join('config', 'white_listed_ips.yml')))
      end

      def create_login_event!(request)
        unless @@white_listed_ips.include?(request.remote_ip)
          database = GeoIP.new(self.class.geoip_database)
          if geo = database.city(request.remote_ip)
            begin
              login_events.create!(
                :ip_address => request.remote_ip,
                :latitude => geo.latitude,
                :longitude => geo.longitude,
                :city => geo.city_name.encode('US-ASCII', :undef => :replace),
                :country_code => geo.country_code2.encode('US-ASCII', :undef => :replace),
                :region_name => geo.region_name.encode('US-ASCII', :undef => :replace))
            rescue ActiveRecord::RecordInvalid => e
              # Just move on and be nice.
              Rails.logger.info("Problem with geo: #{geo.inspect}")
            end
          end
        end
      end

      def ban_for_password_sharing!
        return unless self.class.enable_banning
        self.banned_for_password_sharing_at = Time.now
        save(:validate => false)
      end

      def password_sharing?
        return true unless banned_for_password_sharing_at.nil?
        login_events.cities_in_the_last(self.class.time_frame).count >= self.class.number_of_cities
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
devise_password_sharing_extension-0.0.13 lib/devise_password_sharing_extension/models/password_sharing.rb
devise_password_sharing_extension-0.0.12 lib/devise_password_sharing_extension/models/password_sharing.rb
devise_password_sharing_extension-0.0.11 lib/devise_password_sharing_extension/models/password_sharing.rb
devise_password_sharing_extension-0.0.10 lib/devise_password_sharing_extension/models/password_sharing.rb
devise_password_sharing_extension-0.0.9 lib/devise_password_sharing_extension/models/password_sharing.rb
devise_password_sharing_extension-0.0.8 lib/devise_password_sharing_extension/models/password_sharing.rb