<commit_before>require 'digest/sha1'

# User models the system users, and is generated by the acts_as_authenticated
# Rails generator.
class User < ActiveRecord::Base

  N_('Password')
  N_('Password confirmation')
  N_('Terms accepted')

  def self.[](login)
    self.find_by_login(login)
  end

  # FIXME ugly workaround
  def self.human_attribute_name(attrib)
    case attrib.to_sym
      when :login:  return _('Username')
      when :email:  return _('e-Mail')
      else _(self.superclass.human_attribute_name(attrib))
    end
  end

  before_create do |user|
    if user.environment.nil?
      user.environment = Environment.default
    end
    user.send(:make_activation_code) unless user.environment.enabled?('skip_new_user_email_confirmation')
  end

  after_create do |user|
    user.person ||= Person.new
    user.person.attributes = user.person_data.merge(:identifier => user.login, :user => user, :environment_id => user.environment_id)
    user.person.name ||= user.name
    user.person.visible = false unless user.activated?
    user.person.save!
    if user.environment.enabled?('skip_new_user_email_confirmation')
      user.activate
    end
  end
  after_create :deliver_activation_code
  after_create :delay_activation_check

  attr_writer :person_data
  def person_data
    @person_data || {}
  end

  def email_domain
    self.person.preferred_domain && self.person.preferred_domain.name || self.environment.default_hostname(true)
  end

  class Mailer < ActionMailer::Base
    def activation_email_notify(user)
      user_email = "#{user.login}@#{user.email_domain}"
      recipients user_email
      from "#{user.environment.name} <#{user.environment.contact_email}>"
      subject _("[%{environment}] Welcome to %{environment} mail!") % { :environment => user.environment.name }
      body :name => user.name,
        :email => user_email,
        :webmail => MailConf.webmail_url(user.login, user.email_domain),
        :environment => user.environment.name,
        :url => url_for(:host => user.environment.default_hostname, :controller => 'home')
    end

    def activation_code(user)
      recipients user.email

      from "#{user.environment.name} <#{user.environment.contact_email}>"
      subject _("[%s] Activate your account") % [user.environment.name]
      body :recipient => user.name,
        :activation_code => user.activation_code,
        :environment => user.environment.name,
        :url => user.environment.top_url
    end

    def signup_welcome_email(user)
      email_body = user.environment.signup_welcome_text_body.gsub('{user_name}', user.name)
      email_subject = user.environment.signup_welcome_text_subject

      content_type 'text/html'
      recipients user.email

      from "#{user.environment.name} <#{user.environment.contact_email}>"
      subject email_subject.blank? ? _("Welcome to environment %s") % [user.environment.name] : email_subject
      body email_body
    end
  end

  def signup!
    User.transaction do
      self.save!
      self.person.save!
    end
  end
  
  has_one :person, :dependent => :destroy
  belongs_to :environment

  attr_protected :activated_at

  # Virtual attribute for the unencrypted password
  attr_accessor :password, :name

  validates_presence_of     :login, :email
  validates_format_of       :login, :with => Profile::IDENTIFIER_FORMAT, :if => (lambda {|user| !user.login.blank?})
  validates_presence_of     :password,                   :if => :password_required?
  validates_presence_of     :password_confirmation,      :if => :password_required?
  validates_length_of       :password, :within => 4..40, :if => :password_required?
  validates_confirmation_of :password,                   :if => :password_required?
  validates_length_of       :login,    :within => 2..40, :if => (lambda {|user| !user.login.blank?})
  validates_length_of       :email,    :within => 3..100, :if => (lambda {|user| !user.email.blank?})
  validates_uniqueness_of   :login, :email, :case_sensitive => false, :scope => :environment_id
  before_save :encrypt_password
  validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?})

  validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('%{fn} must be checked in order to signup.').fix_i18n

  # Authenticates a user by their login name and unencrypted password.  Returns the user or nil.
  def self.authenticate(login, password, environment = nil)
    environment ||= Environment.default
    u = self.first :conditions => ['(login = ? OR email = ?) AND environment_id = ? AND activated_at IS NOT NULL',
                                   login, login, environment.id] # need to get the salt
    u && u.authenticated?(password) ? u : nil
  end

  # Activates the user in the database.
  def activate
    return false unless self.person
    self.activated_at = Time.now.utc
    self.activation_code = nil
    self.person.visible = true
    begin
      self.person.save! && self.save!
    rescue Exception => exception
      logger.error(exception.to_s)
      false
    else
      if environment.enabled?('send_welcome_email_to_new_users') && environment.has_signup_welcome_text?
        User::Mailer.delay.deliver_signup_welcome_email(self)
      end
      true
    end
  end

  def activated?
    self.activation_code.nil? && !self.activated_at.nil?<commit_msg>Adding email to login label<commit_after>require 'digest/sha1'

# User models the system users, and is generated by the acts_as_authenticated
# Rails generator.
class User < ActiveRecord::Base

  N_('Password')
  N_('Password confirmation')
  N_('Terms accepted')

  def self.[](login)
    self.find_by_login(login)
  end

  # FIXME ugly workaround
  def self.human_attribute_name(attrib)
    case attrib.to_sym
      when :login:  return [_('Username'), _('Email')].join(' / ')
      when :email:  return _('e-Mail')
      else _(self.superclass.human_attribute_name(attrib))
    end
  end

  before_create do |user|
    if user.environment.nil?
      user.environment = Environment.default
    end
    user.send(:make_activation_code) unless user.environment.enabled?('skip_new_user_email_confirmation')
  end

  after_create do |user|
    user.person ||= Person.new
    user.person.attributes = user.person_data.merge(:identifier => user.login, :user => user, :environment_id => user.environment_id)
    user.person.name ||= user.name
    user.person.visible = false unless user.activated?
    user.person.save!
    if user.environment.enabled?('skip_new_user_email_confirmation')
      user.activate
    end
  end
  after_create :deliver_activation_code
  after_create :delay_activation_check

  attr_writer :person_data
  def person_data
    @person_data || {}
  end

  def email_domain
    self.person.preferred_domain && self.person.preferred_domain.name || self.environment.default_hostname(true)
  end

  class Mailer < ActionMailer::Base
    def activation_email_notify(user)
      user_email = "#{user.login}@#{user.email_domain}"
      recipients user_email
      from "#{user.environment.name} <#{user.environment.contact_email}>"
      subject _("[%{environment}] Welcome to %{environment} mail!") % { :environment => user.environment.name }
      body :name => user.name,
        :email => user_email,
        :webmail => MailConf.webmail_url(user.login, user.email_domain),
        :environment => user.environment.name,
        :url => url_for(:host => user.environment.default_hostname, :controller => 'home')
    end

    def activation_code(user)
      recipients user.email

      from "#{user.environment.name} <#{user.environment.contact_email}>"
      subject _("[%s] Activate your account") % [user.environment.name]
      body :recipient => user.name,
        :activation_code => user.activation_code,
        :environment => user.environment.name,
        :url => user.environment.top_url
    end

    def signup_welcome_email(user)
      email_body = user.environment.signup_welcome_text_body.gsub('{user_name}', user.name)
      email_subject = user.environment.signup_welcome_text_subject

      content_type 'text/html'
      recipients user.email

      from "#{user.environment.name} <#{user.environment.contact_email}>"
      subject email_subject.blank? ? _("Welcome to environment %s") % [user.environment.name] : email_subject
      body email_body
    end
  end

  def signup!
    User.transaction do
      self.save!
      self.person.save!
    end
  end
  
  has_one :person, :dependent => :destroy
  belongs_to :environment

  attr_protected :activated_at

  # Virtual attribute for the unencrypted password
  attr_accessor :password, :name

  validates_presence_of     :login, :email
  validates_format_of       :login, :with => Profile::IDENTIFIER_FORMAT, :if => (lambda {|user| !user.login.blank?})
  validates_presence_of     :password,                   :if => :password_required?
  validates_presence_of     :password_confirmation,      :if => :password_required?
  validates_length_of       :password, :within => 4..40, :if => :password_required?
  validates_confirmation_of :password,                   :if => :password_required?
  validates_length_of       :login,    :within => 2..40, :if => (lambda {|user| !user.login.blank?})
  validates_length_of       :email,    :within => 3..100, :if => (lambda {|user| !user.email.blank?})
  validates_uniqueness_of   :login, :email, :case_sensitive => false, :scope => :environment_id
  before_save :encrypt_password
  validates_format_of :email, :with => Noosfero::Constants::EMAIL_FORMAT, :if => (lambda {|user| !user.email.blank?})

  validates_inclusion_of :terms_accepted, :in => [ '1' ], :if => lambda { |u| ! u.terms_of_use.blank? }, :message => N_('%{fn} must be checked in order to signup.').fix_i18n

  # Authenticates a user by their login name or email and unencrypted password.  Returns the user or nil.
  def self.authenticate(login, password, environment = nil)
    environment ||= Environment.default
    u = self.first :conditions => ['(login = ? OR email = ?) AND environment_id = ? AND activated_at IS NOT NULL',
                                   login, login, environment.id] # need to get the salt
    u && u.authenticated?(password) ? u : nil
  end

  # Activates the user in the database.
  def activate
    return false unless self.person
    self.activated_at = Time.now.utc
    self.activation_code = nil
    self.person.visible = true
    begin
      self.person.save! && self.save!
    rescue Exception => exception
      logger.error(exception.to_s)
      false
    else
      if environment.enabled?('send_welcome_email_to_new_users') && environment.has_signup_welcome_text?
        User::Mailer.delay.deliver_signup_welcome_email(self)
      end
      true
    end
  end

  def activated?
    self.activation_code.nil? && !self.activated_at.nil?
{'fraction_non_alphanumeric': 0.11523276099531723, 'fraction_numerical': 0.0025709301257919384, 'mean_word_length': 3.4566284779050735, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4395271', 'n_tokens_mistral': 3442, 'n_tokens_neox': 3347, 'n_words': 754}