<commit_before># -*- encoding: utf-8 -*-

class CfUser < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable,
    :rememberable, :confirmable, :trackable

  self.primary_key = 'user_id'
  self.table_name  = 'users'


  validates_presence_of :password, :on => :create
  validates :password, length: {:minimum => 3}, confirmation: true, :if => :password, allow_blank: true
  validates :username, presence: true, uniqueness: true
  validates :email, presence: true, uniqueness: true, email: true

  attr_accessor :login

  has_one :settings, class_name: 'CfSetting', :foreign_key => :user_id, :dependent => :destroy

  has_many :groups_users, class_name: 'CfGroupUser', :foreign_key => :user_id
  has_many :groups, class_name: 'CfGroup', :through => :groups_users

  has_many :badges_users, class_name: CfBadgeUser, dependent: :delete_all,
           foreign_key: :user_id
  has_many :badges, through: :badges_users

  def conf(nam, default = nil)
    vals = settings.options unless settings.blank?
    vals ||= {}

    vals[nam.to_s] || default
  end

  def self.find_first_by_auth_conditions(conditions = {})
    conditions = conditions.to_h.dup
    conditions[:active] = true

    if login = conditions.delete(:login)
      where(conditions).where(["LOWER(username) = :value OR LOWER(email) = :value", { :value => login.downcase }]).first
    else
      where(conditions).first
    end
  end

  def has_badge?(type)
    badges.each do |b|
      return true if b.badge_type == type
    end

    return false
  end

  def moderate?(forum)
    return true if admin?
    return true if has_badge?(RightsHelper::MODERATOR_TOOLS)

    @permissions ||= {}
    @permissions[forum.forum_id] ||= CfForumGroupPermission
      .where("group_id IN (SELECT group_id FROM groups_users WHERE user_id = ?) AND forum_id = ?", user_id, forum.forum_id)

    @permissions[forum.forum_id].each do |p|
      return true if p.permission == CfForumGroupPermission::ACCESS_MODERATE
    end

    return false
  end

  def moderator?
    return true if admin?
    return true if has_badge?(RightsHelper::MODERATOR_TOOLS)<commit_msg>fix: forum argument may be blank<commit_after># -*- encoding: utf-8 -*-

class CfUser < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, :recoverable,
    :rememberable, :confirmable, :trackable

  self.primary_key = 'user_id'
  self.table_name  = 'users'


  validates_presence_of :password, :on => :create
  validates :password, length: {:minimum => 3}, confirmation: true, :if => :password, allow_blank: true
  validates :username, presence: true, uniqueness: true
  validates :email, presence: true, uniqueness: true, email: true

  attr_accessor :login

  has_one :settings, class_name: 'CfSetting', :foreign_key => :user_id, :dependent => :destroy

  has_many :groups_users, class_name: 'CfGroupUser', :foreign_key => :user_id
  has_many :groups, class_name: 'CfGroup', :through => :groups_users

  has_many :badges_users, class_name: CfBadgeUser, dependent: :delete_all,
           foreign_key: :user_id
  has_many :badges, through: :badges_users

  def conf(nam, default = nil)
    vals = settings.options unless settings.blank?
    vals ||= {}

    vals[nam.to_s] || default
  end

  def self.find_first_by_auth_conditions(conditions = {})
    conditions = conditions.to_h.dup
    conditions[:active] = true

    if login = conditions.delete(:login)
      where(conditions).where(["LOWER(username) = :value OR LOWER(email) = :value", { :value => login.downcase }]).first
    else
      where(conditions).first
    end
  end

  def has_badge?(type)
    badges.each do |b|
      return true if b.badge_type == type
    end

    return false
  end

  def moderate?(forum)
    return true if admin?
    return true if has_badge?(RightsHelper::MODERATOR_TOOLS)
    return false if forum.blank?

    @permissions ||= {}
    @permissions[forum.forum_id] ||= CfForumGroupPermission
      .where("group_id IN (SELECT group_id FROM groups_users WHERE user_id = ?) AND forum_id = ?", user_id, forum.forum_id)

    @permissions[forum.forum_id].each do |p|
      return true if p.permission == CfForumGroupPermission::ACCESS_MODERATE
    end

    return false
  end

  def moderator?
    return true if admin?
    return true if has_badge?(RightsHelper::MODERATOR_TOOLS)
{'fraction_non_alphanumeric': 0.1129172084958821, 'fraction_numerical': 0.0008669267446900737, 'mean_word_length': 4.088202866593164, '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': '5681568', 'n_tokens_mistral': 1550, 'n_tokens_neox': 1464, 'n_words': 382}