2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-12-11 23:36:15 +08:00
|
|
|
class GroupActionLogger
|
2017-01-04 21:23:21 +08:00
|
|
|
|
2020-08-19 22:41:40 +08:00
|
|
|
def initialize(acting_user, group, opts = {})
|
2016-12-11 23:36:15 +08:00
|
|
|
@acting_user = acting_user
|
|
|
|
@group = group
|
2020-08-19 22:41:40 +08:00
|
|
|
@opts = opts
|
2016-12-11 23:36:15 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def log_make_user_group_owner(target_user)
|
|
|
|
can_edit?
|
|
|
|
|
|
|
|
GroupHistory.create!(default_params.merge(
|
|
|
|
action: GroupHistory.actions[:make_user_group_owner],
|
|
|
|
target_user: target_user
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_remove_user_as_group_owner(target_user)
|
|
|
|
can_edit?
|
|
|
|
|
|
|
|
GroupHistory.create!(default_params.merge(
|
|
|
|
action: GroupHistory.actions[:remove_user_as_group_owner],
|
|
|
|
target_user: target_user
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_add_user_to_group(target_user)
|
2017-07-28 10:37:10 +08:00
|
|
|
(target_user == @acting_user && @group.public_admission) || can_edit?
|
2016-12-11 23:36:15 +08:00
|
|
|
|
|
|
|
GroupHistory.create!(default_params.merge(
|
|
|
|
action: GroupHistory.actions[:add_user_to_group],
|
|
|
|
target_user: target_user
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_remove_user_from_group(target_user)
|
2017-07-28 10:37:10 +08:00
|
|
|
(target_user == @acting_user && @group.public_exit) || can_edit?
|
2016-12-11 23:36:15 +08:00
|
|
|
|
|
|
|
GroupHistory.create!(default_params.merge(
|
|
|
|
action: GroupHistory.actions[:remove_user_from_group],
|
|
|
|
target_user: target_user
|
|
|
|
))
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_change_group_settings
|
2020-08-19 22:41:40 +08:00
|
|
|
@opts[:skip_guardian] || can_edit?
|
2016-12-11 23:36:15 +08:00
|
|
|
|
|
|
|
@group.previous_changes.except(*excluded_attributes).each do |attribute_name, value|
|
|
|
|
next if value[0].blank? && value[1].blank?
|
|
|
|
|
|
|
|
GroupHistory.create!(default_params.merge(
|
|
|
|
action: GroupHistory.actions[:change_group_setting],
|
|
|
|
subject: attribute_name,
|
|
|
|
prev_value: value[0],
|
|
|
|
new_value: value[1]
|
|
|
|
))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-01-04 21:23:21 +08:00
|
|
|
def excluded_attributes
|
|
|
|
[
|
|
|
|
:bio_cooked,
|
|
|
|
:updated_at,
|
|
|
|
:created_at,
|
|
|
|
:user_count
|
|
|
|
]
|
|
|
|
end
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2017-01-04 21:23:21 +08:00
|
|
|
def default_params
|
|
|
|
{ group: @group, acting_user: @acting_user }
|
|
|
|
end
|
2016-12-11 23:36:15 +08:00
|
|
|
|
2017-01-04 21:23:21 +08:00
|
|
|
def can_edit?
|
|
|
|
raise Discourse::InvalidParameters.new unless Guardian.new(@acting_user).can_log_group_changes?(@group)
|
2016-12-11 23:36:15 +08:00
|
|
|
end
|
2017-01-04 21:23:21 +08:00
|
|
|
|
2016-12-11 23:36:15 +08:00
|
|
|
end
|