<commit_before>require 'digest/sha2'
require 'organization'

class User < ApplicationModel
  include Gmaps

  before_create           :check_name, :check_email, :check_login, :check_image, :check_geo, :check_password
  before_update           :check_password, :check_image, :check_geo, :check_email, :check_login_update

  has_and_belongs_to_many :groups,          :after_add => :cache_update, :after_remove => :cache_update
  has_and_belongs_to_many :roles,           :after_add => :cache_update, :after_remove => :cache_update
  has_and_belongs_to_many :organizations,   :after_add => :cache_update, :after_remove => :cache_update
  has_many                :tokens,          :after_add => :cache_update, :after_remove => :cache_update
  has_many                :authorizations,  :after_add => :cache_update, :after_remove => :cache_update
  belongs_to              :organization,    :class_name => 'Organization'

  store                   :preferences

  def fullname
    fullname = ''
    if self.firstname
      fullname = fullname + self.firstname
    end
    if self.lastname
      if fullname != ''
        fullname = fullname + ' '
      end
      fullname = fullname + self.lastname
    end
    return fullname
  end

  def is_role( role_name )
    self.roles.each { |role|
      return role if role.name == role_name
    }
    return false
  end

  def self.authenticate( username, password )

    # do not authenticate with nothing
    return if !username || username == ''
    return if !password || password == '' 

    # try to find user based on login
    user = User.where( :login => username.downcase, :active => true ).first

    # try second lookup with email
    if !user
      user = User.where( :email => username.downcase, :active => true ).first
    end

    # check failed logins
    max_login_failed = Setting.get('password_max_login_failed') || 10
    if user && user.login_failed > max_login_failed
      return false
    end

    # use auth backends
    config = [
      {
        :adapter => 'internal',
      },
      {
        :adapter => 'test',
      },
    ]
    Setting.where( :area => 'Security::Authentication' ).each {|setting|
      if setting.state[:value]
        config.push setting.state[:value]
      end
    }

    # try to login against configure auth backends
    user_auth = nil
    config.each {|config_item|
      next if !config_item[:adapter]
      file = "auth/#{config_item[:adapter]}"
      require file
      user_auth = Auth.const_get("#{config_item[:adapter].to_s.upcase}").check( username, password, config_item, user )

      # auth ok
      if user_auth

        # remember last login date
        user_auth.update_last_login

        # reset login failed
        user_auth.login_failed = 0
        user_auth.save

        return user_auth
      end
    }

    # set login failed +1
    if !user_auth && user
      user.login_failed = user.login_failed + 1
      user.save
    end

    # auth failed
    sleep 1
    return user_auth
  end

  def self.sso(params)

    # use auth backends
    config = [
      {
        :adapter => 'env',
      },
      {
        :adapter => 'otrs',
      },
    ]
#    Setting.where( :area => 'Security::Authentication' ).each {|setting|
#      if setting.state[:value]
#        config.push setting.state[:value]
#      end
#    }

    # try to login against configure auth backends
    user_auth = nil
    config.each {|config_item|
      next if !config_item[:adapter]
      file = "sso/#{config_item[:adapter]}"
      require file
      user_auth = SSO.const_get("#{config_item[:adapter].to_s.upcase}").check( params, config_item )

      # auth ok
      if user_auth

        # remember last login date
        user_auth.update_last_login

        # reset login failed
        user_auth.login_failed = 0
        user_auth.save

        return user_auth
      end
    }

    return false<commit_msg>Improved error handling.<commit_after>require 'digest/sha2'
require 'organization'

class User < ApplicationModel
  include Gmaps

  before_create           :check_name, :check_email, :check_login, :check_image, :check_geo, :check_password
  before_update           :check_password, :check_image, :check_geo, :check_email, :check_login_update

  has_and_belongs_to_many :groups,          :after_add => :cache_update, :after_remove => :cache_update
  has_and_belongs_to_many :roles,           :after_add => :cache_update, :after_remove => :cache_update
  has_and_belongs_to_many :organizations,   :after_add => :cache_update, :after_remove => :cache_update
  has_many                :tokens,          :after_add => :cache_update, :after_remove => :cache_update
  has_many                :authorizations,  :after_add => :cache_update, :after_remove => :cache_update
  belongs_to              :organization,    :class_name => 'Organization'

  store                   :preferences

  def fullname
    fullname = ''
    if self.firstname
      fullname = fullname + self.firstname
    end
    if self.lastname
      if fullname != ''
        fullname = fullname + ' '
      end
      fullname = fullname + self.lastname
    end
    return fullname
  end

  def is_role( role_name )
    self.roles.each { |role|
      return role if role.name == role_name
    }
    return false
  end

  def self.authenticate( username, password )

    # do not authenticate with nothing
    return if !username || username == ''
    return if !password || password == '' 

    # try to find user based on login
    user = User.where( :login => username.downcase, :active => true ).first

    # try second lookup with email
    if !user
      user = User.where( :email => username.downcase, :active => true ).first
    end

    # check failed logins
    max_login_failed = Setting.get('password_max_login_failed') || 10
    if user && user.login_failed > max_login_failed
      return false
    end

    # use auth backends
    config = [
      {
        :adapter => 'internal',
      },
      {
        :adapter => 'test',
      },
    ]
    Setting.where( :area => 'Security::Authentication' ).each {|setting|
      if setting.state[:value]
        config.push setting.state[:value]
      end
    }

    # try to login against configure auth backends
    user_auth = nil
    config.each {|config_item|
      next if !config_item[:adapter]
      next if config_item.class == TrueClass
      file = "auth/#{config_item[:adapter]}"
      require file
      user_auth = Auth.const_get("#{config_item[:adapter].to_s.upcase}").check( username, password, config_item, user )

      # auth ok
      if user_auth

        # remember last login date
        user_auth.update_last_login

        # reset login failed
        user_auth.login_failed = 0
        user_auth.save

        return user_auth
      end
    }

    # set login failed +1
    if !user_auth && user
      user.login_failed = user.login_failed + 1
      user.save
    end

    # auth failed
    sleep 1
    return user_auth
  end

  def self.sso(params)

    # use auth backends
    config = [
      {
        :adapter => 'env',
      },
      {
        :adapter => 'otrs',
      },
    ]
#    Setting.where( :area => 'Security::Authentication' ).each {|setting|
#      if setting.state[:value]
#        config.push setting.state[:value]
#      end
#    }

    # try to login against configure auth backends
    user_auth = nil
    config.each {|config_item|
      next if !config_item[:adapter]
      next if config_item.class == TrueClass
      file = "sso/#{config_item[:adapter]}"
      require file
      user_auth = SSO.const_get("#{config_item[:adapter].to_s.upcase}").check( params, config_item )

      # auth ok
      if user_auth

        # remember last login date
        user_auth.update_last_login

        # reset login failed
        user_auth.login_failed = 0
        user_auth.save

        return user_auth
      end
    }

    return false
{'fraction_non_alphanumeric': 0.09912647170527915, 'fraction_numerical': 0.002025572857323712, 'mean_word_length': 2.3559898045879355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '611226', 'n_tokens_mistral': 2435, 'n_tokens_neox': 2353, 'n_words': 597}