2013-04-17 03:08:21 -04:00
|
|
|
class Group < ActiveRecord::Base
|
2014-04-28 04:31:51 -04:00
|
|
|
include HasCustomFields
|
2014-04-25 09:14:05 -04:00
|
|
|
|
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
|
2014-04-23 13:25:02 -04:00
|
|
|
validates_uniqueness_of :name
|
2013-05-09 03:37:34 -04:00
|
|
|
|
2013-05-06 00:49:56 -04:00
|
|
|
AUTO_GROUPS = {
|
2013-07-13 21:24:16 -04:00
|
|
|
:everyone => 0,
|
2013-05-06 00:49:56 -04:00
|
|
|
: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
|
|
|
|
}
|
|
|
|
|
2014-01-07 11:47:01 -05:00
|
|
|
ALIAS_LEVELS = {
|
|
|
|
:nobody => 0,
|
|
|
|
:only_admins => 1,
|
|
|
|
:mods_and_admins => 2,
|
|
|
|
:members_mods_and_admins => 3,
|
|
|
|
:everyone => 99
|
|
|
|
}
|
|
|
|
|
|
|
|
validate :alias_level, inclusion: { in: ALIAS_LEVELS.values}
|
|
|
|
|
2014-02-12 14:00:45 -05:00
|
|
|
def posts_for(guardian, before_post_id=nil)
|
2014-02-07 10:44:03 -05:00
|
|
|
user_ids = group_users.map {|gu| gu.user_id}
|
|
|
|
result = Post.where(user_id: user_ids).includes(:user, :topic).references(:posts, :topics)
|
2014-03-13 14:06:22 -04:00
|
|
|
.where('topics.archetype <> ?', Archetype.private_message)
|
|
|
|
.where(post_type: Post.types[:regular])
|
2014-02-07 10:44:03 -05:00
|
|
|
|
|
|
|
unless guardian.is_staff?
|
|
|
|
allowed_ids = guardian.allowed_category_ids
|
|
|
|
if allowed_ids.length > 0
|
|
|
|
result = result.where('topics.category_id IS NULL or topics.category_id IN (?)', allowed_ids)
|
|
|
|
else
|
|
|
|
result = result.where('topics.category_id IS NULL')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result = result.where('posts.id < ?', before_post_id) if before_post_id
|
2014-02-12 14:00:45 -05:00
|
|
|
result.order('posts.created_at desc')
|
2014-02-07 10:44:03 -05:00
|
|
|
end
|
|
|
|
|
2013-05-06 00:49:56 -04:00
|
|
|
def self.trust_group_ids
|
|
|
|
(10..19).to_a
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.refresh_automatic_group!(name)
|
|
|
|
|
|
|
|
id = AUTO_GROUPS[name]
|
2013-07-22 20:10:36 -04:00
|
|
|
return unless id
|
2013-05-06 00:49:56 -04:00
|
|
|
|
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-07-13 21:24:16 -04:00
|
|
|
# the everyone group is special, it can include non-users so there is no
|
|
|
|
# way to have the membership in a table
|
|
|
|
return group if name == :everyone
|
|
|
|
|
2013-05-08 01:20:38 -04:00
|
|
|
group.name = I18n.t("groups.default_names.#{name}")
|
2013-05-06 00:49:56 -04:00
|
|
|
|
2013-06-23 00:44:16 -04:00
|
|
|
# don't allow shoddy localization to break this
|
|
|
|
validator = UsernameValidator.new(group.name)
|
|
|
|
unless validator.valid_format?
|
|
|
|
group.name = name
|
|
|
|
end
|
|
|
|
|
2013-05-06 00:49:56 -04:00
|
|
|
real_ids = case name
|
|
|
|
when :admins
|
2013-05-29 15:46:03 -04:00
|
|
|
"SELECT u.id FROM users u WHERE u.admin"
|
2013-05-06 00:49:56 -04:00
|
|
|
when :moderators
|
2013-05-29 15:46:03 -04:00
|
|
|
"SELECT u.id FROM users u WHERE u.moderator"
|
2013-05-06 00:49:56 -04:00
|
|
|
when :staff
|
2013-05-29 15:46:03 -04:00
|
|
|
"SELECT u.id FROM users u WHERE u.moderator OR u.admin"
|
2013-05-06 00:49:56 -04:00
|
|
|
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')
|
2013-05-16 19:03:30 -04:00
|
|
|
missing_users = GroupUser
|
|
|
|
.joins("RIGHT JOIN (#{real_ids}) X ON X.id = user_id AND group_id = #{group.id}")
|
2013-05-06 00:49:56 -04:00
|
|
|
.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
|
2013-06-10 01:08:10 -04:00
|
|
|
args = AUTO_GROUPS.keys
|
2013-05-08 01:20:38 -04:00
|
|
|
end
|
2013-05-06 00:49:56 -04:00
|
|
|
args.each do |group|
|
|
|
|
refresh_automatic_group!(group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-11-17 20:53:14 -05:00
|
|
|
def self.ensure_automatic_groups!
|
|
|
|
AUTO_GROUPS.keys.each do |name|
|
|
|
|
refresh_automatic_group!(name) unless lookup_group(name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-06 00:49:56 -04:00
|
|
|
def self.[](name)
|
2013-05-16 19:03:30 -04:00
|
|
|
lookup_group(name) || refresh_automatic_group!(name)
|
2013-05-09 03:37:34 -04:00
|
|
|
end
|
2013-05-06 00:49:56 -04:00
|
|
|
|
2013-12-23 09:46:00 -05:00
|
|
|
def self.search_group(name, current_user)
|
2014-01-07 11:47:01 -05:00
|
|
|
levels = [ALIAS_LEVELS[:everyone]]
|
2014-01-14 13:18:04 -05:00
|
|
|
|
2013-12-23 09:46:00 -05:00
|
|
|
if current_user.admin?
|
2014-01-07 11:47:01 -05:00
|
|
|
levels = [ALIAS_LEVELS[:everyone],
|
|
|
|
ALIAS_LEVELS[:only_admins],
|
|
|
|
ALIAS_LEVELS[:mods_and_admins],
|
|
|
|
ALIAS_LEVELS[:members_mods_and_admins]]
|
2013-12-23 09:46:00 -05:00
|
|
|
elsif current_user.moderator?
|
2014-01-07 11:47:01 -05:00
|
|
|
levels = [ALIAS_LEVELS[:everyone],
|
|
|
|
ALIAS_LEVELS[:mods_and_admins],
|
|
|
|
ALIAS_LEVELS[:members_mods_and_admins]]
|
2013-12-23 09:46:00 -05:00
|
|
|
end
|
|
|
|
|
2014-01-13 19:00:22 -05:00
|
|
|
Group.where("name ILIKE :term_like AND (" +
|
2014-01-14 13:18:04 -05:00
|
|
|
" alias_level in (:levels)" +
|
2014-01-13 18:27:23 -05:00
|
|
|
" OR (alias_level = #{ALIAS_LEVELS[:members_mods_and_admins]} AND id in (" +
|
2013-12-23 09:46:00 -05:00
|
|
|
"SELECT group_id FROM group_users WHERE user_id= :user_id)" +
|
2014-01-14 13:18:04 -05:00
|
|
|
")" +
|
2013-12-23 09:46:00 -05:00
|
|
|
")", term_like: "#{name.downcase}%", levels: levels, user_id: current_user.id)
|
|
|
|
end
|
|
|
|
|
2013-05-09 03:37:34 -04:00
|
|
|
def self.lookup_group(name)
|
2013-11-22 13:18:45 -05:00
|
|
|
if id = AUTO_GROUPS[name]
|
2014-05-06 09:41:59 -04:00
|
|
|
Group.find_by(id: id)
|
2013-07-22 20:10:36 -04:00
|
|
|
else
|
2014-05-06 09:41:59 -04:00
|
|
|
unless group = Group.find_by(name: name)
|
2013-11-22 13:18:45 -05:00
|
|
|
raise ArgumentError, "unknown group"
|
2013-07-22 20:10:36 -04:00
|
|
|
end
|
|
|
|
group
|
|
|
|
end
|
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-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 = []
|
2014-03-27 03:00:23 -04:00
|
|
|
group_users.each do |gu|
|
2013-05-08 21:33:56 -04:00
|
|
|
@deletions << gu if deletions.include?(gu.user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
additions.each do |a|
|
|
|
|
group_users.build(user_id: map[a])
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def usernames
|
2013-05-17 15:11:37 -04:00
|
|
|
users.pluck(:username).join(",")
|
2013-05-08 21:33:56 -04:00
|
|
|
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
|
2013-07-07 07:05:18 -04:00
|
|
|
UsernameValidator.perform_validation(self, 'name')
|
2013-05-09 03:37:34 -04:00
|
|
|
end
|
|
|
|
|
2013-05-08 21:33:56 -04:00
|
|
|
# hack around AR
|
|
|
|
def destroy_deletions
|
|
|
|
if @deletions
|
|
|
|
@deletions.each do |gu|
|
|
|
|
gu.destroy
|
2014-03-27 03:00:23 -04:00
|
|
|
User.where('id = ? AND primary_group_id = ?', gu.user_id, gu.group_id).update_all 'primary_group_id = NULL'
|
2013-05-08 21:33:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@deletions = nil
|
|
|
|
end
|
|
|
|
|
2013-04-17 03:08:21 -04:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: groups
|
|
|
|
#
|
2014-02-06 19:07:36 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string(255) not null
|
2014-04-15 01:53:48 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2014-02-06 19:07:36 -05:00
|
|
|
# automatic :boolean default(FALSE), not null
|
|
|
|
# user_count :integer default(0), not null
|
|
|
|
# alias_level :integer default(0)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_groups_on_name (name) UNIQUE
|
|
|
|
#
|