2013-04-17 03:08:21 -04:00
|
|
|
class Group < ActiveRecord::Base
|
2013-04-29 02:33:24 -04:00
|
|
|
has_many :category_groups
|
2013-05-08 21:33:56 -04:00
|
|
|
has_many :group_users, dependent: :destroy
|
2013-04-29 02:33:24 -04:00
|
|
|
|
|
|
|
has_many :categories, through: :category_groups
|
|
|
|
has_many :users, through: :group_users
|
|
|
|
|
2013-05-08 21:33:56 -04:00
|
|
|
after_save :destroy_deletions
|
|
|
|
|
2013-05-09 03:37:34 -04:00
|
|
|
validate :name_format_validator
|
|
|
|
|
2013-05-06 00:49:56 -04:00
|
|
|
AUTO_GROUPS = {
|
|
|
|
:admins => 1,
|
|
|
|
:moderators => 2,
|
|
|
|
:staff => 3,
|
|
|
|
:trust_level_1 => 11,
|
|
|
|
:trust_level_2 => 12,
|
|
|
|
:trust_level_3 => 13,
|
|
|
|
:trust_level_4 => 14,
|
|
|
|
:trust_level_5 => 15
|
|
|
|
}
|
|
|
|
|
|
|
|
def self.trust_group_ids
|
|
|
|
(10..19).to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.refresh_automatic_group!(name)
|
|
|
|
|
|
|
|
id = AUTO_GROUPS[name]
|
|
|
|
|
2013-05-09 03:37:34 -04:00
|
|
|
unless group = self.lookup_group(name)
|
|
|
|
group = Group.new(name: name.to_s, automatic: true)
|
2013-05-06 00:49:56 -04:00
|
|
|
group.id = id
|
|
|
|
group.save!
|
|
|
|
end
|
|
|
|
|
2013-05-08 01:20:38 -04:00
|
|
|
group.name = I18n.t("groups.default_names.#{name}")
|
2013-05-06 00:49:56 -04:00
|
|
|
|
|
|
|
real_ids = case name
|
|
|
|
when :admins
|
|
|
|
"SELECT u.id FROM users u WHERE u.admin = 't'"
|
|
|
|
when :moderators
|
|
|
|
"SELECT u.id FROM users u WHERE u.moderator = 't'"
|
|
|
|
when :staff
|
|
|
|
"SELECT u.id FROM users u WHERE u.moderator = 't' OR u.admin = 't'"
|
|
|
|
when :trust_level_1, :trust_level_2, :trust_level_3, :trust_level_4, :trust_level_5
|
|
|
|
"SELECT u.id FROM users u WHERE u.trust_level = #{id-10}"
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
extra_users = group.users.where("users.id NOT IN (#{real_ids})").select('users.id')
|
|
|
|
missing_users = GroupUser.joins("RIGHT JOIN (#{real_ids}) X ON X.id = user_id AND group_id = #{group.id}")
|
|
|
|
.where("user_id IS NULL")
|
|
|
|
.select("X.id")
|
|
|
|
|
|
|
|
group.group_users.where("user_id IN (#{extra_users.to_sql})").delete_all
|
|
|
|
|
|
|
|
missing_users.each do |u|
|
|
|
|
group.group_users.build(user_id: u.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
group.save!
|
2013-05-08 01:20:38 -04:00
|
|
|
|
|
|
|
# we want to ensure consistency
|
|
|
|
Group.reset_counters(group.id, :group_users)
|
2013-05-09 03:37:34 -04:00
|
|
|
|
|
|
|
group
|
2013-05-06 00:49:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.refresh_automatic_groups!(*args)
|
2013-05-08 01:20:38 -04:00
|
|
|
if args.length == 0
|
|
|
|
args = AUTO_GROUPS.map{|k,v| k}
|
|
|
|
end
|
2013-05-06 00:49:56 -04:00
|
|
|
args.each do |group|
|
|
|
|
refresh_automatic_group!(group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.[](name)
|
2013-05-09 03:37:34 -04:00
|
|
|
unless g = lookup_group(name)
|
|
|
|
g = refresh_automatic_group!(name)
|
|
|
|
end
|
|
|
|
g
|
|
|
|
end
|
2013-05-06 00:49:56 -04:00
|
|
|
|
2013-05-09 03:37:34 -04:00
|
|
|
def self.lookup_group(name)
|
|
|
|
raise ArgumentError, "unknown group" unless id = AUTO_GROUPS[name]
|
|
|
|
g = Group.where(id: id).first
|
2013-05-06 00:49:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.user_trust_level_change!(user_id, trust_level)
|
|
|
|
name = "trust_level_#{trust_level}".to_sym
|
|
|
|
|
|
|
|
GroupUser.where(group_id: trust_group_ids, user_id: user_id).delete_all
|
|
|
|
|
2013-05-09 03:37:34 -04:00
|
|
|
if group = lookup_group(name)
|
2013-05-07 20:32:29 -04:00
|
|
|
group.group_users.build(user_id: user_id)
|
|
|
|
group.save!
|
2013-05-06 00:49:56 -04:00
|
|
|
else
|
|
|
|
refresh_automatic_group!(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-09 03:37:34 -04:00
|
|
|
|
2013-04-17 03:08:21 -04:00
|
|
|
def self.builtin
|
|
|
|
Enum.new(:moderators, :admins, :trust_level_1, :trust_level_2)
|
|
|
|
end
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2013-05-08 21:33:56 -04:00
|
|
|
def usernames=(val)
|
|
|
|
current = usernames.split(",")
|
|
|
|
expected = val.split(",")
|
|
|
|
|
|
|
|
additions = expected - current
|
|
|
|
deletions = current - expected
|
|
|
|
|
|
|
|
map = Hash[*User.where(username: additions+deletions)
|
|
|
|
.select('id,username')
|
|
|
|
.map{|u| [u.username,u.id]}.flatten]
|
|
|
|
|
|
|
|
deletions = Set.new(deletions.map{|d| map[d]})
|
|
|
|
|
|
|
|
@deletions = []
|
|
|
|
group_users.delete_if do |gu|
|
|
|
|
@deletions << gu if deletions.include?(gu.user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
additions.each do |a|
|
|
|
|
group_users.build(user_id: map[a])
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def usernames
|
|
|
|
users.select("username").map(&:username).join(",")
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_ids
|
|
|
|
users.select('users.id').map(&:id)
|
|
|
|
end
|
|
|
|
|
2013-04-29 02:33:24 -04:00
|
|
|
def add(user)
|
|
|
|
self.users.push(user)
|
|
|
|
end
|
2013-05-08 21:33:56 -04:00
|
|
|
protected
|
|
|
|
|
2013-05-09 03:37:34 -04:00
|
|
|
def name_format_validator
|
|
|
|
validator = UsernameValidator.new(name)
|
|
|
|
unless validator.valid_format?
|
|
|
|
validator.errors.each { |e| errors.add(:name, e) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-08 21:33:56 -04:00
|
|
|
# hack around AR
|
|
|
|
def destroy_deletions
|
|
|
|
if @deletions
|
|
|
|
@deletions.each do |gu|
|
|
|
|
gu.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@deletions = nil
|
|
|
|
end
|
|
|
|
|
2013-04-17 03:08:21 -04:00
|
|
|
end
|