<commit_before>class User < ActiveRecord::Base
  
  acts_as_authentic do |config|
    config.password_field = :<PASSWORD>
    config.crypted_password_field = :password
    config.crypto_provider = Authlogic::CryptoProviders::Sha1
    config.salt_first = true
    Authlogic::CryptoProviders::Sha1.join_token = ''
    Authlogic::CryptoProviders::Sha1.stretches = 1
  end

  has_many :participants, :class_name => 'Participant', :foreign_key => 'user_id', :dependent => :destroy
  # FIXME:          :class_name should be AssignmentParticipant, probably. In most cases it's used that way. But all?
  has_many :assignments, :through => :participants
  
  belongs_to :parent, :class_name => 'User', :foreign_key => 'parent_id'
  belongs_to :role
  
  has_many :teams_users, :dependent => :destroy
  has_many :teams, :through => :teams_users
  
  validates_presence_of :name
  validates_presence_of :email, :message => "can't be blank; use <EMAIL> for test users"
  validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, :allow_blank => true
  validates_uniqueness_of :name

  # happens in this order. see http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
  before_validation :randomize_password, :if => lambda { |user| user.new_record? && user.clear_password.blank? } # AuthLogic
  after_create :email_welcome # MailerHelper::send_mail_to_user(self,"Your Expertiza password has been created","user_welcome", clear_password)

  def list_mine(object_type, user_id)
    object_type.find(:all, :conditions => ["instructor_id = ?", user_id])
  end
  
  def get_available_users(name)    
    lesser_roles = role.get_parents
    all_users = User.find(:all, :conditions => ['name LIKE ?', "#{name}%"], :limit => 20) # higher limit, since we're filtering
    visible_users = all_users.select{|user| lesser_roles.include? user.role}
    return visible_users[0,10] # the first 10
  end

  def role
    if self.role_id
      @role ||= Role.find(self.role_id)
    end
    return @role
  end

  def can_impersonate?(other_user)
    return true if other_user == self # can impersonate self
    return false if other_user == other_user.parent # no one can impersonate a top-level parent (usually superadmin)
    return other_user.parent == self || can_impersonate?(other_user.parent) # recursive
  end

  def assign_random_password
    if self.password.blank?
      self.password = <PASSWORD>
    end
  end

  def email_welcome
       MailerHelper::send_mail_to_user(self,"Your Expertiza password has been created","user_welcome",clear_password)
  end

  def check_password(clear_password)
    Authlogic::CryptoProviders::Sha1.stretches = 1
    Authlogic::CryptoProviders::Sha1.matches?(password, *[self.password_salt.to_s + clear_password])
  end

  # Generate email to user with new password
  def reset_password
         self.reset_password!
  end

  def self.random_password(size=8)
    random_pronouncable_password((size/2).round) + rand.to_s[2,3]
  end

  def self.import(row,session,id = nil)
      if row.length != 4
       raise ArgumentError, "Not enough items" 
      end    
      user = User.find_by_name(row[0])    <commit_msg>Fixed reset_password<commit_after>class User < ActiveRecord::Base
  
  acts_as_authentic do |config|
    config.password_field = :<PASSWORD>
    config.crypted_password_field = :password
    config.crypto_provider = Authlogic::CryptoProviders::Sha1
    config.salt_first = true
    Authlogic::CryptoProviders::Sha1.join_token = ''
    Authlogic::CryptoProviders::Sha1.stretches = 1
  end

  has_many :participants, :class_name => 'Participant', :foreign_key => 'user_id', :dependent => :destroy
  # FIXME:          :class_name should be AssignmentParticipant, probably. In most cases it's used that way. But all?
  has_many :assignments, :through => :participants
  
  belongs_to :parent, :class_name => 'User', :foreign_key => 'parent_id'
  belongs_to :role
  
  has_many :teams_users, :dependent => :destroy
  has_many :teams, :through => :teams_users
  
  validates_presence_of :name
  validates_presence_of :email, :message => "can't be blank; use <EMAIL> for test users"
  validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, :allow_blank => true
  validates_uniqueness_of :name

  # happens in this order. see http://api.rubyonrails.org/classes/ActiveRecord/Callbacks.html
  before_validation :randomize_password, :if => lambda { |user| user.new_record? && user.clear_password.blank? } # AuthLogic
  after_create :email_welcome # MailerHelper::send_mail_to_user(self,"Your Expertiza password has been created","user_welcome", clear_password)

  def list_mine(object_type, user_id)
    object_type.find(:all, :conditions => ["instructor_id = ?", user_id])
  end
  
  def get_available_users(name)    
    lesser_roles = role.get_parents
    all_users = User.find(:all, :conditions => ['name LIKE ?', "#{name}%"], :limit => 20) # higher limit, since we're filtering
    visible_users = all_users.select{|user| lesser_roles.include? user.role}
    return visible_users[0,10] # the first 10
  end

  def role
    if self.role_id
      @role ||= Role.find(self.role_id)
    end
    return @role
  end

  def can_impersonate?(other_user)
    return true if other_user == self # can impersonate self
    return false if other_user == other_user.parent # no one can impersonate a top-level parent (usually superadmin)
    return other_user.parent == self || can_impersonate?(other_user.parent) # recursive
  end

  def assign_random_password
    if self.password.blank?
      self.password = self.random_password
    end
  end

  def email_welcome
       MailerHelper::send_mail_to_user(self,"Your Expertiza password has been created","user_welcome",clear_password)
  end

  def check_password(clear_password)
    Authlogic::CryptoProviders::Sha1.stretches = 1
    Authlogic::CryptoProviders::Sha1.matches?(password, *[self.password_salt.to_s + clear_password])
  end

  # Generate email to user with new password
  def reset_password
      randomize_password
      save
      clear_password
  end

  def self.random_password(size=8)
    random_pronouncable_password((size/2).round) + rand.to_s[2,3]
  end

  def self.import(row,session,id = nil)
      if row.length != 4
       raise ArgumentError, "Not enough items" 
      end    
      user = User.find_by_name(row[0])    
{'fraction_non_alphanumeric': 0.11301477522791575, 'fraction_numerical': 0.00817353033637221, 'mean_word_length': 4.160583941605839, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 38, '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': '2711697', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2098, 'n_words': 473}