<commit_before>require 'digest/sha1'
class Subscriber < ActiveRecord::Base
  has_many :subscriptions, :dependent => :destroy
  has_many :lists, :through => :subscriptions, :uniq => true

  has_many :writings, :class_name => 'Message', :foreign_key => 'subscriber_id', :dependent => :destroy
  
  validates_uniqueness_of :email
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
  validates_presence_of :password, :if => :saving_password?
  validates_presence_of :name
  validates_confirmation_of :password
  
  attr_accessible :name, :email, :password, :password_confirmation
  
  attr_accessor :password, :saving_password
  before_save :prepare_password, :if => :saving_password?
  before_create :generate_public_key
  
  default_scope :order => :name
  
  named_scope :active, :conditions => { :disabled => false }, :order => :name
  named_scope :disabled, :conditions => { :disabled => true }, :order => :name
  
  # Search based on 'term' parameter
  named_scope :search, lambda { |term| { :conditions => ["subscribers.name LIKE ? OR subscribers.email LIKE ?", "%" + term + "%", "%" + term + "%"], :order => :name }}
  
  def self.find_subscribers_not_in_list(list_id)
    find_by_sql ["SELECT * FROM subscribers WHERE id NOT IN 
                      (SELECT DISTINCT subscriber_id FROM subscriptions WHERE list_id = ?)", list_id ]
  end
  
  # Login with email address
  def self.authenticate(login, pass)
    user = find_by_email(login)
    return user if user && user.matching_password?(pass)
  end
  
  def matching_password?(pass)
    self.password_hash == encrypt_password(pass) && login_permitted?
  end
  
  def login_permitted?<commit_msg>Order list of available subscribers<commit_after>require 'digest/sha1'
class Subscriber < ActiveRecord::Base
  has_many :subscriptions, :dependent => :destroy
  has_many :lists, :through => :subscriptions, :uniq => true

  has_many :writings, :class_name => 'Message', :foreign_key => 'subscriber_id', :dependent => :destroy
  
  validates_uniqueness_of :email
  validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
  validates_presence_of :password, :if => :saving_password?
  validates_presence_of :name
  validates_confirmation_of :password
  
  attr_accessible :name, :email, :password, :password_confirmation
  
  attr_accessor :password, :saving_password
  before_save :prepare_password, :if => :saving_password?
  before_create :generate_public_key
  
  default_scope :order => :name
  
  named_scope :active, :conditions => { :disabled => false }, :order => :name
  named_scope :disabled, :conditions => { :disabled => true }, :order => :name
  
  # Search based on 'term' parameter
  named_scope :search, lambda { |term| { :conditions => ["subscribers.name LIKE ? OR subscribers.email LIKE ?", "%" + term + "%", "%" + term + "%"], :order => :name }}
  
  def self.find_subscribers_not_in_list(list_id)
    find_by_sql ["SELECT * FROM subscribers WHERE id NOT IN 
                      (SELECT DISTINCT subscriber_id FROM subscriptions WHERE list_id = ?) ORDER BY subscribers.name", list_id ]
  end
  
  # Login with email address
  def self.authenticate(login, pass)
    user = find_by_email(login)
    return user if user && user.matching_password?(pass)
  end
  
  def matching_password?(pass)
    self.password_hash == encrypt_password(pass) && login_permitted?
  end
  
  def login_permitted?
{'fraction_non_alphanumeric': 0.1326530612244898, 'fraction_numerical': 0.0052478134110787176, 'mean_word_length': 4.352574102964119, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '7214193', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1112, 'n_words': 264}