<commit_before>require 'bcrypt'

# Derived from DeviseTokenAuth User concern - Patched for Neo4j support
module UserConcern
  extend ActiveSupport::Concern

  def self.tokens_match?(token_hash, token)
    @token_equality_cache ||= {}

    key = "#{token_hash}/#{token}"
    result = @token_equality_cache[key] ||= (::BCrypt::Password.new(token_hash) == token)
    if @token_equality_cache.size > 10000
      @token_equality_cache = {}
    end
    result
  end

  included do

    # Hack to check if devise is already enabled
    unless self.method_defined?(:devise_modules)
      devise :database_authenticatable, :registerable,
             :recoverable, :trackable, :validatable, :confirmable
    else
      self.devise_modules.delete(:omniauthable)
    end

    # get rid of dead tokens
    before_save :destroy_expired_tokens

    validates :email, presence: true, email: true, if: Proc.new { |u| u.provider == 'apidae' }
    validates_presence_of :uid, if: Proc.new { |u| u.provider == 'apidae' }
    validate :unique_email_user

    # before_save :sync_uid
    # before_create :sync_uid

    # allows user to change password without current_password
    attr_writer :allow_password_change
    def allow_password_change
      @allow_password_change || false
    end

    # don't use default devise email validation
    def email_required?
      false
    end

    def email_changed?
      false
    end

    def unique_email_user
      if provider == 'apidae' and self.class.where(provider: 'apidae', email: email).count > 0
        errors.add(:email, I18n.t('errors.messages.already_in_use'))
      end
    end

    def sync_uid
      self.uid = email if provider == 'apidae'
    end
  end

  def valid_token?(token, client_id='default')
    client_id ||= 'default'

    return false unless self.tokens[client_id]

    return true if token_is_current?(token, client_id)
    return true if token_can_be_reused?(token, client_id)

    # return false if none of the above conditions are met
    return false<commit_msg>Fixed issue with person seeds without an email<commit_after>require 'bcrypt'

# Derived from DeviseTokenAuth User concern - Patched for Neo4j support
module UserConcern
  extend ActiveSupport::Concern

  def self.tokens_match?(token_hash, token)
    @token_equality_cache ||= {}

    key = "#{token_hash}/#{token}"
    result = @token_equality_cache[key] ||= (::BCrypt::Password.new(token_hash) == token)
    if @token_equality_cache.size > 10000
      @token_equality_cache = {}
    end
    result
  end

  included do

    # Hack to check if devise is already enabled
    unless self.method_defined?(:devise_modules)
      devise :database_authenticatable, :registerable,
             :recoverable, :trackable, :validatable, :confirmable
    else
      self.devise_modules.delete(:omniauthable)
    end

    # get rid of dead tokens
    before_update :destroy_expired_tokens

    validates :email, presence: true, email: true, if: Proc.new { |u| u.provider == 'apidae' }
    validates_presence_of :uid, if: Proc.new { |u| u.provider == 'apidae' }
    validate :unique_email_user, on: :create

    # allows user to change password without current_password
    attr_writer :allow_password_change
    def allow_password_change
      @allow_password_change || false
    end

    # don't use default devise email validation
    def email_required?
      false
    end

    def email_changed?
      false
    end

    def unique_email_user
      if provider == 'apidae' and self.class.where(provider: 'apidae', email: email).count > 0
        errors.add(:email, I18n.t('errors.messages.already_in_use'))
      end
    end

  end

  def valid_token?(token, client_id='default')
    client_id ||= 'default'

    return false unless self.tokens[client_id]

    return true if token_is_current?(token, client_id)
    return true if token_can_be_reused?(token, client_id)

    # return false if none of the above conditions are met
    return false
{'fraction_non_alphanumeric': 0.08911890936632164, 'fraction_numerical': 0.0045443069931835394, 'mean_word_length': 3.3924611973392462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7468198', 'n_tokens_mistral': 1263, 'n_tokens_neox': 1203, 'n_words': 332}