diff --git a/plugin.rb b/plugin.rb index c6ced1a..178b241 100644 --- a/plugin.rb +++ b/plugin.rb @@ -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 diff --git a/spec/plugin_spec.rb b/spec/plugin_spec.rb new file mode 100644 index 0000000..11fb716 --- /dev/null +++ b/spec/plugin_spec.rb @@ -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