FIX: Unassign users after removing them from an assign allowed group. (#134)

Users that aren't members of an assign allowed group can no longer use the feature. Automatically unassign all their topics.
This commit is contained in:
Roman Rizzi 2021-01-25 14:59:42 -03:00 committed by GitHub
parent 0bf73af1d7
commit 65be8fe69d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 0 deletions

View File

@ -556,4 +556,23 @@ after_initialize do
end
end
on(:user_removed_from_group) do |user, group|
assign_allowed_groups = SiteSetting.assign_allowed_on_groups.split('|').map(&:to_i)
if assign_allowed_groups.include?(group.id)
groups = GroupUser.where(user: user).pluck(:group_id)
if (groups & assign_allowed_groups).empty?
topics = Topic.joins(:_custom_fields)
.where(
'topic_custom_fields.name = ? AND topic_custom_fields.value = ?',
TopicAssigner::ASSIGNED_TO_ID, user.id.to_s
)
topics.each do |topic|
TopicAssigner.new(topic, Discourse.system_user).unassign
end
end
end
end
end

42
spec/plugin_spec.rb Normal file
View File

@ -0,0 +1,42 @@
# frozen_string_literal: true
require 'rails_helper'
describe 'plugin' do
before { SiteSetting.assign_enabled = true }
describe 'events' do
describe 'on user_removed_from_group' do
before do
@topic = Fabricate(:post).topic
@user = Fabricate(:user)
@group_a = Fabricate(:group)
@group_a.add(@user)
end
it 'unassigns the user' do
SiteSetting.assign_allowed_on_groups = @group_a.id.to_s
TopicAssigner.new(@topic, Discourse.system_user).assign(@user)
@group_a.remove(@user)
custom_fields = @topic.reload.custom_fields
expect(custom_fields[TopicAssigner::ASSIGNED_TO_ID]).to be_nil
expect(custom_fields[TopicAssigner::ASSIGNED_BY_ID]).to be_nil
end
it "doesn't unassign the user if it still has access through another group" do
@group_b = Fabricate(:group)
@group_b.add(@user)
SiteSetting.assign_allowed_on_groups = [@group_a.id.to_s, @group_b.id.to_s].join('|')
TopicAssigner.new(@topic, Discourse.system_user).assign(@user)
@group_a.remove(@user)
custom_fields = @topic.reload.custom_fields
expect(custom_fields[TopicAssigner::ASSIGNED_TO_ID]).to eq(@user.id.to_s)
expect(custom_fields[TopicAssigner::ASSIGNED_BY_ID]).to eq(Discourse::SYSTEM_USER_ID.to_s)
end
end
end
end