<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, :timeoutable, :confirmable

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

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create

  validates_presence_of :username
  validates_uniqueness_of :username

  validates_confirmation_of :password, :if => :password
  validates_length_of :password, :minimum => 3, :if => :password, :allow_blank => 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 :rights, class_name: 'CfForumPermission', :foreign_key => :user_id
  has_many :forums, class_name: 'CfForum', :through => :rights

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

    vals[nam.to_s] || default
  end

  def to_param
    username
  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?

    rights.each do |r|
      return true if f.forum_id == forum.forum_id and r.permission == 'moderate'
    end

    return false
  end

  def write?(forum)
    return true if forum.public?
    return true if admin?

    rights.each do |r|
      return true if f.forum_id == forum.forum_id and (r.permission == 'write' or r.permission == 'moderate')
    end

    return false
  end

  def read?(forum)
    return true if forum.public?
    return true if admin?

    rights.each do |r|
      return true if f.forum_id == forum.forum_id
    end

    return false
  end
end

# eof<commit_msg>several bugfixes, unit tests to teh rescue!<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, :timeoutable, :confirmable

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

  validates_confirmation_of :password
  validates_presence_of :password, :on => :create

  validates_presence_of :username, :email
  validates_uniqueness_of :username, :email

  validates_confirmation_of :password, :if => :password
  validates_length_of :password, :minimum => 3, :if => :password, :allow_blank => 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 :rights, class_name: 'CfForumPermission', :foreign_key => :user_id
  has_many :forums, class_name: 'CfForum', :through => :rights

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

    vals[nam.to_s] || default
  end

  def to_param
    username
  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?

    rights.each do |r|
      return true if r.forum_id == forum.forum_id and r.permission == 'moderate'
    end

    return false
  end

  def write?(forum)
    return true if forum.public?
    return true if admin?

    rights.each do |r|
      return true if r.forum_id == forum.forum_id and (r.permission == 'write' or r.permission == 'moderate')
    end

    return false
  end

  def read?(forum)
    return true if forum.public?
    return true if admin?

    rights.each do |r|
      return true if r.forum_id == forum.forum_id
    end

    return false
  end
end

# eof
{'fraction_non_alphanumeric': 0.11048034934497816, 'fraction_numerical': 0.0008733624454148472, 'mean_word_length': 3.717816683831102, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '729561', 'n_tokens_mistral': 1556, 'n_tokens_neox': 1453, 'n_words': 397}