<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

  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_accessible :username, :email, :password, :password_<PASSWORD>,
    :admin, :active, :created_at,
    :updated_at, :confirmed_at, :remember_me

  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

  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.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 moderate?(forum)
    return true if admin?

    permissions = CfForumGroupPermission
      .where("group_id IN (SELECT group_id FROM groups_users WHERE user_id = ?) AND forum_id = ?", user_id, forum.forum_id)
      .all

    permissions.each do |p|
      return true if p.permission == CfForumGroupPermission::ACCESS_MODERATE
    end

    return false
  end

  def write?(forum)
    return true if forum.standard_permission == CfForumGroupPermission::ACCESS_WRITE
    return true if forum.standard_permission == CfForumGroupPermission::ACCESS_KNOWN_WRITE
    return true if admin?

    permissions = CfForumGroupPermission
      .where("group_id IN (SELECT group_id FROM groups_users WHERE user_id = ?) AND forum_id = ?", user_id, forum.forum_id)
      .all

    permissions.each do |p|
      return true if p.permission == CfForumGroupPermission::ACCESS_MODERATE or p.permission == CfForumGroupPermission::ACCESS_WRITE
    end

    return false
  end

  def read?(forum)
    return true if forum.standard_permission == CfForumGroupPermission::ACCESS_READ or forum.standard_permission == CfForumGroupPermission::ACCESS_WRITE
    return true if forum.standard_permission == CfForumGroupPermission::ACCESS_KNOWN_READ or forum.standard_permission == CfForumGroupPermission::ACCESS_KNOWN_WRITE
    return true if admin?

    permissions = CfForumGroupPermission
      .where("group_id IN (SELECT group_id FROM groups_users WHERE user_id = ?) AND forum_id = ?", user_id, forum.forum_id)
      .all

    permissions.each do |p|
      return true if p.permission == CfForumGroupPermission::ACCESS_MODERATE or p.permission == CfForumGroupPermission::ACCESS_WRITE or p.permission == CfForumGroupPermission::ACCESS_READ
    end

    return false
  end
end

# eof
<commit_msg>implemented a cache to avoid too many queries<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

  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_accessible :username, :email, :password, :password_confirmation,
    :admin, :active, :created_at,
    :updated_at, :confirmed_at, :remember_me

  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

  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.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 moderate?(forum)
    return true if admin?

    @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)
      .all

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

    return false
  end

  def write?(forum)
    return true if forum.standard_permission == CfForumGroupPermission::ACCESS_WRITE
    return true if forum.standard_permission == CfForumGroupPermission::ACCESS_KNOWN_WRITE
    return true if admin?

    @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)
      .all

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

    return false
  end

  def read?(forum)
    return true if forum.standard_permission == CfForumGroupPermission::ACCESS_READ or forum.standard_permission == CfForumGroupPermission::ACCESS_WRITE
    return true if forum.standard_permission == CfForumGroupPermission::ACCESS_KNOWN_READ or forum.standard_permission == CfForumGroupPermission::ACCESS_KNOWN_WRITE
    return true if admin?

    @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)
      .all

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

    return false
  end
end

# eof

{'fraction_non_alphanumeric': 0.10303118624307782, 'fraction_numerical': 0.0005829204313611193, 'mean_word_length': 4.370109546165884, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 20, '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': '5681567', 'n_tokens_mistral': 2183, 'n_tokens_neox': 2050, 'n_words': 530}