discourse/app/services/group_action_logger.rb
jbrw aa1fc01307
FEATURE - Moderators can create and manage groups (#10432)
Enabling the moderators_manage_categories_and_groups site setting will allow moderator users to create/manage groups.

* show New Group form to moderators

* Allow moderators to update groups and read logs, where appropriate

* Rename site setting from create -> manage

* improved tests

* Migration should rename old log entries

* Log group changes, even if those changes mean you can no longer see the group

* Slight reshuffle

* RouteTo /g if they no longer have permissions to view group
2020-08-19 10:41:40 -04:00

82 lines
1.9 KiB
Ruby

# frozen_string_literal: true
class GroupActionLogger
def initialize(acting_user, group, opts = {})
@acting_user = acting_user
@group = group
@opts = opts
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)
(target_user == @acting_user && @group.public_admission) || can_edit?
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)
(target_user == @acting_user && @group.public_exit) || can_edit?
GroupHistory.create!(default_params.merge(
action: GroupHistory.actions[:remove_user_from_group],
target_user: target_user
))
end
def log_change_group_settings
@opts[:skip_guardian] || can_edit?
@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
def excluded_attributes
[
:bio_cooked,
:updated_at,
:created_at,
:user_count
]
end
def default_params
{ group: @group, acting_user: @acting_user }
end
def can_edit?
raise Discourse::InvalidParameters.new unless Guardian.new(@acting_user).can_log_group_changes?(@group)
end
end