<commit_before>class User < ActiveRecord::Base
  extend ControlControl::Resource
  include Nameable
  include Activatable
  include RoleControl::Owner
  include RoleControl::Enrolled
  include Linkable
  include KafkaEvent

  attr_accessible :name, :email, :password, :login, :migrated_user,
    :display_name, :credited_name, :global_email_communication,
    :project_email_communication

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, omniauth_providers: [:facebook, :gplus]

  has_many :user_groups, through: :active_memberships
  has_many :classifications
  has_many :authorizations
  has_many :user_collection_preferences
  has_many :project_preferences, class_name: "UserProjectPreference"

  has_many :memberships
  has_many :active_memberships, -> { active }, class_name: 'Membership'

  owns :projects
  owns :collections
  owns :subjects
  owns :oauth_applications, class_name: "Doorkeeper::Application"

  enrolled_for :projects, through: :project_preferences
  enrolled_for :collections, through: :user_collection_preferences
  enrolled_for :user_groups, through: :active_memberships

  validates :login, presence: true, uniqueness: true
  validates_length_of :password, within: 8..128, allow_blank: true, unless: :migrated_user?
  validates :admin, inclusion: { in: [ true, false ], message: "must be a boolean value" }

  can :show, proc { |requester| requester.user == self }
  can :update, proc { |requester| requester.user == self }
  can :destroy, proc { |requester| requester.user == self }

  can_be_linked :membership, :all
  can_be_linked :user_group, :all
  can_be_linked :user_subject_queue, :all
  can_be_linked :user_project_preference, :all

  attr_accessor :migrated_user

  kafka_event :sign_in, attributes: [:login, :current_sign_in_ip, :current_sign_in_at]

  def self.from_omniauth(auth_hash)
    auth = Authorization.from_omniauth(auth_hash)
    auth.user ||= create do |u|
      u.email = auth_hash.info.email
      u.password = Devise.friendly_token[0,20]
      name = auth_hash.info.name
      u.display_name = name
      u.login = StringConverter.downcase_and_replace_spaces(name)
      u.owner_name = OwnerName.new(name: u.login, resource: u)
      u.authorizations << auth
    end
  end

  def password_required?
    super && hash_func != 'sha1'
  end

  def valid_password?(password)
    if hash_func == 'bcrypt'
      super(password)
    elsif hash_func == 'sha1'
      if encrypted_password = <PASSWORD>(password)
        logger.info "User #{id} is using sha1 password. Updating..."
        self.password = password<commit_msg>Remove sign_in event<commit_after>class User < ActiveRecord::Base
  extend ControlControl::Resource
  include Nameable
  include Activatable
  include RoleControl::Owner
  include RoleControl::Enrolled
  include Linkable
  include KafkaEvent

  attr_accessible :name, :email, :password, :login, :migrated_user,
    :display_name, :credited_name, :global_email_communication,
    :project_email_communication

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :omniauthable, omniauth_providers: [:facebook, :gplus]

  has_many :user_groups, through: :active_memberships
  has_many :classifications
  has_many :authorizations
  has_many :user_collection_preferences
  has_many :project_preferences, class_name: "UserProjectPreference"

  has_many :memberships
  has_many :active_memberships, -> { active }, class_name: 'Membership'

  owns :projects
  owns :collections
  owns :subjects
  owns :oauth_applications, class_name: "Doorkeeper::Application"

  enrolled_for :projects, through: :project_preferences
  enrolled_for :collections, through: :user_collection_preferences
  enrolled_for :user_groups, through: :active_memberships

  validates :login, presence: true, uniqueness: true
  validates_length_of :password, within: 8..128, allow_blank: true, unless: :migrated_user?
  validates :admin, inclusion: { in: [ true, false ], message: "must be a boolean value" }

  can :show, proc { |requester| requester.user == self }
  can :update, proc { |requester| requester.user == self }
  can :destroy, proc { |requester| requester.user == self }

  can_be_linked :membership, :all
  can_be_linked :user_group, :all
  can_be_linked :user_subject_queue, :all
  can_be_linked :user_project_preference, :all

  attr_accessor :migrated_user

  def self.from_omniauth(auth_hash)
    auth = Authorization.from_omniauth(auth_hash)
    auth.user ||= create do |u|
      u.email = auth_hash.info.email
      u.password = <PASSWORD>]
      name = auth_hash.info.name
      u.display_name = name
      u.login = StringConverter.downcase_and_replace_spaces(name)
      u.owner_name = OwnerName.new(name: u.login, resource: u)
      u.authorizations << auth
    end
  end

  def password_required?
    super && hash_func != 'sha1'
  end

  def valid_password?(password)
    if hash_func == 'bcrypt'
      super(password)
    elsif hash_func == 'sha1'
      if encrypted_password = <PASSWORD>1_encrypt(password)
        logger.info "User #{id} is using sha1 password. Updating..."
        self.password = password
{'fraction_non_alphanumeric': 0.09533817348108521, 'fraction_numerical': 0.003439052350019106, 'mean_word_length': 4.563230605738576, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 8, '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': '762390', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1653, 'n_words': 395}