<commit_before># encoding: utf-8

class User < ActiveRecord::Base
  STUDY_PATHS = [:bsc_physics, :bsc_maths, :edu_degree]
  VALID_EMAIL_REGEX = /\A[\w+\-._]+@[a-z\d\-.]+\.[a-z]+\z/i

  has_secure_password

  has_and_belongs_to_many :starred, :class_name => :Question, :join_table => :starred

  has_many :reviews, dependent: :destroy, order: 'updated_at DESC'
  has_many :stats, inverse_of: :user
  #~ has_many :questions, :through => :stats
  has_many :seen_questions, :through => :stats, :source => :question


  attr_protected :nick
  validates :nick, presence: true, uniqueness: true

  attr_accessible :mail
  validates :mail, allow_blank: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

  attr_accessible :study_path
  enumerate :study_path
  validates_inclusion_of :study_path, in: StudyPath

  attr_accessible :password, :password_confirmation
  validates :password, length: { minimum: 4 }, :if => :should_validate_password?
  validates :password_confirmation, presence: true, :if => :should_validate_password?

  attr_protected :admin
  attr_protected :enrollment_keys


  # http://stackoverflow.com/questions/7919584/rails-3-1-create-one-user-in-console-with-secure-password

  before_save { mail.downcase! }
  before_save :create_remember_token

  attr_accessor :updating_password

  # returns true if the given question or question id has been starred
  # by the user. It avoids joining the question table for speedups, so
  # in theory this could return true for non-existing questions.
  def has_starred?(question)
    qid = question.is_a?(Integer) ? question : question.id
    User.count_by_sql("SELECT 1 FROM starred WHERE user_id = #{id} AND question_id = #{qid}") > 0
  end

  include StatTools
<commit_msg>prevent possible future sql injection (thanks brakeman devs)<commit_after># encoding: utf-8

class User < ActiveRecord::Base
  STUDY_PATHS = [:bsc_physics, :bsc_maths, :edu_degree]
  VALID_EMAIL_REGEX = /\A[\w+\-._]+@[a-z\d\-.]+\.[a-z]+\z/i

  has_secure_password

  has_and_belongs_to_many :starred, :class_name => :Question, :join_table => :starred

  has_many :reviews, dependent: :destroy, order: 'updated_at DESC'
  has_many :stats, inverse_of: :user
  #~ has_many :questions, :through => :stats
  has_many :seen_questions, :through => :stats, :source => :question


  attr_protected :nick
  validates :nick, presence: true, uniqueness: true

  attr_accessible :mail
  validates :mail, allow_blank: true, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }

  attr_accessible :study_path
  enumerate :study_path
  validates_inclusion_of :study_path, in: StudyPath

  attr_accessible :password, :password_confirmation
  validates :password, length: { minimum: 4 }, :if => :should_validate_password?
  validates :password_confirmation, presence: true, :if => :should_validate_password?

  attr_protected :admin
  attr_protected :enrollment_keys


  # http://stackoverflow.com/questions/7919584/rails-3-1-create-one-user-in-console-with-secure-password

  before_save { mail.downcase! }
  before_save :create_remember_token

  attr_accessor :updating_password

  # returns true if the given question or question id has been starred
  # by the user. It avoids joining the question table for speedups, so
  # in theory this could return true for non-existing questions.
  def has_starred?(question)
    qid = question.is_a?(Integer) ? question : question.id
    sql = self.sanitize_sql_array(["SELECT 1 FROM starred WHERE user_id = ? AND question_id = ?", id, qid])
    User.count_by_sql(sql) > 0
  end

  include StatTools

{'fraction_non_alphanumeric': 0.10926234054353855, 'fraction_numerical': 0.007210205213533, 'mean_word_length': 4.817741935483871, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, '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': '3902923', 'n_tokens_mistral': 1251, 'n_tokens_neox': 1172, 'n_words': 305}